From 99f7ad4497be3d390abe30f7b46edab1aaed9edd Mon Sep 17 00:00:00 2001 From: Pawel Iwan Date: Thu, 16 Mar 2023 10:17:43 +0100 Subject: [PATCH 1/2] changes to js-drive --- .../query/dataContractQueryHandlerFactory.js | 9 ++- .../query/getProofsQueryHandlerFactory.js | 7 ++- .../query/identityQueryHandlerFactory.js | 9 ++- packages/js-drive/lib/createDIContainer.js | 56 +++++++++---------- .../lib/document/fetchDataContractFactory.js | 11 ++-- .../js-drive/lib/dpp/DriveStateRepository.js | 7 ++- .../createMasternodeIdentityFactory.js | 16 +++--- .../masternode/createOperatorIdentifier.js | 6 +- .../createRewardShareDocumentFactory.js | 5 +- .../masternode/createVotingIdentifier.js | 6 +- .../getPublicKeyFromPayoutScript.js | 10 ++-- ...thdrawPubKeyTypeFromPayoutScriptFactory.js | 9 ++- .../masternode/handleNewMasternodeFactory.js | 16 +++--- .../handleUpdatedPubKeyOperatorFactory.js | 12 ++-- .../handleUpdatedScriptPayoutFactory.js | 13 +++-- .../handleUpdatedVotingAddressFactory.js | 7 ++- .../synchronizeMasternodeIdentitiesFactory.js | 11 ++-- .../test/mock/getBiggestPossibleIdentity.js | 18 +++--- .../integration/fee/feesPrediction.spec.js | 43 +++++++------- .../identity/IdentityStoreRepository.spec.js | 10 ++-- ...hronizeMasternodeIdentitiesFactory.spec.js | 38 +++++++------ .../dataContractQueryHandlerFactory.spec.js | 3 +- .../getProofsQueryHandlerFactory.spec.js | 1 + .../query/identityQueryHandlerFactory.spec.js | 1 + .../createMasternodeIdentityFactory.spec.js | 35 ++++++------ .../createOperatorIdentifier.spec.js | 4 +- .../masternode/createVotingIdentifier.spec.js | 4 +- .../getPublicKeyFromPayoutScript.spec.js | 7 ++- ...wPubKeyTypeFromPayoutScriptFactory.spec.js | 10 ++-- .../handleNewMasternodeFactory.spec.js | 22 ++++---- .../handleUpdatedScriptPayoutFactory.spec.js | 20 ++++--- .../handleUpdatedVotingAddressFactory.spec.js | 6 +- 32 files changed, 226 insertions(+), 206 deletions(-) diff --git a/packages/js-drive/lib/abci/handlers/query/dataContractQueryHandlerFactory.js b/packages/js-drive/lib/abci/handlers/query/dataContractQueryHandlerFactory.js index 2948bae4bcf..3de29169ea4 100644 --- a/packages/js-drive/lib/abci/handlers/query/dataContractQueryHandlerFactory.js +++ b/packages/js-drive/lib/abci/handlers/query/dataContractQueryHandlerFactory.js @@ -12,9 +12,6 @@ const { }, } = require('@dashevo/dapi-grpc'); -const Identifier = require('@dashevo/dpp/lib/identifier/Identifier'); -const IdentifierError = require('@dashevo/dpp/lib/identifier/errors/IdentifierError'); - const NotFoundAbciError = require('../../errors/NotFoundAbciError'); const InvalidArgumentAbciError = require('../../errors/InvalidArgumentAbciError'); @@ -22,11 +19,13 @@ const InvalidArgumentAbciError = require('../../errors/InvalidArgumentAbciError' * * @param {DataContractStoreRepository} dataContractRepository * @param {createQueryResponse} createQueryResponse + * @param {WebAssembly.Instance} dppWasm * @return {dataContractQueryHandler} */ function dataContractQueryHandlerFactory( dataContractRepository, createQueryResponse, + dppWasm, ) { /** * @typedef dataContractQueryHandler @@ -39,9 +38,9 @@ function dataContractQueryHandlerFactory( async function dataContractQueryHandler(params, { id }, request) { let contractIdIdentifier; try { - contractIdIdentifier = new Identifier(id); + contractIdIdentifier = new dppWasm.Identifier(id); } catch (e) { - if (e instanceof IdentifierError) { + if (e instanceof dppWasm.IdentifierError) { throw new InvalidArgumentAbciError('id must be a valid identifier (32 bytes long)'); } diff --git a/packages/js-drive/lib/abci/handlers/query/getProofsQueryHandlerFactory.js b/packages/js-drive/lib/abci/handlers/query/getProofsQueryHandlerFactory.js index 585fb8db9b4..8adf3e6ef60 100644 --- a/packages/js-drive/lib/abci/handlers/query/getProofsQueryHandlerFactory.js +++ b/packages/js-drive/lib/abci/handlers/query/getProofsQueryHandlerFactory.js @@ -7,7 +7,6 @@ const { } = require('@dashevo/abci/types'); const cbor = require('cbor'); -const Identifier = require('@dashevo/dpp/lib/identifier/Identifier'); /** * @@ -15,6 +14,7 @@ const Identifier = require('@dashevo/dpp/lib/identifier/Identifier'); * @param {IdentityStoreRepository} identityRepository * @param {DataContractStoreRepository} dataContractRepository * @param {DocumentRepository} documentRepository + * @param {WebAssembly.Instance} dppWasm * @return {getProofsQueryHandler} */ function getProofsQueryHandlerFactory( @@ -22,6 +22,7 @@ function getProofsQueryHandlerFactory( identityRepository, dataContractRepository, documentRepository, + dppWasm, ) { /** * @typedef getProofsQueryHandler @@ -73,7 +74,7 @@ function getProofsQueryHandlerFactory( if (identityIds && identityIds.length) { const identitiesProof = await identityRepository.proveMany( - identityIds.map((identityId) => Identifier.from(identityId)), + identityIds.map((identityId) => dppWasm.Identifier.from(identityId)), ); response.identitiesProof = { @@ -86,7 +87,7 @@ function getProofsQueryHandlerFactory( if (dataContractIds && dataContractIds.length) { const dataContractsProof = await dataContractRepository.proveMany( - dataContractIds.map((dataContractId) => Identifier.from(dataContractId)), + dataContractIds.map((dataContractId) => dppWasm.Identifier.from(dataContractId)), ); response.dataContractsProof = { diff --git a/packages/js-drive/lib/abci/handlers/query/identityQueryHandlerFactory.js b/packages/js-drive/lib/abci/handlers/query/identityQueryHandlerFactory.js index 179002335b1..081937c6c79 100644 --- a/packages/js-drive/lib/abci/handlers/query/identityQueryHandlerFactory.js +++ b/packages/js-drive/lib/abci/handlers/query/identityQueryHandlerFactory.js @@ -12,9 +12,6 @@ const { }, } = require('@dashevo/dapi-grpc'); -const Identifier = require('@dashevo/dpp/lib/identifier/Identifier'); -const IdentifierError = require('@dashevo/dpp/lib/identifier/errors/IdentifierError'); - const NotFoundAbciError = require('../../errors/NotFoundAbciError'); const InvalidArgumentAbciError = require('../../errors/InvalidArgumentAbciError'); @@ -22,11 +19,13 @@ const InvalidArgumentAbciError = require('../../errors/InvalidArgumentAbciError' * * @param {IdentityStoreRepository} identityRepository * @param {createQueryResponse} createQueryResponse + * @param {WebAssembly.Instance} dppWasm * @return {identityQueryHandler} */ function identityQueryHandlerFactory( identityRepository, createQueryResponse, + dppWasm, ) { /** * @typedef identityQueryHandler @@ -39,9 +38,9 @@ function identityQueryHandlerFactory( async function identityQueryHandler(params, { id }, request) { let identifier; try { - identifier = new Identifier(id); + identifier = new dppWasm.Identifier(id); } catch (e) { - if (e instanceof IdentifierError) { + if (e instanceof dppWasm.IdentifierError) { throw new InvalidArgumentAbciError('id must be a valid identifier (32 bytes long)'); } diff --git a/packages/js-drive/lib/createDIContainer.js b/packages/js-drive/lib/createDIContainer.js index 867b0b4067d..107addb8642 100644 --- a/packages/js-drive/lib/createDIContainer.js +++ b/packages/js-drive/lib/createDIContainer.js @@ -18,10 +18,6 @@ const RpcClient = require('@dashevo/dashd-rpc/promise'); const { PublicKey } = require('@dashevo/dashcore-lib'); -const Identifier = require('@dashevo/dpp/lib/identifier/Identifier'); - -const findMyWay = require('find-my-way'); - const pino = require('pino'); const pinoMultistream = require('pino-multi-stream'); @@ -147,7 +143,7 @@ const createContextLoggerFactory = require('./abci/errors/createContextLoggerFac const IdentityBalanceStoreRepository = require('./identity/IdentityBalanceStoreRepository'); /** - * @param {Object} dppWasmLoaded + * @param {WebAssembly.Instance} dppWasm * @param {Object} options * @param {string} options.ABCI_HOST * @param {string} options.ABCI_PORT @@ -186,7 +182,7 @@ const IdentityBalanceStoreRepository = require('./identity/IdentityBalanceStoreR * * @return {AwilixContainer} */ -function createDIContainer(dppWasmLoaded, options) { +function createDIContainer(dppWasm, options) { if (!options.DPNS_MASTER_PUBLIC_KEY) { throw new Error('DPNS_MASTER_PUBLIC_KEY must be set'); } @@ -289,10 +285,10 @@ function createDIContainer(dppWasmLoaded, options) { parseInt(options.VALIDATOR_SET_LLMQ_TYPE, 10), ), masternodeRewardSharesContractId: asValue( - Identifier.from(masternodeRewardsSystemIds.contractId), + dppWasm.Identifier.from(masternodeRewardsSystemIds.contractId), ), masternodeRewardSharesOwnerId: asValue( - Identifier.from(masternodeRewardsSystemIds.ownerId), + dppWasm.Identifier.from(masternodeRewardsSystemIds.ownerId), ), masternodeRewardSharesOwnerMasterPublicKey: asValue( PublicKey.fromString( @@ -308,10 +304,10 @@ function createDIContainer(dppWasmLoaded, options) { masternodeRewardsDocuments, ), featureFlagsContractId: asValue( - Identifier.from(featureFlagsSystemIds.contractId), + dppWasm.Identifier.from(featureFlagsSystemIds.contractId), ), featureFlagsOwnerId: asValue( - Identifier.from(featureFlagsSystemIds.ownerId), + dppWasm.Identifier.from(featureFlagsSystemIds.ownerId), ), featureFlagsOwnerMasterPublicKey: asValue( PublicKey.fromString( @@ -324,8 +320,8 @@ function createDIContainer(dppWasmLoaded, options) { ), ), featureFlagsDocuments: asValue(featureFlagsDocuments), - dpnsContractId: asValue(Identifier.from(dpnsSystemIds.contractId)), - dpnsOwnerId: asValue(Identifier.from(dpnsSystemIds.ownerId)), + dpnsContractId: asValue(dppWasm.Identifier.from(dpnsSystemIds.contractId)), + dpnsOwnerId: asValue(dppWasm.Identifier.from(dpnsSystemIds.ownerId)), dpnsOwnerMasterPublicKey: asValue( PublicKey.fromString( options.DPNS_MASTER_PUBLIC_KEY, @@ -337,8 +333,8 @@ function createDIContainer(dppWasmLoaded, options) { ), ), dpnsDocuments: asValue(dpnsDocuments), - dashpayContractId: asValue(Identifier.from(dashpaySystemIds.contractId)), - dashpayOwnerId: asValue(Identifier.from(dashpaySystemIds.ownerId)), + dashpayContractId: asValue(dppWasm.Identifier.from(dashpaySystemIds.contractId)), + dashpayOwnerId: asValue(dppWasm.Identifier.from(dashpaySystemIds.ownerId)), dashpayOwnerMasterPublicKey: asValue( PublicKey.fromString( options.DASHPAY_MASTER_PUBLIC_KEY, @@ -350,8 +346,8 @@ function createDIContainer(dppWasmLoaded, options) { ), ), dashpayDocuments: asValue(dashpayDocuments), - withdrawalsContractId: asValue(Identifier.from(withdrawalsSystemIds.contractId)), - withdrawalsOwnerId: asValue(Identifier.from(withdrawalsSystemIds.ownerId)), + withdrawalsContractId: asValue(dppWasm.Identifier.from(withdrawalsSystemIds.contractId)), + withdrawalsOwnerId: asValue(dppWasm.Identifier.from(withdrawalsSystemIds.ownerId)), withdrawalsOwnerMasterPublicKey: asValue( PublicKey.fromString( options.WITHDRAWALS_MASTER_PUBLIC_KEY, @@ -439,19 +435,19 @@ function createDIContainer(dppWasmLoaded, options) { logJsonFileLevel, logJsonFileStream, ) => [ - { - level: logStdoutLevel, - stream: logStdoutStream, - }, - { - level: logPrettyFileLevel, - stream: logPrettyFileStream, - }, - { - level: logJsonFileLevel, - stream: logJsonFileStream, - }, - ]), + { + level: logStdoutLevel, + stream: logStdoutStream, + }, + { + level: logPrettyFileLevel, + stream: logPrettyFileStream, + }, + { + level: logJsonFileLevel, + stream: logJsonFileStream, + }, + ]), logger: asFunction( (loggerStreams) => pino({ @@ -622,7 +618,7 @@ function createDIContainer(dppWasmLoaded, options) { * Register DPP */ container.register({ - dppWasm: asValue(dppWasmLoaded), + dppWasm: asValue(dppWasm), DashPlatformProtocol: asFunction((dppWasm) => dppWasm.DashPlatformProtocol), diff --git a/packages/js-drive/lib/document/fetchDataContractFactory.js b/packages/js-drive/lib/document/fetchDataContractFactory.js index b81b04deeee..41f6300a220 100644 --- a/packages/js-drive/lib/document/fetchDataContractFactory.js +++ b/packages/js-drive/lib/document/fetchDataContractFactory.js @@ -1,28 +1,27 @@ -const IdentifierError = require('@dashevo/dpp/lib/identifier/errors/IdentifierError'); -const Identifier = require('@dashevo/dpp/lib/identifier/Identifier'); - const InvalidQueryError = require('./errors/InvalidQueryError'); /** * @param {DataContractStoreRepository} dataContractRepository + * @param {WebAssembly.Instance} dppWasm * @returns {fetchDocuments} */ function fetchDataContractFactory( dataContractRepository, + dppWasm, ) { /** * Fetch Data Contract by Contract ID and type * * @typedef {Promise} fetchDataContract - * @param {Buffer|Identifier} contractId + * @param {Buffer|dppWasm.Identifier} contractId * @returns {Promise>} */ async function fetchDataContract(contractId) { let contractIdIdentifier; try { - contractIdIdentifier = new Identifier(contractId); + contractIdIdentifier = new dppWasm.Identifier(contractId); } catch (e) { - if (e instanceof IdentifierError) { + if (e instanceof dppWasm.IdentifierError) { throw new InvalidQueryError(`invalid data contract ID: ${e.message}`); } diff --git a/packages/js-drive/lib/dpp/DriveStateRepository.js b/packages/js-drive/lib/dpp/DriveStateRepository.js index 173c9f92efa..2634681fcaa 100644 --- a/packages/js-drive/lib/dpp/DriveStateRepository.js +++ b/packages/js-drive/lib/dpp/DriveStateRepository.js @@ -1,5 +1,3 @@ -const { TYPES } = require('@dashevo/dpp/lib/identity/IdentityPublicKey'); - const ReadOperation = require('@dashevo/dpp/lib/stateTransition/fee/operations/ReadOperation'); const SignatureVerificationOperation = require('@dashevo/dpp/lib/stateTransition/fee/operations/SignatureVerificationOperation'); const BlockInfo = require('../blockExecution/BlockInfo'); @@ -22,6 +20,7 @@ class DriveStateRepository { * @param {BlockExecutionContext} blockExecutionContext * @param {SimplifiedMasternodeList} simplifiedMasternodeList * @param {Drive} rsDrive + * @param {WebAssembly.Instance} dppWasm * @param {Object} [options] * @param {Object} [options.useTransaction=false] */ @@ -37,6 +36,7 @@ class DriveStateRepository { blockExecutionContext, simplifiedMasternodeList, rsDrive, + dppWasm, options = {}, ) { this.identityRepository = identityRepository; @@ -50,6 +50,7 @@ class DriveStateRepository { this.blockExecutionContext = blockExecutionContext; this.simplifiedMasternodeList = simplifiedMasternodeList; this.rsDrive = rsDrive; + this.dppWasm = dppWasm; this.#options = options; } @@ -571,7 +572,7 @@ class DriveStateRepository { if (executionContext) { executionContext.addOperation( - new SignatureVerificationOperation(TYPES.ECDSA_SECP256K1), + new SignatureVerificationOperation(this.dppWasm.KeyTypes.ECDSA_SECP256K1), ); if (executionContext.isDryRun()) { diff --git a/packages/js-drive/lib/identity/masternode/createMasternodeIdentityFactory.js b/packages/js-drive/lib/identity/masternode/createMasternodeIdentityFactory.js index 2d4aeed80a0..08702119611 100644 --- a/packages/js-drive/lib/identity/masternode/createMasternodeIdentityFactory.js +++ b/packages/js-drive/lib/identity/masternode/createMasternodeIdentityFactory.js @@ -1,5 +1,3 @@ -const IdentityPublicKey = require('@dashevo/dpp/lib/identity/IdentityPublicKey'); -const Identity = require('@dashevo/dpp/lib/identity/Identity'); const InvalidMasternodeIdentityError = require('./errors/InvalidMasternodeIdentityError'); /** @@ -7,6 +5,7 @@ const InvalidMasternodeIdentityError = require('./errors/InvalidMasternodeIdenti * @param {IdentityStoreRepository} identityRepository * @param {getWithdrawPubKeyTypeFromPayoutScript} getWithdrawPubKeyTypeFromPayoutScript * @param {getPublicKeyFromPayoutScript} getPublicKeyFromPayoutScript + * @param {WebAssembly.Instance} dppWasm * @return {createMasternodeIdentity} */ function createMasternodeIdentityFactory( @@ -14,6 +13,7 @@ function createMasternodeIdentityFactory( identityRepository, getWithdrawPubKeyTypeFromPayoutScript, getPublicKeyFromPayoutScript, + dppWasm, ) { /** * @typedef createMasternodeIdentity @@ -34,8 +34,8 @@ function createMasternodeIdentityFactory( const publicKeys = [{ id: 0, type: pubKeyType, - purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION, - securityLevel: IdentityPublicKey.SECURITY_LEVELS.MASTER, + purpose: dppWasm.KeyPurpose.AUTHENTICATION, + securityLevel: dppWasm.KeySecurityLevel.MASTER, readOnly: true, // Copy data buffer data: Buffer.from(pubKeyData), @@ -47,14 +47,14 @@ function createMasternodeIdentityFactory( publicKeys.push({ id: 1, type: withdrawPubKeyType, - purpose: IdentityPublicKey.PURPOSES.WITHDRAW, - securityLevel: IdentityPublicKey.SECURITY_LEVELS.CRITICAL, + purpose: dppWasm.KeyPurpose.WITHDRAW, + securityLevel: dppWasm.KeySecurityLevel.CRITICAL, readOnly: false, - data: getPublicKeyFromPayoutScript(payoutScript, withdrawPubKeyType), + data: getPublicKeyFromPayoutScript(dppWasm, payoutScript, withdrawPubKeyType), }); } - const identity = new Identity({ + const identity = new dppWasm.Identity({ protocolVersion: dpp.getProtocolVersion(), id: identifier.toBuffer(), publicKeys, diff --git a/packages/js-drive/lib/identity/masternode/createOperatorIdentifier.js b/packages/js-drive/lib/identity/masternode/createOperatorIdentifier.js index 4effefbf931..a556825e555 100644 --- a/packages/js-drive/lib/identity/masternode/createOperatorIdentifier.js +++ b/packages/js-drive/lib/identity/masternode/createOperatorIdentifier.js @@ -1,13 +1,13 @@ -const Identifier = require('@dashevo/dpp/lib/identifier/Identifier'); const { hash } = require('@dashevo/dpp/lib/util/hash'); /** * @param {SimplifiedMNListEntry} smlEntry + * @param {WebAssembly.Instance} dppWasm */ -function createOperatorIdentifier(smlEntry) { +function createOperatorIdentifier(dppWasm, smlEntry) { const operatorPubKey = Buffer.from(smlEntry.pubKeyOperator, 'hex'); - return Identifier.from( + return dppWasm.Identifier.from( hash( Buffer.concat([ Buffer.from(smlEntry.proRegTxHash, 'hex'), diff --git a/packages/js-drive/lib/identity/masternode/createRewardShareDocumentFactory.js b/packages/js-drive/lib/identity/masternode/createRewardShareDocumentFactory.js index d1462448dcc..a46c3b0ec8a 100644 --- a/packages/js-drive/lib/identity/masternode/createRewardShareDocumentFactory.js +++ b/packages/js-drive/lib/identity/masternode/createRewardShareDocumentFactory.js @@ -1,16 +1,17 @@ const { hash } = require('@dashevo/dpp/lib/util/hash'); -const Identifier = require('@dashevo/dpp/lib/identifier/Identifier'); const MAX_DOCUMENTS = 16; /** * @param {DashPlatformProtocol} dpp * @param {DocumentRepository} documentRepository + * @param {WebAssembly.Instance} dppWasm * @return {createRewardShareDocument} */ function createRewardShareDocumentFactory( dpp, documentRepository, + dppWasm, ) { /** * @typedef {createRewardShareDocument} @@ -73,7 +74,7 @@ function createRewardShareDocumentFactory( ]), ); - rewardShareDocument.id = Identifier.from(rewardShareDocumentIdSeed); + rewardShareDocument.id = dppWasm.Identifier.from(rewardShareDocumentIdSeed); await documentRepository.create(rewardShareDocument, blockInfo, { useTransaction: true, diff --git a/packages/js-drive/lib/identity/masternode/createVotingIdentifier.js b/packages/js-drive/lib/identity/masternode/createVotingIdentifier.js index 69e61c89d2f..8d93b6c0fef 100644 --- a/packages/js-drive/lib/identity/masternode/createVotingIdentifier.js +++ b/packages/js-drive/lib/identity/masternode/createVotingIdentifier.js @@ -1,14 +1,14 @@ const { hash } = require('@dashevo/dpp/lib/util/hash'); -const Identifier = require('@dashevo/dpp/lib/identifier/Identifier'); const Address = require('@dashevo/dashcore-lib/lib/address'); /** * @param {SimplifiedMNListEntry} smlEntry + * @param {WebAssembly.Instance} dppWasm */ -function createVotingIdentifier(smlEntry) { +function createVotingIdentifier(smlEntry, dppWasm) { const votingPubKeyHash = Address.fromString(smlEntry.votingAddress).hashBuffer; - return Identifier.from( + return dppWasm.Identifier.from( hash( Buffer.concat([ Buffer.from(smlEntry.proRegTxHash, 'hex'), diff --git a/packages/js-drive/lib/identity/masternode/getPublicKeyFromPayoutScript.js b/packages/js-drive/lib/identity/masternode/getPublicKeyFromPayoutScript.js index e6dc34d4bd1..b25ae0b126c 100644 --- a/packages/js-drive/lib/identity/masternode/getPublicKeyFromPayoutScript.js +++ b/packages/js-drive/lib/identity/masternode/getPublicKeyFromPayoutScript.js @@ -1,17 +1,15 @@ -const IdentityPublicKey = require('@dashevo/dpp/lib/identity/IdentityPublicKey'); -const InvalidIdentityPublicKeyTypeError = require('@dashevo/dpp/lib/stateTransition/errors/InvalidIdentityPublicKeyTypeError'); - /** * @typedef getPublicKeyFromPayoutScript * @param {Script} payoutScript * @param {number} type + * @param {WebAssembly.Instance} dppWasm * @returns {Buffer} */ -function getPublicKeyFromPayoutScript(payoutScript, type) { +function getPublicKeyFromPayoutScript(payoutScript, type, dppWasm) { switch (type) { - case IdentityPublicKey.TYPES.ECDSA_HASH160: + case dppWasm.KeyType.ECDSA_HASH160: return payoutScript.toBuffer().slice(3, 23); - case IdentityPublicKey.TYPES.BIP13_SCRIPT_HASH: + case dppWasm.KeyType.BIP13_SCRIPT_HASH: return payoutScript.toBuffer().slice(2, 22); default: throw new InvalidIdentityPublicKeyTypeError(type); diff --git a/packages/js-drive/lib/identity/masternode/getWithdrawPubKeyTypeFromPayoutScriptFactory.js b/packages/js-drive/lib/identity/masternode/getWithdrawPubKeyTypeFromPayoutScriptFactory.js index 402fd45f45f..969e8dff469 100644 --- a/packages/js-drive/lib/identity/masternode/getWithdrawPubKeyTypeFromPayoutScriptFactory.js +++ b/packages/js-drive/lib/identity/masternode/getWithdrawPubKeyTypeFromPayoutScriptFactory.js @@ -1,13 +1,12 @@ -const IdentityPublicKey = require('@dashevo/dpp/lib/identity/IdentityPublicKey'); - const InvalidPayoutScriptError = require('./errors/InvalidPayoutScriptError'); /** * * @param {string} network + * @param {WebAssembly.Instance} dppWasm * @returns {getWithdrawPubKeyTypeFromPayoutScript} */ -function getWithdrawPubKeyTypeFromPayoutScriptFactory(network) { +function getWithdrawPubKeyTypeFromPayoutScriptFactory(network, dppWasm) { /** * @typedef getWithdrawPubKeyTypeFromPayoutScript * @param {Script} payoutScript @@ -22,9 +21,9 @@ function getWithdrawPubKeyTypeFromPayoutScriptFactory(network) { let withdrawPubKeyType; if (address.isPayToScriptHash()) { - withdrawPubKeyType = IdentityPublicKey.TYPES.BIP13_SCRIPT_HASH; + withdrawPubKeyType = dppWasm.KeyType.BIP13_SCRIPT_HASH; } else if (address.isPayToPublicKeyHash()) { - withdrawPubKeyType = IdentityPublicKey.TYPES.ECDSA_HASH160; + withdrawPubKeyType = dppWasm.KeyType.ECDSA_HASH160; } else { throw new InvalidPayoutScriptError(payoutScript); } diff --git a/packages/js-drive/lib/identity/masternode/handleNewMasternodeFactory.js b/packages/js-drive/lib/identity/masternode/handleNewMasternodeFactory.js index 8303b2ab78a..67788a8f47e 100644 --- a/packages/js-drive/lib/identity/masternode/handleNewMasternodeFactory.js +++ b/packages/js-drive/lib/identity/masternode/handleNewMasternodeFactory.js @@ -1,5 +1,3 @@ -const Identifier = require('@dashevo/dpp/lib/identifier/Identifier'); -const IdentityPublicKey = require('@dashevo/dpp/lib/identity/IdentityPublicKey'); const Address = require('@dashevo/dashcore-lib/lib/address'); const Script = require('@dashevo/dashcore-lib/lib/script'); const createOperatorIdentifier = require('./createOperatorIdentifier'); @@ -11,6 +9,7 @@ const createVotingIdentifier = require('./createVotingIdentifier'); * @param {createMasternodeIdentity} createMasternodeIdentity * @param {createRewardShareDocument} createRewardShareDocument * @param {fetchTransaction} fetchTransaction + * @param {WebAssembly.Instance} dppWasm * @return {handleNewMasternode} */ function handleNewMasternodeFactory( @@ -18,6 +17,7 @@ function handleNewMasternodeFactory( createMasternodeIdentity, createRewardShareDocument, fetchTransaction, + dppWasm, ) { /** * @typedef handleNewMasternode @@ -45,7 +45,7 @@ function handleNewMasternodeFactory( } // Create a masternode identity - const masternodeIdentifier = Identifier.from( + const masternodeIdentifier = dppWasm.Identifier.from( proRegTxHash, ); @@ -56,7 +56,7 @@ function handleNewMasternodeFactory( blockInfo, masternodeIdentifier, ownerPublicKeyHash, - IdentityPublicKey.TYPES.ECDSA_HASH160, + dppWasm.KeyType.ECDSA_HASH160, payoutScript, ), ); @@ -72,14 +72,14 @@ function handleNewMasternodeFactory( operatorPayoutScript = new Script(operatorPayoutAddress); } - const operatorIdentifier = createOperatorIdentifier(masternodeEntry); + const operatorIdentifier = createOperatorIdentifier(dppWasm, masternodeEntry); createdEntities.push( await createMasternodeIdentity( blockInfo, operatorIdentifier, operatorPubKey, - IdentityPublicKey.TYPES.BLS12_381, + dppWasm.KeyType.BLS12_381, operatorPayoutScript, ), ); @@ -103,14 +103,14 @@ function handleNewMasternodeFactory( // don't need to create a separate Identity in case we don't have // voting public key (keyIDVoting === keyIDOwner) if (!votingPubKeyHash.equals(ownerPublicKeyHash)) { - const votingIdentifier = createVotingIdentifier(masternodeEntry); + const votingIdentifier = createVotingIdentifier(masternodeEntry, dppWasm); createdEntities.push( await createMasternodeIdentity( blockInfo, votingIdentifier, votingPubKeyHash, - IdentityPublicKey.TYPES.ECDSA_HASH160, + dppWasm.KeyType.ECDSA_HASH160, ), ); } diff --git a/packages/js-drive/lib/identity/masternode/handleUpdatedPubKeyOperatorFactory.js b/packages/js-drive/lib/identity/masternode/handleUpdatedPubKeyOperatorFactory.js index 663e990662b..2c8988cf3c1 100644 --- a/packages/js-drive/lib/identity/masternode/handleUpdatedPubKeyOperatorFactory.js +++ b/packages/js-drive/lib/identity/masternode/handleUpdatedPubKeyOperatorFactory.js @@ -1,5 +1,3 @@ -const Identifier = require('@dashevo/dpp/lib/identifier/Identifier'); -const IdentityPublicKey = require('@dashevo/dpp/lib/identity/IdentityPublicKey'); const Address = require('@dashevo/dashcore-lib/lib/address'); const Script = require('@dashevo/dashcore-lib/lib/script'); const createOperatorIdentifier = require('./createOperatorIdentifier'); @@ -13,6 +11,7 @@ const createOperatorIdentifier = require('./createOperatorIdentifier'); * @param {DocumentRepository} documentRepository * @param {IdentityStoreRepository} identityRepository * @param {fetchTransaction} fetchTransaction + * @param {WebAssembly.Instance} dppWasm * @return {handleUpdatedPubKeyOperator} */ function handleUpdatedPubKeyOperatorFactory( @@ -23,6 +22,7 @@ function handleUpdatedPubKeyOperatorFactory( documentRepository, identityRepository, fetchTransaction, + dppWasm, ) { /** * @typedef handleUpdatedPubKeyOperator @@ -57,7 +57,7 @@ function handleUpdatedPubKeyOperatorFactory( const proRegTxHash = Buffer.from(masternodeEntry.proRegTxHash, 'hex'); const operatorPublicKey = Buffer.from(masternodeEntry.pubKeyOperator, 'hex'); - const operatorIdentifier = createOperatorIdentifier(masternodeEntry); + const operatorIdentifier = createOperatorIdentifier(dppWasm, masternodeEntry); const operatorIdentityResult = await identityRepository.fetch( operatorIdentifier, @@ -77,7 +77,7 @@ function handleUpdatedPubKeyOperatorFactory( blockInfo, operatorIdentifier, operatorPublicKey, - IdentityPublicKey.TYPES.BLS12_381, + dppWasm.KeyType.BLS12_381, operatorPayoutPubKey, ), ); @@ -86,7 +86,7 @@ function handleUpdatedPubKeyOperatorFactory( // Create a document in rewards data contract with percentage defined // in corresponding ProRegTx - const masternodeIdentifier = Identifier.from( + const masternodeIdentifier = dppWasm.Identifier.from( proRegTxHash, ); @@ -105,7 +105,7 @@ function handleUpdatedPubKeyOperatorFactory( // Delete document from reward shares data contract with ID corresponding to the // masternode identity (ownerId) and previous operator identity (payToId) - const previousOperatorIdentifier = createOperatorIdentifier(previousMasternodeEntry); + const previousOperatorIdentifier = createOperatorIdentifier(dppWasm, previousMasternodeEntry); const previousDocumentsResult = await documentRepository.find( dataContract, diff --git a/packages/js-drive/lib/identity/masternode/handleUpdatedScriptPayoutFactory.js b/packages/js-drive/lib/identity/masternode/handleUpdatedScriptPayoutFactory.js index 2d2fd03a103..e3eabb07a0e 100644 --- a/packages/js-drive/lib/identity/masternode/handleUpdatedScriptPayoutFactory.js +++ b/packages/js-drive/lib/identity/masternode/handleUpdatedScriptPayoutFactory.js @@ -1,4 +1,3 @@ -const IdentityPublicKey = require('@dashevo/dpp/lib/identity/IdentityPublicKey'); const identitySchema = require('@dashevo/dpp/schema/identity/identity.json'); /** @@ -7,6 +6,7 @@ const identitySchema = require('@dashevo/dpp/schema/identity/identity.json'); * @param {IdentityPublicKeyStoreRepository} identityPublicKeyRepository * @param {getWithdrawPubKeyTypeFromPayoutScript} getWithdrawPubKeyTypeFromPayoutScript * @param {getPublicKeyFromPayoutScript} getPublicKeyFromPayoutScript + * @param {WebAssembly.Instance} dppWasm * @returns {handleUpdatedScriptPayout} */ function handleUpdatedScriptPayoutFactory( @@ -14,6 +14,7 @@ function handleUpdatedScriptPayoutFactory( identityPublicKeyRepository, getWithdrawPubKeyTypeFromPayoutScript, getPublicKeyFromPayoutScript, + dppWasm, ) { /** * @typedef handleUpdatedScriptPayout @@ -55,6 +56,7 @@ function handleUpdatedScriptPayoutFactory( if (previousPayoutScript) { const previousPubKeyType = getWithdrawPubKeyTypeFromPayoutScript(previousPayoutScript); const previousPubKeyData = getPublicKeyFromPayoutScript( + dppWasm, previousPayoutScript, previousPubKeyType, ); @@ -78,15 +80,16 @@ function handleUpdatedScriptPayoutFactory( // add new const withdrawPubKeyType = getWithdrawPubKeyTypeFromPayoutScript(newPayoutScript); - const pubKeyData = getPublicKeyFromPayoutScript(newPayoutScript, withdrawPubKeyType); + const pubKeyData = getPublicKeyFromPayoutScript(newPayoutScript, withdrawPubKeyType, dppWasm); - const newWithdrawalIdentityPublicKey = new IdentityPublicKey() + // TODO wasm-dpp doesn' use fluent api + const newWithdrawalIdentityPublicKey = new dppWasm.IdentityPublicKey() .setId(identity.getPublicKeyMaxId() + 1) .setType(withdrawPubKeyType) .setData(pubKeyData) - .setPurpose(IdentityPublicKey.PURPOSES.WITHDRAW) + .setPurpose(dppWasm.KeyPurpose.WITHDRAW) .setReadOnly(true) - .setSecurityLevel(IdentityPublicKey.SECURITY_LEVELS.MASTER); + .setSecurityLevel(dppWasm.KeySecurityLevel.MASTER); await identityPublicKeyRepository.add( identityId, diff --git a/packages/js-drive/lib/identity/masternode/handleUpdatedVotingAddressFactory.js b/packages/js-drive/lib/identity/masternode/handleUpdatedVotingAddressFactory.js index b86e45db12e..b924ca9fbf3 100644 --- a/packages/js-drive/lib/identity/masternode/handleUpdatedVotingAddressFactory.js +++ b/packages/js-drive/lib/identity/masternode/handleUpdatedVotingAddressFactory.js @@ -1,4 +1,3 @@ -const IdentityPublicKey = require('@dashevo/dpp/lib/identity/IdentityPublicKey'); const Address = require('@dashevo/dashcore-lib/lib/address'); const createVotingIdentifier = require('./createVotingIdentifier'); @@ -7,12 +6,14 @@ const createVotingIdentifier = require('./createVotingIdentifier'); * @param {IdentityStoreRepository} identityRepository * @param {createMasternodeIdentity} createMasternodeIdentity * @param {fetchTransaction} fetchTransaction + * @param {WebAssembly.Instance} dppWasm * @return {handleUpdatedVotingAddress} */ function handleUpdatedVotingAddressFactory( identityRepository, createMasternodeIdentity, fetchTransaction, + dppWasm, ) { /** * @typedef handleUpdatedVotingAddress @@ -46,7 +47,7 @@ function handleUpdatedVotingAddressFactory( } // Create a voting identity if there is no identity exist with the same ID - const votingIdentifier = createVotingIdentifier(masternodeEntry); + const votingIdentifier = createVotingIdentifier(masternodeEntry, dppWasm); const votingIdentityResult = await identityRepository.fetch( votingIdentifier, @@ -63,7 +64,7 @@ function handleUpdatedVotingAddressFactory( blockInfo, votingIdentifier, votingPublicKeyHash, - IdentityPublicKey.TYPES.ECDSA_HASH160, + dppWasm.KeyType.ECDSA_HASH160, ), ); } diff --git a/packages/js-drive/lib/identity/masternode/synchronizeMasternodeIdentitiesFactory.js b/packages/js-drive/lib/identity/masternode/synchronizeMasternodeIdentitiesFactory.js index 0e288f59fe0..3bb34db0afb 100644 --- a/packages/js-drive/lib/identity/masternode/synchronizeMasternodeIdentitiesFactory.js +++ b/packages/js-drive/lib/identity/masternode/synchronizeMasternodeIdentitiesFactory.js @@ -1,4 +1,3 @@ -const Identifier = require('@dashevo/dpp/lib/identifier/Identifier'); const Address = require('@dashevo/dashcore-lib/lib/address'); const Script = require('@dashevo/dashcore-lib/lib/script'); const createOperatorIdentifier = require('./createOperatorIdentifier'); @@ -34,7 +33,7 @@ function mergeEntities(result, newData) { * * @param {DataContractStoreRepository} dataContractRepository * @param {SimplifiedMasternodeList} simplifiedMasternodeList - * @param {Identifier} masternodeRewardSharesContractId + * @param {dppWasm.Identifier} masternodeRewardSharesContractId * @param {handleNewMasternode} handleNewMasternode * @param {handleUpdatedPubKeyOperator} handleUpdatedPubKeyOperator * @param {handleRemovedMasternode} handleRemovedMasternode @@ -43,6 +42,7 @@ function mergeEntities(result, newData) { * @param {number} smlMaxListsLimit * @param {LastSyncedCoreHeightRepository} lastSyncedCoreHeightRepository * @param {fetchSimplifiedMNList} fetchSimplifiedMNList + * @param {WebAssembly.Instance} dppWasm * @return {synchronizeMasternodeIdentities} */ function synchronizeMasternodeIdentitiesFactory( @@ -57,6 +57,7 @@ function synchronizeMasternodeIdentitiesFactory( smlMaxListsLimit, lastSyncedCoreHeightRepository, fetchSimplifiedMNList, + dppWasm, ) { let lastSyncedCoreHeight = 0; @@ -169,7 +170,7 @@ function synchronizeMasternodeIdentitiesFactory( : undefined; const affectedEntities = await handleUpdatedScriptPayout( - Identifier.from(Buffer.from(mnEntry.proRegTxHash, 'hex')), + dppWasm.Identifier.from(Buffer.from(mnEntry.proRegTxHash, 'hex')), newPayoutScript, blockInfo, previousPayoutScript, @@ -196,7 +197,7 @@ function synchronizeMasternodeIdentitiesFactory( : undefined; const affectedEntities = await handleUpdatedScriptPayout( - createOperatorIdentifier(mnEntry), + createOperatorIdentifier(dppWasm, mnEntry), new Script(newOperatorPayoutAddress), blockInfo, previousOperatorPayoutScript, @@ -229,7 +230,7 @@ function synchronizeMasternodeIdentitiesFactory( .concat(currentMNList.filter((currentMnListEntry) => !currentMnListEntry.isValid)); for (const masternodeEntry of disappearedOrInvalidMasterNodes) { - const masternodeIdentifier = Identifier.from( + const masternodeIdentifier = dppWasm.Identifier.from( Buffer.from(masternodeEntry.proRegTxHash, 'hex'), ); diff --git a/packages/js-drive/lib/test/mock/getBiggestPossibleIdentity.js b/packages/js-drive/lib/test/mock/getBiggestPossibleIdentity.js index 98a69715569..4f13f99d4fd 100644 --- a/packages/js-drive/lib/test/mock/getBiggestPossibleIdentity.js +++ b/packages/js-drive/lib/test/mock/getBiggestPossibleIdentity.js @@ -1,16 +1,14 @@ const identityCreateTransitionSchema = require('@dashevo/dpp/schema/identity/stateTransition/identityCreate.json'); -const IdentityPublicKey = require('@dashevo/dpp/lib/identity/IdentityPublicKey'); - -const Identity = require('@dashevo/dpp/lib/identity/Identity'); const generateRandomIdentifier = require('@dashevo/dpp/lib/test/utils/generateRandomIdentifier'); let identity; /** - * @return {Identity} + * @param {WebAssembly.Instance} dppWasm + * @return {dppWasm.Identity} */ -function getBiggestPossibleIdentity() { +function getBiggestPossibleIdentity(dppWasm) { if (identity) { return identity; } @@ -19,20 +17,20 @@ function getBiggestPossibleIdentity() { for (let i = 0; i < identityCreateTransitionSchema.properties.publicKeys.maxItems; i++) { const securityLevel = i === 0 - ? IdentityPublicKey.SECURITY_LEVELS.MASTER - : IdentityPublicKey.SECURITY_LEVELS.HIGH; + ? dppWasm.KeySecurityLevel.MASTER + : dppWasm.KeySecurityLevel.HIGH; publicKeys.push({ id: i, - type: IdentityPublicKey.TYPES.BLS12_381, - purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION, + type: dppWasm.KeyType.BLS12_381, + purpose: dppWasm.KeyPurpose.AUTHENTICATION, securityLevel, readOnly: false, data: Buffer.alloc(48).fill(255), }); } - identity = new Identity({ + identity = new dppWasm.Identity({ protocolVersion: 1, id: generateRandomIdentifier().toBuffer(), publicKeys, diff --git a/packages/js-drive/test/integration/fee/feesPrediction.spec.js b/packages/js-drive/test/integration/fee/feesPrediction.spec.js index 7a50d5c17e8..803aeabf842 100644 --- a/packages/js-drive/test/integration/fee/feesPrediction.spec.js +++ b/packages/js-drive/test/integration/fee/feesPrediction.spec.js @@ -119,10 +119,13 @@ describe('feesPrediction', () => { let Identity; let BlsSignatures; let expectPredictedFeeHigherOrEqualThanActual; + let KeyPurpose; + let KeyType; + let KeySecurityLevel; before(function before() { ({ - IdentityPublicKey, Identity, BlsSignatures, + IdentityPublicKey, Identity, BlsSignatures, KeyPurpose, KeyType, KeySecurityLevel } = this.dppWasm); expectPredictedFeeHigherOrEqualThanActual = expectPredictedFeeHigherOrEqualThanActualFactory( @@ -172,7 +175,7 @@ describe('feesPrediction', () => { instantAssetLockProof = getInstantAssetLockProofFixture(assetLockPrivateKey); - identity = getBiggestPossibleIdentity(); + identity = getBiggestPossibleIdentity(this.dppWasm); identity.id = instantAssetLockProof.createIdentifier(); identity.setAssetLockProof(instantAssetLockProof); @@ -209,7 +212,7 @@ describe('feesPrediction', () => { for (let i = 0; i < publicKeys.length; i++) { await stateTransition.signByPrivateKey( privateKeys[i], - IdentityPublicKey.TYPES.BLS12_381, + KeyType.BLS12_381, ); publicKeys[i].setSignature(stateTransition.getSignature()); @@ -220,7 +223,7 @@ describe('feesPrediction', () => { // Sign state transition await stateTransition.signByPrivateKey( assetLockPrivateKey, - IdentityPublicKey.TYPES.ECDSA_SECP256K1, + KeyType.ECDSA_SECP256K1, ); await expectPredictedFeeHigherOrEqualThanActual(dpp, groveDBStore, stateTransition); @@ -238,7 +241,7 @@ describe('feesPrediction', () => { await stateTransition.signByPrivateKey( assetLockPrivateKey, - IdentityPublicKey.TYPES.ECDSA_SECP256K1, + KeyType.ECDSA_SECP256K1, ); await expectPredictedFeeHigherOrEqualThanActual(dpp, groveDBStore, stateTransition); @@ -266,11 +269,11 @@ describe('feesPrediction', () => { newIdentityPublicKeys.push( new IdentityPublicKey({ id: i + identity.getPublicKeys().length, - type: IdentityPublicKey.TYPES.BLS12_381, + type: KeyType.BLS12_381, data: publicKeyBuffer, - purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION, + purpose: KeyPurpose.AUTHENTICATION, securityLevel: i === 0 - ? IdentityPublicKey.SECURITY_LEVELS.MASTER : IdentityPublicKey.SECURITY_LEVELS.HIGH, + ? KeySecurityLevel.MASTER : KeySecurityLevel.HIGH, readOnly: false, }), ); @@ -337,17 +340,17 @@ describe('feesPrediction', () => { publicKeys: [ { id: 0, - type: IdentityPublicKey.TYPES.BLS12_381, - purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION, - securityLevel: IdentityPublicKey.SECURITY_LEVELS.MASTER, + type: KeyType.BLS12_381, + purpose: KeyPurpose.AUTHENTICATION, + securityLevel: KeySecurityLevel.MASTER, readOnly: false, data: Buffer.alloc(48).fill(255), }, { id: 1, - type: IdentityPublicKey.TYPES.ECDSA_SECP256K1, - purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION, - securityLevel: IdentityPublicKey.SECURITY_LEVELS.HIGH, + type: KeyType.ECDSA_SECP256K1, + purpose: KeyPurpose.AUTHENTICATION, + securityLevel: KeySecurityLevel.HIGH, readOnly: false, data: privateKey.toPublicKey().toBuffer(), }, @@ -443,17 +446,17 @@ describe('feesPrediction', () => { publicKeys: [ { id: 0, - type: IdentityPublicKey.TYPES.BLS12_381, - purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION, - securityLevel: IdentityPublicKey.SECURITY_LEVELS.MASTER, + type: KeyType.BLS12_381, + purpose: KeyPurpose.AUTHENTICATION, + securityLevel: KeySecurityLevel.MASTER, readOnly: false, data: Buffer.alloc(48).fill(255), }, { id: 1, - type: IdentityPublicKey.TYPES.ECDSA_SECP256K1, - purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION, - securityLevel: IdentityPublicKey.SECURITY_LEVELS.HIGH, + type: KeyType.ECDSA_SECP256K1, + purpose: KeyPurpose.AUTHENTICATION, + securityLevel: KeySecurityLevel.HIGH, readOnly: false, data: privateKey.toPublicKey().toBuffer(), }, diff --git a/packages/js-drive/test/integration/identity/IdentityStoreRepository.spec.js b/packages/js-drive/test/integration/identity/IdentityStoreRepository.spec.js index d5a09688bd1..8b37e587f2b 100644 --- a/packages/js-drive/test/integration/identity/IdentityStoreRepository.spec.js +++ b/packages/js-drive/test/integration/identity/IdentityStoreRepository.spec.js @@ -22,9 +22,11 @@ describe('IdentityStoreRepository', () => { let publicKeyHashes; let Identity; let IdentityPublicKey; + let KeyType; + let KeySecurityLevel; before(function before() { - ({ Identity, IdentityPublicKey } = this.dppWasm); + ({ Identity, IdentityPublicKey, KeyType, KeySecurityLevel } = this.dppWasm); }); beforeEach(async () => { @@ -462,9 +464,9 @@ describe('IdentityStoreRepository', () => { publicKeys: [ { id: 0, - type: IdentityPublicKey.TYPES.ECDSA_SECP256K1, - purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION, - securityLevel: IdentityPublicKey.SECURITY_LEVELS.MASTER, + type: KeyType.ECDSA_SECP256K1, + purpose: KeyPurpose.AUTHENTICATION, + securityLevel: KeySecurityLevel.MASTER, readOnly: false, data, }, diff --git a/packages/js-drive/test/integration/identity/masternode/synchronizeMasternodeIdentitiesFactory.spec.js b/packages/js-drive/test/integration/identity/masternode/synchronizeMasternodeIdentitiesFactory.spec.js index c92ca8ac6ba..2b360dd28c8 100644 --- a/packages/js-drive/test/integration/identity/masternode/synchronizeMasternodeIdentitiesFactory.spec.js +++ b/packages/js-drive/test/integration/identity/masternode/synchronizeMasternodeIdentitiesFactory.spec.js @@ -20,6 +20,7 @@ const getSystemIdentityPublicKeysFixture = require('../../../../lib/test/fixture * @param {IdentityPublicKeyStoreRepository} identityPublicKeyRepository * @param {getWithdrawPubKeyTypeFromPayoutScript} getWithdrawPubKeyTypeFromPayoutScript * @param {getPublicKeyFromPayoutScript} getPublicKeyFromPayoutScript + * @param {WebAssembly.Instance} dppWasm * @returns {expectOperatorIdentity} */ function expectOperatorIdentityFactory( @@ -44,7 +45,8 @@ function expectOperatorIdentityFactory( const { IdentityPublicKey } = dppWasm; // Validate operator identity - const operatorIdentifier = createOperatorIdentifier(smlEntry); + const operatorIdentifier = createOperatorIdentifier(dppWasm, smlEntry); + const operatorIdentityResult = await identityRepository.fetch( operatorIdentifier, @@ -75,7 +77,7 @@ function expectOperatorIdentityFactory( const firstOperatorMasternodePublicKey = operatorIdentity.getPublicKeyById(0); expect(firstOperatorMasternodePublicKey.getType()) .to - .equal(IdentityPublicKey.TYPES.BLS12_381); + .equal(dppWasm.KeyType.BLS12_381); expect(firstOperatorMasternodePublicKey.getData()) .to .deep @@ -103,7 +105,7 @@ function expectOperatorIdentityFactory( const payoutPublicKey = operatorIdentity.getPublicKeyById(i); expect(payoutPublicKey.getType()).to.equal(publicKeyType); expect(payoutPublicKey.getData()).to.deep.equal( - getPublicKeyFromPayoutScript(payoutScript, publicKeyType), + getPublicKeyFromPayoutScript(dppWasm, payoutScript, publicKeyType), ); const masternodeIdentityByPayoutPublicKeyHashResult = await identityPublicKeyRepository @@ -125,7 +127,7 @@ function expectOperatorIdentityFactory( const payoutPublicKey = operatorIdentity.getPublicKeyById(i); expect(payoutPublicKey.getType()).to.equal(publicKeyType); expect(payoutPublicKey.getData()).to.deep.equal( - getPublicKeyFromPayoutScript(payoutScript, publicKeyType), + getPublicKeyFromPayoutScript(dppWasm, payoutScript, publicKeyType), ); const masternodeIdentityByPayoutPublicKeyHashResult = await identityRepository @@ -165,7 +167,7 @@ function expectVotingIdentityFactory( // Validate voting identity const { IdentityPublicKey } = dppWasm; - const votingIdentifier = createVotingIdentifier(smlEntry); + const votingIdentifier = createVotingIdentifier(smlEntry, dppWas); const votingIdentityResult = await identityRepository.fetch(votingIdentifier, { useTransaction: true, @@ -185,7 +187,7 @@ function expectVotingIdentityFactory( .lengthOf(1); const masternodePublicKey = votingIdentity.getPublicKeyById(0); - expect(masternodePublicKey.getType()).to.equal(IdentityPublicKey.TYPES.ECDSA_HASH160); + expect(masternodePublicKey.getType()).to.equal(dppWasm.KeyType.ECDSA_HASH160); expect(masternodePublicKey.getData()).to.deep.equal( Buffer.from(proRegTx.extraPayload.keyIDVoting, 'hex').reverse(), ); @@ -261,7 +263,7 @@ function expectMasternodeIdentityFactory( expect(masternodeIdentity.getPublicKeys()).to.have.lengthOf(publicKeysNum); const masternodePublicKey = masternodeIdentity.getPublicKeyById(0); - expect(masternodePublicKey.getType()).to.equal(IdentityPublicKey.TYPES.ECDSA_HASH160); + expect(masternodePublicKey.getType()).to.equal(dppWasm.KeyType.ECDSA_HASH160); expect(masternodePublicKey.getData()).to.deep.equal( Buffer.from(proRegTx.extraPayload.keyIDOwner, 'hex').reverse(), ); @@ -285,7 +287,7 @@ function expectMasternodeIdentityFactory( const payoutPublicKey = masternodeIdentity.getPublicKeyById(i); expect(payoutPublicKey.getType()).to.equal(publicKeyType); expect(payoutPublicKey.getData()).to.deep.equal( - getPublicKeyFromPayoutScript(payoutScript, publicKeyType), + getPublicKeyFromPayoutScript(dppWasm, payoutScript, publicKeyType), ); const masternodeIdentityByPayoutPublicKeyHashResult = await identityRepository @@ -307,7 +309,7 @@ function expectMasternodeIdentityFactory( const payoutPublicKey = masternodeIdentity.getPublicKeyById(i); expect(payoutPublicKey.getType()).to.equal(publicKeyType); expect(payoutPublicKey.getData()).to.deep.equal( - getPublicKeyFromPayoutScript(payoutScript, publicKeyType), + getPublicKeyFromPayoutScript(dppWasm, payoutScript, publicKeyType), ); const masternodeIdentityByPayoutPublicKeyHashResult = await identityRepository @@ -576,7 +578,7 @@ describe('synchronizeMasternodeIdentitiesFactory', function main() { Buffer.from(smlFixture[0].proRegTxHash, 'hex'), ); - const firstOperatorIdentifier = createOperatorIdentifier(smlFixture[0]); + const firstOperatorIdentifier = createOperatorIdentifier(this.dppWasm, smlFixture[0]); let documentsResult = await documentRepository.find( rewardsDataContract, @@ -708,7 +710,7 @@ describe('synchronizeMasternodeIdentitiesFactory', function main() { expect(fetchSimplifiedMNListMock).to.have.been.calledOnceWithExactly(1, coreHeight); }); - it('should create masternode identities if new masternode appeared', async () => { + it('should create masternode identities if new masternode appeared', async function test() { // Sync initial list const result = await synchronizeMasternodeIdentities(coreHeight, blockInfo); @@ -765,7 +767,7 @@ describe('synchronizeMasternodeIdentitiesFactory', function main() { Buffer.from(newSmlFixture[0].proRegTxHash, 'hex'), ); - const newOperatorIdentifier = createOperatorIdentifier(newSmlFixture[0]); + const newOperatorIdentifier = createOperatorIdentifier(this.dppWasm, newSmlFixture[0]); const documentsResult = await documentRepository.find( rewardsDataContract, @@ -913,7 +915,7 @@ describe('synchronizeMasternodeIdentitiesFactory', function main() { expect(documents).to.have.lengthOf(0); }); - it('should create operator identity and reward shares if PubKeyOperator was changed', async () => { + it('should create operator identity and reward shares if PubKeyOperator was changed', async function test() { // Initial sync await synchronizeMasternodeIdentities(coreHeight, blockInfo); @@ -988,12 +990,12 @@ describe('synchronizeMasternodeIdentitiesFactory', function main() { const [document] = documents; - const newOperatorIdentifier = createOperatorIdentifier(changedSmlEntry); + const newOperatorIdentifier = createOperatorIdentifier(this.dppWasm, changedSmlEntry); expect(document.get('payToId')).to.deep.equal(newOperatorIdentifier); }); - it('should handle changed payout, voting and operator payout addresses', async () => { + it('should handle changed payout, voting and operator payout addresses', async function test() { // Sync initial list await synchronizeMasternodeIdentities(coreHeight, blockInfo); @@ -1077,12 +1079,12 @@ describe('synchronizeMasternodeIdentitiesFactory', function main() { const [document] = documents; - const newOperatorIdentifier = createOperatorIdentifier(changedSmlEntry); + const newOperatorIdentifier = createOperatorIdentifier(this.dppWasm, changedSmlEntry); expect(document.get('payToId')).to.deep.equal(newOperatorIdentifier); }); - it('should not create voting Identity if owner and voting keys are the same', async () => { + it('should not create voting Identity if owner and voting keys are the same', async function test() { transaction1 = { extraPayload: { operatorReward: 100, @@ -1097,7 +1099,7 @@ describe('synchronizeMasternodeIdentitiesFactory', function main() { await synchronizeMasternodeIdentities(coreHeight, blockInfo); await expectDeterministicAppHash('7a5729e3511c5cc98e8452faa6132f0d600e04fef85df0f95f56e59c776de170'); - const votingIdentifier = createVotingIdentifier(smlFixture[0]); + const votingIdentifier = createVotingIdentifier(smlFixture[0], this.dppWasm); const votingIdentityResult = await identityRepository.fetch( votingIdentifier, diff --git a/packages/js-drive/test/unit/abci/handlers/query/dataContractQueryHandlerFactory.spec.js b/packages/js-drive/test/unit/abci/handlers/query/dataContractQueryHandlerFactory.spec.js index 3e135dcebc4..83b0e547e2f 100644 --- a/packages/js-drive/test/unit/abci/handlers/query/dataContractQueryHandlerFactory.spec.js +++ b/packages/js-drive/test/unit/abci/handlers/query/dataContractQueryHandlerFactory.spec.js @@ -51,6 +51,7 @@ describe('dataContractQueryHandlerFactory', () => { dataContractQueryHandler = dataContractQueryHandlerFactory( dataContractRepositoryMock, createQueryResponseMock, + this.dppWasm, ); params = {}; @@ -118,7 +119,7 @@ describe('dataContractQueryHandlerFactory', () => { const result = await dataContractQueryHandler(params, data, { prove: true }); expect(dataContractRepositoryMock.prove).to.be.calledOnceWithExactly( - new Identifier(data.id), + data.id, ); expect(result).to.be.an.instanceof(ResponseQuery); diff --git a/packages/js-drive/test/unit/abci/handlers/query/getProofsQueryHandlerFactory.spec.js b/packages/js-drive/test/unit/abci/handlers/query/getProofsQueryHandlerFactory.spec.js index ca2970a27b5..efbb023114d 100644 --- a/packages/js-drive/test/unit/abci/handlers/query/getProofsQueryHandlerFactory.spec.js +++ b/packages/js-drive/test/unit/abci/handlers/query/getProofsQueryHandlerFactory.spec.js @@ -72,6 +72,7 @@ describe('getProofsQueryHandlerFactory', () => { identityRepositoryMock, dataContractRepositoryMock, documentRepository, + this.dpp, ); dataContractData = { diff --git a/packages/js-drive/test/unit/abci/handlers/query/identityQueryHandlerFactory.spec.js b/packages/js-drive/test/unit/abci/handlers/query/identityQueryHandlerFactory.spec.js index d0e9167fee5..3522675bffb 100644 --- a/packages/js-drive/test/unit/abci/handlers/query/identityQueryHandlerFactory.spec.js +++ b/packages/js-drive/test/unit/abci/handlers/query/identityQueryHandlerFactory.spec.js @@ -45,6 +45,7 @@ describe('identityQueryHandlerFactory', () => { identityQueryHandler = identityQueryHandlerFactory( identityRepositoryMock, createQueryResponseMock, + this.dpp, ); identity = getIdentityFixture(); diff --git a/packages/js-drive/test/unit/identity/masternode/createMasternodeIdentityFactory.spec.js b/packages/js-drive/test/unit/identity/masternode/createMasternodeIdentityFactory.spec.js index 118a6324f6a..6ae853dc70a 100644 --- a/packages/js-drive/test/unit/identity/masternode/createMasternodeIdentityFactory.spec.js +++ b/packages/js-drive/test/unit/identity/masternode/createMasternodeIdentityFactory.spec.js @@ -18,16 +18,19 @@ describe('createMasternodeIdentityFactory', () => { let Identity; let IdentityPublicKey; let ValidationResult; + let KeyPurpose; + let KeyType; + let KeySecurityLevel; before(function before() { - ({ Identity, IdentityPublicKey, ValidationResult } = this.dppWasm); + ({ Identity, IdentityPublicKey, ValidationResult, KeyPurpose, KeyType, KeySecurityLevel } = this.dppWasm); }); beforeEach(function beforeEach() { dppMock = createDPPMock(this.sinon); getWithdrawPubKeyTypeFromPayoutScriptMock = this.sinon.stub().returns( - IdentityPublicKey.TYPES.BIP13_SCRIPT_HASH, + KeyType.BIP13_SCRIPT_HASH, ); getPublicKeyFromPayoutScriptMock = this.sinon.stub().returns( @@ -55,9 +58,9 @@ describe('createMasternodeIdentityFactory', () => { it('should create masternode identity', async () => { const identityId = generateRandomIdentifier(); const pubKeyData = Buffer.from([0]); - const pubKeyType = IdentityPublicKey.TYPES.ECDSA_HASH160; + const pubKeyType = KeyType.ECDSA_HASH160; - const result = await createMasternodeIdentity(blockInfo, identityId, pubKeyData, pubKeyType); + const result = await createMasternodeIdentity(this.dppWasm, blockInfo, identityId, pubKeyData, pubKeyType); const identity = new Identity({ protocolVersion: 1, @@ -65,8 +68,8 @@ describe('createMasternodeIdentityFactory', () => { publicKeys: [{ id: 0, type: pubKeyType, - purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION, - securityLevel: IdentityPublicKey.SECURITY_LEVELS.MASTER, + purpose: KeyPurpose.AUTHENTICATION, + securityLevel: KeySecurityLevel.MASTER, readOnly: true, // Copy data buffer data: Buffer.from([0]), @@ -93,7 +96,7 @@ describe('createMasternodeIdentityFactory', () => { it('should store identity and public key hashed to the previous store', async () => { const identityId = generateRandomIdentifier(); const pubKeyData = Buffer.from([0]); - const pubKeyType = IdentityPublicKey.TYPES.ECDSA_HASH160; + const pubKeyType = KeyType.ECDSA_HASH160; const result = await createMasternodeIdentity(blockInfo, identityId, pubKeyData, pubKeyType); @@ -103,8 +106,8 @@ describe('createMasternodeIdentityFactory', () => { publicKeys: [{ id: 0, type: pubKeyType, - purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION, - securityLevel: IdentityPublicKey.SECURITY_LEVELS.MASTER, + purpose: KeyPurpose.AUTHENTICATION, + securityLevel: KeySecurityLevel.MASTER, readOnly: true, // Copy data buffer data: Buffer.from([0]), @@ -129,7 +132,7 @@ describe('createMasternodeIdentityFactory', () => { const identityId = generateRandomIdentifier(); const pubKeyData = Buffer.from([0]); - const pubKeyType = IdentityPublicKey.TYPES.ECDSA_HASH160; + const pubKeyType = KeyType.ECDSA_HASH160; try { await createMasternodeIdentity(blockInfo, identityId, pubKeyData, pubKeyType); @@ -145,7 +148,7 @@ describe('createMasternodeIdentityFactory', () => { it('should create masternode identity with payoutScript public key', async () => { const identityId = generateRandomIdentifier(); const pubKeyData = Buffer.from([0]); - const pubKeyType = IdentityPublicKey.TYPES.ECDSA_HASH160; + const pubKeyType = KeyType.ECDSA_HASH160; const payoutScript = new Script(Address.fromString('7UkJidhNjEPJCQnCTXeaJKbJmL4JuyV66w')); const result = await createMasternodeIdentity( @@ -162,15 +165,15 @@ describe('createMasternodeIdentityFactory', () => { publicKeys: [{ id: 0, type: pubKeyType, - purpose: IdentityPublicKey.PURPOSES.AUTHENTICATION, - securityLevel: IdentityPublicKey.SECURITY_LEVELS.MASTER, + purpose: KeyPurpose.AUTHENTICATION, + securityLevel: KeySecurityLevel.MASTER, readOnly: true, data: Buffer.from([0]), }, { id: 1, - type: IdentityPublicKey.TYPES.BIP13_SCRIPT_HASH, - purpose: IdentityPublicKey.PURPOSES.WITHDRAW, - securityLevel: IdentityPublicKey.SECURITY_LEVELS.CRITICAL, + type: KeyType.BIP13_SCRIPT_HASH, + purpose: KeyPurpose.WITHDRAW, + securityLevel: KeySecurityLevel.CRITICAL, readOnly: false, data: Buffer.alloc(20, 1), }], diff --git a/packages/js-drive/test/unit/identity/masternode/createOperatorIdentifier.spec.js b/packages/js-drive/test/unit/identity/masternode/createOperatorIdentifier.spec.js index fe41cd9d766..395054caedd 100644 --- a/packages/js-drive/test/unit/identity/masternode/createOperatorIdentifier.spec.js +++ b/packages/js-drive/test/unit/identity/masternode/createOperatorIdentifier.spec.js @@ -4,7 +4,7 @@ describe('createOperatorIdentifier', () => { let smlEntry; let Identifier; - beforeEach(() => { + beforeEach(function beforeEach() { ({ Identifier } = this.dppWasm); smlEntry = { @@ -18,7 +18,7 @@ describe('createOperatorIdentifier', () => { }); it('should return operator identifier from smlEntry', () => { - const identifier = createOperatorIdentifier(smlEntry); + const identifier = createOperatorIdentifier(this.dppWasm, smlEntry); expect(identifier).to.deep.equal(Identifier.from('EwLi1FgGwvmLQ9nkfnttpXzv4SfC7XGBvs61QBCtnHEL')); }); diff --git a/packages/js-drive/test/unit/identity/masternode/createVotingIdentifier.spec.js b/packages/js-drive/test/unit/identity/masternode/createVotingIdentifier.spec.js index 30d4fdc2024..7098c544634 100644 --- a/packages/js-drive/test/unit/identity/masternode/createVotingIdentifier.spec.js +++ b/packages/js-drive/test/unit/identity/masternode/createVotingIdentifier.spec.js @@ -19,8 +19,8 @@ describe('createVotingIdentifier', () => { }; }); - it('should return voting identifier from smlEntry', () => { - const identifier = createVotingIdentifier(smlEntry); + it('should return voting identifier from smlEntry', function test() { + const identifier = createVotingIdentifier(smlEntry, this.dppWasm); expect(identifier).to.deep.equal(Identifier.from('G1p14MYdpNRLNWuKgQ9SjJUPxfuaJMTwYjdRWu9sLzvL')); }); diff --git a/packages/js-drive/test/unit/identity/masternode/getPublicKeyFromPayoutScript.spec.js b/packages/js-drive/test/unit/identity/masternode/getPublicKeyFromPayoutScript.spec.js index 7567859381c..dd49f350eee 100644 --- a/packages/js-drive/test/unit/identity/masternode/getPublicKeyFromPayoutScript.spec.js +++ b/packages/js-drive/test/unit/identity/masternode/getPublicKeyFromPayoutScript.spec.js @@ -6,16 +6,17 @@ const getPublicKeyFromPayoutScript = require('../../../../lib/identity/masternod describe('getPublicKeyFromPayoutScript', () => { let IdentityPublicKey; let InvalidIdentityPublicKeyTypeError; + let KeyType; before(function before() { - ({ IdentityPublicKey, InvalidIdentityPublicKeyTypeError } = this.dppWasm); + ({ IdentityPublicKey, InvalidIdentityPublicKeyTypeError, KeyType } = this.dppWasm); }); it('should return public key for ECDSA_HASH160 script', () => { const payoutAddress = Address.fromString('yLceJztHVZFbeqE9v86sLD9bDKFBmNqHQD'); const scriptBuffer = new Script(payoutAddress); - const type = IdentityPublicKey.TYPES.ECDSA_HASH160; + const type = KeyType.ECDSA_HASH160; const result = getPublicKeyFromPayoutScript(scriptBuffer, type); @@ -26,7 +27,7 @@ describe('getPublicKeyFromPayoutScript', () => { const payoutAddress = Address.fromString('7UkJidhNjEPJCQnCTXeaJKbJmL4JuyV66w'); const scriptBuffer = new Script(payoutAddress); - const type = IdentityPublicKey.TYPES.BIP13_SCRIPT_HASH; + const type = KeyType.BIP13_SCRIPT_HASH; const result = getPublicKeyFromPayoutScript(scriptBuffer, type); diff --git a/packages/js-drive/test/unit/identity/masternode/getWithdrawPubKeyTypeFromPayoutScriptFactory.spec.js b/packages/js-drive/test/unit/identity/masternode/getWithdrawPubKeyTypeFromPayoutScriptFactory.spec.js index 7b415670929..146708a14a8 100644 --- a/packages/js-drive/test/unit/identity/masternode/getWithdrawPubKeyTypeFromPayoutScriptFactory.spec.js +++ b/packages/js-drive/test/unit/identity/masternode/getWithdrawPubKeyTypeFromPayoutScriptFactory.spec.js @@ -7,14 +7,16 @@ describe('getWithdrawPubKeyTypeFromPayoutScriptFactory', () => { let getWithdrawPubKeyTypeFromPayoutScript; let network; let IdentityPublicKey; + let KeyType; before(function before() { - ({ IdentityPublicKey } = this.dppWasm); + ({ IdentityPublicKey, KeyType } = this.dppWasm); }); - beforeEach(() => { + beforeEach(function beforeEach() { network = 'testnet'; getWithdrawPubKeyTypeFromPayoutScript = getWithdrawPubKeyTypeFromPayoutScriptFactory( + this.dppWasm, network, ); }); @@ -23,14 +25,14 @@ describe('getWithdrawPubKeyTypeFromPayoutScriptFactory', () => { const payoutScript = Script(Address.fromString('yTsGq4wV8WF5GKLaYV2C43zrkr2sfTtysT')); const type = getWithdrawPubKeyTypeFromPayoutScript(payoutScript); - expect(type).to.be.equal(IdentityPublicKey.TYPES.ECDSA_HASH160); + expect(type).to.be.equal(KeyType.ECDSA_HASH160); }); it('should return BIP13_SCRIPT_HASH if address has p2sh type', () => { const payoutScript = Script(Address.fromString('7UkJidhNjEPJCQnCTXeaJKbJmL4JuyV66w')); const type = getWithdrawPubKeyTypeFromPayoutScript(payoutScript); - expect(type).to.be.equal(IdentityPublicKey.TYPES.BIP13_SCRIPT_HASH); + expect(type).to.be.equal(KeyType.BIP13_SCRIPT_HASH); }); it('should throw InvalidPayoutScriptError if address is not p2sh or p2pkh', () => { diff --git a/packages/js-drive/test/unit/identity/masternode/handleNewMasternodeFactory.spec.js b/packages/js-drive/test/unit/identity/masternode/handleNewMasternodeFactory.spec.js index 8529afcde1e..7a957334d97 100644 --- a/packages/js-drive/test/unit/identity/masternode/handleNewMasternodeFactory.spec.js +++ b/packages/js-drive/test/unit/identity/masternode/handleNewMasternodeFactory.spec.js @@ -23,9 +23,10 @@ describe('handleNewMasternodeFactory', () => { let identityFixture; let Identifier; let IdentityPublicKey; + let KeyType; before(function before() { - ({ Identifier, IdentityPublicKey } = this.dppWasm); + ({ Identifier, IdentityPublicKey, KeyType } = this.dppWasm); }); beforeEach(function beforeEach() { @@ -55,6 +56,7 @@ describe('handleNewMasternodeFactory', () => { fetchTransactionMock = this.sinon.stub().resolves(transactionFixture); handleNewMasternode = handleNewMasternodeFactory( + this.dpp, dppMock, createMasternodeIdentityMock, createRewardShareDocumentMock, @@ -82,7 +84,7 @@ describe('handleNewMasternodeFactory', () => { blockInfo, Identifier.from('HYyu6DdUQyiHZwzeWpmahu7AUrsEF9MKkRcrdQnKeNSj'), Buffer.from('6161616161616161616161616161616161616161', 'hex'), - IdentityPublicKey.TYPES.ECDSA_HASH160, + KeyType.ECDSA_HASH160, payoutScript, ); @@ -90,7 +92,7 @@ describe('handleNewMasternodeFactory', () => { blockInfo, Identifier.from('GVYoKVDd29gbmHzbVGepFjCbdymCS5Jq26CCiLnWNL6C'), Buffer.from('6262626262626262626262626262626262626262', 'hex'), - IdentityPublicKey.TYPES.ECDSA_HASH160, + KeyType.ECDSA_HASH160, ); expect(createRewardShareDocumentMock).to.not.be.called(); @@ -119,14 +121,14 @@ describe('handleNewMasternodeFactory', () => { blockInfo, Identifier.from('HYyu6DdUQyiHZwzeWpmahu7AUrsEF9MKkRcrdQnKeNSj'), Buffer.from('6161616161616161616161616161616161616161', 'hex'), - IdentityPublicKey.TYPES.ECDSA_HASH160, + KeyType.ECDSA_HASH160, payoutScript, ); expect(createRewardShareDocumentMock).to.not.be.called(); }); - it('should create masternode identity and a document in rewards data contract with percentage', async () => { + it('should create masternode identity and a document in rewards data contract with percentage', async function test() { transactionFixture.extraPayload.operatorReward = 10; const result = await handleNewMasternode(masternodeEntry, dataContract, blockInfo); @@ -139,11 +141,11 @@ describe('handleNewMasternodeFactory', () => { expect(result.createdEntities[1].toJSON()).to.deep.equal(identityFixture.toJSON()); expect(result.createdEntities[2].toJSON()).to.deep.equal(identityFixture.toJSON()); - const operatorIdentifier = createOperatorIdentifier(masternodeEntry); + const operatorIdentifier = createOperatorIdentifier(this.dppWasm, masternodeEntry); const operatorPayoutAddress = Address.fromString(masternodeEntry.operatorPayoutAddress); const operatorPayoutScript = new Script(operatorPayoutAddress); - const votingIdentifier = createVotingIdentifier(masternodeEntry); + const votingIdentifier = createVotingIdentifier(masternodeEntry, this.dppWasm); const payoutAddress = Address.fromString(masternodeEntry.payoutAddress); const payoutScript = new Script(payoutAddress); @@ -153,7 +155,7 @@ describe('handleNewMasternodeFactory', () => { blockInfo, Identifier.from('HYyu6DdUQyiHZwzeWpmahu7AUrsEF9MKkRcrdQnKeNSj'), Buffer.from('6161616161616161616161616161616161616161', 'hex'), - IdentityPublicKey.TYPES.ECDSA_HASH160, + KeyType.ECDSA_HASH160, payoutScript, ); @@ -161,7 +163,7 @@ describe('handleNewMasternodeFactory', () => { blockInfo, operatorIdentifier, Buffer.from('951a3208ba531ea75aedd2dc0a9efc75f2c4d9492f1ee0a989b593bcd9722b1a101774d80a426552a9f91d24eb55af6e', 'hex'), - IdentityPublicKey.TYPES.BLS12_381, + KeyType.BLS12_381, operatorPayoutScript, ); @@ -169,7 +171,7 @@ describe('handleNewMasternodeFactory', () => { blockInfo, votingIdentifier, Buffer.from('6262626262626262626262626262626262626262', 'hex'), - IdentityPublicKey.TYPES.ECDSA_HASH160, + KeyType.ECDSA_HASH160, ); expect(createRewardShareDocumentMock).to.be.calledOnceWithExactly( diff --git a/packages/js-drive/test/unit/identity/masternode/handleUpdatedScriptPayoutFactory.spec.js b/packages/js-drive/test/unit/identity/masternode/handleUpdatedScriptPayoutFactory.spec.js index 21a59fc2a83..866982ba6b3 100644 --- a/packages/js-drive/test/unit/identity/masternode/handleUpdatedScriptPayoutFactory.spec.js +++ b/packages/js-drive/test/unit/identity/masternode/handleUpdatedScriptPayoutFactory.spec.js @@ -17,9 +17,12 @@ describe('handleUpdatedScriptPayoutFactory', () => { let identityPublicKeyRepositoryMock; let IdentityPublicKey; let Identity; + let KeyPurpose; + let KeyType; + let KeySecurityLevel; before(function before() { - ({ Identity, IdentityPublicKey } = this.dppWasm); + ({ Identity, IdentityPublicKey, KeyPurpose, KeyType, KeySecurityLevel } = this.dppWasm); }); beforeEach(function beforeEach() { @@ -28,7 +31,7 @@ describe('handleUpdatedScriptPayoutFactory', () => { blockInfo = new BlockInfo(1, 0, Date.now()); getWithdrawPubKeyTypeFromPayoutScriptMock = this.sinon.stub().returns( - IdentityPublicKey.TYPES.ECDSA_HASH160, + KeyType.ECDSA_HASH160, ); getPublicKeyFromPayoutScriptMock = this.sinon.stub().returns(Buffer.alloc(20, '0')); @@ -44,6 +47,7 @@ describe('handleUpdatedScriptPayoutFactory', () => { }; handleUpdatedScriptPayout = handleUpdatedScriptPayoutFactory( + this.dppWasm, identityRepositoryMock, identityPublicKeyRepositoryMock, getWithdrawPubKeyTypeFromPayoutScriptMock, @@ -88,11 +92,11 @@ describe('handleUpdatedScriptPayoutFactory', () => { const newWithdrawalIdentityPublicKey = new IdentityPublicKey() .setId(2) - .setType(IdentityPublicKey.TYPES.ECDSA_HASH160) + .setType(KeyType.ECDSA_HASH160) .setData(Buffer.from(newPubKeyData)) .setReadOnly(true) - .setPurpose(IdentityPublicKey.PURPOSES.WITHDRAW) - .setSecurityLevel(IdentityPublicKey.SECURITY_LEVELS.MASTER); + .setPurpose(KeyPurpose.WITHDRAW) + .setSecurityLevel(KeySecurityLevel.MASTER); expect(identityRepositoryMock.updateRevision).to.be.calledOnceWithExactly( identity.getId(), @@ -133,11 +137,11 @@ describe('handleUpdatedScriptPayoutFactory', () => { const newWithdrawalIdentityPublicKey = new IdentityPublicKey() .setId(2) - .setType(IdentityPublicKey.TYPES.ECDSA_HASH160) + .setType(KeyType.ECDSA_HASH160) .setData(Buffer.from(newPubKeyData)) .setReadOnly(true) - .setPurpose(IdentityPublicKey.PURPOSES.WITHDRAW) - .setSecurityLevel(IdentityPublicKey.SECURITY_LEVELS.MASTER); + .setPurpose(KeyPurpose.WITHDRAW) + .setSecurityLevel(KeySecurityLevel.MASTER); expect(identityRepositoryMock.updateRevision).to.be.calledOnceWithExactly( identity.getId(), diff --git a/packages/js-drive/test/unit/identity/masternode/handleUpdatedVotingAddressFactory.spec.js b/packages/js-drive/test/unit/identity/masternode/handleUpdatedVotingAddressFactory.spec.js index 780751876b5..37856ee58c6 100644 --- a/packages/js-drive/test/unit/identity/masternode/handleUpdatedVotingAddressFactory.spec.js +++ b/packages/js-drive/test/unit/identity/masternode/handleUpdatedVotingAddressFactory.spec.js @@ -15,9 +15,10 @@ describe('handleUpdatedVotingAddressFactory', () => { let blockInfo; let IdentityPublicKey; let Identifier; + let KeyType; before(function before() { - ({ Identifier, IdentityPublicKey } = this.dppWasm); + ({ Identifier, IdentityPublicKey, KeyType } = this.dppWasm); }); beforeEach(function beforeEach() { @@ -52,6 +53,7 @@ describe('handleUpdatedVotingAddressFactory', () => { blockInfo = new BlockInfo(1, 0, Date.now()); handleUpdatedVotingAddress = handleUpdatedVotingAddressFactory( + this.dpp, identityRepositoryMock, createMasternodeIdentityMock, fetchTransactionMock, @@ -72,7 +74,7 @@ describe('handleUpdatedVotingAddressFactory', () => { blockInfo, Identifier.from('G1p14MYdpNRLNWuKgQ9SjJUPxfuaJMTwYjdRWu9sLzvL'), Buffer.from('8fd1a9502c58ab103792693e951bf39f10ee46a9', 'hex'), - IdentityPublicKey.TYPES.ECDSA_HASH160, + KeyType.ECDSA_HASH160, ); expect(fetchTransactionMock).to.be.calledWithExactly(smlEntry.proRegTxHash); }); From 9bdc846b7a4228cfb6baf57d15646474a5c13e3c Mon Sep 17 00:00:00 2001 From: Pawel Iwan Date: Thu, 16 Mar 2023 10:47:33 +0100 Subject: [PATCH 2/2] fix part of test errors --- .../synchronizeMasternodeIdentitiesFactory.spec.js | 8 ++++---- .../query/getProofsQueryHandlerFactory.spec.js | 2 +- .../createMasternodeIdentityFactory.spec.js | 1 + .../masternode/createOperatorIdentifier.spec.js | 4 ++-- .../masternode/createVotingIdentifier.spec.js | 2 +- .../getPublicKeyFromPayoutScript.spec.js | 12 ++++++------ ...thdrawPubKeyTypeFromPayoutScriptFactory.spec.js | 14 +++++++------- 7 files changed, 22 insertions(+), 21 deletions(-) diff --git a/packages/js-drive/test/integration/identity/masternode/synchronizeMasternodeIdentitiesFactory.spec.js b/packages/js-drive/test/integration/identity/masternode/synchronizeMasternodeIdentitiesFactory.spec.js index 2b360dd28c8..bd9a255f881 100644 --- a/packages/js-drive/test/integration/identity/masternode/synchronizeMasternodeIdentitiesFactory.spec.js +++ b/packages/js-drive/test/integration/identity/masternode/synchronizeMasternodeIdentitiesFactory.spec.js @@ -105,7 +105,7 @@ function expectOperatorIdentityFactory( const payoutPublicKey = operatorIdentity.getPublicKeyById(i); expect(payoutPublicKey.getType()).to.equal(publicKeyType); expect(payoutPublicKey.getData()).to.deep.equal( - getPublicKeyFromPayoutScript(dppWasm, payoutScript, publicKeyType), + getPublicKeyFromPayoutScript(payoutScript, publicKeyType, dppWasm), ); const masternodeIdentityByPayoutPublicKeyHashResult = await identityPublicKeyRepository @@ -127,7 +127,7 @@ function expectOperatorIdentityFactory( const payoutPublicKey = operatorIdentity.getPublicKeyById(i); expect(payoutPublicKey.getType()).to.equal(publicKeyType); expect(payoutPublicKey.getData()).to.deep.equal( - getPublicKeyFromPayoutScript(dppWasm, payoutScript, publicKeyType), + getPublicKeyFromPayoutScript(payoutScript, publicKeyType, dppWasm), ); const masternodeIdentityByPayoutPublicKeyHashResult = await identityRepository @@ -287,7 +287,7 @@ function expectMasternodeIdentityFactory( const payoutPublicKey = masternodeIdentity.getPublicKeyById(i); expect(payoutPublicKey.getType()).to.equal(publicKeyType); expect(payoutPublicKey.getData()).to.deep.equal( - getPublicKeyFromPayoutScript(dppWasm, payoutScript, publicKeyType), + getPublicKeyFromPayoutScript(payoutScript, publicKeyType, dppWasm), ); const masternodeIdentityByPayoutPublicKeyHashResult = await identityRepository @@ -309,7 +309,7 @@ function expectMasternodeIdentityFactory( const payoutPublicKey = masternodeIdentity.getPublicKeyById(i); expect(payoutPublicKey.getType()).to.equal(publicKeyType); expect(payoutPublicKey.getData()).to.deep.equal( - getPublicKeyFromPayoutScript(dppWasm, payoutScript, publicKeyType), + getPublicKeyFromPayoutScript(payoutScript, publicKeyType, dppWasm), ); const masternodeIdentityByPayoutPublicKeyHashResult = await identityRepository diff --git a/packages/js-drive/test/unit/abci/handlers/query/getProofsQueryHandlerFactory.spec.js b/packages/js-drive/test/unit/abci/handlers/query/getProofsQueryHandlerFactory.spec.js index efbb023114d..a61eb6587e9 100644 --- a/packages/js-drive/test/unit/abci/handlers/query/getProofsQueryHandlerFactory.spec.js +++ b/packages/js-drive/test/unit/abci/handlers/query/getProofsQueryHandlerFactory.spec.js @@ -72,7 +72,7 @@ describe('getProofsQueryHandlerFactory', () => { identityRepositoryMock, dataContractRepositoryMock, documentRepository, - this.dpp, + this.dppWasm, ); dataContractData = { diff --git a/packages/js-drive/test/unit/identity/masternode/createMasternodeIdentityFactory.spec.js b/packages/js-drive/test/unit/identity/masternode/createMasternodeIdentityFactory.spec.js index 6ae853dc70a..d7670841f11 100644 --- a/packages/js-drive/test/unit/identity/masternode/createMasternodeIdentityFactory.spec.js +++ b/packages/js-drive/test/unit/identity/masternode/createMasternodeIdentityFactory.spec.js @@ -50,6 +50,7 @@ describe('createMasternodeIdentityFactory', () => { identityRepositoryMock, getWithdrawPubKeyTypeFromPayoutScriptMock, getPublicKeyFromPayoutScriptMock, + this.dppWasm ); blockInfo = new BlockInfo(1, 0, Date.now()); diff --git a/packages/js-drive/test/unit/identity/masternode/createOperatorIdentifier.spec.js b/packages/js-drive/test/unit/identity/masternode/createOperatorIdentifier.spec.js index 395054caedd..f20707454e3 100644 --- a/packages/js-drive/test/unit/identity/masternode/createOperatorIdentifier.spec.js +++ b/packages/js-drive/test/unit/identity/masternode/createOperatorIdentifier.spec.js @@ -1,6 +1,6 @@ const createOperatorIdentifier = require('../../../../lib/identity/masternode/createOperatorIdentifier'); -describe('createOperatorIdentifier', () => { +describe('createOperatorIdentifier', function test() { let smlEntry; let Identifier; @@ -17,7 +17,7 @@ describe('createOperatorIdentifier', () => { }; }); - it('should return operator identifier from smlEntry', () => { + it('should return operator identifier from smlEntry', function test() { const identifier = createOperatorIdentifier(this.dppWasm, smlEntry); expect(identifier).to.deep.equal(Identifier.from('EwLi1FgGwvmLQ9nkfnttpXzv4SfC7XGBvs61QBCtnHEL')); diff --git a/packages/js-drive/test/unit/identity/masternode/createVotingIdentifier.spec.js b/packages/js-drive/test/unit/identity/masternode/createVotingIdentifier.spec.js index 7098c544634..2e7bf892e6b 100644 --- a/packages/js-drive/test/unit/identity/masternode/createVotingIdentifier.spec.js +++ b/packages/js-drive/test/unit/identity/masternode/createVotingIdentifier.spec.js @@ -5,7 +5,7 @@ describe('createVotingIdentifier', () => { let Identifier; before(function before() { - ({ Identifier } = this.dpp); + ({ Identifier } = this.dppWasm); }); beforeEach(() => { diff --git a/packages/js-drive/test/unit/identity/masternode/getPublicKeyFromPayoutScript.spec.js b/packages/js-drive/test/unit/identity/masternode/getPublicKeyFromPayoutScript.spec.js index dd49f350eee..ecd3fcf93bd 100644 --- a/packages/js-drive/test/unit/identity/masternode/getPublicKeyFromPayoutScript.spec.js +++ b/packages/js-drive/test/unit/identity/masternode/getPublicKeyFromPayoutScript.spec.js @@ -12,34 +12,34 @@ describe('getPublicKeyFromPayoutScript', () => { ({ IdentityPublicKey, InvalidIdentityPublicKeyTypeError, KeyType } = this.dppWasm); }); - it('should return public key for ECDSA_HASH160 script', () => { + it('should return public key for ECDSA_HASH160 script', function test() { const payoutAddress = Address.fromString('yLceJztHVZFbeqE9v86sLD9bDKFBmNqHQD'); const scriptBuffer = new Script(payoutAddress); const type = KeyType.ECDSA_HASH160; - const result = getPublicKeyFromPayoutScript(scriptBuffer, type); + const result = getPublicKeyFromPayoutScript(scriptBuffer, type, this.dppWasm); expect(result).to.deep.equal(Buffer.from('0340a3abf7e6eccf42b4dd71ef8c20ed53a78d1f', 'hex')); }); - it('should return public key for BIP13_SCRIPT_HASH script', () => { + it('should return public key for BIP13_SCRIPT_HASH script', function test() { const payoutAddress = Address.fromString('7UkJidhNjEPJCQnCTXeaJKbJmL4JuyV66w'); const scriptBuffer = new Script(payoutAddress); const type = KeyType.BIP13_SCRIPT_HASH; - const result = getPublicKeyFromPayoutScript(scriptBuffer, type); + const result = getPublicKeyFromPayoutScript(scriptBuffer, type, this.dppWasm); expect(result).to.deep.equal(Buffer.from('19a7d869032368fd1f1e26e5e73a4ad0e474960e', 'hex')); }); - it('should throw InvalidIdentityPublicKeyTypeError if type is unknown', () => { + it('should throw InvalidIdentityPublicKeyTypeError if type is unknown', function test() { const payoutAddress = Address.fromString('7UkJidhNjEPJCQnCTXeaJKbJmL4JuyV66w'); const scriptBuffer = new Script(payoutAddress); try { - getPublicKeyFromPayoutScript(scriptBuffer, -1); + getPublicKeyFromPayoutScript(scriptBuffer, -1, this.dppWasm); expect.fail('should throw InvalidIdentityPublicKeyTypeError'); } catch (e) { diff --git a/packages/js-drive/test/unit/identity/masternode/getWithdrawPubKeyTypeFromPayoutScriptFactory.spec.js b/packages/js-drive/test/unit/identity/masternode/getWithdrawPubKeyTypeFromPayoutScriptFactory.spec.js index 146708a14a8..6b109841c6c 100644 --- a/packages/js-drive/test/unit/identity/masternode/getWithdrawPubKeyTypeFromPayoutScriptFactory.spec.js +++ b/packages/js-drive/test/unit/identity/masternode/getWithdrawPubKeyTypeFromPayoutScriptFactory.spec.js @@ -16,30 +16,30 @@ describe('getWithdrawPubKeyTypeFromPayoutScriptFactory', () => { beforeEach(function beforeEach() { network = 'testnet'; getWithdrawPubKeyTypeFromPayoutScript = getWithdrawPubKeyTypeFromPayoutScriptFactory( - this.dppWasm, network, + this.dppWasm, ); }); - it('should return ECDSA_HASH160 if address has p2pkh type', () => { + it('should return ECDSA_HASH160 if address has p2pkh type', function test() { const payoutScript = Script(Address.fromString('yTsGq4wV8WF5GKLaYV2C43zrkr2sfTtysT')); - const type = getWithdrawPubKeyTypeFromPayoutScript(payoutScript); + const type = getWithdrawPubKeyTypeFromPayoutScript(payoutScript, this.dppWasm); expect(type).to.be.equal(KeyType.ECDSA_HASH160); }); - it('should return BIP13_SCRIPT_HASH if address has p2sh type', () => { + it('should return BIP13_SCRIPT_HASH if address has p2sh type', function test() { const payoutScript = Script(Address.fromString('7UkJidhNjEPJCQnCTXeaJKbJmL4JuyV66w')); - const type = getWithdrawPubKeyTypeFromPayoutScript(payoutScript); + const type = getWithdrawPubKeyTypeFromPayoutScript(payoutScript, this.dppWasm); expect(type).to.be.equal(KeyType.BIP13_SCRIPT_HASH); }); - it('should throw InvalidPayoutScriptError if address is not p2sh or p2pkh', () => { + it('should throw InvalidPayoutScriptError if address is not p2sh or p2pkh', function test() { const payoutScript = new Script(); try { - getWithdrawPubKeyTypeFromPayoutScript(payoutScript); + getWithdrawPubKeyTypeFromPayoutScript(payoutScript, this.dppWasm); expect.fail('should throw InvalidPayoutScriptError'); } catch (e) {