fix: code review

This commit is contained in:
Richard Ramos 2022-12-07 15:33:03 -04:00
parent b4ad35597d
commit 984dc3a8a2
No known key found for this signature in database
GPG Key ID: BD36D48BC9FFC88C
4 changed files with 54 additions and 18 deletions

View File

@ -1,12 +1,13 @@
import { createLightNode } from "js-waku/lib/create_waku";
import { waitForRemotePeer } from "js-waku/lib/wait_for_remote_peer";
import * as noise from "@waku/js-noise";
import * as noise from "@waku/noise";
import QRCode from "qrcode";
// TODO: Get rid of these
import hexToArrayBuffer from "hex-to-array-buffer";
import arrayBufferToHex from "array-buffer-to-hex";
import { WakuPairing } from "@waku/noise";
function getPairingInfo() {
function getPairingInfofromUrl() {
const urlParts = window.location.href.split("?");
if (urlParts.length < 2) return undefined;
@ -72,7 +73,7 @@ async function main() {
const myStaticKey = noise.generateX25519KeyPair();
const pairingParameters = getPairingInfo();
const pairingParameters = getPairingInfofromUrl();
if (pairingParameters) {
console.log("Initiator");
@ -86,6 +87,18 @@ async function main() {
} catch (err) {
alert(err);
}
// The information needs to be backed up to decrypt messages sent with
// codecs generated with the handshake
const contentTopic = pairingObj.contentTopic;
const handshakeResult = pairingObj.getHandshakeResult();
// This information should not be printed, it's done
// to see the information in the dev console
console.log("HandshakeResult", handshakeResult);
// To restore the codecs:
const codecs = WakuPairing.getSecureCodec(contentTopic, handshakeResult);
} else {
console.log("Receiver");
@ -100,7 +113,7 @@ async function main() {
const qrString = arrayBufferToHex(pInfo.qrMessageNameTag) + ":" + pInfo.qrCode;
const qrURL = window.location.href + "?" + encodeURIComponent(qrString);
console.log("Generating QR...")
console.log("Generating QR...");
QRCode.toCanvas(document.getElementById("qrCanvas"), qrURL, (error) => {
if (error) console.error(error);
});
@ -122,6 +135,18 @@ async function main() {
// TODO: handle timeout
alert(err);
}
// The information needs to be backed up to decrypt messages sent with
// codecs generated with the handshake
const contentTopic = pairingObj.contentTopic;
const handshakeResult = pairingObj.getHandshakeResult();
// This information should not be printed, it's done
// to see the information in the dev console
console.log("HandshakeResult", handshakeResult);
// To restore the codecs:
const codecs = WakuPairing.getSecureCodec(contentTopic, handshakeResult);
}
}
main();

View File

@ -1,14 +1,14 @@
{
"name": "@waku/js-noise-example",
"name": "@waku/noise-example",
"version": "0.1.0",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@waku/js-noise-example",
"name": "@waku/noise-example",
"version": "0.1.0",
"dependencies": {
"@waku/js-noise": "file:../",
"@waku/noise": "file:../",
"array-buffer-to-hex": "^1.0.0",
"hex-to-array-buffer": "^2.0.0",
"js-waku": "^0.29.0-29436ea",
@ -2263,7 +2263,7 @@
"@types/node": "*"
}
},
"node_modules/@waku/js-noise": {
"node_modules/@waku/noise": {
"resolved": "..",
"link": true
},
@ -8851,7 +8851,7 @@
"@types/node": "*"
}
},
"@waku/js-noise": {
"@waku/noise": {
"version": "file:..",
"requires": {
"@rollup/plugin-commonjs": "^22.0.2",

View File

@ -1,5 +1,5 @@
{
"name": "@waku/js-noise-example",
"name": "@waku/noise-example",
"private": true,
"version": "0.1.0",
"description": "",
@ -9,7 +9,7 @@
"start": "webpack-dev-server"
},
"dependencies": {
"@waku/js-noise": "file:../",
"@waku/noise": "file:../",
"array-buffer-to-hex": "^1.0.0",
"hex-to-array-buffer": "^2.0.0",
"js-waku": "^0.29.0-29436ea",

View File

@ -63,6 +63,7 @@ export class WakuPairing {
private qrMessageNameTag: Uint8Array;
private authCode?: string;
private started = false;
private handshakeResult: HandshakeResult | undefined;
private eventEmitter = new EventEmitter();
@ -230,11 +231,11 @@ export class WakuPairing {
await this.sender.publish(encoder, {});
// Secure Transfer Phase
const hsResult = this.handshake.finalizeHandshake();
this.handshakeResult = this.handshake.finalizeHandshake();
this.eventEmitter.emit("pairingComplete");
return this.getSecureCodec(hsResult);
return WakuPairing.getSecureCodec(this.contentTopic, this.handshakeResult);
}
private async receiverHandshake(): Promise<[NoiseSecureTransferEncoder, NoiseSecureTransferDecoder]> {
@ -283,20 +284,30 @@ export class WakuPairing {
}
// Secure Transfer Phase
const hsResult = this.handshake.finalizeHandshake();
this.handshakeResult = this.handshake.finalizeHandshake();
this.eventEmitter.emit("pairingComplete");
return this.getSecureCodec(hsResult);
return WakuPairing.getSecureCodec(this.contentTopic, this.handshakeResult);
}
private getSecureCodec(hsResult: HandshakeResult): [NoiseSecureTransferEncoder, NoiseSecureTransferDecoder] {
const secureEncoder = new NoiseSecureTransferEncoder(this.contentTopic, hsResult);
const secureDecoder = new NoiseSecureTransferDecoder(this.contentTopic, hsResult);
static getSecureCodec(
contentTopic: string,
hsResult: HandshakeResult
): [NoiseSecureTransferEncoder, NoiseSecureTransferDecoder] {
const secureEncoder = new NoiseSecureTransferEncoder(contentTopic, hsResult);
const secureDecoder = new NoiseSecureTransferDecoder(contentTopic, hsResult);
return [secureEncoder, secureDecoder];
}
public getHandshakeResult(): HandshakeResult {
if (!this.handshakeResult) {
throw new Error("handshake is not complete");
}
return this.handshakeResult;
}
async execute(timeoutMs = 30000): Promise<[NoiseSecureTransferEncoder, NoiseSecureTransferDecoder]> {
if (this.started) {
throw new Error("pairing already executed. Create new pairing object");