forked from bcoin-org/bledger
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bcoin-p2wpkh.js
75 lines (57 loc) · 1.65 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
'use strict';
const bledger = require('../lib/bledger');
const {LedgerBcoin, LedgerTXInput} = bledger;
const {Device} = bledger.HID;
const MTX = require('bcoin/lib/primitives/mtx');
const KeyRing = require('bcoin/lib/primitives/keyring');
const fundUtil = require('../test/util/fund');
const ring = KeyRing.generate();
ring.witness = true;
const randWitness = ring.getAddress();
(async () => {
const devices = await Device.getDevices();
const device = new Device({
device: devices[0],
timeout: 5000
});
await device.open();
const bcoinApp = new LedgerBcoin({
device: device
});
// 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);
});