2025-01-26 12:16:17 +11:00
|
|
|
/**
|
|
|
|
* 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');
|
2025-01-27 21:33:14 +11:00
|
|
|
const minimist = require('minimist');
|
|
|
|
const args = minimist(process.argv.slice(2));
|
2025-01-26 12:16:17 +11:00
|
|
|
|
|
|
|
const NETWORK = 'main';
|
2025-01-27 21:33:14 +11:00
|
|
|
const ACCOUNT_XPUB = args.xpub || 'xpub6BvF6DnLZRV6my62FuQ8nw5TRebL8qr9Wa7u2tE1aEedHWBt4XAfSaNTmP1SM2nXErGWSzxRAHVwqstPzRrsXtA2vn1a4KzLwAVEgLNmfca';
|
2025-01-26 12:16:17 +11:00
|
|
|
|
2025-01-27 21:33:14 +11:00
|
|
|
const START = parseInt(args.start || '0');
|
|
|
|
const END = parseInt(args.end || '0x7fffffff');
|
|
|
|
const SEARCH_ADDRESS = args.address || 'hs1q3tlnmk9sp2luxdvkdcyvtj8ucj5pa4hwvr8ql6';
|
2025-01-26 12:16:17 +11:00
|
|
|
|
|
|
|
(() => {
|
|
|
|
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.`);
|
|
|
|
})();
|