Skip to content

fix!: can't load BLS wasm module in Chrome #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// TODO: Remove previous line and work through linting issues at next edit

'use strict';
var path = require('path')

var src = './index.js',
tests = './test.spec.js';
Expand All @@ -11,7 +10,8 @@ var karmaConfig = {
frameworks: ['mocha', 'chai'],
files: [
src,
tests
tests,
{ pattern: 'node_modules/bls-signatures/blsjs.wasm', included: false }
],
preprocessors: {},
webpack: {
Expand All @@ -29,7 +29,7 @@ var karmaConfig = {
port: 9876,
colors: true,
autoWatch: false,
browsers: ['FirefoxHeadless'],
browsers: ['ChromeHeadless', 'FirefoxHeadless'],
singleRun: false,
concurrency: Infinity,
plugins: [
Expand All @@ -46,6 +46,9 @@ var karmaConfig = {
flags: ['-headless'],
},
},
proxies: {
'/base/blsjs.wasm': `/base/node_modules/bls-signatures/blsjs.wasm`,
}
};
karmaConfig.preprocessors[src] = ['webpack'];
karmaConfig.preprocessors[tests] = ['webpack'];
Expand Down
42 changes: 42 additions & 0 deletions lib/crypto/bls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable */
// TODO: Remove previous line and work through linting issues at next edit

'use strict';

var EventEmitter = require('events');
var BlsSignatures = require('bls-signatures');

var bls = {
isLoading: false,
instance: null,
events: new EventEmitter(),
LOADED: 'LOADED',
load() {
this.isLoading = true;
return BlsSignatures()
.then((instance) => {
this.instance = instance;
this.isLoading = false;
this.events.emit(this.LOADED);
});
},
getInstance() {
return new Promise((resolve) => {
if (this.instance) {
resolve(this.instance);
}

if (this.isLoading) {
this.events.once(this.LOADED, () => {
resolve(this.instance);
});
} else {
this.load().then(() => {
resolve(this.instance);
});
}
});
}
};

module.exports = bls;
177 changes: 104 additions & 73 deletions lib/deterministicmnlist/QuorumEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

'use strict';
var _ = require('lodash');
var { PublicKey, Signature, AggregationInfo } = require('bls-signatures');
var $ = require('../util/preconditions');
var BitArray = require('../util/bitarray');
var BufferReader = require('../encoding/bufferreader');
var BufferWriter = require('../encoding/bufferwriter');
var BufferUtil = require('../util/buffer');
var constants = require('../constants');
var Hash = require('../crypto/hash');
var bls = require('../crypto/bls');
var utils = require('../util/js');

var isHexString = utils.isHexaString;
Expand Down Expand Up @@ -39,7 +39,8 @@ var SHA256_HASH_SIZE = constants.SHA256_HASH_SIZE;

/**
* @class QuorumEntry
* @param {string|Object|Buffer} [arg] - A Buffer, JSON string, or Object representing a SMLQuorumEntry
* @param {string|Object|Buffer} [arg] - A Buffer, JSON string,
* or Object representing a SMLQuorumEntry
* @constructor
* @property {number} version
* @property {number} llmqType
Expand All @@ -57,17 +58,24 @@ function QuorumEntry(arg) {
if (arg) {
if (arg instanceof QuorumEntry) {
return arg.copy();
} else if (BufferUtil.isBuffer(arg)) {
}

if (BufferUtil.isBuffer(arg)) {
return QuorumEntry.fromBuffer(arg);
} else if (_.isObject(arg)) {
}

if (_.isObject(arg)) {
return QuorumEntry.fromObject(arg);
} else if (arg instanceof QuorumEntry) {
}

if (arg instanceof QuorumEntry) {
return arg.copy();
} else if (isHexString(arg)) {
}

if (isHexString(arg)) {
return QuorumEntry.fromHexString(arg);
} else {
throw new TypeError('Unrecognized argument for QuorumEntry');
}
throw new TypeError('Unrecognized argument for QuorumEntry');
}
}

Expand Down Expand Up @@ -284,70 +292,80 @@ QuorumEntry.prototype.getCommitmentHash = function getCommitmentHash() {

/**
* Verifies the quorum's bls threshold signature
* @return {boolean}
* @return {Promise<boolean>}
*/
QuorumEntry.prototype.isValidQuorumSig = function isValidQuorumSig() {
if (this.isOutdatedRPC) {
throw new Error(`Quorum cannot be verified: node running on outdated DashCore version (< 0.16)`);
}
return new Promise((resolve, reject) => {
if (this.isOutdatedRPC) {
return reject(new Error('Quorum cannot be verified: node running on outdated DashCore version (< 0.16)'));
}

var quorumPubKey = PublicKey.fromBytes(Uint8Array.from(Buffer.from(this.quorumPublicKey, 'hex')));
var msgHash = Uint8Array.from(this.getCommitmentHash());
var aggregationInfo = AggregationInfo.fromMsgHash(quorumPubKey, msgHash);
var thresholdSignature = Signature.fromBytes(Uint8Array.from(Buffer.from(this.quorumSig, 'hex')));
return bls.getInstance().then((blsInstance) => {
var quorumPubKey = blsInstance.PublicKey.fromBytes(Uint8Array.from(Buffer.from(this.quorumPublicKey, 'hex')));
var msgHash = Uint8Array.from(this.getCommitmentHash());
var aggregationInfo = blsInstance.AggregationInfo.fromMsgHash(quorumPubKey, msgHash);
var thresholdSignature = blsInstance.Signature.fromBytes(Uint8Array.from(Buffer.from(this.quorumSig, 'hex')));

thresholdSignature.setAggregationInfo(aggregationInfo);
thresholdSignature.setAggregationInfo(aggregationInfo);

var result = thresholdSignature.verify();
quorumPubKey.delete();
thresholdSignature.delete();
aggregationInfo.delete();
return result;
var result = thresholdSignature.verify();
quorumPubKey.delete();
thresholdSignature.delete();
aggregationInfo.delete();
resolve(result);
});
});
};

/**
* Verifies the quorum's aggregated operator key signature
* @param {SimplifiedMNList} mnList - MNList for the block (quorumHash)
* @return {boolean}
* @return {Promise<boolean>}
*/
QuorumEntry.prototype.isValidMemberSig = 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)`);
}
return new Promise((resolve, reject) => {
if (mnList.blockHash !== this.quorumHash) {
return reject(new Error(`Wrong Masternode List for quorum: blockHash
${mnList.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)'));
}

var quorumMembers = this.getAllQuorumMembers(mnList);
var aggregatedSignature = Signature.fromBytes(
Uint8Array.from(Buffer.from(this.membersSig, 'hex'))
);
var signersBits = BitArray.uint8ArrayToBitArray(
Uint8Array.from(Buffer.from(this.signers, 'hex'))
);
return bls.getInstance().then((blsInstance) => {
var quorumMembers = this.getAllQuorumMembers(mnList);
var aggregatedSignature = blsInstance.Signature.fromBytes(
Uint8Array.from(Buffer.from(this.membersSig, 'hex'))
);
var signersBits = BitArray.uint8ArrayToBitArray(
Uint8Array.from(Buffer.from(this.signers, 'hex'))
);

// aggregate all pubKeyOperators of signing members
var pks = [];
var i = 0;
quorumMembers.forEach((member) => {
if(signersBits[i]) {
pks.push(PublicKey.fromBytes(
Uint8Array.from(Buffer.from(member.pubKeyOperator, 'hex')))
// aggregate all pubKeyOperators of signing members
var pks = [];
var i = 0;

quorumMembers.forEach((member) => {
if (signersBits[i]) {
pks.push(blsInstance.PublicKey.fromBytes(
Uint8Array.from(Buffer.from(member.pubKeyOperator, 'hex')))
);
}
i += 1;
});

var aggregationInfo = blsInstance.AggregationInfo.fromMsgHash(
blsInstance.PublicKey.aggregate(pks), Uint8Array.from(this.getCommitmentHash())
);
}
i += 1;
});
aggregatedSignature.setAggregationInfo(aggregationInfo);

var aggregationInfo = AggregationInfo.fromMsgHash(
PublicKey.aggregate(pks), Uint8Array.from(this.getCommitmentHash())
);
aggregatedSignature.setAggregationInfo(aggregationInfo);
var result = aggregatedSignature.verify();
aggregatedSignature.delete();
aggregationInfo.delete();

var result = aggregatedSignature.verify();
aggregatedSignature.delete();
aggregationInfo.delete();
return result;
resolve(result);
});
});
};

/**
Expand All @@ -359,26 +377,37 @@ QuorumEntry.prototype.isValidMemberSig = function isValidMemberSig(mnList) {
* verified against their aggregated pubKeyOperator values
* @param {SimplifiedMNList} quorumSMNList - MNList for the block (quorumHash)
* the quorum was starting its DKG session with
* @return {boolean}
* @return {Promise<boolean>}
*/
QuorumEntry.prototype.verify = function verify(quorumSMNList) {
if (quorumSMNList.blockHash !== this.quorumHash) {
throw new Error(`Wrong Masternode List for quorum: blockHash
${quorumSMNList.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)`);
}
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 allready been verified
if (!this.isVerified) {
if (this.isValidMemberSig(quorumSMNList) && this.isValidQuorumSig()) {
this.isVerified = true;
return true
// only verify if quorum hasn't already been verified
if (this.isVerified) {
return resolve(true);
}
return false
}
return true;

return this.isValidMemberSig(quorumSMNList)
.then((isValidMemberSig) => {
if (!isValidMemberSig) {
return false;
}

return this.isValidQuorumSig();
})
.then((isVerified) => {
this.isVerified = isVerified;

resolve(isVerified);
});
});
};

/**
Expand Down Expand Up @@ -411,8 +440,10 @@ QuorumEntry.prototype.getSelectionModifier = function getSelectionModifier() {
* @param {string} requestId - the requestId for the signing session to be verified
* @return {Buffer}
*/
QuorumEntry.prototype.getOrderingHashForRequestId = function getOrderingHashForRequestId(requestId) {
var buf = Buffer.concat(
QuorumEntry.prototype.getOrderingHashForRequestId = function getOrderingHashForRequestId(
requestId,
) {
const buf = Buffer.concat(
[Buffer.from(this.llmqType),
Buffer.from(this.quorumHash, 'hex'),
Buffer.from(requestId, 'hex')]
Expand Down
2 changes: 1 addition & 1 deletion lib/deterministicmnlist/SimplifiedMNList.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ SimplifiedMNList.prototype.addAndMaybeRemoveQuorums = function addAndMaybeRemove
};

/**
* @privat
* @private
* Deletes MNs from the MN list
* @param {string[]} proRegTxHashes - list of proRegTxHashes to delete from MNList
*/
Expand Down
Loading