Skip to content
This repository was archived by the owner on Jan 4, 2022. It is now read-only.
Open
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
62 changes: 32 additions & 30 deletions lib/DashPlatformProtocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const DocumentFacade = require('./document/DocumentFacade');
const StateTransitionFacade = require('./stateTransition/StateTransitionFacade');

const IdentityFacade = require('./identity/IdentityFacade');
const { initBlake3 } = require('./util/hash');

/**
* @class DashPlatformProtocol
Expand Down Expand Up @@ -43,36 +44,37 @@ class DashPlatformProtocol {
return this.initialized;
}

this.initialized = getRE2Class().then((RE2) => {
this.stateRepository = this.options.stateRepository;

this.jsonSchemaValidator = this.options.jsonSchemaValidator;
if (this.jsonSchemaValidator === undefined) {
const ajv = createAjv(RE2);

this.jsonSchemaValidator = new JsonSchemaValidator(ajv);
}

this.dataContract = new DataContractFacade(
this,
RE2,
);

this.document = new DocumentFacade(
this,
);

this.stateTransition = new StateTransitionFacade(
this,
RE2,
);

this.identity = new IdentityFacade(
this,
);

return true;
});
this.initialized = getRE2Class()
.then((RE2) => {
this.stateRepository = this.options.stateRepository;

this.jsonSchemaValidator = this.options.jsonSchemaValidator;
if (this.jsonSchemaValidator === undefined) {
const ajv = createAjv(RE2);

this.jsonSchemaValidator = new JsonSchemaValidator(ajv);
}

this.dataContract = new DataContractFacade(
this,
RE2,
);

this.document = new DocumentFacade(
this,
);

this.stateTransition = new StateTransitionFacade(
this,
RE2,
);

this.identity = new IdentityFacade(
this,
);
})
.then(initBlake3)
.then(() => true);

return this.initialized;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/dataContract/DataContract.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hash = require('../util/hash');
const { hash } = require('../util/hash');
const { encode } = require('../util/serializer');

const getBinaryPropertiesFromSchema = require('./getBinaryPropertiesFromSchema');
Expand Down
2 changes: 1 addition & 1 deletion lib/dataContract/generateDataContractId.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hash = require('../util/hash');
const { hash } = require('../util/hash');

/**
* Generate data contract id based on owner id and entropy
Expand Down
2 changes: 1 addition & 1 deletion lib/dataTrigger/dpnsTriggers/createDomainDataTrigger.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hash = require('../../util/hash');
const { hash } = require('../../util/hash');

const DataTriggerExecutionResult = require('../DataTriggerExecutionResult');
const DataTriggerConditionError = require('../../errors/consensus/state/dataContract/dataTrigger/DataTriggerConditionError');
Expand Down
2 changes: 1 addition & 1 deletion lib/document/Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const lodashCloneDeepWith = require('lodash.clonedeepwith');

const cloneDeepWithIdentifiers = require('../util/cloneDeepWithIdentifiers');

const hash = require('../util/hash');
const { hash } = require('../util/hash');
const { encode } = require('../util/serializer');
const Identifier = require('../identifier/Identifier');

Expand Down
2 changes: 1 addition & 1 deletion lib/document/generateDocumentId.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hash = require('../util/hash');
const { hash } = require('../util/hash');

/**
* Generates document ID
Expand Down
2 changes: 1 addition & 1 deletion lib/identity/Identity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const hash = require('../util/hash');
const { hash } = require('../util/hash');
const { encode } = require('../util/serializer');
const IdentityPublicKey = require('./IdentityPublicKey');
const Identifier = require('../identifier/Identifier');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Identifier = require('../../../../identifier/Identifier');
const hash = require('../../../../util/hash');
const { hash } = require('../../../../util/hash');

class ChainAssetLockProof {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { InstantLock, Transaction } = require('@dashevo/dashcore-lib');
const hash = require('../../../../util/hash');
const { hash } = require('../../../../util/hash');
const Identifier = require('../../../../identifier/Identifier');

class InstantAssetLockProof {
Expand Down
12 changes: 6 additions & 6 deletions lib/stateTransition/AbstractStateTransition.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const {
PublicKey,
PrivateKey,
Signer: { sign, verifySignature },
Signer: { signHash, verifyHashSignature },
} = require('@dashevo/dashcore-lib');

const StateTransitionIsNotSignedError = require(
Expand All @@ -10,7 +10,7 @@ const StateTransitionIsNotSignedError = require(

const stateTransitionTypes = require('./stateTransitionTypes');

const hash = require('../util/hash');
const { hash } = require('../util/hash');
const { encode } = require('../util/serializer');

const calculateStateTransitionFee = require('./calculateStateTransitionFee');
Expand Down Expand Up @@ -155,10 +155,10 @@ class AbstractStateTransition {
* @return {AbstractStateTransition}
*/
signByPrivateKey(privateKey) {
const data = this.toBuffer({ skipSignature: true });
const stHash = this.hash({ skipSignature: true });
const privateKeyModel = new PrivateKey(privateKey);

this.setSignature(sign(data, privateKeyModel));
this.setSignature(signHash(stHash, privateKeyModel));

return this;
}
Expand All @@ -174,13 +174,13 @@ class AbstractStateTransition {
throw new StateTransitionIsNotSignedError(this);
}

const data = this.toBuffer({ skipSignature: true });
const stHash = this.hash({ skipSignature: true });

const publicKeyModel = new PublicKey(publicKey, {});

let isSignatureVerified;
try {
isSignatureVerified = verifySignature(data, signature, publicKeyModel);
isSignatureVerified = verifyHashSignature(stHash, signature, publicKeyModel.hash);
} catch (e) {
isSignatureVerified = false;
}
Expand Down
7 changes: 6 additions & 1 deletion lib/stateTransition/AbstractStateTransitionIdentitySigned.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,14 @@ class AbstractStateTransitionIdentitySigned extends AbstractStateTransition {
let privateKeyModel;
let pubKeyBase;

let privateKeyBase = privateKey;
if (Buffer.isBuffer(privateKey) || Array.isArray(privateKey)) {
privateKeyBase = Buffer.from(privateKeyBase).toString('hex');
}

switch (identityPublicKey.getType()) {
case IdentityPublicKey.TYPES.ECDSA_SECP256K1:
privateKeyModel = new PrivateKey(privateKey);
privateKeyModel = new PrivateKey(privateKeyBase);

/* We store compressed public key in the identity as a base64 string,
/* and here we compare the private key used to sign the state transition
Expand Down
4 changes: 3 additions & 1 deletion lib/test/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ const sinonChai = require('sinon-chai');
const dirtyChai = require('dirty-chai');
const chaiAsPromised = require('chai-as-promised');
const chaiString = require('chai-string');
const { initBlake3 } = require('../util/hash');

use(sinonChai);
use(chaiAsPromised);
use(dirtyChai);
use(chaiString);

beforeEach(function beforeEach() {
beforeEach(async function beforeEach() {
await initBlake3();
if (!this.sinonSandbox) {
this.sinonSandbox = sinon.createSandbox();
} else {
Expand Down
8 changes: 4 additions & 4 deletions lib/test/fixtures/getDashPayDocumentFixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ const generateRandomIdentifier = require('../utils/generateRandomIdentifier');
const createDPPMock = require('../mocks/createDPPMock');

const ownerId = generateRandomIdentifier();
const dataContract = getDashPayContractFixture();

/**
* @return {Document}
*/
function getContactRequestDocumentFixture(options = {}) {
function getContactRequestDocumentFixture(
dataContract = getDashPayContractFixture(),
options = {},
) {
const factory = new DocumentFactory(
createDPPMock(),
() => ({
Expand All @@ -33,5 +35,3 @@ function getContactRequestDocumentFixture(options = {}) {
module.exports = {
getContactRequestDocumentFixture,
};

module.exports.dataContract = dataContract;
9 changes: 3 additions & 6 deletions lib/test/fixtures/getDpnsDocumentFixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ const generateRandomIdentifier = require('../utils/generateRandomIdentifier');
const createDPPMock = require('../mocks/createDPPMock');

const ownerId = generateRandomIdentifier();
const dataContract = getDpnsContractFixture();

/**
* @return {Document}
*/
function getTopDocumentFixture(options = {}) {
function getTopDocumentFixture(dataContract = getDpnsContractFixture(), options = {}) {
const factory = new DocumentFactory(
createDPPMock(),
() => ({
Expand Down Expand Up @@ -41,7 +40,7 @@ function getTopDocumentFixture(options = {}) {
/**
* @return {Document}
*/
function getParentDocumentFixture(options = {}) {
function getParentDocumentFixture(dataContract = getDpnsContractFixture(), options = {}) {
const factory = new DocumentFactory(
createDPPMock(),
() => ({
Expand Down Expand Up @@ -72,7 +71,7 @@ function getParentDocumentFixture(options = {}) {
/**
* @return {Document}
*/
function getChildDocumentFixture(options = {}) {
function getChildDocumentFixture(dataContract = getDpnsContractFixture(), options = {}) {
const factory = new DocumentFactory(
createDPPMock(),
() => ({
Expand Down Expand Up @@ -107,5 +106,3 @@ module.exports = {
getParentDocumentFixture,
getChildDocumentFixture,
};

module.exports.dataContract = dataContract;
5 changes: 1 addition & 4 deletions lib/test/fixtures/getFeatureFlagsDocumentsFixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ const generateRandomIdentifier = require('../utils/generateRandomIdentifier');
const createDPPMock = require('../mocks/createDPPMock');

const ownerId = generateRandomIdentifier();
const dataContract = getFeatureFlagsContractFixture();

/**
* @return {Document}
*/
function getFeatureFlagsDocumentsFixture() {
function getFeatureFlagsDocumentsFixture(dataContract = getFeatureFlagsContractFixture()) {
const factory = new DocumentFactory(
createDPPMock(),
() => ({
Expand All @@ -27,5 +26,3 @@ function getFeatureFlagsDocumentsFixture() {
}

module.exports = getFeatureFlagsDocumentsFixture;

module.exports.dataContract = dataContract;
2 changes: 1 addition & 1 deletion lib/test/fixtures/getIdentityCreateTransitionFixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = function getIdentityCreateTransitionFixture(oneTimePrivateKey =
{
id: 0,
type: IdentityPublicKey.TYPES.ECDSA_SECP256K1,
data: Buffer.from('AuryIuMtRrl/VviQuyLD1l4nmxi9ogPzC9LT7tdpo0di', 'base64'),
data: oneTimePrivateKey.toPublicKey().toBuffer(),
},
],
};
Expand Down
25 changes: 17 additions & 8 deletions lib/util/hash.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
const crypto = require('crypto');
const blake3Promise = require('blake3/dist/node');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work in karma?


function sha256(payload) {
return crypto.createHash('sha256')
.update(payload)
.digest();
let blake3 = {};
/**
* Init the blake 3 hasher
* @returns {Promise<void>}
*/
async function initBlake3() {
blake3 = await blake3Promise;
}

/**
* Serialize and hash payload using double sha256
* Serialize and hash payload using blake 3
*
* @param {Buffer} buffer
* @return {Buffer}
*/
module.exports = function hash(buffer) {
return sha256(sha256(buffer));
function hash(buffer) {
return blake3.hash(buffer);
}

module.exports = {
initBlake3,
hash,
};
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"ajv": "^8.6.0",
"ajv-formats": "^2.1.1",
"bignumber.js": "^9.0.1",
"blake3": "^2.1.4",
"bs58": "^4.0.1",
"cbor": "^8.0.0",
"json-schema-traverse": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test/unit/dataContract/DataContract.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('DataContract', () => {
getBinaryPropertiesFromSchemaMock = this.sinonSandbox.stub();

DataContract = rewiremock.proxy('../../../lib/dataContract/DataContract', {
'../../../lib/util/hash': hashMock,
'../../../lib/util/hash': { hash: hashMock },
'../../../lib/util/serializer': serializerMock,
'../../../lib/dataContract/getBinaryPropertiesFromSchema': getBinaryPropertiesFromSchemaMock,
});
Expand Down
2 changes: 1 addition & 1 deletion test/unit/dataContract/generateDataContractId.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('generateDataContractId', () => {
});

it('should generate bs58 id based on ', () => {
const id = bs58.decode('CnS7cz4z1qoPsNfEgpgyVnKdtH2u7bgzZXHLcCQt24US');
const id = bs58.decode('DJftzP4Db8pdN76p72SScmw27jcVjcxrqbT4mmZNMJcw');
const generatedId = generateDataContractId(ownerId, entropy);

expect(Buffer.compare(id, generatedId)).to.equal(0);
Expand Down
Loading