0

I created a random HD address using my seed phrase a while back. lets call it 0x1....I then send funds to the wallet and continued using it normally. I have since lost the devise i had generated the random HD address 0X1.... I still have my recovery phrase and I remember the address 0x1.. Is there a way I can sort through all my randomly created on my HD wallet to get back my address 0x1.. I do not have a private key for this address.I could have pasted the seed phrase on metamask to recreate the address but i have to keep generating addresses until it which could take ages.

2 Answers 2

1

I could have pasted the seed phrase on metamask to recreate the address but i have to keep generating addresses until it which could take ages.

That'd be one way to go, yes. Alternatively, you can write code that does that for you. This would work, using the ethers.js library :

const phrase = 'YOUR_SEED_PHRASE';
const wantAddress = 'YOUR_ADDRESS';

let wallet;
let currentAddress;
let i = 0;

while(1) {
    const path = `m/44'/60'/0'/0/${i}`;
    wallet = ethers.Wallet.fromMnemonic(phrase, path);
    currentAddress = wallet.address;
    
    if (currentAddress.toLowerCase() === wantAddress.toLowerCase()) {
        console.log('Match found:', currentAddress);
        console.log('Private Key:', wallet.privateKey);
        break;
    }
    i++;
}
2
  • I kept on getting errors while using the above ethers.js code . I used chat gpt to generate the below code n it worked flawlessly. hope it helps someone.
    – Kilonzo
    Commented Jun 20, 2023 at 21:05
  • @Kilonzo That was written for ethers v5, ethers just loves to change absolutely everything on every new major releases, it's awesome (in v6 it's fromPhrase, not fromMnemonic, definitely a much needed syntax change). Awesome that chatGPT code worked, i formatted the code in your answer so its easier to read, next time make sure to surround your code with 3 backticks (```) so it appears like that. You should also accept your own answer by clicking the greyed out checkmark on the left
    – Foxxxey
    Commented Jun 20, 2023 at 22:47
1
const crypto = require('crypto');
const HDKey = require('hdkey');

const phrase = 'YOUR_SEED_PHRASE';
const wantAddress = 'YOUR_ADDRESS';

async function recoverWallet() {
  let i = 0;
  const progressInterval = 1000; // Adjust this value to update the progress indicator frequency

  console.log('Starting brute force...');

  while (true) {
    const seed = crypto.createHash('sha256').update(phrase).digest();
    const masterNode = HDKey.fromMasterSeed(seed);
    const derivedNode = masterNode.derive(`m/44'/60'/0'/0/${i}`);
    const currentAddress = derivedNode.publicKey.toString('hex');

    if (currentAddress.toLowerCase() === wantAddress.toLowerCase()) {
      console.log('Match found:', currentAddress);
      console.log('Private Key:', derivedNode.privateKey.toString('hex'));
      return;
    }

    if (i % progressInterval === 0) {
      console.log('Progress:', i);
    }

    i++;
  }
}

recoverWallet().catch((error) => {
  console.error('Error occurred during wallet recovery:', error);
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.