Skip to content

Commit 4d8c167

Browse files
Cofresishumkov
authored andcommitted
Update cbTx with merkleroot of quorum commitments
1 parent 70bf022 commit 4d8c167

File tree

4 files changed

+181
-105
lines changed

4 files changed

+181
-105
lines changed

lib/transaction/payload/coinbasepayload.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ var HASH_SIZE = constants.SHA256_HASH_SIZE;
1919
* @property {number} version
2020
* @property {number} height
2121
* @property {string} merkleRootMNList
22+
* @property {string} merkleRootQuorums
2223
*/
2324

2425
/**
2526
* @class CoinbasePayload
2627
* @property {number} version
2728
* @property {number} height
2829
* @property {string} merkleRootMNList
30+
* @property {string} merkleRootQuorums
2931
*/
3032
function CoinbasePayload() {
3133
AbstractPayload.call(this);
@@ -48,6 +50,9 @@ CoinbasePayload.fromBuffer = function (rawPayload) {
4850
payload.version = payloadBufferReader.readUInt16LE();
4951
payload.height = payloadBufferReader.readUInt32LE();
5052
payload.merkleRootMNList = payloadBufferReader.read(HASH_SIZE).reverse().toString('hex');
53+
if (payload.version >= 2) {
54+
payload.merkleRootQuorums = payloadBufferReader.read(HASH_SIZE).reverse().toString('hex');
55+
}
5156

5257
if (!payloadBufferReader.finished()) {
5358
throw new Error('Failed to parse payload: raw payload is bigger than expected.');
@@ -67,6 +72,9 @@ CoinbasePayload.fromJSON = function fromJSON(payloadJson) {
6772
payload.version = payloadJson.version;
6873
payload.height = payloadJson.height;
6974
payload.merkleRootMNList = payloadJson.merkleRootMNList;
75+
if (payload.version >= 2) {
76+
payload.merkleRootQuorums = payloadJson.merkleRootQuorums;
77+
}
7078

7179
payload.validate();
7280
return payload;
@@ -83,6 +91,10 @@ CoinbasePayload.prototype.validate = function() {
8391
Preconditions.checkArgument(isUnsignedInteger(this.height), 'Expect height to be an unsigned integer');
8492
Preconditions.checkArgument(isHexString(this.merkleRootMNList), 'expect merkleRootMNList to be a hex string but got ' + typeof this.merkleRootMNList);
8593
Preconditions.checkArgument(this.merkleRootMNList.length === constants.SHA256_HASH_SIZE * 2, 'Invalid merkleRootMNList size');
94+
if (this.version >= 2) {
95+
Preconditions.checkArgument(isHexString(this.merkleRootQuorums), 'expect merkleRootQuorums to be a hex string but got ' + typeof this.merkleRootQuorums);
96+
Preconditions.checkArgument(this.merkleRootQuorums.length === constants.SHA256_HASH_SIZE * 2, 'Invalid merkleRootQuorums size');
97+
}
8698
return true;
8799
};
88100

@@ -92,11 +104,15 @@ CoinbasePayload.prototype.validate = function() {
92104
*/
93105
CoinbasePayload.prototype.toJSON = function toJSON() {
94106
this.validate();
95-
return {
107+
const json = {
96108
version: this.version,
97109
height: this.height,
98-
merkleRootMNList: this.merkleRootMNList
110+
merkleRootMNList: this.merkleRootMNList,
99111
};
112+
if (this.version >= 2) {
113+
json.merkleRootQuorums = this.merkleRootQuorums
114+
}
115+
return json;
100116
};
101117

102118
/**
@@ -112,6 +128,10 @@ CoinbasePayload.prototype.toBuffer = function toBuffer() {
112128
.writeUInt32LE(this.height)
113129
.write(Buffer.from(this.merkleRootMNList, 'hex').reverse());
114130

131+
if (this.version >= 2) {
132+
payloadBufferWriter.write(Buffer.from(this.merkleRootQuorums, 'hex').reverse());
133+
}
134+
115135
return payloadBufferWriter.toBuffer();
116136
};
117137

0 commit comments

Comments
 (0)