forked from bcoin-org/bledger
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bcoin-spendP2SH.js
91 lines (68 loc) · 2.02 KB
/
bcoin-spendP2SH.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
83
84
85
86
87
88
89
90
91
'use strict';
const MTX = require('bcoin/lib/primitives/mtx');
const Amount = require('bcoin/lib/btc/amount');
const {Script} = require('bcoin/lib/script');
const bledger = require('../lib/bledger');
const {LedgerBcoin, LedgerTXInput} = bledger;
const {Device} = bledger.HID;
const Logger = require('blgr');
const fundUtil = require('../test/util/fund');
(async () => {
const devices = await Device.getDevices();
const logger = new Logger({
console: true,
level: 'info'
});
await logger.open();
const device = new Device({
device: devices[0],
timeout: 20000,
logger: logger
});
await device.open();
const accounts = [
{ path: 'm/44\'/0\'/0\'/0/0' },
{ path: 'm/44\'/0\'/1\'/0/0' }
];
const bcoinApp = new LedgerBcoin({ device });
for (const acc of accounts) {
acc.hd = await bcoinApp.getPublicKey(acc.path);
acc.pk = acc.hd.publicKey;
}
const [m, n] = [2, accounts.length];
const [pk1, pk2] = [accounts[0].pk, accounts[1].pk];
const multisigScript = Script.fromMultisig(m, n, [pk1, pk2]);
const addr = multisigScript.getAddress().toBase58();
console.log(`Funding Address: ${addr}\n`);
const {coins, txs} = await fundUtil.fundAddress(addr, 2);
console.log('Constructing spend transaction');
const mtx = new MTX();
mtx.addOutput({
address: '3Bi9H1hzCHWJoFEjc4xzVzEMywi35dyvsV',
value: Amount.fromBTC(1).toValue()
});
await mtx.fund(coins, {
changeAddress: addr
});
console.log('Create LedgerInputs for each input and each signer');
const ledgerInputs = [];
for (const acc of accounts) {
for (const tx of txs) {
const ledgerInput = new LedgerTXInput({
tx: tx,
index: 0,
redeem: multisigScript,
path: acc.path,
publicKey: acc.pk
});
ledgerInputs.push(ledgerInput);
}
}
await bcoinApp.signTransaction(mtx, ledgerInputs);
console.log(mtx);
console.log(`Valid Transaction: ${mtx.verify()}.`);
await device.close();
})().catch((e) => {
console.error(e);
process.exit(1);
});