/
spend-p2sh-p2wpkh.js
55 lines (41 loc) · 1.21 KB
/
spend-p2sh-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
'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.getNestedAddress();
console.log(`Address: ${address}`);
const sendTo = 'RTJCrETrS6m1otqXRRxkGCReRpbGzabDRi';
const txhash = revHex('36c36ffa8f192a424d0a8e3ba5fde0e7764a5a2f'
+' 146db32854a5d71b18218cfa');
const txinfo = {
// prevout
hash: txhash,
index: 0,
value: Amount.fromBTC('20').toValue(),
script: Script.fromAddress(address)
};
const coin = Coin.fromOptions(txinfo);
(async () => {
const spend = new MTX();
// Let's spend 10 BTC only
spend.addOutput(sendTo, Amount.fromBTC('10').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);
});