From c4d779d1db08b681995b8be9c936a2a3ff4d0cce Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 25 Mar 2022 11:59:14 +1100 Subject: [PATCH] Remove `equalByteArrays` --- CHANGELOG.md | 7 +++++- .../eth-pm-wallet-encryption/src/crypto.ts | 5 ++-- examples/eth-pm-wallet-encryption/src/waku.ts | 8 ++++--- examples/eth-pm/src/crypto.ts | 5 ++-- examples/eth-pm/src/waku.ts | 8 ++++--- src/lib/utils.ts | 24 ------------------- 6 files changed, 22 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4aa1bcd93d..cf45c54830 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed -- Replace Base 64 buggy conversion functions with `uint8arrays`. +- Replace Base 64 buggy conversion functions with `uint8arrays`. + +### Removed + +- **Breaking**: Removed `equalByteArrays`, use `uint8arrays/equals` instead. + See changes in `eth-pm` example. ## [0.19.2] - 2022-03-21 diff --git a/examples/eth-pm-wallet-encryption/src/crypto.ts b/examples/eth-pm-wallet-encryption/src/crypto.ts index 2d23b57da2..23a791ff40 100644 --- a/examples/eth-pm-wallet-encryption/src/crypto.ts +++ b/examples/eth-pm-wallet-encryption/src/crypto.ts @@ -1,8 +1,9 @@ import "@ethersproject/shims"; import { PublicKeyMessage } from "./messaging/wire"; -import { hexToBytes, equalByteArrays, bytesToHex } from "js-waku/lib/utils"; +import { hexToBytes, bytesToHex } from "js-waku/lib/utils"; import * as sigUtil from "eth-sig-util"; +import { equals } from "uint8arrays/equals"; /** * Sign the encryption public key with Web3. This can then be published to let other @@ -97,5 +98,5 @@ export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean { console.log("Recovered", recovered); console.log("ethAddress", "0x" + bytesToHex(msg.ethAddress)); - return equalByteArrays(recovered, msg.ethAddress); + return equals(hexToBytes(recovered), msg.ethAddress); } diff --git a/examples/eth-pm-wallet-encryption/src/waku.ts b/examples/eth-pm-wallet-encryption/src/waku.ts index 7297ab27f2..fefe4ee60d 100644 --- a/examples/eth-pm-wallet-encryption/src/waku.ts +++ b/examples/eth-pm-wallet-encryption/src/waku.ts @@ -3,7 +3,8 @@ import { Waku, WakuMessage } from "js-waku"; import { PrivateMessage, PublicKeyMessage } from "./messaging/wire"; import { validatePublicKeyMessage } from "./crypto"; import { Message } from "./messaging/Messages"; -import { bytesToHex, equalByteArrays } from "js-waku/lib/utils"; +import { bytesToHex, hexToBytes } from "js-waku/lib/utils"; +import { equals } from "uint8arrays/equals"; export const PublicKeyContentTopic = "/eth-pm-wallet/1/encryption-public-key/proto"; @@ -36,7 +37,8 @@ export function handlePublicKeyMessage( if (!msg.payload) return; const publicKeyMsg = PublicKeyMessage.decode(msg.payload); if (!publicKeyMsg) return; - if (myAddress && equalByteArrays(publicKeyMsg.ethAddress, myAddress)) return; + if (myAddress && equals(publicKeyMsg.ethAddress, hexToBytes(myAddress))) + return; const res = validatePublicKeyMessage(publicKeyMsg); console.log("Is Public Key Message valid?", res); @@ -77,7 +79,7 @@ export async function handlePrivateMessage( console.log("Failed to decode Private Message"); return; } - if (!equalByteArrays(privateMessage.toAddress, address)) return; + if (!equals(privateMessage.toAddress, hexToBytes(address))) return; const timestamp = wakuMsg.timestamp ? wakuMsg.timestamp : new Date(); diff --git a/examples/eth-pm/src/crypto.ts b/examples/eth-pm/src/crypto.ts index 18c42cb714..375da92f90 100644 --- a/examples/eth-pm/src/crypto.ts +++ b/examples/eth-pm/src/crypto.ts @@ -1,11 +1,12 @@ import "@ethersproject/shims"; import { PublicKeyMessage } from "./messaging/wire"; -import { hexToBytes, equalByteArrays, bytesToHex } from "js-waku/lib/utils"; +import { hexToBytes, bytesToHex } 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"; +import { equals } from "uint8arrays/equals"; export const PublicKeyMessageEncryptionKey = hexToBytes( keccak256(Buffer.from(PublicKeyContentTopic, "utf-8")) @@ -118,5 +119,5 @@ export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean { console.log("Recovered", recovered); console.log("ethAddress", "0x" + bytesToHex(msg.ethAddress)); - return equalByteArrays(recovered, msg.ethAddress); + return equals(hexToBytes(recovered), msg.ethAddress); } diff --git a/examples/eth-pm/src/waku.ts b/examples/eth-pm/src/waku.ts index b602f19900..cad6a9c41f 100644 --- a/examples/eth-pm/src/waku.ts +++ b/examples/eth-pm/src/waku.ts @@ -3,7 +3,8 @@ import { Waku, WakuMessage } from "js-waku"; import { PrivateMessage, PublicKeyMessage } from "./messaging/wire"; import { validatePublicKeyMessage } from "./crypto"; import { Message } from "./messaging/Messages"; -import { bytesToHex, equalByteArrays } from "js-waku/lib/utils"; +import { bytesToHex, hexToBytes } from "js-waku/lib/utils"; +import { equals } from "uint8arrays/equals"; export const PublicKeyContentTopic = "/eth-pm/1/public-key/proto"; export const PrivateMessageContentTopic = "/eth-pm/1/private-message/proto"; @@ -34,7 +35,8 @@ export function handlePublicKeyMessage( if (!msg.payload) return; const publicKeyMsg = PublicKeyMessage.decode(msg.payload); if (!publicKeyMsg) return; - if (myAddress && equalByteArrays(publicKeyMsg.ethAddress, myAddress)) return; + if (myAddress && equals(publicKeyMsg.ethAddress, hexToBytes(myAddress))) + return; const res = validatePublicKeyMessage(publicKeyMsg); console.log("Is Public Key Message valid?", res); @@ -62,7 +64,7 @@ export async function handlePrivateMessage( console.log("Failed to decode Private Message"); return; } - if (!equalByteArrays(privateMessage.toAddress, address)) return; + if (!equals(privateMessage.toAddress, hexToBytes(address))) return; const timestamp = wakuMsg.timestamp ? wakuMsg.timestamp : new Date(); diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 21934486e7..6c3a1c1540 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -51,30 +51,6 @@ export function bytesToHex(bytes: Uint8Array): string { return hex.join(""); } -/** - * Compare both inputs, return true if they represent the same byte array. - */ -export function equalByteArrays( - a: Uint8Array | string, - b: Uint8Array | string -): boolean { - let _a: string; - let _b: string; - if (typeof a === "string") { - _a = a.replace(/^0x/i, "").toLowerCase(); - } else { - _a = bytesToHex(a); - } - - if (typeof b === "string") { - _b = b.replace(/^0x/i, "").toLowerCase(); - } else { - _b = bytesToHex(b); - } - - return _a === _b; -} - /** * Return Keccak-256 of the input. */