Skip to content

Commit c183c70

Browse files
authored
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.
1 parent 9839b71 commit c183c70

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/utils.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,23 @@ export const toMessage = async (message: PubSubRPCMessage): Promise<Message> =>
9797
}
9898
}
9999

100-
return {
100+
const from = peerIdFromBytes(message.from)
101+
102+
const msg: Message = {
101103
type: 'signed',
102104
from: peerIdFromBytes(message.from),
103105
topic: message.topic ?? '',
104106
sequenceNumber: bigIntFromBytes(message.sequenceNumber ?? new Uint8Array(0)),
105107
data: message.data ?? new Uint8Array(0),
106108
signature: message.signature ?? new Uint8Array(0),
107-
// @ts-expect-error key need not be defined
108-
key: message.key
109+
key: message.key ?? from.publicKey ?? new Uint8Array(0)
109110
}
111+
112+
if (msg.key.length === 0) {
113+
throw new CodeError('Signed RPC message was missing key', codes.ERR_MISSING_KEY)
114+
}
115+
116+
return msg
110117
}
111118

112119
export const toRpcMessage = (message: Message): PubSubRPCMessage => {

test/utils.spec.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,17 @@ describe('utils', () => {
121121
sequenceNumber: utils.bigIntToBytes(1n),
122122
signature: new Uint8Array(0),
123123
key: dummyPeerID.publicKey
124+
},
125+
{
126+
from: (await PeerIdFactory.createEd25519PeerId()).toBytes(),
127+
topic: 'test',
128+
data: new Uint8Array(0),
129+
sequenceNumber: utils.bigIntToBytes(1n),
130+
signature: new Uint8Array(0)
124131
}
125132
]
126-
const expected = ['signed', 'unsigned', 'signed']
127133

134+
const expected = ['signed', 'unsigned', 'signed', 'signed']
128135
const actual = (await Promise.all(cases.map(utils.toMessage))).map(m => m.type)
129136

130137
expect(actual).to.deep.equal(expected)

0 commit comments

Comments
 (0)