-
Notifications
You must be signed in to change notification settings - Fork 2
/
regtestTest.ts
94 lines (78 loc) · 3.12 KB
/
regtestTest.ts
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
/* tslint:disable:no-console */
import child_process = require('child_process');
import {
keyring as KeyRing,
script as Script,
coin as Coin,
amount as Amount,
util,
} from 'bcoin';
import { genCommitTx, genLockTx, genUnlockTx } from './lib/txs';
const wif = 'cTV3FM3RfiFwmHfX6x43g4Xp8qeLbi15pNELuWF9sV3renVZ63nB';
const ring = KeyRing.fromSecret(wif);
const addr = ring.getAddress().toBase58('testnet');
const serviceWif = 'cRMzGH4towfYVCref4Qz9iyfKaRkvfgVvZ2qk4hExMR7FcpzzVg6';
const serviceRing = KeyRing.fromSecret(serviceWif);
const servicePubKey = serviceRing.getPublicKey();
// console.log(serviceRing.getAddress().toBase58('testnet'));
console.log(ring.getPublicKey().toString('hex'));
console.log(servicePubKey.toString('hex'));
child_process.exec(`bitcoin-cli -regtest listunspent 0 9999999 "[\\"${addr}\\"]"`, (err, stdout, stderr) => {
if (err) {
console.log(err);
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
// console.log(`stdout: ${stdout}`);
// console.log(`stderr: ${stderr}`);
const unspentData = JSON.parse(stdout);
// console.log(unspentData);
const coins: Coin[] = [];
for (const utxo of unspentData) {
const amt = Amount.fromBTC(utxo.amount).toSatoshis(true) as number;
// console.log(typeof(amt));
// console.log(amt as any instanceof Amount);
const coinOpts = {
version: 1,
height: -1,
value: amt,
script: Script.fromRaw(utxo.scriptPubKey, 'hex'),
hash: util.revHex(utxo.txid),
index: utxo.vout,
};
coins.push(new Coin(coinOpts));
}
console.log(addr);
const commitFee = 500000;
const registerFee = 500000;
const escrowFee = 1000000;
const feeRate = 11000;
child_process.exec('bitcoin-cli -regtest getblockchaininfo', (err2, stdout2, stderr2) => {
if (err2) {
console.log(err2);
// node couldn't execute the command
return;
}
const blockchainData = JSON.parse(stdout2);
// console.log(blockchainData);
const height = blockchainData.blocks;
// console.log(height, typeof height);
const name = 'colin';
const locktime = height + 10;
const commitTX = genCommitTx(
coins, name, locktime, commitFee, registerFee, escrowFee, feeRate, ring, servicePubKey,
);
console.log('Commit');
console.log(commitTX.toRaw().toString('hex'));
const lockTX = genLockTx(commitTX, name, registerFee, escrowFee, feeRate, ring, servicePubKey, locktime);
console.log('Lock');
console.log(lockTX.toRaw().toString('hex'));
const serviceUnlockTX = genUnlockTx(lockTX, commitTX, feeRate * 2, true, serviceRing, ring.getPublicKey());
console.log('Service Unlock');
console.log(serviceUnlockTX.toRaw().toString('hex'));
const userUnlockTX = genUnlockTx(lockTX, commitTX, feeRate, false, ring, servicePubKey);
console.log('User Unlock');
console.log(userUnlockTX.toRaw().toString('hex'));
});
});