From 0437bfa35365a8d9520e0f4b9604c9a70462c4b9 Mon Sep 17 00:00:00 2001 From: Cayman Date: Wed, 29 Jun 2022 12:38:43 -0500 Subject: [PATCH] feat!: pubsub Message types for signature policies BREAKING CHANGE: The `Message` type is now either a `StrictSignMessage` or a `StrictNoSignMessage` --- .../src/messages.ts | 10 ++++++---- .../src/two-nodes.ts | 13 +++++++++---- packages/interface-pubsub/src/index.ts | 17 +++++++++++++---- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/packages/interface-pubsub-compliance-tests/src/messages.ts b/packages/interface-pubsub-compliance-tests/src/messages.ts index d1da58173..477d97070 100644 --- a/packages/interface-pubsub-compliance-tests/src/messages.ts +++ b/packages/interface-pubsub-compliance-tests/src/messages.ts @@ -49,10 +49,12 @@ export default (common: TestSetup) => { const event = await eventPromise const message = event.detail - expect(message.from.toString()).to.equal(components.getPeerId().toString()) - expect(message.sequenceNumber).to.not.eql(undefined) - expect(message.key).to.not.eql(undefined) - expect(message.signature).to.not.eql(undefined) + if (message.type === 'signed') { + expect(message.from.toString()).to.equal(components.getPeerId().toString()) + expect(message.sequenceNumber).to.not.eql(undefined) + expect(message.key).to.not.eql(undefined) + expect(message.signature).to.not.eql(undefined) + } }) }) } diff --git a/packages/interface-pubsub-compliance-tests/src/two-nodes.ts b/packages/interface-pubsub-compliance-tests/src/two-nodes.ts index 08d163841..73e73c5be 100644 --- a/packages/interface-pubsub-compliance-tests/src/two-nodes.ts +++ b/packages/interface-pubsub-compliance-tests/src/two-nodes.ts @@ -165,10 +165,15 @@ export default (common: TestSetup) => { function receivedMsg (evt: CustomEvent) { const msg = evt.detail - expect(uint8ArrayToString(msg.data)).to.equal('banana') - expect(msg.from.toString()).to.equal(componentsB.getPeerId().toString()) - expect(msg.sequenceNumber).to.be.a('BigInt') - expect(msg.topic).to.be.equal(topic) + if (msg.type === 'unsigned') { + expect(uint8ArrayToString(msg.data)).to.equal('banana') + expect(msg.topic).to.be.equal(topic) + } else { + expect(uint8ArrayToString(msg.data)).to.equal('banana') + expect(msg.from.toString()).to.equal(componentsB.getPeerId().toString()) + expect(msg.sequenceNumber).to.be.a('BigInt') + expect(msg.topic).to.be.equal(topic) + } if (++counter === 10) { psA.removeEventListener('message', receivedMsg) diff --git a/packages/interface-pubsub/src/index.ts b/packages/interface-pubsub/src/index.ts index 10b1c5121..20aa6bc1e 100644 --- a/packages/interface-pubsub/src/index.ts +++ b/packages/interface-pubsub/src/index.ts @@ -27,15 +27,24 @@ export const StrictNoSign = 'StrictNoSign' export type SignaturePolicy = typeof StrictSign | typeof StrictNoSign -export interface Message { +export interface SignedMessage { + type: 'signed' from: PeerId topic: string data: Uint8Array - sequenceNumber?: bigint - signature?: Uint8Array - key?: Uint8Array + sequenceNumber: bigint + signature: Uint8Array + key: Uint8Array } +export interface UnsignedMessage { + type: 'unsigned' + topic: string + data: Uint8Array +} + +export type Message = SignedMessage | UnsignedMessage + export interface PubSubRPCMessage { from?: Uint8Array topic?: string