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'); +}