From a6a4c97a3492db28e5e1d078e6858c6fbbda6415 Mon Sep 17 00:00:00 2001 From: Wadym Date: Thu, 11 Jan 2024 10:48:23 +0100 Subject: [PATCH] extended catch error flow (#62) * extended catch error flow * version up * update build * clean not used code * remove error from json * Version up * minor example update --- dist/tsc/package.json | 2 +- dist/tsc/src/lib/KeyShares/KeyShares.js.map | 2 +- dist/tsc/src/lib/KeyShares/KeySharesItem.d.ts | 2 ++ dist/tsc/src/lib/KeyShares/KeySharesItem.js | 27 ++++++++++++------- .../src/lib/KeyShares/KeySharesItem.js.map | 2 +- examples/console/example-complex.js | 17 +++++++++--- examples/console/example-ts.ts | 27 ++++++++++--------- package.json | 2 +- src/lib/KeyShares/KeyShares.ts | 1 - src/lib/KeyShares/KeySharesItem.ts | 26 +++++++++++------- .../KeyShares/__test__/KeyShares.json.test.ts | 12 +++++---- 11 files changed, 75 insertions(+), 45 deletions(-) diff --git a/dist/tsc/package.json b/dist/tsc/package.json index 8a3df02f..9b5d1bfc 100644 --- a/dist/tsc/package.json +++ b/dist/tsc/package.json @@ -1,6 +1,6 @@ { "name": "ssv-keys", - "version": "1.0.6", + "version": "1.0.9", "description": "Tool for splitting a validator key into a predefined threshold of shares via Shamir-Secret-Sharing (SSS), and encrypt them with a set of operator keys.", "author": "SSV.Network", "repository": "https://github.com/bloxapp/ssv-keys", diff --git a/dist/tsc/src/lib/KeyShares/KeyShares.js.map b/dist/tsc/src/lib/KeyShares/KeyShares.js.map index 8c0d10bf..00d37f84 100644 --- a/dist/tsc/src/lib/KeyShares/KeyShares.js.map +++ b/dist/tsc/src/lib/KeyShares/KeyShares.js.map @@ -1 +1 @@ -{"version":3,"file":"KeyShares.js","sourceRoot":"","sources":["../../../../../src/lib/KeyShares/KeyShares.ts"],"names":[],"mappings":";;;;AAAA,4DAA4B;AAC5B,iFAAwC;AAExC,qDAA2E;AAC3E,mDAAgD;AAChD,oDAA6D;AAE7D;;;GAGG;AACH,MAAa,SAAS;IAKpB,YAAY,SAA0B,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AK,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE;gBAC9B,iCAAiC;gBACjC,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,MAAM,EAAE;oBAC9B,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,6BAAa,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;iBAC1D;aACF;iBAAM;gBACL,kCAAkC;gBAClC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,6BAAa,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;aAC1D;YACD,OAAO,QAAQ,CAAC;QAClB,CAAC;KAAA;CACF;AAvEC;IAFC,IAAA,4BAAU,GAAE;IACZ,IAAA,gCAAc,EAAC,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;yCACC;AAHlC,8BA0EC"} \ No newline at end of file diff --git a/dist/tsc/src/lib/KeyShares/KeySharesItem.d.ts b/dist/tsc/src/lib/KeyShares/KeySharesItem.d.ts index df140587..229167c9 100644 --- a/dist/tsc/src/lib/KeyShares/KeySharesItem.d.ts +++ b/dist/tsc/src/lib/KeyShares/KeySharesItem.d.ts @@ -3,6 +3,7 @@ import { KeySharesPayload } from './KeySharesData/KeySharesPayload'; import { EncryptShare } from '../Encryption/Encryption'; import { IKeySharesPartitialData } from './KeySharesData/IKeySharesData'; import { IOperator } from './KeySharesData/IOperator'; +import { SSVKeysException } from '../../lib/exceptions/base'; export interface IKeySharesPayloadData { publicKey: string; operators: IOperator[]; @@ -24,6 +25,7 @@ export interface IKeySharesFromSignatureData { export declare class KeySharesItem { data: KeySharesData; payload: KeySharesPayload; + error: SSVKeysException | null; constructor(); /** * Build payload from operators list, encrypted shares and validator public key diff --git a/dist/tsc/src/lib/KeyShares/KeySharesItem.js b/dist/tsc/src/lib/KeyShares/KeySharesItem.js index 4c65c766..52470fd1 100644 --- a/dist/tsc/src/lib/KeyShares/KeySharesItem.js +++ b/dist/tsc/src/lib/KeyShares/KeySharesItem.js @@ -17,6 +17,7 @@ const PUBLIC_KEY_LENGHT = 96; */ class KeySharesItem { constructor() { + this.error = null; this.data = new KeySharesData_1.KeySharesData(); this.payload = new KeySharesPayload_1.KeySharesPayload(); } @@ -132,15 +133,20 @@ class KeySharesItem { return tslib_1.__awaiter(this, void 0, void 0, function* () { const body = typeof content === 'string' ? JSON.parse(content) : content; const instance = new KeySharesItem(); - instance.data.update(body.data); - instance.payload.update(body.payload); - instance.validate(); - // Custom validation: verify signature - yield instance.validateSingleShares(instance.payload.sharesData, { - ownerAddress: instance.data.ownerAddress, - ownerNonce: instance.data.ownerNonce, - publicKey: instance.data.publicKey, - }); + try { + instance.data.update(body.data); + instance.payload.update(body.payload); + instance.validate(); + // Custom validation: verify signature + yield instance.validateSingleShares(instance.payload.sharesData, { + ownerAddress: instance.data.ownerAddress, + ownerNonce: instance.data.ownerNonce, + publicKey: instance.data.publicKey, + }); + } + catch (e) { + instance.error = e; + } return instance; }); } @@ -153,5 +159,8 @@ tslib_1.__decorate([ (0, class_validator_1.IsOptional)(), (0, class_validator_1.ValidateNested)() ], KeySharesItem.prototype, "payload", void 0); +tslib_1.__decorate([ + (0, class_validator_1.IsOptional)() +], KeySharesItem.prototype, "error", void 0); exports.KeySharesItem = KeySharesItem; //# sourceMappingURL=KeySharesItem.js.map \ No newline at end of file diff --git a/dist/tsc/src/lib/KeyShares/KeySharesItem.js.map b/dist/tsc/src/lib/KeyShares/KeySharesItem.js.map index 6170afcd..10eec16a 100644 --- a/dist/tsc/src/lib/KeyShares/KeySharesItem.js.map +++ b/dist/tsc/src/lib/KeyShares/KeySharesItem.js.map @@ -1 +1 @@ -{"version":3,"file":"KeySharesItem.js","sourceRoot":"","sources":["../../../../../src/lib/KeyShares/KeySharesItem.ts"],"names":[],"mappings":";;;;AAAA,uDAAiC;AACjC,2EAAqD;AACrD,qDAIyB;AAEzB,iEAA8D;AAC9D,uEAAoE;AAIpE,gEAAgE;AAChE,qDAAwF;AACxF,oDAA6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,MAAM,QAAQ,CAAC,oBAAoB,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,EAAE;gBAC/D,YAAY,EAAE,QAAQ,CAAC,IAAI,CAAC,YAAsB;gBAClD,UAAU,EAAE,QAAQ,CAAC,IAAI,CAAC,UAAoB;gBAC9C,SAAS,EAAE,QAAQ,CAAC,IAAI,CAAC,SAAmB;aAC7C,CAAC,CAAC;YAEH,OAAO,QAAQ,CAAC;QAClB,CAAC;KAAA;CACF;AAhKC;IAFC,IAAA,4BAAU,GAAE;IACZ,IAAA,gCAAc,GAAE;2CACU;AAI3B;IAFC,IAAA,4BAAU,GAAE;IACZ,IAAA,gCAAc,GAAE;8CACgB;AAPnC,sCAmKC"} \ No newline at end of file +{"version":3,"file":"KeySharesItem.js","sourceRoot":"","sources":["../../../../../src/lib/KeyShares/KeySharesItem.ts"],"names":[],"mappings":";;;;AAAA,uDAAiC;AACjC,2EAAqD;AACrD,qDAIyB;AAEzB,iEAA8D;AAC9D,uEAAoE;AAIpE,gEAAgE;AAChE,qDAAwF;AACxF,oDAA6D;AAoB7D,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAC7B,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B;;GAEG;AACH,MAAa,aAAa;IAYxB;QAFO,UAAK,GAA4B,IAAI,CAAC;QAG3C,IAAI,CAAC,IAAI,GAAG,IAAI,6BAAa,EAAE,CAAC;QAChC,IAAI,CAAC,OAAO,GAAG,IAAI,mCAAgB,EAAE,CAAC;IACxC,CAAC;IAED;;;;;OAKG;IACG,YAAY,CAAC,QAA+B,EAAE,eAA0C;;YAC5F,MAAM,EACJ,YAAY,EACZ,UAAU,EACV,UAAU,GACX,GAAG,eAAe,CAAC;YAEpB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,UAAU,GAAG,CAAC,EAAE;gBACnD,MAAM,IAAI,gCAAqB,CAAC,UAAU,EAAE,qCAAqC,CAAC,CAAC;aACpF;YAED,IAAI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AAA,4BAAU,GAAE;4CACgC;AAV/C,sCAyKC"} \ No newline at end of file diff --git a/examples/console/example-complex.js b/examples/console/example-complex.js index 279bf449..dadd4d11 100644 --- a/examples/console/example-complex.js +++ b/examples/console/example-complex.js @@ -1,6 +1,6 @@ const path = require('path'); const fsp = require('fs').promises; -const { SSVKeys, KeyShares, KeySharesItem } = require('ssv-keys'); +const { SSVKeys, KeyShares, KeySharesItem, SSVKeysException } = require('ssv-keys'); const operatorKeys = require('./operators.json'); const keystore = require('./test.keystore.json'); @@ -66,8 +66,19 @@ async function main() { const jsonKeyShares = await fsp.readFile(getKeySharesFilePath(4), { encoding: 'utf-8' }); const keyShares2 = await KeyShares.fromJson(jsonKeyShares); - - console.log('KeyShares list', keyShares2.list().map(item => item.toJson())); + for (const keySharesItem of keyShares2.list()) { + if (keySharesItem.error) { + if (keySharesItem.error instanceof SSVKeysException) { + console.log('SSVKeys Error name:', keySharesItem.error.name); + console.log('SSVKeys Error message:', keySharesItem.error.message); + console.log('SSVKeys Error trace:', keySharesItem.error.trace); + } else { + // Handle other types of errors + console.log('Other Error caught:', keySharesItem.error); + } + } + console.log(keySharesItem.toJson()); + } } void main(); diff --git a/examples/console/example-ts.ts b/examples/console/example-ts.ts index 6d6c9cd8..8641fd8c 100644 --- a/examples/console/example-ts.ts +++ b/examples/console/example-ts.ts @@ -63,20 +63,21 @@ async function main() { await fsp.writeFile(getKeySharesFilePath(4), keyShares.toJson(), { encoding: 'utf-8' }); // example to work with keyshares from file - const jsonKeyShares = await fsp.readFile(getKeySharesFilePath(4), { encoding: 'utf-8' }); - - try { - const keyShares2 = await KeyShares.fromJson(jsonKeyShares); - console.log('Created keyShares list from json', keyShares2.list().map(item => item.toJson())); - } catch (e: any) { - if (e instanceof SSVKeysException) { - console.log('SSVKeys Error name:', e.name); - console.log('SSVKeys Error message:', e.message); - console.log('SSVKeys Error trace:', e.trace); - } else { - // Handle other types of errors - console.log('Other Error caught:', e.message); + const jsonKeyShares = await fsp.readFile('./data/test-error.json', { encoding: 'utf-8' }); + + const keyShares2 = await KeyShares.fromJson(jsonKeyShares); + for (const keySharesItem of keyShares2.list()) { + if (keySharesItem.error) { + if (keySharesItem.error instanceof SSVKeysException) { + console.log('SSVKeys Error name:', keySharesItem.error.name); + console.log('SSVKeys Error message:', keySharesItem.error.message); + console.log('SSVKeys Error trace:', keySharesItem.error.trace); + } else { + // Handle other types of errors + console.log('Other Error caught:', keySharesItem.error); + } } + console.log(keySharesItem.toJson()); } } diff --git a/package.json b/package.json index 789ef512..5a480973 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ssv-keys", - "version": "1.0.7", + "version": "1.0.9", "description": "Tool for splitting a validator key into a predefined threshold of shares via Shamir-Secret-Sharing (SSS), and encrypt them with a set of operator keys.", "author": "SSV.Network", "repository": "https://github.com/bloxapp/ssv-keys", diff --git a/src/lib/KeyShares/KeyShares.ts b/src/lib/KeyShares/KeyShares.ts index ace8815f..24b15b81 100644 --- a/src/lib/KeyShares/KeyShares.ts +++ b/src/lib/KeyShares/KeyShares.ts @@ -81,7 +81,6 @@ export class KeyShares { // Handle old format (single item) instance.shares.push(await KeySharesItem.fromJson(body)); } - return instance; } } diff --git a/src/lib/KeyShares/KeySharesItem.ts b/src/lib/KeyShares/KeySharesItem.ts index 5feec029..2805fe43 100644 --- a/src/lib/KeyShares/KeySharesItem.ts +++ b/src/lib/KeyShares/KeySharesItem.ts @@ -48,6 +48,9 @@ export class KeySharesItem { @ValidateNested() public payload: KeySharesPayload; + @IsOptional() + public error: SSVKeysException | null = null; + constructor() { this.data = new KeySharesData(); this.payload = new KeySharesPayload(); @@ -190,16 +193,19 @@ export class KeySharesItem { static async fromJson(content: string | any): Promise { const body = typeof content === 'string' ? JSON.parse(content) : content; const instance = new KeySharesItem(); - instance.data.update(body.data); - instance.payload.update(body.payload); - instance.validate(); - // Custom validation: verify signature - await instance.validateSingleShares(instance.payload.sharesData, { - ownerAddress: instance.data.ownerAddress as string, - ownerNonce: instance.data.ownerNonce as number, - publicKey: instance.data.publicKey as string, - }); - + try { + instance.data.update(body.data); + instance.payload.update(body.payload); + instance.validate(); + // Custom validation: verify signature + await instance.validateSingleShares(instance.payload.sharesData, { + ownerAddress: instance.data.ownerAddress as string, + ownerNonce: instance.data.ownerNonce as number, + publicKey: instance.data.publicKey as string, + }); + } catch (e: any) { + instance.error = e; + } return instance; } } diff --git a/src/lib/KeyShares/__test__/KeyShares.json.test.ts b/src/lib/KeyShares/__test__/KeyShares.json.test.ts index 6da35503..3fab7676 100644 --- a/src/lib/KeyShares/__test__/KeyShares.json.test.ts +++ b/src/lib/KeyShares/__test__/KeyShares.json.test.ts @@ -37,9 +37,9 @@ describe('KeyShares.fromJson/toJson', () => { }); it('Should throw an error for invalid operator data', async () => { - expect(async() => await KeySharesItem.fromJson(mockKeySharesItemWithMissedOperatorKey)).rejects.toThrow(OperatorsCountsMismatchError); - expect(async() => await KeySharesItem.fromJson(mockKeySharesItemWithWrongOperatorId)).rejects.toThrow(OperatorsCountsMismatchError); - expect(async() => await KeySharesItem.fromJson(mockKeySharesItemWithDuplicatedOperatorKey)).rejects.toThrow(DuplicatedOperatorPublicKeyError); + expect((await KeySharesItem.fromJson(mockKeySharesItemWithMissedOperatorKey)).error).toBeInstanceOf(OperatorsCountsMismatchError); + expect((await KeySharesItem.fromJson(mockKeySharesItemWithWrongOperatorId)).error).toBeInstanceOf(OperatorsCountsMismatchError); + expect((await KeySharesItem.fromJson(mockKeySharesItemWithDuplicatedOperatorKey)).error).toBeInstanceOf(DuplicatedOperatorPublicKeyError); }); it('Should create KeyShares Item', async () => { @@ -79,7 +79,8 @@ describe('KeyShares.fromJson/toJson', () => { }); it('should throw error on invalid JSON format', async () => { - await expect(KeyShares.fromJson(mockKeySharesInvalidStruct)).rejects.toThrow(); + const keyShares = await KeyShares.fromJson(mockKeySharesInvalidStruct); + expect(keyShares.list()[0].error).not.toBeNull(); }); it('should throw error on version incompatibility', async () => { @@ -95,6 +96,7 @@ describe('KeyShares.fromJson/toJson', () => { ...mockKeyShares, shares: [{ "invalid": "data" }], }; - await expect(KeyShares.fromJson(invalidSharesJson)).rejects.toThrow(); + const keyShares = await KeyShares.fromJson(invalidSharesJson); + expect(keyShares.list()[0].error).not.toBeNull(); }); });