-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction.js
51 lines (42 loc) · 1.25 KB
/
transaction.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
var bt = require("bitcoinjs-lib")
var fs = require('fs');
const argv = require('yargs').alias('T', 'testnet').boolean('T').argv
if (argv.T) {
var network = bt.networks.testnet
var cointype = 1
} else {
var network = bt.networks.bitcoin
var cointype = 0
}
var seed = process.env.SEED
if (!seed) {
throw new Error("no seed specified")
}
var master = bt.HDNode.fromSeedBuffer(Buffer.from(seed, 'base64'), network)
if (argv._.length < 1) {
throw new Error("txinfo not specified")
}
var infofile = argv._[0]
var txinfo = JSON.parse(fs.readFileSync(infofile, 'utf8'))
var txb = new bt.TransactionBuilder(network)
txinfo.inputs.forEach(function(txin) {
txb.addInput(txin.tx_hash, txin.index, null, Buffer.from(txin.pubkey_script, 'hex'))
})
txinfo.outputs.forEach(function(txout) {
txb.addOutput(txout.address, txout.amount)
})
txinfo.inputs.forEach(function(txin, idx) {
txin.keys.forEach(function(path) {
var key = master.derivePath(path)
var redeem
var witscr
if (txin.redeem_script) {
redeem = Buffer.from(txin.redeem_script, 'hex')
}
if (txin.witness_script) {
witscr = Buffer.from(txin.witness_script, 'hex')
}
txb.sign(idx, key, redeem, null, txin.amount, witscr)
})
})
console.log(txb.build().toHex())