Skip to content

Commit

Permalink
Merge branch '24-fix-get-index' into 'dev'
Browse files Browse the repository at this point in the history
fix the getIndex method to throw an error in case of inconsistencies between...

Closes #24

See merge request ergo/rosen-bridge/sign-protocols!44
  • Loading branch information
vorujack committed Jan 22, 2025
2 parents 74f98cc + 2cf996f commit a6ba191
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-cups-relate.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions packages/communication/lib/Communicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
87 changes: 78 additions & 9 deletions packages/communication/tests/Comunicator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EdDSA>;
let guardPks: Array<string>;
const payload = { foo: 'bar' };

beforeEach(async () => {
guardMessageEncs = [];
const guardPks: Array<string> = [];
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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/communication/tests/TestCommunicator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ export class TestCommunicator extends Communicator {

processMessage = vi.fn();

mockedGetIndex = async () => await this.getIndex();

mockedGetDate = () => this.getDate();
}

0 comments on commit a6ba191

Please sign in to comment.