-
Notifications
You must be signed in to change notification settings - Fork 0
/
finalize.js
87 lines (74 loc) · 2.44 KB
/
finalize.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
const Coin = require('hsd/lib/primitives/coin.js');
const Keyring = require('hsd/lib/primitives/keyring.js');
const Output = require('hsd/lib/primitives/output.js');
const rules = require('hsd/lib/covenants/rules.js');
const Address = require('hsd/lib/primitives/address.js');
const Witness = require('hsd/lib/script/witness.js');
const MTX = require('hsd/lib/primitives/mtx.js');
const { createLockScript } = require('./script.js');
const common = require('hsd/lib/script/common.js');
const assert = require('assert').strict;
const { ALL } = common.hashType;
module.exports = {
fundMTX,
signMTX,
getCoins
};
async function getCoins () {
const { wallet, network } = this;
const coinsJSON = await wallet.getCoins('default');
const coins = coinsJSON.sort((a, b) => b.value - a.value).map((c) => new Coin().fromJSON(c));
return coins;
}
async function fundMTX (mtx, coins, changeAddress) {
const { wallet, network } = this;
const info = await this.execNode('getblockchaininfo');
const feeRes = await this.execNode('estimatesmartfee', 10);
const rate = Math.max(Number(feeRes.fee), 5000);
// console.log('change address:', changeAddress);
await mtx.fund(coins, {
rate,
changeAddress,
height: info.blocks,
coinbaseMaturity: network.coinbaseMaturity
});
return mtx;
}
// ---
async function createRings (mtx, startIdx = 0) {
const { wallet } = this;
const passphrase = await this.getPassphrase();
const rings = [];
for (let i = startIdx; i < mtx.inputs.length; i++) {
const input = mtx.inputs[i];
const prevout = mtx.view.getEntry(input.prevout).output;
const address = prevout.address.toString(this.networkName);
const privKeyWIF = await wallet.getWIF(address, passphrase);
rings.push(
new Keyring().fromSecret(privKeyWIF.privateKey, this.networkName)
);
}
return rings;
}
async function signMTX (mtx) {
const rings = await createRings.call(this, mtx, 0);
const signed = mtx.sign(rings, ALL);
assert(signed, 'Transaction failed to sign.');
return mtx;
}
async function getRenewalBlock (context) {
const { network, nodeClient } = context;
const info = await context.execNode('getblockchaininfo');
let height = info.blocks - network.names.renewalMaturity * 2;
if (height < 0) {
height = 0;
}
const block = await nodeClient.getBlock(height);
return block.hash;
}
function stringEnum (items) {
return items.reduce((acc, curr) => {
acc[curr] = curr;
return acc;
}, {});
}