From 398f34b57f4d3a017eaef7fb2e56f2e06d1680d1 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 26 Aug 2021 15:22:42 +1000 Subject: [PATCH 1/3] Fixed Buffer.concat error when using symmetric encryption in the browser --- CHANGELOG.md | 3 +++ src/lib/waku_message/version_1.ts | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f23985dec..b419a7cdd6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - **Breaking**: Moved `startTime` and `endTime` for history queries to a `timeFilter` property as both or neither must be passed; passing only one parameter is not supported. +### Fixed +- Buffer concat error when using symmetric encryption in the browser. + ## [0.11.0] - 2021-08-20 ### Added diff --git a/src/lib/waku_message/version_1.ts b/src/lib/waku_message/version_1.ts index ca681579ef..95fa2922b5 100644 --- a/src/lib/waku_message/version_1.ts +++ b/src/lib/waku_message/version_1.ts @@ -147,7 +147,7 @@ export async function encryptSymmetric( // Returns `cipher | tag` const cipher = await symmetric.encrypt(iv, hexToBuf(key), Buffer.from(data)); - return Buffer.concat([cipher, iv]); + return Buffer.concat([cipher, Buffer.from(iv)]); } /** From 2f2eada32224671c61805d2a6a528ed95a5bae1f Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 26 Aug 2021 15:23:36 +1000 Subject: [PATCH 2/3] Log error when failing to encode Public Key Message --- examples/eth-pm/src/BroadcastPublicKey.tsx | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/eth-pm/src/BroadcastPublicKey.tsx b/examples/eth-pm/src/BroadcastPublicKey.tsx index 3b8c781439..ee31869ac0 100644 --- a/examples/eth-pm/src/BroadcastPublicKey.tsx +++ b/examples/eth-pm/src/BroadcastPublicKey.tsx @@ -35,8 +35,8 @@ export default function BroadcastPublicKey({ console.error('Failed to send Public Key Message', e); }); }) - .catch(() => { - console.log('Failed to encode Public Key Message in Waku Message'); + .catch((e) => { + console.log('Failed to encode Public Key Message in Waku Message', e); }); } else { createPublicKeyMessage( @@ -55,9 +55,10 @@ export default function BroadcastPublicKey({ console.error('Failed to send Public Key Message', e); }); }) - .catch(() => { + .catch((e) => { console.log( - 'Failed to encode Public Key Message in Waku Message' + 'Failed to encode Public Key Message in Waku Message', + e ); }); }) From 5af6d848526b28ea3fb2514b0b7f2c3d6ff605cc Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 26 Aug 2021 15:24:57 +1000 Subject: [PATCH 3/3] Encrypt Public Key Messages using symmetric encryption --- CHANGELOG.md | 3 +++ examples/eth-pm/README.md | 1 + examples/eth-pm/src/App.tsx | 5 ++++- examples/eth-pm/src/BroadcastPublicKey.tsx | 10 ++++++++-- examples/eth-pm/src/crypto.ts | 6 ++++++ 5 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b419a7cdd6..d6a5ab4f29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Examples (eth-pm): Encrypt Public Key Messages using symmetric encryption. + ### Changed - **Breaking**: Moved `startTime` and `endTime` for history queries to a `timeFilter` property as both or neither must be passed; passing only one parameter is not supported. diff --git a/examples/eth-pm/README.md b/examples/eth-pm/README.md index dfea147fc2..a1dd1918f5 100644 --- a/examples/eth-pm/README.md +++ b/examples/eth-pm/README.md @@ -7,6 +7,7 @@ - Waku Light Push - Signature with Web3 - Asymmetric Encryption +- Symmetric Encryption A PoC implementation of [20/ETH-DM](https://rfc.vac.dev/spec/20/). diff --git a/examples/eth-pm/src/App.tsx b/examples/eth-pm/src/App.tsx index 8f5a15371e..600d639d60 100644 --- a/examples/eth-pm/src/App.tsx +++ b/examples/eth-pm/src/App.tsx @@ -3,7 +3,7 @@ import '@ethersproject/shims'; import React, { useEffect, useState } from 'react'; import './App.css'; import { Waku } from 'js-waku'; -import { KeyPair } from './crypto'; +import { KeyPair, PublicKeyMessageEncryptionKey } from './crypto'; import { Message } from './messaging/Messages'; import 'fontsource-roboto'; import { AppBar, IconButton, Toolbar, Typography } from '@material-ui/core'; @@ -108,10 +108,13 @@ function App() { setPublicKeys ); + waku.relay.addDecryptionKey(PublicKeyMessageEncryptionKey); waku.relay.addObserver(observerPublicKeyMessage, [PublicKeyContentTopic]); return function cleanUp() { if (!waku) return; + + waku.relay.deleteDecryptionKey(PublicKeyMessageEncryptionKey); waku.relay.deleteObserver(observerPublicKeyMessage, [ PublicKeyContentTopic, ]); diff --git a/examples/eth-pm/src/BroadcastPublicKey.tsx b/examples/eth-pm/src/BroadcastPublicKey.tsx index ee31869ac0..22964a01b7 100644 --- a/examples/eth-pm/src/BroadcastPublicKey.tsx +++ b/examples/eth-pm/src/BroadcastPublicKey.tsx @@ -1,6 +1,10 @@ import { Button } from '@material-ui/core'; import React, { useState } from 'react'; -import { createPublicKeyMessage, KeyPair } from './crypto'; +import { + createPublicKeyMessage, + KeyPair, + PublicKeyMessageEncryptionKey, +} from './crypto'; import { PublicKeyMessage } from './messaging/wire'; import { WakuMessage, Waku } from 'js-waku'; import { PublicKeyContentTopic } from './waku'; @@ -84,5 +88,7 @@ async function encodePublicKeyWakuMessage( publicKeyMessage: PublicKeyMessage ): Promise { const payload = publicKeyMessage.encode(); - return await WakuMessage.fromBytes(payload, PublicKeyContentTopic); + return await WakuMessage.fromBytes(payload, PublicKeyContentTopic, { + symKey: PublicKeyMessageEncryptionKey, + }); } diff --git a/examples/eth-pm/src/crypto.ts b/examples/eth-pm/src/crypto.ts index 43c4205bd8..e2a54815bd 100644 --- a/examples/eth-pm/src/crypto.ts +++ b/examples/eth-pm/src/crypto.ts @@ -4,6 +4,12 @@ import { PublicKeyMessage } from './messaging/wire'; import { hexToBuf, equalByteArrays, bufToHex } from 'js-waku/lib/utils'; import { generatePrivateKey, getPublicKey } from 'js-waku'; import * as sigUtil from 'eth-sig-util'; +import { PublicKeyContentTopic } from './waku'; +import { keccak256 } from 'ethers/lib/utils'; + +export const PublicKeyMessageEncryptionKey = hexToBuf( + keccak256(Buffer.from(PublicKeyContentTopic, 'utf-8')) +); export interface KeyPair { privateKey: Uint8Array;