Skip to content

Instantly share code, notes, and snippets.

@riordant
Created February 24, 2023 15:33
Show Gist options
  • Save riordant/cf49ec185a672bcf1be4dbe3c81d8570 to your computer and use it in GitHub Desktop.
Save riordant/cf49ec185a672bcf1be4dbe3c81d8570 to your computer and use it in GitHub Desktop.
Get the PATH master viewing key from a USB-connected Ledger device opened in the Ethereum app, use this to acquire all the child addresses in this path within the defined RANGE.
import TransportNodeHid from "@ledgerhq/hw-transport-node-hid";
import Eth from "@ledgerhq/hw-app-eth";
import BIP32Factory from 'bip32';
import * as ecc from 'tiny-secp256k1';
import publicKeyToAddress from 'ethereum-public-key-to-address';
const bip32 = BIP32Factory(ecc);
const PATH = "44'/60'/0'";
const RANGE = 10;
function compressPubkey(pubkey) {
if (pubkey.length === 65) {
const parity = pubkey[64] & 1;
const newKey = pubkey.slice(0, 33);
newKey[0] = 2 | parity;
return newKey;
}
return pubkey.slice();
}
TransportNodeHid.create().then(async transport => {
const master = await new Eth(transport).getAddress(PATH, false, true);
const publicKey = compressPubkey( Buffer.from( master.publicKey, 'hex' ));
const chainCode = Buffer.from( master.chainCode, 'hex' );
const node = bip32.fromPublicKey(publicKey, chainCode);
for(let i=0; i<RANGE; i++) {
const derived = node.derivePath(i.toString());
const address = publicKeyToAddress(derived.publicKey);
console.log(PATH+"/"+i+" => "+address);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment