diff --git a/src/lib/waku_message.spec.ts b/src/lib/waku_message.spec.ts index 5d82f8db8c..5865f7fe18 100644 --- a/src/lib/waku_message.spec.ts +++ b/src/lib/waku_message.spec.ts @@ -14,4 +14,15 @@ describe('Waku Message', function () { }) ); }); + + it('Payload to utf-8', function () { + fc.assert( + fc.property(fc.string(), (s) => { + const msg = Message.fromUtf8String(s); + const utf8 = msg.utf8Payload(); + + return utf8 === s; + }) + ); + }); }); diff --git a/src/lib/waku_message.ts b/src/lib/waku_message.ts index f7734d4ba1..1b46978fb0 100644 --- a/src/lib/waku_message.ts +++ b/src/lib/waku_message.ts @@ -37,6 +37,18 @@ export class Message { }).finish(); } + utf8Payload(): string { + if (!this.payload) { + return ''; + } + + return Array.from(this.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.