diff --git a/bin/darwin/arm64/.gitkeep b/bin/darwin/arm64/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bin/darwin/arm64/bls-tools b/bin/darwin/arm64/bls-tools deleted file mode 100755 index 193603a1ff..0000000000 Binary files a/bin/darwin/arm64/bls-tools and /dev/null differ diff --git a/bin/darwin/x64/.gitkeep b/bin/darwin/x64/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bin/darwin/x64/bls-tools b/bin/darwin/x64/bls-tools deleted file mode 100755 index a17623ee5e..0000000000 Binary files a/bin/darwin/x64/bls-tools and /dev/null differ diff --git a/bin/linux/arm64/.gitkeep b/bin/linux/arm64/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bin/linux/arm64/bls-tools b/bin/linux/arm64/bls-tools deleted file mode 100755 index 59cfedda6b..0000000000 Binary files a/bin/linux/arm64/bls-tools and /dev/null differ diff --git a/bin/linux/x64/.gitkeep b/bin/linux/x64/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bin/linux/x64/bls-tools b/bin/linux/x64/bls-tools deleted file mode 100755 index 0e61972459..0000000000 Binary files a/bin/linux/x64/bls-tools and /dev/null differ diff --git a/bin/win32/x64/.gitkeep b/bin/win32/x64/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/bin/win32/x64/bls-tools.exe b/bin/win32/x64/bls-tools.exe deleted file mode 100755 index 56075c7eac..0000000000 Binary files a/bin/win32/x64/bls-tools.exe and /dev/null differ diff --git a/ot-node.js b/ot-node.js index bb05cad1bd..c4a2d405d7 100644 --- a/ot-node.js +++ b/ot-node.js @@ -54,7 +54,6 @@ class OTNode { await this.initializeRouters(); await this.startNetworkModule(); - await this.initializeBLSService(); this.resumeCommandExecutor(); this.logger.info('Node is up and running!'); } @@ -385,18 +384,6 @@ class OTNode { tripleStoreService.initializeRepositories(); } - async initializeBLSService() { - try { - const blsService = this.container.resolve('blsService'); - await blsService.initialize(); - } catch (error) { - this.logger.error( - `Unable to initialize BLS Service. Error message: ${error.message} OT-node shutting down...`, - ); - this.stop(1); - } - } - stop(code = 0) { this.logger.info('Stopping node...'); process.exit(code); diff --git a/package-lock.json b/package-lock.json index b4c4a1a7b7..268b1651a6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -47,6 +47,7 @@ "libp2p-mplex": "^0.10.7", "libp2p-noise": "^4.0.0", "libp2p-tcp": "^0.17.2", + "mcl-wasm": "^1.7.0", "minimist": "^1.2.7", "ms": "^2.1.3", "mysql2": "^3.3.0", @@ -16216,6 +16217,33 @@ "node": ">= 14" } }, + "node_modules/mcl-wasm": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/mcl-wasm/-/mcl-wasm-1.7.0.tgz", + "integrity": "sha512-ok9uE7ekFh5+orI0dFT19KeY/y5P6ONp0dks8oo/KniyNK6mJ0zloL3+s6LiEQXW8VxQHwsfZslitL/R7MM9ew==", + "license": "BSD-3-Clause", + "dependencies": { + "@types/node": "^20.2.5" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/mcl-wasm/node_modules/@types/node": { + "version": "20.17.9", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.17.9.tgz", + "integrity": "sha512-0JOXkRyLanfGPE2QRCwgxhzlBAvaRdCNMcvbd7jFfpmD4eEXll7LRwy5ymJmyeZqk7Nh7eD2LeUyQ68BbndmXw==", + "license": "MIT", + "dependencies": { + "undici-types": "~6.19.2" + } + }, + "node_modules/mcl-wasm/node_modules/undici-types": { + "version": "6.19.8", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", + "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", + "license": "MIT" + }, "node_modules/md5.js": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz", @@ -35574,6 +35602,29 @@ "typed-function": "^2.1.0" } }, + "mcl-wasm": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/mcl-wasm/-/mcl-wasm-1.7.0.tgz", + "integrity": "sha512-ok9uE7ekFh5+orI0dFT19KeY/y5P6ONp0dks8oo/KniyNK6mJ0zloL3+s6LiEQXW8VxQHwsfZslitL/R7MM9ew==", + "requires": { + "@types/node": "^20.2.5" + }, + "dependencies": { + "@types/node": { + "version": "20.17.9", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.17.9.tgz", + "integrity": "sha512-0JOXkRyLanfGPE2QRCwgxhzlBAvaRdCNMcvbd7jFfpmD4eEXll7LRwy5ymJmyeZqk7Nh7eD2LeUyQ68BbndmXw==", + "requires": { + "undici-types": "~6.19.2" + } + }, + "undici-types": { + "version": "6.19.8", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", + "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==" + } + } + }, "md5.js": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz", diff --git a/package.json b/package.json index 2fbfac54d7..a535c8fe78 100644 --- a/package.json +++ b/package.json @@ -68,7 +68,6 @@ "@ethersproject/bytes": "^5.7.0", "@ethersproject/hash": "^5.7.0", "@ethersproject/wallet": "^5.7.0", - "@questdb/nodejs-client": "^3.0.0", "@polkadot/api": "^9.3.2", "@polkadot/keyring": "^10.1.7", "@polkadot/util": "^10.1.7", @@ -102,6 +101,7 @@ "libp2p-mplex": "^0.10.7", "libp2p-noise": "^4.0.0", "libp2p-tcp": "^0.17.2", + "mcl-wasm": "^1.7.0", "minimist": "^1.2.7", "ms": "^2.1.3", "mysql2": "^3.3.0", diff --git a/src/commands/local-store/local-store-command.js b/src/commands/local-store/local-store-command.js index f87077d7f0..6577a11939 100644 --- a/src/commands/local-store/local-store-command.js +++ b/src/commands/local-store/local-store-command.js @@ -21,8 +21,7 @@ class LocalStoreCommand extends Command { this.blockchainModuleManager = ctx.blockchainModuleManager; this.commandExecutor = ctx.commandExecutor; this.repositoryModuleManager = ctx.repositoryModuleManager; - this.blsService = ctx.blsService; - this.signatureStorageService = ctx.signatureStorageService; + this.signatureService = ctx.signatureService; this.errorType = ERROR_TYPE.LOCAL_STORE.LOCAL_STORE_ERROR; } @@ -63,12 +62,19 @@ class LocalStoreCommand extends Command { ); const identityId = await this.blockchainModuleManager.getIdentityId(blockchain); - const signature = await this.blsService.sign(datasetRoot); + const { signer, v, r, s, vs } = await this.signatureService.signMessage( + blockchain, + datasetRoot, + ); - await this.signatureStorageService.addSignatureToStorage( + await this.signatureService.addSignatureToStorage( operationId, identityId, - signature, + signer, + v, + r, + s, + vs, ); await this.operationService.processResponse( @@ -76,10 +82,9 @@ class LocalStoreCommand extends Command { OPERATION_REQUEST_STATUS.COMPLETED, { messageType: NETWORK_MESSAGE_TYPES.RESPONSES.ACK, - messageData: { identityId, signature }, + messageData: { identityId, signer, v, r, s, vs }, }, null, - true, ); this.operationIdService.emitChangeEvent( OPERATION_ID_STATUS.LOCAL_STORE.LOCAL_STORE_PROCESS_RESPONSE_END, diff --git a/src/commands/protocols/publish/receiver/v1.0.0/v1-0-0-handle-store-request-command.js b/src/commands/protocols/publish/receiver/v1.0.0/v1-0-0-handle-store-request-command.js index e10bcaed66..46980b3168 100644 --- a/src/commands/protocols/publish/receiver/v1.0.0/v1-0-0-handle-store-request-command.js +++ b/src/commands/protocols/publish/receiver/v1.0.0/v1-0-0-handle-store-request-command.js @@ -18,9 +18,9 @@ class HandleStoreRequestCommand extends HandleProtocolMessageCommand { this.tripleStoreService = ctx.tripleStoreService; this.ualService = ctx.ualService; this.pendingStorageService = ctx.pendingStorageService; - this.blsService = ctx.blsService; this.operationIdService = ctx.operationIdService; this.pendingStorageService = ctx.pendingStorageService; + this.signatureService = ctx.signatureService; this.errorType = ERROR_TYPE.PUBLISH.PUBLISH_LOCAL_STORE_REMOTE_ERROR; this.operationStartEvent = OPERATION_ID_STATUS.PUBLISH.PUBLISH_LOCAL_STORE_REMOTE_START; @@ -106,7 +106,10 @@ class HandleStoreRequestCommand extends HandleProtocolMessageCommand { operationId, blockchain, ); - const signature = await this.blsService.sign(datasetRoot); + const { signer, v, r, s, vs } = await this.signatureService.signMessage( + blockchain, + datasetRoot, + ); await this.operationIdService.emitChangeEvent( OPERATION_ID_STATUS.PUBLISH.PUBLISH_LOCAL_STORE_REMOTE_SIGN_END, operationId, @@ -115,7 +118,7 @@ class HandleStoreRequestCommand extends HandleProtocolMessageCommand { return { messageType: NETWORK_MESSAGE_TYPES.RESPONSES.ACK, - messageData: { identityId, signature }, + messageData: { identityId, signer, v, r, s, vs }, }; } diff --git a/src/commands/protocols/publish/sender/publish-find-shard-command.js b/src/commands/protocols/publish/sender/publish-find-shard-command.js index 35ce3673aa..7843c4456b 100644 --- a/src/commands/protocols/publish/sender/publish-find-shard-command.js +++ b/src/commands/protocols/publish/sender/publish-find-shard-command.js @@ -23,8 +23,9 @@ class PublishFindShardCommand extends FindShardCommand { sequence.push('publishValidateAssetCommand'); if (nodePartOfShard) { sequence.push('localStoreCommand'); + } else { + sequence.push('networkPublishCommand'); } - sequence.push('networkPublishCommand'); return sequence; } diff --git a/src/commands/protocols/publish/sender/v1.0.0/v1-0-0-publish-request-command.js b/src/commands/protocols/publish/sender/v1.0.0/v1-0-0-publish-request-command.js index a4809e3276..d846c82e8f 100644 --- a/src/commands/protocols/publish/sender/v1.0.0/v1-0-0-publish-request-command.js +++ b/src/commands/protocols/publish/sender/v1.0.0/v1-0-0-publish-request-command.js @@ -9,7 +9,7 @@ class PublishRequestCommand extends ProtocolRequestCommand { constructor(ctx) { super(ctx); this.operationService = ctx.publishService; - this.signatureStorageService = ctx.signatureStorageService; + this.signatureService = ctx.signatureService; this.operationIdService = ctx.operationIdService; this.errorType = ERROR_TYPE.PUBLISH.PUBLISH_STORE_REQUEST_ERROR; @@ -59,10 +59,14 @@ class PublishRequestCommand extends ProtocolRequestCommand { operationId, blockchain, ); - await this.signatureStorageService.addSignatureToStorage( + await this.signatureService.addSignatureToStorage( operationId, responseData.identityId, - responseData.signature, + responseData.signer, + responseData.v, + responseData.r, + responseData.s, + responseData.vs, ); await this.operationIdService.emitChangeEvent( OPERATION_ID_STATUS.PUBLISH.PUBLISH_ADD_SIGNATURE_TO_STORAGE_END, diff --git a/src/commands/protocols/update/receiver/v1.0.0/v1-0-0-handle-update-request-command.js b/src/commands/protocols/update/receiver/v1.0.0/v1-0-0-handle-update-request-command.js index a59c65ad5b..eb66e5a133 100644 --- a/src/commands/protocols/update/receiver/v1.0.0/v1-0-0-handle-update-request-command.js +++ b/src/commands/protocols/update/receiver/v1.0.0/v1-0-0-handle-update-request-command.js @@ -12,9 +12,9 @@ class HandleUpdateRequestCommand extends HandleProtocolMessageCommand { this.operationService = ctx.updateService; this.blockchainModuleManager = ctx.blockchainModuleManager; this.pendingStorageService = ctx.pendingStorageService; - this.blsService = ctx.blsService; this.operationIdService = ctx.operationIdService; this.pendingStorageService = ctx.pendingStorageService; + this.signatureService = ctx.signatureService; this.errorType = ERROR_TYPE.UPDATE.UPDATE_LOCAL_STORE_REMOTE_ERROR; this.operationStartEvent = OPERATION_ID_STATUS.UPDATE.UPDATE_LOCAL_STORE_REMOTE_START; @@ -100,7 +100,10 @@ class HandleUpdateRequestCommand extends HandleProtocolMessageCommand { operationId, blockchain, ); - const signature = await this.blsService.sign(datasetRoot); + const { signer, v, r, s, vs } = await this.signatureService.signMessage( + blockchain, + datasetRoot, + ); this.operationIdService.emitChangeEvent( OPERATION_ID_STATUS.UPDATE.UPDATE_LOCAL_STORE_REMOTE_SIGN_END, operationId, @@ -109,7 +112,7 @@ class HandleUpdateRequestCommand extends HandleProtocolMessageCommand { return { messageType: NETWORK_MESSAGE_TYPES.RESPONSES.ACK, - messageData: { identityId, signature }, + messageData: { identityId, signer, v, r, s, vs }, }; } diff --git a/src/commands/protocols/update/sender/v1.0.0/v1-0-0-update-request-command.js b/src/commands/protocols/update/sender/v1.0.0/v1-0-0-update-request-command.js index 5891f1f640..2c800a645f 100644 --- a/src/commands/protocols/update/sender/v1.0.0/v1-0-0-update-request-command.js +++ b/src/commands/protocols/update/sender/v1.0.0/v1-0-0-update-request-command.js @@ -9,7 +9,7 @@ class PublishRequestCommand extends ProtocolRequestCommand { constructor(ctx) { super(ctx); this.operationService = ctx.updateService; - this.signatureStorageService = ctx.signatureStorageService; + this.signatureService = ctx.signatureService; this.operationIdService = ctx.operationIdService; this.errorType = ERROR_TYPE.UPDATE.UPDATE_STORE_REQUEST_ERROR; @@ -58,10 +58,14 @@ class PublishRequestCommand extends ProtocolRequestCommand { operationId, blockchain, ); - await this.signatureStorageService.addSignatureToStorage( + await this.signatureService.addSignatureToStorage( operationId, responseData.identityId, - responseData.signature, + responseData.signer, + responseData.v, + responseData.r, + responseData.s, + responseData.vs, ); await this.operationIdService.emitChangeEvent( OPERATION_ID_STATUS.UPDATE.UPDATE_ADD_SIGNATURE_TO_STORAGE_END, diff --git a/src/modules/blockchain/blockchain-module-manager.js b/src/modules/blockchain/blockchain-module-manager.js index 3454c4302f..7a644fe80e 100644 --- a/src/modules/blockchain/blockchain-module-manager.js +++ b/src/modules/blockchain/blockchain-module-manager.js @@ -5,6 +5,16 @@ class BlockchainModuleManager extends BaseModuleManager { return 'blockchain'; } + callImplementationFunction(blockchain, functionName, args = []) { + if (blockchain) { + if (this.getImplementation(blockchain)) { + return this.getImplementation(blockchain).module[functionName](...args); + } + } else { + return this.getImplementation().module[functionName](...args); + } + } + initializeTransactionQueues(blockchain, concurrency) { return this.callImplementationFunction(blockchain, 'getTotalTransactionQueueLength', [ concurrency, @@ -199,6 +209,18 @@ class BlockchainModuleManager extends BaseModuleManager { return this.callImplementationFunction(blockchain, 'convertFromWei', [value, toUnit]); } + hashMessage(blockchain, message) { + return this.callImplementationFunction(blockchain, 'hashMessage', [message]); + } + + async signMessage(blockchain, messageHash) { + return this.callImplementationFunction(blockchain, 'signMessage', [messageHash]); + } + + splitSignature(blockchain, flatSignature) { + return this.callImplementationFunction(blockchain, 'splitSignature', [flatSignature]); + } + async isCommitWindowOpen(blockchain, agreementId, epoch, stateIndex) { return this.callImplementationFunction(blockchain, 'isCommitWindowOpen', [ agreementId, @@ -413,16 +435,6 @@ class BlockchainModuleManager extends BaseModuleManager { return this.callImplementationFunction(blockchain, 'getLog2PLDSFParams'); } - callImplementationFunction(blockchain, functionName, args = []) { - if (blockchain) { - if (this.getImplementation(blockchain)) { - return this.getImplementation(blockchain).module[functionName](...args); - } - } else { - return this.getImplementation().module[functionName](...args); - } - } - async getLatestBlock(blockchain) { return this.callImplementationFunction(blockchain, 'getLatestBlock'); } diff --git a/src/modules/blockchain/implementation/web3-service.js b/src/modules/blockchain/implementation/web3-service.js index 76acbb1b90..e367481496 100644 --- a/src/modules/blockchain/implementation/web3-service.js +++ b/src/modules/blockchain/implementation/web3-service.js @@ -132,6 +132,11 @@ class Web3Service { return wallets; } + getRandomOperationalWallet() { + const randomIndex = Math.floor(Math.random() * this.operationalWallets.length); + return this.operationalWallets[randomIndex]; + } + async initializeWeb3() { const providers = []; for (const rpcEndpoint of this.config.rpcEndpoints) { @@ -1326,6 +1331,20 @@ class Web3Service { return ethers.utils.formatUnits(value, toUnit); } + hashMessage(message) { + return ethers.utils.hashMessage(message); + } + + async signMessage(messageHash) { + const wallet = this.getRandomOperationalWallet(); + const signature = await wallet.signMessage(ethers.utils.arrayify(messageHash)); + return { signer: wallet.address, signature }; + } + + splitSignature(flatSignature) { + return ethers.utils.splitSignature(flatSignature); + } + async healthCheck() { try { const gasPrice = await this.operationalWallets[0].getGasPrice(); diff --git a/src/service/bls-service.js b/src/service/bls-service.js deleted file mode 100644 index 8bc32812a0..0000000000 --- a/src/service/bls-service.js +++ /dev/null @@ -1,101 +0,0 @@ -import os from 'os'; -import { chmod } from 'fs'; -import { spawn } from 'child_process'; -import { BLS_KEY_FILENAME, NODE_ENVIRONMENTS } from '../constants/constants.js'; - -class BLSService { - constructor(ctx) { - this.config = ctx.config; - this.logger = ctx.logger; - - this.fileService = ctx.fileService; - this.binaryPath = this.fileService.getBinaryPath('bls-tools'); - } - - async initialize() { - if (os.platform() !== 'win32') { - chmod(this.binaryPath, '755', (err) => { - if (err) { - throw err; - } - this.logger.debug( - `Permissions for binary ${this.binaryPath} have been set to 755.`, - ); - }); - } - - if (!this.config.blsPublicKey) { - const devEnvironment = - process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT || - process.env.NODE_ENV === NODE_ENVIRONMENTS.TEST; - - if (!devEnvironment || !this.config.blsSecretKey) { - this.config.blsSecretKey = await this.readSecretKeyFromFile(); - } - - if (!this.config.blsSecretKey) { - const keys = await this.generateKeys(); - this.config.blsPublicKey = keys.publicKey; - this.config.blsSecretKey = keys.secretKey; - await this.fileService.writeContentsToFile( - this.fileService.getBLSSecretKeyFolderPath(), - BLS_KEY_FILENAME, - this.config.blsSecretKey, - false, - ); - } else { - this.config.blsPublicKey = await this.publicKeyFromSecret(); - } - } - - this.logger.info(`BLS Public Key is ${this.config.blsPublicKey}`); - } - - async readSecretKeyFromFile() { - const secretKeyPath = this.fileService.getBLSSecretKeyPath(); - const secretKeyFileExists = await this.fileService.pathExists(secretKeyPath); - if (secretKeyFileExists) { - return this.fileService.readFile(secretKeyPath); - } - } - - async runBLSBinary(args, jsonOutput = false) { - return new Promise((resolve) => { - const process = spawn(this.binaryPath, args); - let result = ''; - process.stdout.on('data', (data) => { - result += data.toString(); - }); - process.stderr.on('data', (data) => { - this.logger.error(`Error: ${data}`); - }); - process.on('close', () => { - if (jsonOutput) { - resolve(JSON.parse(result)); - } else { - resolve(result.trim()); - } - }); - }); - } - - async generateKeys() { - return this.runBLSBinary(['generate-keys'], true); - } - - async publicKeyFromSecret() { - return this.runBLSBinary(['public-key-from-secret', '--secret', this.config.blsSecretKey]); - } - - async sign(message) { - return this.runBLSBinary([ - 'sign', - '--secret', - this.config.blsSecretKey, - '--message', - message, - ]); - } -} - -export default BLSService; diff --git a/src/service/file-service.js b/src/service/file-service.js index ebe8a74ec2..05f320ed28 100644 --- a/src/service/file-service.js +++ b/src/service/file-service.js @@ -1,5 +1,16 @@ +import os from 'os'; import path from 'path'; -import { mkdir, writeFile, readFile, unlink, stat, readdir, rm, appendFile } from 'fs/promises'; +import { + mkdir, + writeFile, + readFile, + unlink, + stat, + readdir, + rm, + appendFile, + chmod, +} from 'fs/promises'; import appRootPath from 'app-root-path'; import { BLS_KEY_DIRECTORY, BLS_KEY_FILENAME, NODE_ENVIRONMENTS } from '../constants/constants.js'; @@ -127,6 +138,18 @@ class FileService { return path.join(this.getBinariesFolderPath(), process.platform, process.arch, binaryName); } + async makeBinaryExecutable(binary) { + const binaryPath = this.getBinaryPath(binary); + if (os.platform() !== 'win32') { + await chmod(binaryPath, '755', (err) => { + if (err) { + throw err; + } + this.logger.debug(`Permissions for binary ${binaryPath} have been set to 755.`); + }); + } + } + getBLSSecretKeyFolderPath() { return path.join(this.getDataFolderPath(), BLS_KEY_DIRECTORY); } diff --git a/src/service/operation-service.js b/src/service/operation-service.js index 27f1a91e8d..e35723e068 100644 --- a/src/service/operation-service.js +++ b/src/service/operation-service.js @@ -68,6 +68,7 @@ class OperationService { ); if (responseData != null) { + await this.operationIdService.cacheOperationIdDataToMemory(operationId, responseData); await this.operationIdService.cacheOperationIdDataToFile(operationId, responseData); } diff --git a/src/service/publish-service.js b/src/service/publish-service.js index 59528b3f54..c64f0ed9ff 100644 --- a/src/service/publish-service.js +++ b/src/service/publish-service.js @@ -23,16 +23,10 @@ class PublishService extends OperationService { OPERATION_ID_STATUS.COMPLETED, ]; this.operationMutex = new Mutex(); - this.signatureStorageService = ctx.signatureStorageService; + this.signatureService = ctx.signatureService; } - async processResponse( - command, - responseStatus, - responseData, - errorMessage = null, - localStore = false, - ) { + async processResponse(command, responseStatus, responseData, errorMessage = null) { const { operationId, blockchain, @@ -49,10 +43,6 @@ class PublishService extends OperationService { operationId, ); - if (localStore) { - return; - } - const { completedNumber, failedNumber } = datasetRootStatus[operationId]; const totalResponses = completedNumber + failedNumber; @@ -79,9 +69,7 @@ class PublishService extends OperationService { responseStatus === OPERATION_REQUEST_STATUS.COMPLETED && completedNumber === minAckResponses ) { - const signatures = await this.signatureStorageService.getSignaturesFromStorage( - operationId, - ); + const signatures = await this.signatureService.getSignaturesFromStorage(operationId); await this.markOperationAsCompleted( operationId, blockchain, diff --git a/src/service/signature-storage-service.js b/src/service/signature-service.js similarity index 52% rename from src/service/signature-storage-service.js rename to src/service/signature-service.js index 6fa6c63865..a5a0a62c93 100644 --- a/src/service/signature-storage-service.js +++ b/src/service/signature-service.js @@ -1,16 +1,30 @@ -class SignatureStorageService { +class SignatureService { constructor(ctx) { this.config = ctx.config; this.logger = ctx.logger; + this.blockchainModuleManager = ctx.blockchainModuleManager; this.fileService = ctx.fileService; } - async addSignatureToStorage(operationId, identityId, signature) { + async signMessage(blockchain, message) { + const messageHash = this.blockchainModuleManager.hashMessage(blockchain, message); + const { signer, signature: flatSignature } = await this.blockchainModuleManager.signMessage( + blockchain, + messageHash, + ); + const { v, r, s, _vs } = this.blockchainModuleManager.splitSignature( + blockchain, + flatSignature, + ); + return { signer, v, r, s, vs: _vs }; + } + + async addSignatureToStorage(operationId, identityId, signer, v, r, s, vs) { await this.fileService.appendContentsToFile( this.fileService.getSignatureStorageCachePath(), operationId, - `${JSON.stringify({ identityId, signature })}\n`, + `${JSON.stringify({ identityId, signer, v, r, s, vs })}\n`, ); } @@ -30,4 +44,4 @@ class SignatureStorageService { } } -export default SignatureStorageService; +export default SignatureService;