Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

feat!: pubsub Message types for signature policies #266

Merged
merged 1 commit into from
Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/interface-pubsub-compliance-tests/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {
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)
}
})
})
}
13 changes: 9 additions & 4 deletions packages/interface-pubsub-compliance-tests/src/two-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,15 @@ export default (common: TestSetup<PubSub, PubSubArgs>) => {

function receivedMsg (evt: CustomEvent<Message>) {
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)
Expand Down
17 changes: 13 additions & 4 deletions packages/interface-pubsub/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No from field in unsigned messages?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, see the comment on StrictNoSign. looks like the text was taken from here: https://github.com/libp2p/specs/tree/master/pubsub#signature-policy-options

data: Uint8Array
}

export type Message = SignedMessage | UnsignedMessage

export interface PubSubRPCMessage {
from?: Uint8Array
topic?: string
Expand Down