Remove equalByteArrays

This commit is contained in:
Franck Royer 2022-03-25 11:59:14 +11:00
parent 7410e83798
commit c4d779d1db
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
6 changed files with 22 additions and 35 deletions

View File

@ -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

View File

@ -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);
}

View File

@ -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();

View File

@ -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);
}

View File

@ -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();

View File

@ -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.
*/