forked from bitpay/bitcore-lib
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathQuorumEntry.js
More file actions
549 lines (487 loc) · 16.6 KB
/
QuorumEntry.js
File metadata and controls
549 lines (487 loc) · 16.6 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
const _ = require('lodash');
const $ = require('../util/preconditions');
const BitArray = require('../util/bitarray');
const BufferReader = require('../encoding/bufferreader');
const BufferWriter = require('../encoding/bufferwriter');
const BufferUtil = require('../util/buffer');
const constants = require('../constants');
const Hash = require('../crypto/hash');
const bls = require('../crypto/bls');
const utils = require('../util/js');
const isHashQuorumIndexRequired = require('../util/isHashQuorumIndexRequired');
const {
isHexStringOfSize,
isUnsignedInteger,
isSha256HexString: isSha256,
isHexaString: isHexString,
} = utils;
const {
BLS_PUBLIC_KEY_SIZE,
BLS_SIGNATURE_SIZE,
SHA256_HASH_SIZE,
} = constants;
/**
* @typedef {Object} SMLQuorumEntry
* @property {number} version
* @property {number} llmqType
* @property {string} quorumHash
* @property {number} [quorumIndex]
* @property {number} signersCount
* @property {string} signers
* @property {number} validMembersCount
* @property {string} validMembers
* @property {string} quorumPublicKey
* @property {string} quorumVvecHash
* @property {string} quorumSig
* @property {string} membersSig
*/
/**
* @class QuorumEntry
* @param {string|Object|Buffer} [arg] - A Buffer, JSON string,
* or Object representing a SMLQuorumEntry
* @param {Object} [llmqParamsOverride] - LLMQ type override
* @constructor
* @property {number} version
* @property {number} llmqType
* @property {string} quorumHash
* @property {number} [quorumIndex]
* @property {number} signersCount
* @property {string} signers
* @property {number} validMembersCount
* @property {string} validMembers
* @property {string} quorumPublicKey
* @property {string} quorumVvecHash
* @property {string} quorumSig
* @property {string} membersSig
*/
function QuorumEntry(arg, llmqParamsOverride = {}) {
if (arg) {
if (arg instanceof QuorumEntry) {
return arg.copy();
}
if (BufferUtil.isBuffer(arg)) {
return QuorumEntry.fromBuffer(arg, llmqParamsOverride);
}
if (_.isObject(arg)) {
return QuorumEntry.fromObject(arg, llmqParamsOverride);
}
if (arg instanceof QuorumEntry) {
return arg.copy();
}
if (isHexString(arg)) {
return QuorumEntry.fromHexString(arg, llmqParamsOverride);
}
throw new TypeError('Unrecognized argument for QuorumEntry');
}
}
/**
* Parse buffer and returns QuorumEntry
* @param {Buffer} buffer
* @param {Object} [llmqParamsOverride]
* @return {QuorumEntry}
*/
QuorumEntry.fromBuffer = function fromBuffer(buffer, llmqParamsOverride = {}) {
const bufferReader = new BufferReader(buffer);
const SMLQuorumEntry = new QuorumEntry();
SMLQuorumEntry.llmqParamsOverride = llmqParamsOverride;
SMLQuorumEntry.isVerified = false;
if (buffer.length < 100) {
SMLQuorumEntry.isOutdatedRPC = true;
SMLQuorumEntry.version = bufferReader.readUInt16LE();
SMLQuorumEntry.llmqType = bufferReader.readUInt8();
SMLQuorumEntry.quorumHash = bufferReader
.read(constants.SHA256_HASH_SIZE)
.reverse()
.toString('hex');
if (isHashQuorumIndexRequired(this.version)) {
SMLQuorumEntry.quorumIndex = buffer.readInt16LE();
} else {
SMLQuorumEntry.quorumIndex = 0;
}
SMLQuorumEntry.signersCount = bufferReader.readVarintNum();
SMLQuorumEntry.validMembersCount = bufferReader.readVarintNum();
SMLQuorumEntry.quorumPublicKey = bufferReader
.read(BLS_PUBLIC_KEY_SIZE)
.toString('hex');
return SMLQuorumEntry;
}
SMLQuorumEntry.isOutdatedRPC = false;
SMLQuorumEntry.version = bufferReader.readUInt16LE();
SMLQuorumEntry.llmqType = bufferReader.readUInt8();
SMLQuorumEntry.quorumHash = bufferReader
.read(constants.SHA256_HASH_SIZE)
.reverse()
.toString('hex');
if (isHashQuorumIndexRequired(SMLQuorumEntry.version)) {
SMLQuorumEntry.quorumIndex = bufferReader.readInt16LE();
} else {
SMLQuorumEntry.quorumIndex = 0;
}
SMLQuorumEntry.signersCount = bufferReader.readVarintNum();
const signersBytesToRead =
Math.floor((SMLQuorumEntry.getParams().size + 7) / 8) || 1;
SMLQuorumEntry.signers = bufferReader
.read(signersBytesToRead)
.toString('hex');
SMLQuorumEntry.validMembersCount = bufferReader.readVarintNum();
const validMembersBytesToRead =
Math.floor((SMLQuorumEntry.getParams().size + 7) / 8) || 1;
SMLQuorumEntry.validMembers = bufferReader
.read(validMembersBytesToRead)
.toString('hex');
SMLQuorumEntry.quorumPublicKey = bufferReader
.read(BLS_PUBLIC_KEY_SIZE)
.toString('hex');
SMLQuorumEntry.quorumVvecHash = bufferReader
.read(SHA256_HASH_SIZE)
.reverse()
.toString('hex');
SMLQuorumEntry.quorumSig = bufferReader
.read(BLS_SIGNATURE_SIZE)
.toString('hex');
SMLQuorumEntry.membersSig = bufferReader
.read(BLS_SIGNATURE_SIZE)
.toString('hex');
return SMLQuorumEntry;
};
/**
* @param {string} string
* @param {Object} llmqParamsOverride
* @return {QuorumEntry}
*/
QuorumEntry.fromHexString = function fromString(string, llmqParamsOverride = {}) {
return QuorumEntry.fromBuffer(Buffer.from(string, 'hex'), llmqParamsOverride);
};
/**
* Serialize SML entry to buf
* @return {Buffer}
*/
QuorumEntry.prototype.toBuffer = function toBuffer() {
this.validate();
const bufferWriter = new BufferWriter();
bufferWriter.writeUInt16LE(this.version);
bufferWriter.writeUInt8(this.llmqType);
bufferWriter.write(Buffer.from(this.quorumHash, 'hex').reverse());
if (isHashQuorumIndexRequired(this.version)) {
bufferWriter.writeInt16LE(this.quorumIndex);
}
bufferWriter.writeVarintNum(this.signersCount);
if (this.isOutdatedRPC) {
bufferWriter.writeVarintNum(this.validMembersCount);
bufferWriter.write(Buffer.from(this.quorumPublicKey, 'hex'));
return bufferWriter.toBuffer();
}
bufferWriter.write(Buffer.from(this.signers, 'hex'));
bufferWriter.writeVarintNum(this.validMembersCount);
bufferWriter.write(Buffer.from(this.validMembers, 'hex'));
bufferWriter.write(Buffer.from(this.quorumPublicKey, 'hex'));
bufferWriter.write(Buffer.from(this.quorumVvecHash, 'hex').reverse());
bufferWriter.write(Buffer.from(this.quorumSig, 'hex'));
bufferWriter.write(Buffer.from(this.membersSig, 'hex'));
return bufferWriter.toBuffer();
};
/**
* Serialize SML entry to buf
* @return {Buffer}
*/
QuorumEntry.prototype.toBufferForHashing = function toBufferForHashing() {
this.validate();
const bufferWriter = new BufferWriter();
const fixedCounterLength = this.getParams().size;
bufferWriter.writeUInt16LE(this.version);
bufferWriter.writeUInt8(this.llmqType);
bufferWriter.write(Buffer.from(this.quorumHash, 'hex').reverse());
if (isHashQuorumIndexRequired(this.version)) {
bufferWriter.writeInt16LE(this.quorumIndex);
}
bufferWriter.writeVarintNum(fixedCounterLength);
bufferWriter.write(Buffer.from(this.signers, 'hex'));
bufferWriter.writeVarintNum(fixedCounterLength);
bufferWriter.write(Buffer.from(this.validMembers, 'hex'));
bufferWriter.write(Buffer.from(this.quorumPublicKey, 'hex'));
bufferWriter.write(Buffer.from(this.quorumVvecHash, 'hex').reverse());
bufferWriter.write(Buffer.from(this.quorumSig, 'hex'));
bufferWriter.write(Buffer.from(this.membersSig, 'hex'));
return bufferWriter.toBuffer();
};
/**
* Create SMLQuorumEntry from an object
* @param {SMLQuorumEntry} obj
* @param {Object} llmqParamsOverride
* @return {QuorumEntry}
*/
QuorumEntry.fromObject = function fromObject(obj, llmqParamsOverride = {}) {
const SMLQuorumEntry = new QuorumEntry();
SMLQuorumEntry.isVerified = false;
SMLQuorumEntry.isOutdatedRPC = false;
SMLQuorumEntry.version = obj.version;
SMLQuorumEntry.llmqType = obj.llmqType;
SMLQuorumEntry.quorumHash = obj.quorumHash;
SMLQuorumEntry.quorumIndex = obj.quorumIndex;
SMLQuorumEntry.signersCount = obj.signersCount;
SMLQuorumEntry.signers = obj.signers;
SMLQuorumEntry.validMembersCount = obj.validMembersCount;
SMLQuorumEntry.validMembers = obj.validMembers;
SMLQuorumEntry.quorumPublicKey = obj.quorumPublicKey;
SMLQuorumEntry.quorumVvecHash = obj.quorumVvecHash;
SMLQuorumEntry.quorumSig = obj.quorumSig;
SMLQuorumEntry.membersSig = obj.membersSig;
SMLQuorumEntry.llmqParamsOverride = llmqParamsOverride;
if (SMLQuorumEntry.signers === undefined) {
SMLQuorumEntry.isOutdatedRPC = true;
}
SMLQuorumEntry.validate();
return SMLQuorumEntry;
};
QuorumEntry.prototype.validate = function validate() {
$.checkArgument(
utils.isUnsignedInteger(this.version),
'Expect version to be an unsigned integer'
);
$.checkArgument(
utils.isUnsignedInteger(this.llmqType),
'Expect llmqType to be an unsigned integer'
);
$.checkArgument(
isSha256(this.quorumHash),
'Expected quorumHash to be a sha256 hex string'
);
if (isHashQuorumIndexRequired(this.version)) {
$.checkArgument(
Number.isInteger(this.quorumIndex),
'Expected quorumIndex to be an integer'
);
}
$.checkArgument(
isUnsignedInteger(this.signersCount),
'Expect signersCount to be an unsigned integer'
);
$.checkArgument(
isUnsignedInteger(this.validMembersCount),
'Expect validMembersCount to be an unsigned integer'
);
$.checkArgument(
isHexStringOfSize(this.quorumPublicKey, BLS_PUBLIC_KEY_SIZE * 2),
'Expected quorumPublicKey to be a bls pubkey'
);
if (!this.isOutdatedRPC) {
$.checkArgument(
utils.isHexaString(this.signers),
'Expect signers to be a hex string'
);
$.checkArgument(
utils.isHexaString(this.validMembers),
'Expect validMembers to be a hex string'
);
$.checkArgument(
isHexStringOfSize(this.quorumVvecHash, SHA256_HASH_SIZE * 2),
`Expected quorumVvecHash to be a hex string of size ${SHA256_HASH_SIZE}`
);
$.checkArgument(
isHexStringOfSize(this.quorumSig, BLS_SIGNATURE_SIZE * 2),
'Expected quorumSig to be a bls signature'
);
$.checkArgument(
isHexStringOfSize(this.membersSig, BLS_SIGNATURE_SIZE * 2),
'Expected membersSig to be a bls signature'
);
}
};
QuorumEntry.prototype.toObject = function toObject() {
const result = {
version: this.version,
llmqType: this.llmqType,
quorumHash: this.quorumHash,
signersCount: this.signersCount,
signers: this.signers,
validMembersCount: this.validMembersCount,
validMembers: this.validMembers,
quorumPublicKey: this.quorumPublicKey,
quorumVvecHash: this.quorumVvecHash,
quorumSig: this.quorumSig,
membersSig: this.membersSig,
};
result.quorumIndex = this.quorumIndex;
return result;
};
QuorumEntry.getParams = function getParams(llmqType, llmqParamsOverride = {}) {
let llmqParams = constants.LLMQ_TYPE_PARAMS;
if (Object.keys(llmqParamsOverride).length > 0) {
llmqParams = _.merge({}, constants.LLMQ_TYPE_PARAMS, llmqParamsOverride);
}
if (!llmqParams[llmqType]) {
throw new Error(`Invalid llmq type ${llmqType}`);
}
return llmqParams[llmqType];
};
/**
* @return {number}
*/
QuorumEntry.prototype.getParams = function getParams() {
return QuorumEntry.getParams(this.llmqType, this.llmqParamsOverride);
};
/**
* Serialize quorum entry commitment to buf
* This is the message hash signed by the quorum for verification
* @return {Uint8Array}
*/
QuorumEntry.prototype.getCommitmentHash = function getCommitmentHash() {
const bufferWriter = new BufferWriter();
bufferWriter.writeUInt8(this.llmqType);
bufferWriter.write(Buffer.from(this.quorumHash, 'hex').reverse());
bufferWriter.writeVarintNum(this.getParams().size);
bufferWriter.write(Buffer.from(this.validMembers, 'hex'));
bufferWriter.write(Buffer.from(this.quorumPublicKey, 'hex'));
bufferWriter.write(Buffer.from(this.quorumVvecHash, 'hex').reverse());
return Hash.sha256sha256(bufferWriter.toBuffer());
};
/**
* Verifies the quorum's bls threshold signature
* @return {Promise<boolean>}
*/
QuorumEntry.prototype.isValidQuorumSig = async function isValidQuorumSig() {
if (this.isOutdatedRPC) {
throw new Error(
'Quorum cannot be verified: node running on outdated DashCore version (< 0.16)'
);
}
return bls.verifySignature(
this.quorumSig,
Uint8Array.from(this.getCommitmentHash()),
this.quorumPublicKey
);
};
/**
* Verifies the quorum's aggregated operator key signature
* @param {SimplifiedMNList} mnList - MNList for the block (quorumHash)
* @return {Promise<boolean>}
*/
QuorumEntry.prototype.isValidMemberSig = async function isValidMemberSig(
mnList
) {
if (mnList.blockHash !== this.quorumHash) {
throw new Error(`Wrong Masternode List for quorum: blockHash
${mnList.blockHash} doesn't correspond with quorumHash ${this.quorumHash}`);
}
if (this.isOutdatedRPC) {
throw new Error(
'Quorum cannot be verified: node running on outdated DashCore version (< 0.16)'
);
}
const quorumMembers = this.getAllQuorumMembers(mnList);
const publicKeyStrings = quorumMembers.map(
(quorumMember) => quorumMember.pubKeyOperator
);
const signersBits = BitArray.uint8ArrayToBitArray(
Uint8Array.from(Buffer.from(this.signers, 'hex'))
);
return bls.verifyAggregatedSignature(
this.membersSig,
Uint8Array.from(this.getCommitmentHash()),
publicKeyStrings,
signersBits
);
};
/**
* verifies the quorum against the det. MNList that was active
* when the quorum was starting its DKG session. Two different
* types of BLS signature verifications are performed:
* 1. the quorumSig is verified with the quorumPublicKey
* 2. the quorum members are re-calculated and the memberSig is
* verified against their aggregated pubKeyOperator values
* @param {SimplifiedMNList} quorumSMNList - MNList for the block (quorumHash)
* the quorum was starting its DKG session with
* @return {Promise<boolean>}
*/
QuorumEntry.prototype.verify = function verify(quorumSMNList) {
return new Promise((resolve, reject) => {
if (quorumSMNList.blockHash !== this.quorumHash) {
return reject(
new Error(`Wrong Masternode List for quorum: blockHash
${quorumSMNList.blockHash} doesn't correspond with quorumHash ${this.quorumHash}`)
);
}
if (this.isOutdatedRPC) {
return reject(
new Error(
'Quorum cannot be verified: node running on outdated DashCore version (< 0.16)'
)
);
}
// only verify if quorum hasn't already been verified
if (this.isVerified) {
return resolve(true);
}
return this.isValidMemberSig(quorumSMNList)
.then((isValidMemberSig) => {
if (!isValidMemberSig) {
return false;
}
return this.isValidQuorumSig();
})
.then((isVerified) => {
this.isVerified = isVerified;
resolve(isVerified);
});
});
};
/**
* Get all members for this quorum
* @param {SimplifiedMNList} SMNList - MNlist for the quorum
* @return {SimplifiedMNListEntry[]}
*/
QuorumEntry.prototype.getAllQuorumMembers = function getAllQuorumMembers(
SMNList
) {
// TODO does not work for dip-0024 llmq type quorums
// See https://github.com/dashpay/dips/blob/master/dip-0024.md#changes-to-the-initialization-phase
if (SMNList.blockHash !== this.quorumHash) {
throw new Error(`Wrong Masternode List for quorum: blockHash
${SMNList.blockHash} doesn't correspond with quorumHash ${this.quorumHash}`);
}
const onlyHighPerformanceMasternodes = this.llmqType === SMNList.getPlatformLLMQType();
return SMNList.calculateQuorum(
this.getSelectionModifier(),
this.getParams().size,
onlyHighPerformanceMasternodes,
);
};
/**
* Gets the modifier for deterministic sorting of the MNList
* for quorum member selection
* @return {Buffer}
*/
QuorumEntry.prototype.getSelectionModifier = function getSelectionModifier() {
const bufferWriter = new BufferWriter();
bufferWriter.writeUInt8(this.llmqType);
bufferWriter.write(Buffer.from(this.quorumHash, 'hex').reverse());
return Hash.sha256sha256(bufferWriter.toBuffer()).reverse();
};
/**
* Gets the ordering hash for a requestId
* @param {string} requestId - the requestId for the signing session to be verified
* @return {Buffer}
*/
QuorumEntry.prototype.getOrderingHashForRequestId =
function getOrderingHashForRequestId(requestId) {
const buf = Buffer.concat([
Buffer.from(this.llmqType),
Buffer.from(this.quorumHash, 'hex'),
Buffer.from(requestId, 'hex'),
]);
return Hash.sha256sha256(buf).reverse();
};
/**
* @return {Buffer}
*/
QuorumEntry.prototype.calculateHash = function calculateHash() {
return Hash.sha256sha256(this.toBufferForHashing()).reverse();
};
/**
* Creates a copy of QuorumEntry
* @return {QuorumEntry}
*/
QuorumEntry.prototype.copy = function copy() {
return QuorumEntry.fromBuffer(this.toBuffer());
};
module.exports = QuorumEntry;