Created
April 19, 2023 18:39
-
-
Save rithvikvibhu/2f9ad7a180500bfa7e795cc6a63b112b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This script finds an address given an account extended public key | |
* Usage: edit these fields below and run it like `node find-address.js` | |
*/ | |
const assert = require('node:assert/strict'); | |
const HDPublicKey = require('hsd/lib/hd/public'); | |
const Address = require('hsd/lib/primitives/address'); | |
// ********** | |
// FILL THESE | |
// ********** | |
const NETWORK = 'main'; | |
const ACCOUNT_XPUB = 'xpub6BvF6DnLZRV6my62FuQ8nw5TRebL8qr9Wa7u2tE1aEedHWBt4XAfSaNTmP1SM2nXErGWSzxRAHVwqstPzRrsXtA2vn1a4KzLwAVEgLNmfca'; // should start with: xpub, rpub, etc. | |
const SEARCH_ADDRESS = 'hs1q3tlnmk9sp2luxdvkdcyvtj8ucj5pa4hwvr8ql6'; | |
const [START, END] = [0, 0x7fffffff]; | |
// const [START, END] = [0, 100]; | |
(() => { | |
const account = HDPublicKey.fromBase58(ACCOUNT_XPUB, NETWORK); | |
console.log('[*] Using account public key:', account.xpubkey(NETWORK)); | |
const expectedAddr = new Address(SEARCH_ADDRESS, NETWORK); | |
console.log('[*] Searching for address:', expectedAddr.toString(NETWORK)); | |
const len = END - START; | |
assert((len >>> 0) > 0); | |
for (let i = START; i <= END; i++) { | |
if (i % 1e3 === 0) { | |
console.log(`[${i}/${len}] searching...`); | |
} | |
/** @type {Buffer[]} */ | |
const keys = [ | |
account.derive(0).derive(i).publicKey, // receive | |
account.derive(1).derive(i).publicKey, // change | |
]; | |
for (const [branch, key] of keys.entries()) { | |
const addr = Address.fromPubkey(key); | |
if (expectedAddr.equals(addr)) { | |
console.log('Found address', { | |
address: addr, | |
branch: branch, | |
index: i, | |
publicKey: key.toString('hex'), | |
}); | |
return; | |
} | |
} | |
} | |
console.log(`Searched index from ${START} to ${END}. Address not found.`); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment