-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
143 lines (120 loc) · 3.87 KB
/
index.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
let BITBOXCli = require("bitbox-cli/lib/bitbox-cli").default;
let BITBOX = new BITBOXCli({ restURL: "https://trest.bitcoin.com/v1/" });
const _ = require("lodash");
function getNode(mnemonic) {
let rootSeed = BITBOX.Mnemonic.toSeed(mnemonic);
// master HDNode
let masterHDNode = BITBOX.HDNode.fromSeed(rootSeed, "testnet");
// HDNode of BIP44 account
let account = BITBOX.HDNode.derivePath(masterHDNode, "m/44'/145'/0'");
// derive the first external change address HDNode
let change = BITBOX.HDNode.derivePath(account, "0/0");
return change;
}
function p2pkhScript(toAddress) {
return [
BITBOX.Script.opcodes.OP_DUP,
BITBOX.Script.opcodes.OP_HASH160,
Buffer.from(
BITBOX.BitcoinCash.decodeBase58Check(
BITBOX.Address.toLegacyAddress(toAddress)
),
"hex"
),
BITBOX.Script.opcodes.OP_EQUALVERIFY,
BITBOX.Script.opcodes.OP_CHECKSIG
];
}
async function sendTransaction(
node,
sendAmount,
toAddress,
hashedSecret,
unlockFor
) {
let transactionBuilder = new BITBOX.TransactionBuilder("testnet");
// add input
let utxos = _.chain(await BITBOX.Address.utxo([cashAddress]))
.flatten()
.orderBy(["satoshis"], ["desc"])
.value();
// const originalAmount = utxos.reduce((r, utxo) => {
// // TODO: confirmation数を加味する
// // console.log(utxo);
// return r + utxo.satoshis;
// }, 0);
transactionBuilder.addInput(utxos[0].txid, utxos[0].vout);
let originalAmount = utxos[0].satoshis;
console.log("utxos:", originalAmount);
// お釣りを計算
let fee = 600;
let changeAmount = originalAmount - sendAmount - fee;
console.log("change", changeAmount);
// 取引用outputを追加
let lockTimeBuf = Buffer.alloc(4);
lockTimeBuf.writeUInt32LE(Math.floor(Date.now() / 1000) + unlockFor, 0);
let data = BITBOX.Script.encode([
BITBOX.Script.opcodes.OP_IF,
BITBOX.Script.opcodes.OP_HASH160,
hashedSecret,
BITBOX.Script.opcodes.OP_EQUALVERIFY,
...p2pkhScript(toAddress),
BITBOX.Script.opcodes.OP_ELSE,
lockTimeBuf,
BITBOX.Script.opcodes.OP_CHECKLOCKTIMEVERIFY,
BITBOX.Script.opcodes.OP_DROP,
...p2pkhScript(BITBOX.HDNode.toCashAddress(node)),
BITBOX.Script.opcodes.OP_ENDIF
]);
// console.log(`p2script: ${data.toString("hex")}`);
let p2sh_hash160 = BITBOX.Crypto.hash160(data);
let scriptPubKey = BITBOX.Script.scriptHash.output.encode(p2sh_hash160);
let address = BITBOX.Address.fromOutputScript(scriptPubKey, "testnet");
// console.log(`script addr: ${address}`);
transactionBuilder.addOutput(address, sendAmount);
// お釣りを追加
transactionBuilder.addOutput(BITBOX.HDNode.toCashAddress(node), changeAmount);
let keyPair = BITBOX.HDNode.toKeyPair(node);
let redeemScript;
transactionBuilder.sign(
0,
keyPair,
redeemScript,
transactionBuilder.hashTypes.SIGHASH_ALL,
originalAmount
);
let tx = transactionBuilder.build();
let hex = tx.toHex();
BITBOX.RawTransactions.sendRawTransaction(hex).then(
result => {
console.log(result);
},
err => {
console.log(err);
}
);
}
const secret = BITBOX.Crypto.randomBytes(32);
// console.log(`secret: ${secret.toString("hex")}`);
let mnemonic =
"abstract general fiscal enough behind patch nephew fever float parrot afford barely describe motion long that neither have raw shift index reveal cloth change";
let node = getNode(mnemonic);
// console.log(node);
let cashAddress = BITBOX.HDNode.toCashAddress(node);
let legacyAddress = BITBOX.Address.toLegacyAddress(cashAddress);
// console.log(`cashAddress: ${cashAddress}`);
// console.log(`cashAddress(legacy): ${legacyAddress}`);
(async () => {
try {
let result = await sendTransaction(
node,
2000,
cashAddress,
BITBOX.Crypto.hash160(secret),
60 * 60 * 24 * 14 // 2 weeks
);
console.log(result);
} catch (error) {
console.error(error);
}
})();