-
Notifications
You must be signed in to change notification settings - Fork 808
/
watch-only-wallet.js
70 lines (52 loc) · 1.92 KB
/
watch-only-wallet.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
'use strict';
const bcoin = require('../..');
(async () => {
// use well known test mnemonic
const phrase = [
'abandon', 'abandon', 'abandon', 'abandon',
'abandon', 'abandon', 'abandon', 'abandon',
'abandon', 'abandon', 'abandon', 'about'
].join(' ');
const network = bcoin.Network.get('regtest');
const mnemonic = bcoin.Mnemonic.fromPhrase(phrase);
// m'
const priv = bcoin.HDPrivateKey.fromMnemonic(mnemonic);
// m'/44'
const bip44Key = priv.derive(44, true);
// m'/44'/0'
const bitcoinKey = bip44Key.derive(0, true);
// m'/44'/0'/0'
const accountKey = bitcoinKey.derive(0, true);
// account extended public key
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#Serialization_format
const xpub = accountKey.xpubkey(network.type);
console.log('xpub to import:\n', xpub);
// recommended to use hardware wallet to derive keys
// see github.com/bcoin-org/bledger
// create watch only wallet
// the wallet will generate lookahead
// addresses from the account extended public key
// and can find spendable coins in the blockchain state
const wdb = new bcoin.wallet.WalletDB({
network: 'regtest',
memory: true
});
await wdb.open();
// new wallet still generates a master private key, but it will not be used
const wallet = await wdb.create({
name: 'my-watch-only-wallet',
accountKey: xpub,
watchOnly: true
});
// xpub account key placed at Account 0. Address 0 is already derived.
const acct0 = await wallet.getAccount(0);
// create new receive addresses through the deterministic chain
const key1 = await wallet.createReceive(0);
const addr1 = key1.getAddress('string', 'regtest');
const key2 = await wallet.createReceive(0);
const addr2 = key2.getAddress('string', 'regtest');
console.log('Wallet:\n', wallet);
console.log('Account:\n', acct0);
console.log('Address 1:\n', addr1);
console.log('Address 2:\n', addr2);
})();