-
Notifications
You must be signed in to change notification settings - Fork 5
/
class.Coinbase.js
165 lines (126 loc) · 4.67 KB
/
class.Coinbase.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
'use strict';
const
scripts = require('@mintpond/mint-bitcoin-script'),
precon = require('@mintpond/mint-precon'),
mu = require('@mintpond/mint-utils'),
buffers = require('@mintpond/mint-utils').buffers,
Client = require('./class.Client'),
Share = require('./class.Share');
const
EXTRANONCE_SIZE = 8,
BUFFER_U32_ZERO = buffers.packUInt32LE(0),
BUFFER_VAR_ONE = buffers.packVarInt(1),
BUFFER_32_MAX = Buffer.from('FFFFFFFF', 'hex'),
BUFFER_TX_VERSION_1 = buffers.packUInt32LE(1),
BUFFER_INPUT_HASH = buffers.packUInt256LE(0);
class Coinbase {
/**
* Constructor.
*
* @param args
* @param args.coinbaseAddress {string}
* @param args.blockTemplate {object}
* @param [args.blockBrand] {string}
*/
constructor(args) {
precon.string(args.coinbaseAddress, 'coinbaseAddress');
precon.notNull(args.blockTemplate, 'blockTemplate');
precon.opt_string(args.blockBrand, 'blockBrand');
const _ = this;
_._coinbaseAddress = args.coinbaseAddress;
_._blockTemplate = args.blockTemplate;
_._blockBrand = args.blockBrand || '/@mintpond/ref-stratum/'
_._coinbase1 = null;
_._coinbase2 = null;
_._outputCount = 0;
_._time = mu.now();
_._blockBrandBuf = scripts.serializeString(_._blockBrand)
}
/**
* Get the first part of the serialized coinbase.
* @returns {Buffer}
*/
get coinbase1Buf() { return this._coinbase1 || (this._coinbase1 = this._createCoinbase1()); }
/**
* Get the seconds part of the serialized coinbase.
* @returns {Buffer}
*/
get coinbase2Buf() { return this._coinbase2 || (this._coinbase2 = this._createCoinbase2()); }
/**
* Use information from a client to serialize coinbase.
*
* @param client {Client}
* @returns {Buffer}
*/
serialize(client) {
precon.notNull(client, 'client');
const _ = this;
const coinbase1Buf = _.coinbase1Buf;
const coinbase2Buf = _.coinbase2Buf;
return Buffer.concat([
coinbase1Buf,
Buffer.from(client.extraNonce1Hex),
coinbase2Buf
]);
}
_createCoinbase1() {
const _ = this;
const inputScript = Buffer.concat([
/* block height */ scripts.serializeNumber(_._blockTemplate.height),
/* flags */ Buffer.from(_._blockTemplate.coinbaseaux.flags, 'hex'),
/* time */ scripts.serializeNumber(_._time),
/* extranonce length */ Buffer.from([EXTRANONCE_SIZE])
]);
const inputScriptLen = inputScript.length + EXTRANONCE_SIZE + _._blockBrandBuf.length;
// First part of coinbase which is split at the extra nonce values
return Buffer.concat([
/* version */ BUFFER_TX_VERSION_1,
// Tx Inputs
/* input count */ BUFFER_VAR_ONE,
/* input tx hash */ BUFFER_INPUT_HASH,
/* input vout */ BUFFER_32_MAX,
/* input scr len */ buffers.packVarInt(inputScriptLen),
/* input scr */ inputScript
// ...
]);
}
_createCoinbase2() {
const _ = this;
const outputsBuf = _._createOutputsBuf();
// Second part of coinbase which is split at the extra nonce values
return Buffer.concat([
// ...
/* block branding */ _._blockBrandBuf,
/* input sequence */ BUFFER_32_MAX,
// Tx Outputs
/* output count */ buffers.packVarInt(_._outputCount),
/* outputs */ outputsBuf,
/* lock time */ BUFFER_U32_ZERO
]);
}
_createOutputsBuf() {
const _ = this;
const outputsArr = [];
const blockTemplate = _._blockTemplate;
const poolAddressScript = scripts.makeAddressScript(_._coinbaseAddress)
let poolRewardSt = blockTemplate.coinbasevalue;
_._outputCount = 0;
_._addOutput(outputsArr, poolRewardSt, poolAddressScript, true);
const default_witness_commitment = blockTemplate.default_witness_commitment;
if (default_witness_commitment) {
const witnessCommitmentBuf = Buffer.from(default_witness_commitment, 'hex');
_._addOutput(outputsArr, 0, witnessCommitmentBuf);
}
return Buffer.concat(outputsArr);
}
_addOutput(outputBufArr, rewardSt, scriptBuff) {
const _ = this;
outputBufArr.push(
buffers.packUInt64LE(rewardSt),
buffers.packVarInt(scriptBuff.length),
scriptBuff
);
_._outputCount++;
}
}
module.exports = Coinbase;