diff --git a/CHANGELOG.md b/CHANGELOG.md index 928b6754bf..1d9005987c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - `WakuRelay.deleteObserver` to allow removal of observers, useful when a React component add observers when mounting and needs to delete it when unmounting. - Keep alive feature that pings host regularly, reducing the chance of connections being dropped due to idle. - Can be disabled or default frequency (10s) can be changed when calling `Waku.create`. + Can be disabled or default frequency (10s) can be changed when calling `Waku.create`. +- New `lib/utils` module for easy, dependency-less hex/bytes conversions. ### Changed - **Breaking**: Auto select peer if none provided for store and light push protocols. diff --git a/examples/eth-dm/src/crypto.ts b/examples/eth-dm/src/crypto.ts index 792fb03251..1205e2d7f7 100644 --- a/examples/eth-dm/src/crypto.ts +++ b/examples/eth-dm/src/crypto.ts @@ -4,7 +4,7 @@ import * as EthCrypto from 'eth-crypto'; import { ethers } from 'ethers'; import { Signer } from '@ethersproject/abstract-signer'; import { DirectMessage, PublicKeyMessage } from './messaging/wire'; -import { byteArrayToHex, equalByteArrays, hexToBuf } from './utils'; +import { hexToBuf, equalByteArrays, bufToHex } from 'js-waku/lib/utils'; export interface KeyPair { privateKey: string; @@ -69,7 +69,7 @@ export function validatePublicKeyMessage(msg: PublicKeyMessage): boolean { */ function formatPublicKeyForSignature(ethDmPublicKey: Uint8Array): string { return JSON.stringify({ - ethDmPublicKey: byteArrayToHex(ethDmPublicKey), + ethDmPublicKey: bufToHex(ethDmPublicKey), }); } diff --git a/examples/eth-dm/src/utils.ts b/examples/eth-dm/src/utils.ts deleted file mode 100644 index 3007efb418..0000000000 --- a/examples/eth-dm/src/utils.ts +++ /dev/null @@ -1,29 +0,0 @@ -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'); -} - -export function equalByteArrays( - a: Uint8Array | Buffer | string, - b: Uint8Array | Buffer | string -): boolean { - let aBuf: Buffer; - let bBuf: Buffer; - if (typeof a === 'string') { - aBuf = hexToBuf(a); - } else { - aBuf = Buffer.from(a); - } - - if (typeof b === 'string') { - bBuf = hexToBuf(b); - } else { - bBuf = Buffer.from(b); - } - - return aBuf.compare(bBuf) === 0; -} diff --git a/examples/eth-dm/src/waku.ts b/examples/eth-dm/src/waku.ts index 59454da0ed..9681d079b8 100644 --- a/examples/eth-dm/src/waku.ts +++ b/examples/eth-dm/src/waku.ts @@ -3,7 +3,7 @@ import { getStatusFleetNodes, Waku, WakuMessage } from 'js-waku'; import { decode, DirectMessage, PublicKeyMessage } from './messaging/wire'; import { decryptMessage, validatePublicKeyMessage } from './crypto'; import { Message } from './messaging/Messages'; -import { byteArrayToHex, equalByteArrays } from './utils'; +import { bufToHex, equalByteArrays } from 'js-waku/lib/utils'; export const PublicKeyContentTopic = '/eth-dm/1/public-key/proto'; export const DirectMessageContentTopic = '/eth-dm/1/direct-message/json'; @@ -41,7 +41,7 @@ export function handlePublicKeyMessage( if (!msg.payload) return; const publicKeyMsg = PublicKeyMessage.decode(msg.payload); if (!publicKeyMsg) return; - const ethDmPublicKey = byteArrayToHex(publicKeyMsg.ethDmPublicKey); + const ethDmPublicKey = bufToHex(publicKeyMsg.ethDmPublicKey); console.log(ethDmPublicKey, myAddress); if (myAddress && equalByteArrays(publicKeyMsg.ethAddress, myAddress)) return; @@ -50,7 +50,7 @@ export function handlePublicKeyMessage( if (res) { setter((prevPks: Map) => { - prevPks.set(byteArrayToHex(publicKeyMsg.ethAddress), ethDmPublicKey); + prevPks.set(bufToHex(publicKeyMsg.ethAddress), ethDmPublicKey); return new Map(prevPks); }); } diff --git a/src/index.ts b/src/index.ts index 0c37dc1565..4e029962b1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,7 @@ export { getStatusFleetNodes, Environment, Protocol } from './lib/discover'; +export * as utils from './lib/utils'; + export { Waku } from './lib/waku'; export { WakuMessage } from './lib/waku_message'; diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 0de74f04e3..1f0d7ecb7f 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,3 +1,29 @@ export function hexToBuf(str: string): Buffer { - return Buffer.from(str.replace(/0x/i, ''), 'hex'); + return Buffer.from(str.replace(/^0x/i, ''), 'hex'); +} + +export function bufToHex(buf: Uint8Array | Buffer): string { + const _buf = Buffer.from(buf); + return _buf.toString('hex'); +} + +export function equalByteArrays( + a: Uint8Array | Buffer | string, + b: Uint8Array | Buffer | string +): boolean { + let aBuf: Buffer; + let bBuf: Buffer; + if (typeof a === 'string') { + aBuf = hexToBuf(a); + } else { + aBuf = Buffer.from(a); + } + + if (typeof b === 'string') { + bBuf = hexToBuf(b); + } else { + bBuf = Buffer.from(b); + } + + return aBuf.compare(bBuf) === 0; }