-
Notifications
You must be signed in to change notification settings - Fork 1
/
spend-p2wpkh.js
59 lines (44 loc) · 1.32 KB
/
spend-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
'use strict';
const ringUtils = require('./utils/keys');
const assert = require('assert');
const bcoin = require('bcoin');
const Amount = bcoin.amount;
const Coin = bcoin.coin;
const MTX = bcoin.mtx;
const Script = bcoin.script;
const revHex = bcoin.util.revHex;
const network = 'regtest';
const [ring] = ringUtils.getRings(1, network);
ring.witness = true;
const address = ring.getAddress();
console.log(`Address where we received money: ${address}`);
const sendTo = 'RTJCrETrS6m1otqXRRxkGCReRpbGzabDRi';
const txhash = revHex('88885ac82ab0b61e909755e7f64f2deeedb89c83'
+ '3b68242da7de98c0934e1143');
const txinfo = {
// prevout
hash: txhash,
index: 1,
value: Amount.fromBTC('200').toValue(),
script: Script.fromAddress(address)
};
const coin = Coin.fromOptions(txinfo);
(async () => {
const spend = new MTX();
// We need to calculate fee too
// let's use .fund that will calculate fee
// for us
// Let's spend 100 BTC only
spend.addOutput(sendTo, Amount.fromBTC('100').toValue());
await spend.fund([coin], {
rate: 10000,
changeAddress: address
});
spend.sign(ring);
assert(spend.verify());
console.log('Transaction is ready');
console.log('Now you can broadcast it to the network');
console.log(spend.toRaw().toString('hex'));
})().catch((e) => {
console.error(e);
});