diff --git a/.changeset/gentle-cups-relate.md b/.changeset/gentle-cups-relate.md new file mode 100644 index 0000000..534ae26 --- /dev/null +++ b/.changeset/gentle-cups-relate.md @@ -0,0 +1,5 @@ +--- +'@rosen-bridge/communication': patch +--- + +Fix the getIndex method to throw an error in case of inconsistencies between guardPks and the guard messageEnc. diff --git a/packages/communication/lib/Communicator.ts b/packages/communication/lib/Communicator.ts index 4805be1..205f7cc 100644 --- a/packages/communication/lib/Communicator.ts +++ b/packages/communication/lib/Communicator.ts @@ -42,6 +42,8 @@ export abstract class Communicator { if (this.index === -1) { const pk = await this.messageEnc.getPk(); this.index = this.guardPks.indexOf(pk); + if (this.index == -1) + throw Error(`Public key ${pk} not found among the listed guardPks`); } return this.index; }; diff --git a/packages/communication/tests/Comunicator.spec.ts b/packages/communication/tests/Comunicator.spec.ts index bd63693..59f3d93 100644 --- a/packages/communication/tests/Comunicator.spec.ts +++ b/packages/communication/tests/Comunicator.spec.ts @@ -3,28 +3,74 @@ import { EdDSA } from '@rosen-bridge/encryption'; import { describe, expect, it, vi, beforeEach } from 'vitest'; describe('Communicator', () => { - let communicator: TestCommunicator; - let mockSubmit = vi.fn(); let guardMessageEncs: Array; + let guardPks: Array; const payload = { foo: 'bar' }; beforeEach(async () => { guardMessageEncs = []; - const guardPks: Array = []; + guardPks = []; for (let index = 0; index < 10; index++) { const sk = new EdDSA(await EdDSA.randomKey()); guardMessageEncs.push(sk); guardPks.push(await sk.getPk()); } - mockSubmit = vi.fn(); - communicator = new TestCommunicator( - guardMessageEncs[1], - mockSubmit, - guardPks, - ); + }); + + describe('getIndex', () => { + const mockSubmit = vi.fn(); + + /** + * @target Communicator.getIndex should return exception when pk of guard doesn't exist between guardPks + * @dependencies + * @scenario + * - override current guard message encryption with wrong + * - create communicator + * - call getIndex + * @expected + * - must throw Error + */ + it("should return exception when pk of guard doesn't exist between guardPks", async () => { + guardMessageEncs[1] = new EdDSA(await EdDSA.randomKey()); + const communicator = new TestCommunicator( + guardMessageEncs[1], + mockSubmit, + guardPks, + ); + expect(communicator.mockedGetIndex()).rejects.toThrow(Error); + }); + + /** + * @target Communicator.getIndex should return correct index 1 + * @dependencies + * @scenario + * - create communicator and assign guardMessageEnc with index 1 as current guard + * - call getIndex + * @expected + * - should return correct index 1 + */ + it('should return correct index', async () => { + const communicator = new TestCommunicator( + guardMessageEncs[1], + mockSubmit, + guardPks, + ); + expect(communicator.mockedGetIndex()).resolves.toEqual(1); + }); }); describe('getDate', () => { + let communicator: TestCommunicator; + + beforeEach(async () => { + const mockSubmit = vi.fn(); + communicator = new TestCommunicator( + guardMessageEncs[1], + mockSubmit, + guardPks, + ); + }); + /** * @target Communicator.sendMessage should return current timestamp rounded to seconds * @dependencies @@ -43,6 +89,18 @@ describe('Communicator', () => { }); describe('sendMessage', () => { + let communicator: TestCommunicator; + let mockSubmit = vi.fn(); + + beforeEach(async () => { + mockSubmit = vi.fn(); + communicator = new TestCommunicator( + guardMessageEncs[1], + mockSubmit, + guardPks, + ); + }); + /** * @target Communicator.sendMessage should call submit message * @dependencies @@ -76,6 +134,17 @@ describe('Communicator', () => { }); describe('handleMessage', () => { + let communicator: TestCommunicator; + + beforeEach(async () => { + const mockSubmit = vi.fn(); + communicator = new TestCommunicator( + guardMessageEncs[1], + mockSubmit, + guardPks, + ); + }); + /** * @target Communicator.handleMessage should pass arguments to process message function when sign is valid * @dependencies diff --git a/packages/communication/tests/TestCommunicator.ts b/packages/communication/tests/TestCommunicator.ts index 86d7c0d..e82ebe8 100644 --- a/packages/communication/tests/TestCommunicator.ts +++ b/packages/communication/tests/TestCommunicator.ts @@ -22,5 +22,7 @@ export class TestCommunicator extends Communicator { processMessage = vi.fn(); + mockedGetIndex = async () => await this.getIndex(); + mockedGetDate = () => this.getDate(); }