forked from trezor/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-reftx.js
67 lines (58 loc) · 2.2 KB
/
gen-reftx.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
const BitcoinJs = require('@trezor/utxo-lib');
// Referenced transaction generator script.
// Transform bitcoin-like transaction data in to format required by tests of signTransaction method.
// Data are stored in <project-root>/tests/__txcache__/[network]
// Step 1.
// Go to blockchain explorer and find referenced tx by input.prev_hash and locate tx HEX
const hex = '';
if (!hex) throw new Error('tx hex not provided');
// Step 2. set network
const tx = BitcoinJs.Transaction.fromHex(hex, { network: BitcoinJs.networks.testnet });
const reverseBuffer = buf => {
const copy = Buffer.alloc(buf.length);
buf.copy(copy);
[].reverse.call(copy);
return copy;
};
const inputsMap = input => ({
prev_index: input.index,
sequence: input.sequence,
prev_hash: reverseBuffer(input.hash).toString('hex'),
script_sig: input.script.toString('hex'),
});
const binOutputsMap = output => ({
amount: output.value,
script_pubkey: output.script.toString('hex'),
});
const enhanceTransaction = (refTx, srcTx) => {
const extraData = srcTx.getExtraData();
if (extraData) {
refTx.extra_data = extraData.toString('hex');
}
const specific = srcTx.getSpecificData();
if (specific) {
if (specific.type === 'zcash' && specific.versionGroupId && refTx.version >= 3) {
refTx.version_group_id = specific.versionGroupId;
}
if (specific.type === 'dash' && srcTx.type && srcTx.version >= 3) {
refTx.version |= srcTx.type << 16;
}
}
return refTx;
};
const refTx = enhanceTransaction(
{
version: tx.version,
inputs: tx.ins.map(inputsMap),
bin_outputs: tx.outs.map(binOutputsMap),
lock_time: tx.locktime,
timestamp: tx.timestamp,
expiry: tx.expiry,
},
tx,
);
// Step 3. run script
// node tests/__txcache__/gen-reftx.js > tests/__txcache__/[network]/[input.prev_hash].json
// node tests/__txcache__/gen-reftx.js > tests/__txcache__/dash/adb43bcd8fc99d6ed353c30ca8e5bd5996cd7bcf719bd4253f103dfb7227f6ed.json
// node tests/__txcache__/gen-reftx.js > tests/__txcache__/testnet/f405b50dff7053f3697f485f95fe1c0f6a4f5e52446281b4ef470c2762a15dae.json
console.log(JSON.stringify(refTx, null, 2));