Skip to content

Commit

Permalink
Merge pull request #147 from waku-org/chore/js-waku-0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
fryorcraken authored Nov 20, 2022
2 parents acd5386 + cefe44b commit 229e1ae
Show file tree
Hide file tree
Showing 30 changed files with 3,525 additions and 2,883 deletions.
8 changes: 6 additions & 2 deletions eth-pm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
"@material-ui/icons": "^4.11.2",
"ethers": "5.7.1",
"fontsource-roboto": "^4.0.0",
"js-waku": "0.30.0",
"@waku/create": "0.0.4",
"@waku/core": "0.0.6",
"@waku/interfaces": "0.0.5",
"@waku/byte-utils": "0.0.2",
"@waku/message-encryption": "0.0.4",
"protobufjs": "^7.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"uint8arrays": "^3.1.0"
},
"scripts": {
"start": "GENERATE_SOURCEMAP=false PORT=3001 craco start",
"start": "GENERATE_SOURCEMAP=false PORT=3004 craco start",
"build": "GENERATE_SOURCEMAP=false craco build",
"fix": "run-s fix:*",
"test": "run-s build test:*",
Expand Down
739 changes: 450 additions & 289 deletions eth-pm/pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions eth-pm/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import "@ethersproject/shims";

import React, { useEffect, useState } from "react";
import "./App.css";
import type { WakuPrivacy } from "js-waku/lib/interfaces";
import { AsymDecoder, SymDecoder } from "js-waku/lib/waku_message/version_1";
import type { WakuPrivacy } from "@waku/interfaces";
import { AsymDecoder, SymDecoder } from "@waku/message-encryption";
import { KeyPair, PublicKeyMessageEncryptionKey } from "./crypto";
import { Message } from "./messaging/Messages";
import "fontsource-roboto";
Expand Down
4 changes: 2 additions & 2 deletions eth-pm/src/BroadcastPublicKey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
PublicKeyMessageEncryptionKey,
} from "./crypto";
import { PublicKeyMessage } from "./messaging/wire";
import type { WakuPrivacy } from "js-waku/lib/interfaces";
import { SymEncoder } from "js-waku/lib/waku_message/version_1";
import type { WakuPrivacy } from "@waku/interfaces";
import { SymEncoder } from "@waku/message-encryption";
import { PublicKeyContentTopic } from "./waku";
import type { TypedDataSigner } from "@ethersproject/abstract-signer";

Expand Down
21 changes: 11 additions & 10 deletions eth-pm/src/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import "@ethersproject/shims";

import { PublicKeyMessage } from "./messaging/wire";
import { generatePrivateKey, getPublicKey, utils } from "js-waku";
import { generatePrivateKey, getPublicKey } from "@waku/message-encryption";
import { PublicKeyContentTopic } from "./waku";
import { keccak256, _TypedDataEncoder, recoverAddress } from "ethers/lib/utils";
import { equals } from "uint8arrays/equals";
import type { TypedDataSigner } from "@ethersproject/abstract-signer";
import { bytesToHex, hexToBytes, utf8ToBytes } from "@waku/byte-utils";

export const PublicKeyMessageEncryptionKey = utils.hexToBytes(
keccak256(utils.utf8ToBytes(PublicKeyContentTopic))
export const PublicKeyMessageEncryptionKey = hexToBytes(
keccak256(utf8ToBytes(PublicKeyContentTopic))
);

export interface KeyPair {
Expand Down Expand Up @@ -43,8 +44,8 @@ export async function createPublicKeyMessage(

return new PublicKeyMessage({
encryptionPublicKey: encryptionPublicKey,
ethAddress: utils.hexToBytes(address),
signature: utils.hexToBytes(signature),
ethAddress: hexToBytes(address),
signature: hexToBytes(signature),
});
}

Expand All @@ -57,7 +58,7 @@ function buildMsgParams(encryptionPublicKey: Uint8Array, fromAddress: string) {
value: {
message:
"By signing this message you certify that messages addressed to `ownerAddress` must be encrypted with `encryptionPublicKey`",
encryptionPublicKey: utils.bytesToHex(encryptionPublicKey),
encryptionPublicKey: bytesToHex(encryptionPublicKey),
ownerAddress: fromAddress,
},
// Refers to the keys of the *types* object below.
Expand Down Expand Up @@ -86,7 +87,7 @@ export async function signEncryptionKey(

console.log("TYPED SIGNED:" + JSON.stringify(result));

return utils.hexToBytes(result);
return hexToBytes(result);
}

/**
Expand All @@ -95,17 +96,17 @@ export async function signEncryptionKey(
export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean {
const { domain, types, value } = buildMsgParams(
msg.encryptionPublicKey,
"0x" + utils.bytesToHex(msg.ethAddress)
"0x" + bytesToHex(msg.ethAddress)
);

try {
const hash = _TypedDataEncoder.hash(domain, types, value);

const recovered = recoverAddress(hash, msg.signature);
console.log("Recovered", recovered);
console.log("ethAddress", "0x" + utils.bytesToHex(msg.ethAddress));
console.log("ethAddress", "0x" + bytesToHex(msg.ethAddress));

return equals(utils.hexToBytes(recovered), msg.ethAddress);
return equals(hexToBytes(recovered), msg.ethAddress);
} catch (e) {
console.error("Could not recover public key from signature", e);
return false;
Expand Down
14 changes: 7 additions & 7 deletions eth-pm/src/key_pair_handling/key_pair_storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeyPair } from "../crypto";
import { utils } from "js-waku";
import { bytesToHex, hexToBytes } from "@waku/byte-utils";

/**
* Save key pair to storage, encrypted with password
Expand All @@ -11,9 +11,9 @@ export async function saveKeyPairToStorage(
const { salt, iv, cipher } = await encryptKey(EncryptionKeyPair, password);

const data = {
salt: utils.bytesToHex(salt),
iv: utils.bytesToHex(iv),
cipher: utils.bytesToHex(new Uint8Array(cipher)),
salt: bytesToHex(salt),
iv: bytesToHex(iv),
cipher: bytesToHex(new Uint8Array(cipher)),
};

localStorage.setItem("cipherEncryptionKeyPair", JSON.stringify(data));
Expand All @@ -29,9 +29,9 @@ export async function loadKeyPairFromStorage(
if (!str) return;
const data = JSON.parse(str);

const salt = utils.hexToBytes(data.salt);
const iv = utils.hexToBytes(data.iv);
const cipher = utils.hexToBytes(data.cipher);
const salt = hexToBytes(data.salt);
const iv = hexToBytes(data.iv);
const cipher = hexToBytes(data.cipher);

return await decryptKey(salt, iv, cipher, password);
}
Expand Down
2 changes: 1 addition & 1 deletion eth-pm/src/messaging/Messaging.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Messages, { Message } from "./Messages";
import type { WakuPrivacy } from "js-waku/lib/interfaces";
import type { WakuPrivacy } from "@waku/interfaces";
import SendMessage from "./SendMessage";
import { makeStyles } from "@material-ui/core";

Expand Down
8 changes: 4 additions & 4 deletions eth-pm/src/messaging/SendMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
TextField,
} from "@material-ui/core";
import React, { ChangeEvent, useState, KeyboardEvent } from "react";
import { utils } from "js-waku";
import type { WakuPrivacy } from "js-waku/lib/interfaces";
import { AsymEncoder } from "js-waku/lib/waku_message/version_1";
import type { WakuPrivacy } from "@waku/interfaces";
import { AsymEncoder } from "@waku/message-encryption";
import { PrivateMessage } from "./wire";
import { PrivateMessageContentTopic } from "../waku";
import { hexToBytes } from "@waku/byte-utils";

const useStyles = makeStyles((theme) => ({
formControl: {
Expand Down Expand Up @@ -113,7 +113,7 @@ async function sendMessage(
callback: (res: boolean) => void
) {
const privateMessage = new PrivateMessage({
toAddress: utils.hexToBytes(recipientAddress),
toAddress: hexToBytes(recipientAddress),
message: message,
});
const payload = privateMessage.encode();
Expand Down
18 changes: 8 additions & 10 deletions eth-pm/src/waku.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Dispatch, SetStateAction } from "react";
import { Protocols, utils } from "js-waku";
import type {
Message as WakuMessage,
WakuPrivacy,
} from "js-waku/lib/interfaces";
import type { Message as WakuMessage, WakuPrivacy } from "@waku/interfaces";
import { Protocols } from "@waku/interfaces";
import { PrivateMessage, PublicKeyMessage } from "./messaging/wire";
import { validatePublicKeyMessage } from "./crypto";
import { Message } from "./messaging/Messages";
import { equals } from "uint8arrays/equals";
import { waitForRemotePeer } from "js-waku/lib/wait_for_remote_peer";
import { createPrivacyNode } from "js-waku/lib/create_waku";
import { waitForRemotePeer } from "@waku/core/lib/wait_for_remote_peer";
import { createPrivacyNode } from "@waku/create";
import { bytesToHex, hexToBytes } from "@waku/byte-utils";

export const PublicKeyContentTopic = "/eth-pm/1/public-key/proto";
export const PrivateMessageContentTopic = "/eth-pm/1/private-message/proto";
Expand All @@ -31,7 +29,7 @@ export function handlePublicKeyMessage(
if (!msg.payload) return;
const publicKeyMsg = PublicKeyMessage.decode(msg.payload);
if (!publicKeyMsg) return;
if (myAddress && equals(publicKeyMsg.ethAddress, utils.hexToBytes(myAddress)))
if (myAddress && equals(publicKeyMsg.ethAddress, hexToBytes(myAddress)))
return;

const res = validatePublicKeyMessage(publicKeyMsg);
Expand All @@ -40,7 +38,7 @@ export function handlePublicKeyMessage(
if (res) {
setter((prevPks: Map<string, Uint8Array>) => {
prevPks.set(
utils.bytesToHex(publicKeyMsg.ethAddress),
bytesToHex(publicKeyMsg.ethAddress),
publicKeyMsg.encryptionPublicKey
);
return new Map(prevPks);
Expand All @@ -60,7 +58,7 @@ export async function handlePrivateMessage(
console.log("Failed to decode Private Message");
return;
}
if (!equals(privateMessage.toAddress, utils.hexToBytes(address))) return;
if (!equals(privateMessage.toAddress, hexToBytes(address))) return;

const timestamp = wakuMsg.timestamp ? wakuMsg.timestamp : new Date();

Expand Down
8 changes: 4 additions & 4 deletions light-js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
<div id="messages"></div>

<script type='module'>
import {utils} from 'https://unpkg.com/js-waku@0.30.0/bundle/index.js';
import {createLightNode} from 'https://unpkg.com/js-waku@0.30.0/bundle/lib/create_waku.js'
import {waitForRemotePeer} from 'https://unpkg.com/js-waku@0.30.0/bundle/lib/wait_for_remote_peer.js'
import {EncoderV0, DecoderV0} from 'https://unpkg.com/js-waku@0.30.0/bundle/lib/waku_message/version_0.js'
import * as utils from 'https://unpkg.com/@waku/byte-utils@0.0.2/bundle/index.js';
import {createLightNode} from 'https://unpkg.com/@waku/create@0.0.4/bundle/index.js'
import {waitForRemotePeer} from 'https://unpkg.com/@waku/core@0.0.6/bundle/lib/wait_for_remote_peer.js'
import {EncoderV0, DecoderV0} from 'https://unpkg.com/@waku/core@0.0.6/bundle/lib/waku_message/version_0.js'

const peerIdDiv = document.getElementById('peer-id');
const remotePeerIdDiv = document.getElementById('remote-peer-id');
Expand Down
38 changes: 20 additions & 18 deletions relay-angular-chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,35 @@
},
"private": true,
"dependencies": {
"@angular/animations": "~14.2.0",
"@angular/common": "~14.2.0",
"@angular/compiler": "~14.2.0",
"@angular/core": "~14.2.0",
"@angular/forms": "~14.2.0",
"@angular/platform-browser": "~14.2.0",
"@angular/platform-browser-dynamic": "~14.2.0",
"@angular/router": "~14.2.0",
"js-waku": "0.30.0",
"protobufjs": "^7.1.0",
"rxjs": "~7.5.0",
"tslib": "^2.3.0",
"@angular/animations": "~14.2.10",
"@angular/common": "~14.2.10",
"@angular/compiler": "~14.2.10",
"@angular/core": "~14.2.10",
"@angular/forms": "~14.2.10",
"@angular/platform-browser": "~14.2.10",
"@angular/platform-browser-dynamic": "~14.2.10",
"@angular/router": "~14.2.10",
"@waku/core": "^0.0.6",
"@waku/create": "^0.0.4",
"@waku/interfaces": "^0.0.5",
"protobufjs": "^7.1.2",
"rxjs": "~7.5.7",
"tslib": "^2.4.1",
"zone.js": "~0.11.8"
},
"browser": {
"util": false
},
"devDependencies": {
"@angular-devkit/build-angular": "~14.2.1",
"@angular/cli": "~14.2.1",
"@angular/compiler-cli": "~14.2.0",
"@angular-devkit/build-angular": "~14.2.9",
"@angular/cli": "~14.2.9",
"@angular/compiler-cli": "~14.2.10",
"@types/jasmine": "~4.3.0",
"@types/node": "^17.0.21",
"@types/node": "^17.0.45",
"is-ci-cli": "^2.2.0",
"jasmine-core": "~4.3.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.1.0",
"karma": "~6.4.1",
"karma-chrome-launcher": "~3.1.1",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.0.0",
Expand Down
Loading

0 comments on commit 229e1ae

Please sign in to comment.