-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtransactions.js
118 lines (98 loc) · 3.19 KB
/
transactions.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
var bitcoin = require('bitcoinjs-lib');
var util = require('./util.js');
// public members
var txHash;
exports.txHash = function(){
return txHash;
};
function scriptCompile(addrHash){
script = bitcoin.script.compile(
[
bitcoin.opcodes.OP_DUP,
bitcoin.opcodes.OP_HASH160,
addrHash,
bitcoin.opcodes.OP_EQUALVERIFY,
bitcoin.opcodes.OP_CHECKSIG
]);
return script;
}
function scriptFoundersCompile(address){
script = bitcoin.script.compile(
[
bitcoin.opcodes.OP_HASH160,
address,
bitcoin.opcodes.OP_EQUAL
]);
return script;
}
exports.createGeneration = function(rpcData, blockReward, feeReward, recipients, poolAddress){
var _this = this;
var blockPollingIntervalId;
var emitLog = function (text) {
_this.emit('log', 'debug', text);
};
var emitWarningLog = function (text) {
_this.emit('log', 'warning', text);
};
var emitErrorLog = function (text) {
_this.emit('log', 'error', text);
};
var emitSpecialLog = function (text) {
_this.emit('log', 'special', text);
};
var poolAddrHash = bitcoin.address.fromBase58Check(poolAddress).hash;
var tx = new bitcoin.Transaction();
var blockHeight = rpcData.height;
// input for coinbase tx
if (blockHeight.toString(16).length % 2 === 0) {
var blockHeightSerial = blockHeight.toString(16);
} else {
var blockHeightSerial = '0' + blockHeight.toString(16);
}
var height = Math.ceil((blockHeight << 1).toString(2).length / 8);
var lengthDiff = blockHeightSerial.length/2 - height;
for (var i = 0; i < lengthDiff; i++) {
blockHeightSerial = blockHeightSerial + '00';
}
length = '0' + height;
var serializedBlockHeight = new Buffer.concat([
new Buffer(length, 'hex'),
util.reverseBuffer(new Buffer(blockHeightSerial, 'hex')),
new Buffer('00', 'hex') // OP_0
]);
tx.addInput(new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex'),
0xFFFFFFFF,
0xFFFFFFFF,
new Buffer.concat([serializedBlockHeight,
Buffer('6b6177706f77', 'hex')])
);
// calculate total fees
var feePercent = 0;
for (var i = 0; i < recipients.length; i++) {
feePercent = feePercent + recipients[i].percent;
}
tx.addOutput(
scriptCompile(poolAddrHash),
Math.floor(blockReward * (1 - (feePercent / 100)))
);
for (var i = 0; i < recipients.length; i++) {
tx.addOutput(
scriptCompile(bitcoin.address.fromBase58Check(recipients[i].address).hash),
Math.round((blockReward) * (recipients[i].percent / 100))
);
}
if (rpcData.default_witness_commitment !== undefined) {
tx.addOutput(new Buffer(rpcData.default_witness_commitment, 'hex'), 0);
}
txHex = tx.toHex();
// this txHash is used elsewhere. Don't remove it.
txHash = tx.getHash().toString('hex');
return txHex;
};
module.exports.getFees = function(feeArray){
var fee = Number();
feeArray.forEach(function(value) {
fee = fee + Number(value.fee);
});
return fee;
};