test: nwaku JSON RPC API now uses base64 encoding

This commit is contained in:
fryorcraken.eth 2023-02-16 13:27:39 +11:00
parent 5a92d74c62
commit f66e9835f1
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 15 additions and 18 deletions

View File

@ -81,13 +81,6 @@ export interface MessageRpcQuery {
} }
export interface MessageRpcResponse { export interface MessageRpcResponse {
payload: number[];
contentTopic?: string;
version?: number;
timestamp?: bigint; // Unix epoch time in nanoseconds as a 64-bits integer value.
}
export interface MessageRpcResponseHex {
payload: string; payload: string;
contentTopic?: string; contentTopic?: string;
version?: number; version?: number;
@ -122,7 +115,7 @@ export class Nwaku {
} }
return { return {
payload: bytesToHex(message.payload), payload: Buffer.from(message.payload).toString("base64url"),
contentTopic: message.contentTopic, contentTopic: message.contentTopic,
timestamp, timestamp,
}; };
@ -321,10 +314,10 @@ export class Nwaku {
async getAsymmetricMessages( async getAsymmetricMessages(
privateKey: Uint8Array, privateKey: Uint8Array,
pubSubTopic?: string pubSubTopic?: string
): Promise<MessageRpcResponseHex[]> { ): Promise<MessageRpcResponse[]> {
this.checkProcess(); this.checkProcess();
return await this.rpcCall<MessageRpcResponseHex[]>( return await this.rpcCall<MessageRpcResponse[]>(
"get_waku_v2_private_v1_asymmetric_messages", "get_waku_v2_private_v1_asymmetric_messages",
[ [
pubSubTopic ? pubSubTopic : DefaultPubSubTopic, pubSubTopic ? pubSubTopic : DefaultPubSubTopic,
@ -363,10 +356,10 @@ export class Nwaku {
async getSymmetricMessages( async getSymmetricMessages(
symKey: Uint8Array, symKey: Uint8Array,
pubSubTopic?: string pubSubTopic?: string
): Promise<MessageRpcResponseHex[]> { ): Promise<MessageRpcResponse[]> {
this.checkProcess(); this.checkProcess();
return await this.rpcCall<MessageRpcResponseHex[]>( return await this.rpcCall<MessageRpcResponse[]>(
"get_waku_v2_private_v1_symmetric_messages", "get_waku_v2_private_v1_symmetric_messages",
[ [
pubSubTopic ? pubSubTopic : DefaultPubSubTopic, pubSubTopic ? pubSubTopic : DefaultPubSubTopic,
@ -475,3 +468,7 @@ interface RpcInfoResponse {
listenAddresses: string[]; listenAddresses: string[];
enrUri?: string; enrUri?: string;
} }
export function base64ToUtf8(b64: string): string {
return Buffer.from(b64, "base64").toString("utf-8");
}

View File

@ -2,11 +2,12 @@ import { createEncoder, waitForRemotePeer } from "@waku/core";
import { createLightNode } from "@waku/create"; import { createLightNode } from "@waku/create";
import type { LightNode } from "@waku/interfaces"; import type { LightNode } from "@waku/interfaces";
import { Protocols } from "@waku/interfaces"; import { Protocols } from "@waku/interfaces";
import { bytesToUtf8, utf8ToBytes } from "@waku/utils"; import { utf8ToBytes } from "@waku/utils";
import { expect } from "chai"; import { expect } from "chai";
import debug from "debug"; import debug from "debug";
import { import {
base64ToUtf8,
delay, delay,
makeLogFileName, makeLogFileName,
MessageRpcResponse, MessageRpcResponse,
@ -58,7 +59,7 @@ describe("Waku Light Push [node only]", () => {
} }
expect(msgs[0].contentTopic).to.equal(TestContentTopic); expect(msgs[0].contentTopic).to.equal(TestContentTopic);
expect(bytesToUtf8(new Uint8Array(msgs[0].payload))).to.equal(messageText); expect(base64ToUtf8(msgs[0].payload)).to.equal(messageText);
}); });
it("Push on custom pubsub topic", async function () { it("Push on custom pubsub topic", async function () {
@ -105,6 +106,6 @@ describe("Waku Light Push [node only]", () => {
} }
expect(msgs[0].contentTopic).to.equal(TestContentTopic); expect(msgs[0].contentTopic).to.equal(TestContentTopic);
expect(bytesToUtf8(new Uint8Array(msgs[0].payload))!).to.equal(messageText); expect(base64ToUtf8(msgs[0].payload!)).to.equal(messageText);
}); });
}); });

View File

@ -25,6 +25,7 @@ import { expect } from "chai";
import debug from "debug"; import debug from "debug";
import { import {
base64ToUtf8,
delay, delay,
makeLogFileName, makeLogFileName,
MessageRpcResponse, MessageRpcResponse,
@ -389,9 +390,7 @@ describe("Waku Relay [node only]", () => {
expect(msgs[0].contentTopic).to.equal(TestContentTopic); expect(msgs[0].contentTopic).to.equal(TestContentTopic);
expect(msgs[0].version).to.equal(0); expect(msgs[0].version).to.equal(0);
expect(bytesToUtf8(new Uint8Array(msgs[0].payload))).to.equal( expect(base64ToUtf8(msgs[0].payload)).to.equal(messageText);
messageText
);
}); });
it("Nwaku publishes", async function () { it("Nwaku publishes", async function () {