fix!: update to latest waku + use ISender

This commit is contained in:
Sasha 2023-04-04 23:13:38 +02:00 committed by GitHub
commit 3201743ed7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1150 additions and 537 deletions

1598
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -59,7 +59,7 @@
"@types/uuid": "^8.3.0",
"@typescript-eslint/eslint-plugin": "^5.8.1",
"@typescript-eslint/parser": "^5.8.1",
"@waku/interfaces": "^0.0.7",
"@waku/interfaces": "0.0.11",
"app-root-path": "^3.0.0",
"chai": "^4.3.4",
"cspell": "^5.14.0",
@ -121,8 +121,8 @@
"@stablelib/random": "^1.0.2",
"@stablelib/sha256": "^1.0.1",
"@stablelib/x25519": "^1.0.1",
"@waku/core": "^0.0.10",
"@waku/proto": "^0.0.2",
"@waku/core": "0.0.16",
"@waku/proto": "0.0.4",
"bn.js": "^5.2.1",
"eventemitter3": "^5.0.0",
"p-event": "^5.0.1",

View File

@ -1,4 +1,4 @@
import { DecodedMessage } from "@waku/core";
import { DecodedMessage } from "@waku/core/lib/message/version_0";
import type { IDecodedMessage, IDecoder, IEncoder, IMessage, IProtoMessage } from "@waku/interfaces";
import { WakuMessage } from "@waku/proto";
import debug from "debug";
@ -52,6 +52,7 @@ export class NoiseHandshakeEncoder implements IEncoder {
rateLimitProof: undefined,
payload: this.hsStepResult.payload2.serialize(),
version: version,
meta: undefined,
contentTopic: this.contentTopic,
timestamp: BigInt(timestamp.valueOf()) * OneMillion,
};
@ -74,7 +75,7 @@ export class NoiseHandshakeDecoder implements IDecoder<NoiseHandshakeMessage> {
return Promise.resolve(protoMessage as IProtoMessage);
}
async fromProtoObj(proto: IProtoMessage): Promise<NoiseHandshakeMessage | undefined> {
async fromProtoObj(pubSubTopic: string, proto: IProtoMessage): Promise<NoiseHandshakeMessage | undefined> {
// https://github.com/status-im/js-waku/issues/921
if (proto.version === undefined) {
proto.version = 0;
@ -90,7 +91,7 @@ export class NoiseHandshakeDecoder implements IDecoder<NoiseHandshakeMessage> {
return;
}
return new NoiseHandshakeMessage(proto);
return new NoiseHandshakeMessage(pubSubTopic, proto);
}
}
@ -101,8 +102,8 @@ export class NoiseHandshakeDecoder implements IDecoder<NoiseHandshakeMessage> {
export class NoiseSecureMessage extends DecodedMessage implements IDecodedMessage {
private readonly _decodedPayload: Uint8Array;
constructor(proto: WakuMessage, decodedPayload: Uint8Array) {
super(proto);
constructor(pubSubTopic: string, proto: WakuMessage, decodedPayload: Uint8Array) {
super(pubSubTopic, proto);
this._decodedPayload = decodedPayload;
}
@ -146,6 +147,7 @@ export class NoiseSecureTransferEncoder implements IEncoder {
rateLimitProof: undefined,
ephemeral: this.ephemeral,
version: version,
meta: undefined,
contentTopic: this.contentTopic,
timestamp: BigInt(timestamp.valueOf()) * OneMillion,
};
@ -171,7 +173,7 @@ export class NoiseSecureTransferDecoder implements IDecoder<NoiseSecureMessage>
return Promise.resolve(protoMessage as IProtoMessage);
}
async fromProtoObj(proto: IProtoMessage): Promise<NoiseSecureMessage | undefined> {
async fromProtoObj(pubSubTopic: string, proto: IProtoMessage): Promise<NoiseSecureMessage | undefined> {
// https://github.com/status-im/js-waku/issues/921
if (proto.version === undefined) {
proto.version = 0;
@ -187,7 +189,7 @@ export class NoiseSecureTransferDecoder implements IDecoder<NoiseSecureMessage>
try {
const payloadV2 = PayloadV2.deserialize(proto.payload);
const decryptedPayload = this.hsResult.readMessage(payloadV2);
return new NoiseSecureMessage(proto, decryptedPayload);
return new NoiseSecureMessage(pubSubTopic, proto, decryptedPayload);
} catch (err) {
log("could not decode message ", err);
return;

View File

@ -14,7 +14,7 @@ import {
StepHandshakeParameters,
} from "./handshake.js";
import { MessageNametagBuffer } from "./messagenametag.js";
import { InitiatorParameters, Responder, ResponderParameters, Sender, WakuPairing } from "./pairing.js";
import { InitiatorParameters, Responder, ResponderParameters, WakuPairing } from "./pairing.js";
import {
HandshakePattern,
MessageDirection,
@ -49,4 +49,4 @@ export { ChaChaPolyCipherState, NoisePublicKey };
export { MessageNametagBuffer };
export { NoiseHandshakeDecoder, NoiseHandshakeEncoder, NoiseSecureTransferDecoder, NoiseSecureTransferEncoder };
export { QR };
export { InitiatorParameters, ResponderParameters, Sender, Responder, WakuPairing };
export { InitiatorParameters, ResponderParameters, Responder, WakuPairing };

View File

@ -1,6 +1,6 @@
import { HMACDRBG } from "@stablelib/hmac-drbg";
import { randomBytes } from "@stablelib/random";
import type { IDecoder, IEncoder, IMessage } from "@waku/interfaces";
import type { IDecoder, IEncoder, IMessage, ISender } from "@waku/interfaces";
import { expect } from "chai";
import { EventEmitter } from "eventemitter3";
import { pEvent } from "p-event";
@ -11,6 +11,8 @@ import { generateX25519KeyPair } from "./crypto";
import { MessageNametagBufferSize } from "./messagenametag";
import { ResponderParameters, WakuPairing } from "./pairing";
const PUBSUB_TOPIC = "default";
describe("js-noise: pairing object", () => {
const rng = new HMACDRBG();
@ -23,10 +25,13 @@ describe("js-noise: pairing object", () => {
// =================
// Simulate waku. This code is not meant to be used IRL
const msgEmitter = new EventEmitter();
const sender = {
async publish(encoder: IEncoder, msg: IMessage): Promise<void> {
const sender: ISender = {
async send(encoder: IEncoder, msg: IMessage) {
const protoMsg = await encoder.toProtoObj(msg);
msgEmitter.emit(encoder.contentTopic, protoMsg);
return {
recipients: [],
};
},
};
const decoderMap: { [key: string]: IDecoder<NoiseHandshakeMessage> } = {};
@ -39,7 +44,7 @@ describe("js-noise: pairing object", () => {
},
async nextMessage(contentTopic: string): Promise<NoiseHandshakeMessage> {
const msg = await pEvent(msgEmitter, contentTopic);
const decodedMessage = await decoderMap[contentTopic].fromProtoObj(msg);
const decodedMessage = await decoderMap[contentTopic].fromProtoObj(PUBSUB_TOPIC, msg);
return decodedMessage!;
},
async stop(contentTopic: string): Promise<void> {
@ -81,7 +86,7 @@ describe("js-noise: pairing object", () => {
let message = randomBytes(32, rng);
let encodedMsg = await aliceEncoder.toWire({ payload: message });
let readMessageProto = await bobDecoder.fromWireToProtoObj(encodedMsg!);
let readMessage = await bobDecoder.fromProtoObj(readMessageProto!);
let readMessage = await bobDecoder.fromProtoObj(PUBSUB_TOPIC, readMessageProto!);
expect(uint8ArrayEquals(message, readMessage!.payload)).to.be.true;
@ -89,7 +94,7 @@ describe("js-noise: pairing object", () => {
message = randomBytes(32, rng);
encodedMsg = await bobEncoder.toWire({ payload: message });
readMessageProto = await aliceDecoder.fromWireToProtoObj(encodedMsg!);
readMessage = await aliceDecoder.fromProtoObj(readMessageProto!);
readMessage = await aliceDecoder.fromProtoObj(PUBSUB_TOPIC, readMessageProto!);
expect(uint8ArrayEquals(message, readMessage!.payload)).to.be.true;
}

View File

@ -1,6 +1,6 @@
import { HMACDRBG } from "@stablelib/hmac-drbg";
import { randomBytes } from "@stablelib/random";
import type { IDecoder, IEncoder, IMessage } from "@waku/interfaces";
import type { IDecoder, ISender } from "@waku/interfaces";
import debug from "debug";
import { EventEmitter } from "eventemitter3";
import { pEvent } from "p-event";
@ -23,18 +23,6 @@ import { QR } from "./qr.js";
const log = debug("waku:noise:pairing");
/**
* Sender interface that an object must implement so the pairing object can publish noise messages
*/
export interface Sender {
/**
* Publish a message
* @param encoder NoiseHandshakeEncoder encoder to use to encrypt the messages
* @param msg message to broadcast
*/
publish(encoder: IEncoder, msg: IMessage): Promise<void>;
}
/**
* Responder interface than an object must implement so the pairing object can receive noise messages
*/
@ -122,7 +110,7 @@ export class WakuPairing {
* @param myEphemeralKey optional ephemeral key
*/
constructor(
private sender: Sender,
private sender: ISender,
private responder: Responder,
private myStaticKey: KeyPair,
pairingParameters: InitiatorParameters | ResponderParameters,
@ -258,7 +246,9 @@ export class WakuPairing {
// We prepare a message from initiator's payload2
// At this point wakuMsg is sent over the Waku network to responder content topic
let encoder = new NoiseHandshakeEncoder(this.contentTopic, hsStep);
await this.sender.publish(encoder, {});
await this.sender.send(encoder, {
payload: new Uint8Array(),
});
// We generate an authorization code using the handshake state
// this check has to be confirmed with a user interaction, comparing auth codes in both ends
@ -294,7 +284,9 @@ export class WakuPairing {
});
encoder = new NoiseHandshakeEncoder(this.contentTopic, hsStep);
await this.sender.publish(encoder, {});
await this.sender.send(encoder, {
payload: new Uint8Array(),
});
// Secure Transfer Phase
this.handshakeResult = this.handshake.finalizeHandshake();
@ -333,7 +325,9 @@ export class WakuPairing {
// We prepare a Waku message from responder's payload2
const encoder = new NoiseHandshakeEncoder(this.contentTopic, hsStep);
await this.sender.publish(encoder, {});
await this.sender.send(encoder, {
payload: new Uint8Array(),
});
// 3rd step
// -> sA, sAeB, sAsB {s}

View File

@ -16,6 +16,8 @@ import { NoiseHandshakePatterns } from "./patterns";
import { NoisePublicKey } from "./publickey";
import { QR } from "./qr";
const PUBSUB_TOPIC = "default";
describe("Waku Noise Sessions", () => {
const rng = new HMACDRBG();
@ -113,12 +115,12 @@ describe("Waku Noise Sessions", () => {
// We prepare a Waku message from Alice's payload2
// At this point wakuMsg is sent over the Waku network and is received
// We simulate this by creating the ProtoBuffer from wakuMsg
let wakuMsgBytes = await encoder.toWire({});
let wakuMsgBytes = await encoder.toWire({ payload: new Uint8Array() });
// We decode the WakuMessage from the ProtoBuffer
let decoder = new NoiseHandshakeDecoder(contentTopic);
let wakuMsgProto = await decoder.fromWireToProtoObj(wakuMsgBytes!);
let v2Msg = await decoder.fromProtoObj(wakuMsgProto!);
let v2Msg = await decoder.fromProtoObj(PUBSUB_TOPIC, wakuMsgProto!);
expect(v2Msg!.contentTopic).to.be.equals(contentTopic);
expect(v2Msg?.payloadV2.equals(aliceStep.payload2)).to.be.true;
@ -155,12 +157,12 @@ describe("Waku Noise Sessions", () => {
// At this point wakuMsg is sent over the Waku network and is received
// We simulate this by creating the ProtoBuffer from wakuMsg
wakuMsgBytes = await encoder.toWire({});
wakuMsgBytes = await encoder.toWire({ payload: new Uint8Array() });
// We decode the WakuMessage from the ProtoBuffer
decoder = new NoiseHandshakeDecoder(contentTopic);
wakuMsgProto = await decoder.fromWireToProtoObj(wakuMsgBytes!);
v2Msg = await decoder.fromProtoObj(wakuMsgProto!);
v2Msg = await decoder.fromProtoObj(PUBSUB_TOPIC, wakuMsgProto!);
expect(v2Msg?.payloadV2.equals(bobStep.payload2)).to.be.true;
@ -192,12 +194,12 @@ describe("Waku Noise Sessions", () => {
// At this point wakuMsg is sent over the Waku network and is received
// We simulate this by creating the ProtoBuffer from wakuMsg
wakuMsgBytes = await encoder.toWire({});
wakuMsgBytes = await encoder.toWire({ payload: new Uint8Array() });
// We decode the WakuMessage from the ProtoBuffer
decoder = new NoiseHandshakeDecoder(contentTopic);
wakuMsgProto = await decoder.fromWireToProtoObj(wakuMsgBytes!);
v2Msg = await decoder.fromProtoObj(wakuMsgProto!);
v2Msg = await decoder.fromProtoObj(PUBSUB_TOPIC, wakuMsgProto!);
expect(v2Msg?.payloadV2.equals(aliceStep.payload2)).to.be.true;
@ -231,7 +233,7 @@ describe("Waku Noise Sessions", () => {
let message = randomBytes(32, rng);
let encodedMsg = await aliceEncoder.toWire({ payload: message });
let readMessageProto = await bobDecoder.fromWireToProtoObj(encodedMsg!);
let readMessage = await bobDecoder.fromProtoObj(readMessageProto!);
let readMessage = await bobDecoder.fromProtoObj(PUBSUB_TOPIC, readMessageProto!);
expect(uint8ArrayEquals(message, readMessage!.payload)).to.be.true;
@ -239,7 +241,7 @@ describe("Waku Noise Sessions", () => {
message = randomBytes(32, rng);
encodedMsg = await bobEncoder.toWire({ payload: message });
readMessageProto = await aliceDecoder.fromWireToProtoObj(encodedMsg!);
readMessage = await aliceDecoder.fromProtoObj(readMessageProto!);
readMessage = await aliceDecoder.fromProtoObj(PUBSUB_TOPIC, readMessageProto!);
expect(uint8ArrayEquals(message, readMessage!.payload)).to.be.true;
}