Skip to content

Commit

Permalink
fix: get key from peer id if not specified in the message (#129)
Browse files Browse the repository at this point in the history
Remove the `@ts-expect-error` and use the public key of the peer id
if it's not specified in the message.
  • Loading branch information
achingbrain authored Feb 22, 2023
1 parent 9839b71 commit c183c70
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,23 @@ export const toMessage = async (message: PubSubRPCMessage): Promise<Message> =>
}
}

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 => {
Expand Down
9 changes: 8 additions & 1 deletion test/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c183c70

Please sign in to comment.