From 5c3f768c83c7b54854720b6b5e13a0dcfa0e51a8 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 2 Jul 2021 13:27:50 +1000 Subject: [PATCH 1/6] Do not crash if Ethereum is locked --- examples/eth-dm/src/App.tsx | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/examples/eth-dm/src/App.tsx b/examples/eth-dm/src/App.tsx index 12590f370e..82ac3b7ff9 100644 --- a/examples/eth-dm/src/App.tsx +++ b/examples/eth-dm/src/App.tsx @@ -4,7 +4,7 @@ import React, { useEffect, useState } from 'react'; import './App.css'; import { Waku } from 'js-waku'; import { ethers } from 'ethers'; -import { Web3Provider } from '@ethersproject/providers'; +import { Signer } from '@ethersproject/abstract-signer'; import { KeyPair } from './crypto'; import { Message } from './messaging/Messages'; import 'fontsource-roboto'; @@ -57,7 +57,7 @@ const useStyles = makeStyles({ function App() { const [waku, setWaku] = useState(); - const [provider, setProvider] = useState(); + const [signer, setSigner] = useState(); const [ethDmKeyPair, setEthDmKeyPair] = useState(); const [publicKeys, setPublicKeys] = useState>(new Map()); const [messages, setMessages] = useState([]); @@ -66,22 +66,18 @@ function App() { const classes = useStyles(); useEffect(() => { - if (provider) return; try { - window.ethereum.request({ method: 'eth_requestAccounts' }); - const _provider = new ethers.providers.Web3Provider(window.ethereum); - setProvider(_provider); + window.ethereum + .request({ method: 'eth_requestAccounts' }) + .then((accounts: string[]) => { + const _provider = new ethers.providers.Web3Provider(window.ethereum); + setAddress(accounts[0]); + setSigner(_provider.getSigner()); + }); } catch (e) { console.error('No web3 provider available'); } - }, [provider]); - - useEffect(() => { - provider - ?.getSigner() - .getAddress() - .then((address) => setAddress(address)); - }); + }, [address, signer]); let peers; if (waku) { @@ -125,7 +121,7 @@ function App() { setEthDmKeyPair={(keyPair) => setEthDmKeyPair(keyPair)} /> From a4dd8771f620814bf4022540d3f8ed3fde9a367b Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 2 Jul 2021 13:45:22 +1000 Subject: [PATCH 2/6] re-arrange app bar and display address in use --- examples/eth-dm/src/App.tsx | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/examples/eth-dm/src/App.tsx b/examples/eth-dm/src/App.tsx index 82ac3b7ff9..1d0de0594a 100644 --- a/examples/eth-dm/src/App.tsx +++ b/examples/eth-dm/src/App.tsx @@ -52,7 +52,13 @@ const useStyles = makeStyles({ flex: 1, margin: '10px', }, - wakuStatus: {}, + wakuStatus: { + marginRight: theme.spacing(2), + }, + title: { + flexGrow: 1, + }, + peers: {}, }); function App() { @@ -79,19 +85,24 @@ function App() { } }, [address, signer]); - let peers; + let peers = 0; if (waku) { peers = waku.libp2p.connectionManager.connections.size; } + let addressDisplay = ''; + if (address) { + addressDisplay = + address.substr(0, 6) + '...' + address.substr(address.length - 4, 4); + } + return (
- Ethereum Direct Message @@ -100,7 +111,13 @@ function App() { style={waku ? { color: green[500] } : {}} /> - {peers} + + {peers} peer{peers && peers > 1 ? 's' : ''} + + + Ethereum Direct Message + + {addressDisplay} From c5419630fc1959652d68fa14cedf7415739d8232 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 2 Jul 2021 13:52:10 +1000 Subject: [PATCH 3/6] Define util function for hex<>buf conversions --- examples/eth-dm/src/InitWaku.tsx | 12 ++++-------- examples/eth-dm/src/crypto.ts | 23 +++++++---------------- examples/eth-dm/src/messaging/wire.ts | 5 ----- examples/eth-dm/src/utils.ts | 8 ++++++++ 4 files changed, 19 insertions(+), 29 deletions(-) create mode 100644 examples/eth-dm/src/utils.ts diff --git a/examples/eth-dm/src/InitWaku.tsx b/examples/eth-dm/src/InitWaku.tsx index da105ff848..4427d46c7d 100644 --- a/examples/eth-dm/src/InitWaku.tsx +++ b/examples/eth-dm/src/InitWaku.tsx @@ -1,13 +1,9 @@ import { Dispatch, SetStateAction, useEffect } from 'react'; import { Environment, getStatusFleetNodes, Waku, WakuMessage } from 'js-waku'; -import { - bytesToHexStr, - decode, - DirectMessage, - PublicKeyMessage, -} from './messaging/wire'; +import { decode, DirectMessage, PublicKeyMessage } from './messaging/wire'; import { decryptMessage, KeyPair, validatePublicKeyMessage } from './crypto'; import { Message } from './messaging/Messages'; +import { byteArrayToHex } from './utils'; export const PublicKeyContentTopic = '/eth-dm/1/public-key/proto'; export const DirectMessageContentTopic = '/eth-dm/1/direct-message/json'; @@ -123,7 +119,7 @@ function handlePublicKeyMessage( if (!msg.payload) return; const publicKeyMsg = PublicKeyMessage.decode(msg.payload); if (!publicKeyMsg) return; - const ethDmPublicKey = bytesToHexStr(publicKeyMsg.ethDmPublicKey); + const ethDmPublicKey = byteArrayToHex(publicKeyMsg.ethDmPublicKey); if (ethDmPublicKey === myPublicKey) return; const res = validatePublicKeyMessage(publicKeyMsg); @@ -131,7 +127,7 @@ function handlePublicKeyMessage( if (res) { setter((prevPks: Map) => { - prevPks.set(bytesToHexStr(publicKeyMsg.ethAddress), ethDmPublicKey); + prevPks.set(byteArrayToHex(publicKeyMsg.ethAddress), ethDmPublicKey); return new Map(prevPks); }); } diff --git a/examples/eth-dm/src/crypto.ts b/examples/eth-dm/src/crypto.ts index 0b72b88f69..1dd7c7b0f8 100644 --- a/examples/eth-dm/src/crypto.ts +++ b/examples/eth-dm/src/crypto.ts @@ -3,11 +3,8 @@ import '@ethersproject/shims'; import * as EthCrypto from 'eth-crypto'; import { ethers } from 'ethers'; import { Signer } from '@ethersproject/abstract-signer'; -import { - bytesToHexStr, - DirectMessage, - PublicKeyMessage, -} from './messaging/wire'; +import { DirectMessage, PublicKeyMessage } from './messaging/wire'; +import { byteArrayToHex, hexToBuf } from './utils'; export interface KeyPair { privateKey: string; @@ -33,21 +30,15 @@ export async function createPublicKeyMessage( ethDmPublicKey: string ): Promise { const ethAddress = await web3Signer.getAddress(); - const bytesEthDmPublicKey = Buffer.from( - ethDmPublicKey.replace(/0x/, ''), - 'hex' - ); + const bytesEthDmPublicKey = hexToBuf(ethDmPublicKey); const signature = await web3Signer.signMessage( formatPublicKeyForSignature(bytesEthDmPublicKey) ); - const bytesEthAddress = Buffer.from(ethAddress.replace(/0x/, ''), 'hex'); - const bytesSignature = Buffer.from(signature.replace(/0x/, ''), 'hex'); - return new PublicKeyMessage({ ethDmPublicKey: bytesEthDmPublicKey, - ethAddress: bytesEthAddress, - signature: bytesSignature, + ethAddress: hexToBuf(ethAddress), + signature: hexToBuf(signature), }); } @@ -58,7 +49,7 @@ export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean { const formattedMsg = formatPublicKeyForSignature(msg.ethDmPublicKey); try { const sigAddress = ethers.utils.verifyMessage(formattedMsg, msg.signature); - const sigAddressBytes = Buffer.from(sigAddress.replace(/0x/, ''), 'hex'); + const sigAddressBytes = hexToBuf(sigAddress); // Compare the actual byte arrays instead of strings that may differ in casing or prefixing. const cmp = sigAddressBytes.compare(new Buffer(msg.ethAddress)); console.log( @@ -86,7 +77,7 @@ export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean { */ function formatPublicKeyForSignature(ethDmPublicKey: Uint8Array): string { return JSON.stringify({ - ethDmPublicKey: bytesToHexStr(ethDmPublicKey), + ethDmPublicKey: byteArrayToHex(ethDmPublicKey), }); } diff --git a/examples/eth-dm/src/messaging/wire.ts b/examples/eth-dm/src/messaging/wire.ts index d595ea978c..5a07803416 100644 --- a/examples/eth-dm/src/messaging/wire.ts +++ b/examples/eth-dm/src/messaging/wire.ts @@ -56,11 +56,6 @@ export class PublicKeyMessage { } } -export function bytesToHexStr(bytes: Uint8Array): string { - const buf = new Buffer(bytes); - return buf.toString('hex'); -} - /** * Direct Encrypted Message used for private communication over the Waku network. */ diff --git a/examples/eth-dm/src/utils.ts b/examples/eth-dm/src/utils.ts new file mode 100644 index 0000000000..b1f36ac825 --- /dev/null +++ b/examples/eth-dm/src/utils.ts @@ -0,0 +1,8 @@ +export function byteArrayToHex(bytes: Uint8Array): string { + const buf = new Buffer(bytes); + return buf.toString('hex'); +} + +export function hexToBuf(str: string): Buffer { + return Buffer.from(str.replace(/0x/, ''), 'hex'); +} From 354dcd66a53cd303e30176bb5ae82917befce12b Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 2 Jul 2021 13:52:50 +1000 Subject: [PATCH 4/6] Constructor is more appropriate when comparing function signatures --- src/lib/waku_relay/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/waku_relay/index.ts b/src/lib/waku_relay/index.ts index f5d5266d0e..784935e918 100644 --- a/src/lib/waku_relay/index.ts +++ b/src/lib/waku_relay/index.ts @@ -114,7 +114,7 @@ export class WakuRelay extends Gossipsub { */ public async send(message: WakuMessage): Promise { const msg = message.encode(); - await super.publish(this.pubsubTopic, Buffer.from(msg)); + await super.publish(this.pubsubTopic, new Buffer(msg)); } /** From 707979bf4ca6dba9be63dc35b25961d129c1c06c Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 2 Jul 2021 15:28:26 +1000 Subject: [PATCH 5/6] Only use Prod nodes js-waku maintains compatibility with latest nim-waku release, deployed in the prod fleet. js-waku may not be compatible with nim-waku master branch if backward compatible changes were introduced. --- examples/eth-dm/src/InitWaku.tsx | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/examples/eth-dm/src/InitWaku.tsx b/examples/eth-dm/src/InitWaku.tsx index 4427d46c7d..1a90ec3a63 100644 --- a/examples/eth-dm/src/InitWaku.tsx +++ b/examples/eth-dm/src/InitWaku.tsx @@ -102,12 +102,7 @@ async function initWaku(): Promise { } function getNodes() { - // Works with react-scripts - if (process?.env?.NODE_ENV === 'development') { - return getStatusFleetNodes(Environment.Test); - } else { - return getStatusFleetNodes(Environment.Prod); - } + return getStatusFleetNodes(Environment.Prod); } function handlePublicKeyMessage( From aaf3b1867e844de9d2b2b556f57645b832a14087 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 2 Jul 2021 16:08:29 +1000 Subject: [PATCH 6/6] Disable keep alive by default as latest nim-waku release does not support ping protocol. --- CHANGELOG.md | 1 + src/lib/waku.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e844c4308c..700e300483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - **Breaking**: Auto select peer if none provided for store and light push protocols. - Upgrade to `libp2p@0.31.7` and `libp2p-gossipsub@0.10.0` to avoid `TextEncoder` errors in ReactJS tests. +- Disable keep alive by default as latest nim-waku release does not support ping protocol. ### Fixed - Disable `keepAlive` if set to `0`. diff --git a/src/lib/waku.ts b/src/lib/waku.ts index 093192217f..92479dc842 100644 --- a/src/lib/waku.ts +++ b/src/lib/waku.ts @@ -76,7 +76,7 @@ export class Waku { this.lightPush = lightPush; this.keepAliveTimers = {}; - const keepAlive = options.keepAlive !== undefined ? options.keepAlive : 10; + const keepAlive = options.keepAlive !== undefined ? options.keepAlive : 0; if (keepAlive !== 0) { libp2p.connectionManager.on('peer:connect', (connection: Connection) => {