Skip to content

Commit

Permalink
Signatures rework
Browse files Browse the repository at this point in the history
  • Loading branch information
u-hubar committed Dec 11, 2024
1 parent 7d64076 commit e27a954
Show file tree
Hide file tree
Showing 26 changed files with 179 additions and 165 deletions.
Empty file added bin/darwin/arm64/.gitkeep
Empty file.
Binary file removed bin/darwin/arm64/bls-tools
Binary file not shown.
Empty file added bin/darwin/x64/.gitkeep
Empty file.
Binary file removed bin/darwin/x64/bls-tools
Binary file not shown.
Empty file added bin/linux/arm64/.gitkeep
Empty file.
Binary file removed bin/linux/arm64/bls-tools
Binary file not shown.
Empty file added bin/linux/x64/.gitkeep
Empty file.
Binary file removed bin/linux/x64/bls-tools
Binary file not shown.
Empty file added bin/win32/x64/.gitkeep
Empty file.
Binary file removed bin/win32/x64/bls-tools.exe
Binary file not shown.
13 changes: 0 additions & 13 deletions ot-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
}
Expand Down Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
19 changes: 12 additions & 7 deletions src/commands/local-store/local-store-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -63,23 +62,29 @@ 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(
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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 },
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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 },
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
Expand Down
32 changes: 22 additions & 10 deletions src/modules/blockchain/blockchain-module-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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');
}
Expand Down
19 changes: 19 additions & 0 deletions src/modules/blockchain/implementation/web3-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
Loading

0 comments on commit e27a954

Please sign in to comment.