-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbcoin-p2wpkh.js
82 lines (62 loc) · 1.77 KB
/
bcoin-p2wpkh.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
75
76
77
78
79
80
81
82
'use strict';
const bledger = require('../lib/bledger');
const {LedgerBcoin, LedgerTXInput} = bledger;
const {Device} = bledger.USB;
const MTX = require('bcoin/lib/primitives/mtx');
const KeyRing = require('bcoin/lib/primitives/keyring');
const fundUtil = require('../test/util/fund');
const Logger = require('blgr');
const ring = KeyRing.generate();
ring.witness = true;
const randWitness = ring.getAddress();
(async () => {
const logger = new Logger({
console: true,
level: 'info'
});
await logger.open();
// get first device available.
const device = await Device.requestDevice();
device.set({
timeout: 5000,
logger
});
await device.open();
const bcoinApp = new LedgerBcoin({ device, logger });
// Create witness address
const path = 'm/44\'/0\'/1\'/0/0';
const hdpub = await bcoinApp.getPublicKey(path);
const ring = await KeyRing.fromPublic(hdpub.publicKey);
ring.witness = true;
const address = ring.getAddress();
// Using our fundUtil we can mock a funding
// transaction and use an output from that tx to
// create our new transaction
const {coins, txs} = await fundUtil.fundAddressFromWitness(address, 1);
const mtx = new MTX();
mtx.addOutput({
address: randWitness,
value: 10000000
});
await mtx.fund(coins, {
changeAddress: ring.getAddress(),
subtractFee: true
});
const ledgerInputs = [];
for (const tx of txs) {
const ledgerInput = new LedgerTXInput({
witness: true,
tx: tx,
index: 0,
path: path,
publicKey: hdpub.publicKey
});
ledgerInputs.push(ledgerInput);
}
await bcoinApp.signTransaction(mtx, ledgerInputs);
console.log(`Valid Transaction: ${mtx.verify()}.`);
await device.close();
})().catch((e) => {
console.error(e);
process.exit(1);
});