From c183c70f4d57af486e74fd2920eb1d0a878e6ab3 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Wed, 22 Feb 2023 17:16:26 +0000 Subject: [PATCH] fix: get key from peer id if not specified in the message (#129) Remove the `@ts-expect-error` and use the public key of the peer id if it's not specified in the message. --- src/utils.ts | 13 ++++++++++--- test/utils.spec.ts | 9 ++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index ed3abee..8ed2799 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -97,16 +97,23 @@ export const toMessage = async (message: PubSubRPCMessage): Promise => } } - return { + const from = peerIdFromBytes(message.from) + + const msg: Message = { type: 'signed', from: peerIdFromBytes(message.from), topic: message.topic ?? '', sequenceNumber: bigIntFromBytes(message.sequenceNumber ?? new Uint8Array(0)), data: message.data ?? new Uint8Array(0), signature: message.signature ?? new Uint8Array(0), - // @ts-expect-error key need not be defined - key: message.key + key: message.key ?? from.publicKey ?? new Uint8Array(0) } + + if (msg.key.length === 0) { + throw new CodeError('Signed RPC message was missing key', codes.ERR_MISSING_KEY) + } + + return msg } export const toRpcMessage = (message: Message): PubSubRPCMessage => { diff --git a/test/utils.spec.ts b/test/utils.spec.ts index 93b5bcd..6a969d2 100644 --- a/test/utils.spec.ts +++ b/test/utils.spec.ts @@ -121,10 +121,17 @@ describe('utils', () => { sequenceNumber: utils.bigIntToBytes(1n), signature: new Uint8Array(0), key: dummyPeerID.publicKey + }, + { + from: (await PeerIdFactory.createEd25519PeerId()).toBytes(), + topic: 'test', + data: new Uint8Array(0), + sequenceNumber: utils.bigIntToBytes(1n), + signature: new Uint8Array(0) } ] - const expected = ['signed', 'unsigned', 'signed'] + const expected = ['signed', 'unsigned', 'signed', 'signed'] const actual = (await Promise.all(cases.map(utils.toMessage))).map(m => m.type) expect(actual).to.deep.equal(expected)