Created
November 27, 2017 13:09
-
-
Save nodech/3d6ac782b73e2fd76fb3bb0d35cd8adc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use strict'; | |
const fs = require('fs'); | |
const bcoin = require('bcoin'); | |
const secp256k1 = bcoin.secp256k1; | |
const KeyRing = bcoin.keyring; | |
const PublicKey = bcoin.hd.PublicKey; | |
const HID = require('node-hid'); | |
const ledger = require('./src'); | |
const LedgerNode = ledger.comm_node; | |
const LedgerBTC = ledger.btc; | |
const DEBUG = false; | |
(async () => { | |
const devices = await LedgerNode.list_async(); | |
const HIDDevice = new HID.HID(devices[0]); | |
const device = new LedgerNode(HIDDevice, true, 1000, DEBUG); | |
const btc = new LedgerBTC(device); | |
const account = 0; | |
const keypath = `m/0'/0'/${account}'`; | |
const xpub = await getPublicKey(keypath, btc); | |
const addrs = {}; | |
for (let i = 0; i < 10; i++) { | |
addrs[`${keypath}/0/${i}`] = getAddressPK(xpub, i); | |
} | |
console.log(addrs); | |
const list = {}; | |
for (let i = 0; i < 10; i++) { | |
const keypath = `m/0'/0'/${account}'/0/${i}`; | |
const addr = await getAddress(keypath, btc); | |
list[keypath] = addr; | |
if (list[keypath] != addrs[keypath]) { | |
console.log('Mismatch !!:', addrs[keypath], list[keypath]); | |
} else { | |
console.log('Its okay'); | |
} | |
} | |
})().catch((e) => { | |
console.log(e); | |
process.exit(1); | |
}) | |
async function getAddress(path, btc) { | |
const res = await btc.getWalletPublicKey_async(path); | |
return res.bitcoinAddress; | |
} | |
async function getPublicKey(path, btc) { | |
const res = await btc.getWalletPublicKey_async(path); | |
const chaincode = Buffer.from(res.chainCode, 'hex'); | |
const pubkey = Buffer.from(res.publicKey, 'hex'); | |
const cpubkey = secp256k1.publicKeyConvert(pubkey, true); | |
return new PublicKey({ | |
depth: 5, | |
parentFingerPrint: 1, | |
childIndex: 0, | |
chainCode: chaincode, | |
publicKey: cpubkey | |
}); | |
} | |
function getAddressPK(pk, idx) { | |
const derived = pk.derive(0).derive(idx); | |
const kr = KeyRing.fromPublic(derived.publicKey); | |
return kr.getAddress().toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment