-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsignTransaction-p2pkh.js
98 lines (81 loc) · 2.46 KB
/
signTransaction-p2pkh.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
92
93
94
95
96
97
98
'use strict';
const Logger = require('blgr');
const {Coin, MTX, KeyRing, Script} = require('hsd');
const rules = require('hsd/lib/covenants/rules');
const util = require('../test/utils/fund');
const {HID, LedgerHSD, LedgerInput} = require('../lib/hsd-ledger');
const {Device} = HID;
(async () => {
// Create logger.
const logger = new Logger({
console: true,
level: 'info'
});
// Get first device available and
// set optional properties.
const device = await Device.requestDevice();
device.set({
timeout: 1000 * 60 * 5, // optional (default is 5mins)
logger: logger // optional
});
// Create ledger client object.
const ledger = new LedgerHSD({
device: device,
network: 'regtest',
logger: logger // optional
});
// Open logger and device.
await logger.open();
await device.open();
// Creating a test transaction using testing utils.
// '../test/e2e/' has examples using an `hsd` full node.
const mtx = new MTX();
const pubkey = await ledger.getPublicKey('m/44\'/5355\'/0\'/0/0');
const ring = await KeyRing.fromPublic(pubkey);
const addr = ring.getAddress();
const {coins, txs} = await util.fundAddress(addr, 1);
mtx.addOutput({
address: KeyRing.generate().getAddress(),
value: 10000000
});
await mtx.fund(coins, {
changeAddress: ring.getAddress(),
subtractFee: true
});
const ledgerInput = new LedgerInput({
coin: Coin.fromTX(txs[0], 0, -1),
input: mtx.inputs[0],
index: 0,
path: 'm/44\'/5355\'/0\'/0/0',
publicKey: pubkey,
type: Script.hashType.ALL
});
let fees = 0;
for (let i = 0; i < mtx.inputs.length; i++) {
const input = mtx.inputs[i];
const coin = mtx.view.getCoinFor(input);
fees += coin.value;
}
logger.info(`Confirm details for TXID: ${mtx.txid()}`);
logger.info('');
for (let i = 0; i < mtx.outputs.length; i++) {
const output = mtx.outputs[i];
fees -= output.value;
logger.info(`Output #${i+1}`);
logger.info(`Covenant: ${rules.typesByVal[output.covenant.type]}`);
logger.info(`Value: ${output.value/1e6}`);
logger.info(`Address: ${output.address.toString('regtest')}`);
logger.info('');
}
logger.info(`Fees: ${fees/1e6}`);
const signed = await ledger.signTransaction(mtx, {
inputs: [ledgerInput]
});
logger.info(`Result of TX.verify(): ${signed.verify()}.`);
// Close logger and device.
await device.close();
await logger.close();
})().catch((e) => {
console.error(e);
process.exit(1);
});