diff --git a/proto/waku/v2/message.proto b/proto/waku/v2/message.proto index 48ee93cfb0..bf3f78954f 100644 --- a/proto/waku/v2/message.proto +++ b/proto/waku/v2/message.proto @@ -2,7 +2,7 @@ syntax = "proto3"; package waku.v2; -message WakuMessageProto { +message WakuMessage { optional bytes payload = 1; optional string content_topic = 2; optional uint32 version = 3; diff --git a/proto/waku/v2/store.proto b/proto/waku/v2/store.proto index aa8f03edd6..7d9db6b8f8 100644 --- a/proto/waku/v2/store.proto +++ b/proto/waku/v2/store.proto @@ -32,7 +32,7 @@ message HistoryQuery { } message HistoryResponse { - repeated WakuMessageProto messages = 1; + repeated WakuMessage messages = 1; PagingInfo paging_info = 2; } diff --git a/src/lib/waku.ts b/src/lib/waku.ts index cd1e15ebc3..0fba0e37ae 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -102,7 +102,7 @@ export default class Waku { await this.libp2p.dialProtocol(peer, [RelayCodec, StoreCodec]); } - async dialWithMultiAddr(peerId: PeerId, multiaddr: Multiaddr[]) { + addPeerToAddressBook(peerId: PeerId, multiaddr: Multiaddr[]) { this.libp2p.peerStore.addressBook.set(peerId, multiaddr); } diff --git a/src/lib/waku_message.spec.ts b/src/lib/waku_message.spec.ts index 8b1f386697..cf37aa5e23 100644 --- a/src/lib/waku_message.spec.ts +++ b/src/lib/waku_message.spec.ts @@ -1,3 +1,4 @@ +import { expect } from 'chai'; import fc from 'fast-check'; import { WakuMessage } from './waku_message'; @@ -7,10 +8,10 @@ describe('Waku Message', function () { fc.assert( fc.property(fc.string(), (s) => { const msg = WakuMessage.fromUtf8String(s); - const binary = msg.toBinary(); + const binary = msg.encode(); const actual = WakuMessage.decode(binary); - return actual.isEqualTo(msg); + expect(actual).to.deep.equal(msg); }) ); }); @@ -19,7 +20,7 @@ describe('Waku Message', function () { fc.assert( fc.property(fc.string(), (s) => { const msg = WakuMessage.fromUtf8String(s); - const utf8 = msg.utf8Payload(); + const utf8 = msg.payloadAsUtf8; return utf8 === s; }) diff --git a/src/lib/waku_message.ts b/src/lib/waku_message.ts index 81d372242d..cb1331c537 100644 --- a/src/lib/waku_message.ts +++ b/src/lib/waku_message.ts @@ -2,31 +2,30 @@ import { Reader } from 'protobufjs/minimal'; // Protecting the user from protobuf oddities -import { WakuMessageProto } from '../proto/waku/v2/message'; +import * as proto from '../proto/waku/v2/message'; export const DEFAULT_CONTENT_TOPIC = '/waku/2/default-content/proto'; const DEFAULT_VERSION = 0; export class WakuMessage { - // TODO: Adopt similar design to HistoryRPC - private constructor( - public payload?: Uint8Array, - public contentTopic?: string, - public version?: number - ) {} - - static fromProto(proto: WakuMessageProto) { - return new WakuMessage(proto.payload, proto.contentTopic, proto.version); - } + public constructor(public proto: proto.WakuMessage) {} /** * Create Message with a utf-8 string as payload - * @param payload + * @param utf8 + * @param contentTopic * @returns {WakuMessage} */ - static fromUtf8String(payload: string): WakuMessage { - const buf = Buffer.from(payload, 'utf-8'); - return new WakuMessage(buf, DEFAULT_CONTENT_TOPIC, DEFAULT_VERSION); + static fromUtf8String( + utf8: string, + contentTopic: string = DEFAULT_CONTENT_TOPIC + ): WakuMessage { + const payload = Buffer.from(utf8, 'utf-8'); + return new WakuMessage({ + payload, + version: DEFAULT_VERSION, + contentTopic, + }); } /** @@ -39,50 +38,43 @@ export class WakuMessage { payload: Uint8Array, contentTopic: string = DEFAULT_CONTENT_TOPIC ): WakuMessage { - return new WakuMessage(payload, contentTopic, DEFAULT_VERSION); + return new WakuMessage({ + payload, + version: DEFAULT_VERSION, + contentTopic, + }); } static decode(bytes: Uint8Array): WakuMessage { - const wakuMsg = WakuMessageProto.decode(Reader.create(bytes)); - return new WakuMessage( - wakuMsg.payload, - wakuMsg.contentTopic, - wakuMsg.version - ); + const wakuMsg = proto.WakuMessage.decode(Reader.create(bytes)); + return new WakuMessage(wakuMsg); } - toBinary(): Uint8Array { - return WakuMessageProto.encode({ - payload: this.payload, - version: this.version, - contentTopic: this.contentTopic, - }).finish(); + encode(): Uint8Array { + return proto.WakuMessage.encode(this.proto).finish(); } - utf8Payload(): string { - if (!this.payload) { + get payloadAsUtf8(): string { + if (!this.proto.payload) { return ''; } - return Array.from(this.payload) + return Array.from(this.proto.payload) .map((char) => { return String.fromCharCode(char); }) .join(''); } - // Purely for tests purposes. - // We do consider protobuf field when checking equality - // As the content is held by the other fields. - isEqualTo(other: WakuMessage) { - const payloadsAreEqual = - this.payload && other.payload - ? Buffer.compare(this.payload, other.payload) === 0 - : !(this.payload || other.payload); - return ( - payloadsAreEqual && - this.contentTopic === other.contentTopic && - this.version === other.version - ); + get payload(): Uint8Array | undefined { + return this.proto.payload; + } + + get contentTopic(): string | undefined { + return this.proto.contentTopic; + } + + get version(): number | undefined { + return this.proto.version; } } diff --git a/src/lib/waku_relay/index.spec.ts b/src/lib/waku_relay/index.spec.ts index c091d08e19..427af3c661 100644 --- a/src/lib/waku_relay/index.spec.ts +++ b/src/lib/waku_relay/index.spec.ts @@ -32,7 +32,10 @@ describe('Waku Relay', () => { }), ]); - await waku1.dialWithMultiAddr(waku2.libp2p.peerId, waku2.libp2p.multiaddrs); + await waku1.addPeerToAddressBook( + waku2.libp2p.peerId, + waku2.libp2p.multiaddrs + ); await Promise.all([ new Promise((resolve) => @@ -319,7 +322,7 @@ describe('Waku Relay', () => { console.log('Waiting for message'); const waku2ReceivedMsg = await waku2ReceivedPromise; - expect(waku2ReceivedMsg.utf8Payload()).to.eq(msgStr); + expect(waku2ReceivedMsg.payloadAsUtf8).to.eq(msgStr); }); }); }); diff --git a/src/lib/waku_relay/index.ts b/src/lib/waku_relay/index.ts index 56e00ebc26..7b92441ace 100644 --- a/src/lib/waku_relay/index.ts +++ b/src/lib/waku_relay/index.ts @@ -90,7 +90,7 @@ export class WakuRelay extends Gossipsub { * @returns {Promise} */ async send(message: WakuMessage) { - const msg = message.toBinary(); + const msg = message.encode(); await super.publish(constants.RelayDefaultTopic, Buffer.from(msg)); } diff --git a/src/lib/waku_store/index.spec.ts b/src/lib/waku_store/index.spec.ts index 817059ff7d..1cec38b0b9 100644 --- a/src/lib/waku_store/index.spec.ts +++ b/src/lib/waku_store/index.spec.ts @@ -41,7 +41,7 @@ describe('Waku Store', () => { expect(messages?.length).eq(2); const result = messages?.findIndex((msg) => { - return msg.utf8Payload() === 'Message 0'; + return msg.payloadAsUtf8 === 'Message 0'; }); expect(result).to.not.eq(-1); }); @@ -75,7 +75,7 @@ describe('Waku Store', () => { for (let index = 0; index < 2; index++) { expect( messages?.findIndex((msg) => { - return msg.utf8Payload() === `Message ${index}`; + return msg.payloadAsUtf8 === `Message ${index}`; }) ).to.eq(index); } diff --git a/src/lib/waku_store/index.ts b/src/lib/waku_store/index.ts index 0f68041803..e78b0bb995 100644 --- a/src/lib/waku_store/index.ts +++ b/src/lib/waku_store/index.ts @@ -65,7 +65,7 @@ export class WakuStore { } response.messages.map((protoMsg) => { - messages.push(WakuMessage.fromProto(protoMsg)); + messages.push(new WakuMessage(protoMsg)); }); const responsePageSize = response.pagingInfo?.pageSize; diff --git a/src/proto/waku/v2/message.ts b/src/proto/waku/v2/message.ts index 8816f568f4..5ded6928e5 100644 --- a/src/proto/waku/v2/message.ts +++ b/src/proto/waku/v2/message.ts @@ -4,18 +4,18 @@ import _m0 from 'protobufjs/minimal'; export const protobufPackage = 'waku.v2'; -export interface WakuMessageProto { +export interface WakuMessage { payload?: Uint8Array | undefined; contentTopic?: string | undefined; version?: number | undefined; timestamp?: number | undefined; } -const baseWakuMessageProto: object = {}; +const baseWakuMessage: object = {}; -export const WakuMessageProto = { +export const WakuMessage = { encode( - message: WakuMessageProto, + message: WakuMessage, writer: _m0.Writer = _m0.Writer.create() ): _m0.Writer { if (message.payload !== undefined) { @@ -33,10 +33,10 @@ export const WakuMessageProto = { return writer; }, - decode(input: _m0.Reader | Uint8Array, length?: number): WakuMessageProto { + decode(input: _m0.Reader | Uint8Array, length?: number): WakuMessage { const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input); let end = length === undefined ? reader.len : reader.pos + length; - const message = { ...baseWakuMessageProto } as WakuMessageProto; + const message = { ...baseWakuMessage } as WakuMessage; while (reader.pos < end) { const tag = reader.uint32(); switch (tag >>> 3) { @@ -60,8 +60,8 @@ export const WakuMessageProto = { return message; }, - fromJSON(object: any): WakuMessageProto { - const message = { ...baseWakuMessageProto } as WakuMessageProto; + fromJSON(object: any): WakuMessage { + const message = { ...baseWakuMessage } as WakuMessage; if (object.payload !== undefined && object.payload !== null) { message.payload = bytesFromBase64(object.payload); } @@ -83,7 +83,7 @@ export const WakuMessageProto = { return message; }, - toJSON(message: WakuMessageProto): unknown { + toJSON(message: WakuMessage): unknown { const obj: any = {}; message.payload !== undefined && (obj.payload = @@ -97,8 +97,8 @@ export const WakuMessageProto = { return obj; }, - fromPartial(object: DeepPartial): WakuMessageProto { - const message = { ...baseWakuMessageProto } as WakuMessageProto; + fromPartial(object: DeepPartial): WakuMessage { + const message = { ...baseWakuMessage } as WakuMessage; if (object.payload !== undefined && object.payload !== null) { message.payload = object.payload; } else { diff --git a/src/proto/waku/v2/store.ts b/src/proto/waku/v2/store.ts index e575156192..8bfba420f1 100644 --- a/src/proto/waku/v2/store.ts +++ b/src/proto/waku/v2/store.ts @@ -1,7 +1,7 @@ /* eslint-disable */ import Long from 'long'; import _m0 from 'protobufjs/minimal'; -import { WakuMessageProto } from '../../waku/v2/message'; +import { WakuMessage } from '../../waku/v2/message'; export const protobufPackage = 'waku.v2'; @@ -65,7 +65,7 @@ export interface HistoryQuery { } export interface HistoryResponse { - messages: WakuMessageProto[]; + messages: WakuMessage[]; pagingInfo: PagingInfo | undefined; } @@ -453,7 +453,7 @@ export const HistoryResponse = { writer: _m0.Writer = _m0.Writer.create() ): _m0.Writer { for (const v of message.messages) { - WakuMessageProto.encode(v!, writer.uint32(10).fork()).ldelim(); + WakuMessage.encode(v!, writer.uint32(10).fork()).ldelim(); } if (message.pagingInfo !== undefined) { PagingInfo.encode(message.pagingInfo, writer.uint32(18).fork()).ldelim(); @@ -470,9 +470,7 @@ export const HistoryResponse = { const tag = reader.uint32(); switch (tag >>> 3) { case 1: - message.messages.push( - WakuMessageProto.decode(reader, reader.uint32()) - ); + message.messages.push(WakuMessage.decode(reader, reader.uint32())); break; case 2: message.pagingInfo = PagingInfo.decode(reader, reader.uint32()); @@ -490,7 +488,7 @@ export const HistoryResponse = { message.messages = []; if (object.messages !== undefined && object.messages !== null) { for (const e of object.messages) { - message.messages.push(WakuMessageProto.fromJSON(e)); + message.messages.push(WakuMessage.fromJSON(e)); } } if (object.pagingInfo !== undefined && object.pagingInfo !== null) { @@ -505,7 +503,7 @@ export const HistoryResponse = { const obj: any = {}; if (message.messages) { obj.messages = message.messages.map((e) => - e ? WakuMessageProto.toJSON(e) : undefined + e ? WakuMessage.toJSON(e) : undefined ); } else { obj.messages = []; @@ -522,7 +520,7 @@ export const HistoryResponse = { message.messages = []; if (object.messages !== undefined && object.messages !== null) { for (const e of object.messages) { - message.messages.push(WakuMessageProto.fromPartial(e)); + message.messages.push(WakuMessage.fromPartial(e)); } } if (object.pagingInfo !== undefined && object.pagingInfo !== null) { diff --git a/web-chat/src/App.tsx b/web-chat/src/App.tsx index 937db54fb2..64ef9bc25f 100644 --- a/web-chat/src/App.tsx +++ b/web-chat/src/App.tsx @@ -86,7 +86,7 @@ export default function App() { } else { stateWaku.libp2p.pubsub.on(RelayDefaultTopic, handleNewMessages); - stateWaku.libp2p.peerStore.once( + stateWaku.libp2p.peerStore.on( 'change:protocols', handleProtocolChange.bind({}, stateWaku) ); diff --git a/web-chat/src/command.ts b/web-chat/src/command.ts index 3034c686ec..1da11566d4 100644 --- a/web-chat/src/command.ts +++ b/web-chat/src/command.ts @@ -42,7 +42,7 @@ function connect(peer: string | undefined, waku: Waku | undefined): string[] { if (!peerId) { return ['Peer Id needed to dial']; } - waku.libp2p.peerStore.addressBook.add(PeerId.createFromB58String(peerId), [ + waku.addPeerToAddressBook(PeerId.createFromB58String(peerId), [ peerMultiaddr, ]); return [