forked from bcoin-org/bledger
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bcoin-getXPUBs.js
74 lines (54 loc) · 1.54 KB
/
bcoin-getXPUBs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
const bledger = require('../lib/bledger');
const {LedgerBcoin} = bledger;
const {Device} = bledger.HID;
const Logger = require('blgr');
const KeyRing = require('bcoin/lib/primitives/keyring');
const NETWORK = 'regtest';
const XPUBS = 1;
const ADDRESSES = 4;
const CHANGE = true;
(async () => {
const devices = await Device.getDevices();
const logger = new Logger({
console: true,
level: 'debug'
});
await logger.open();
const device = new Device({
device: devices[0],
timeout: 5000,
logger
});
await device.open();
const ledgerBcoin = new LedgerBcoin({ device });
const xpubs = {};
for (let i = 0; i < XPUBS; i++) {
const path = `m/44'/0'/${i}'`;
xpubs[path] = await getPublicKey(ledgerBcoin, path);
}
for (const key of Object.keys(xpubs)) {
const xpub = xpubs[key];
console.log(`Account: ${key} addresses:`);
for (let i = 0; i < ADDRESSES; i++) {
const address = deriveAddress(xpub, 0, i, NETWORK);
console.log(` /0/${i}: ${address}`);
if (CHANGE) {
const change = deriveAddress(xpub, 1, i, NETWORK);
console.log(` /1/${i}: ${change}\n`);
}
}
}
await device.close();
})().catch((e) => {
console.error(e);
process.exit(1);
});
async function getPublicKey(btcApp, path) {
return await btcApp.getPublicKey(path);
}
function deriveAddress(hd, change, index, network) {
const pubkey = hd.derive(change).derive(index);
const keyring = KeyRing.fromPublic(pubkey.publicKey, network);
return keyring.getAddress().toString();
}