diff --git a/src/lib/crypto.ts b/src/lib/crypto.ts index cc28642942..ffd98757a0 100644 --- a/src/lib/crypto.ts +++ b/src/lib/crypto.ts @@ -1,6 +1,7 @@ import nodeCrypto from "crypto"; import * as secp from "@noble/secp256k1"; +import * as sha3 from "js-sha3"; import * as symmetric from "./waku_message/symmetric"; import { PrivateKeySize } from "./waku_message/version_1"; @@ -47,3 +48,7 @@ export function generateSymmetricKey(): Uint8Array { * encryption. */ export const getPublicKey = secp.getPublicKey; + +export function keccak256(input: Uint8Array): Uint8Array { + return new Uint8Array(sha3.keccak256.arrayBuffer(input)); +} diff --git a/src/lib/enr/v4.ts b/src/lib/enr/v4.ts index ad6b7920cc..4fd640df0d 100644 --- a/src/lib/enr/v4.ts +++ b/src/lib/enr/v4.ts @@ -1,16 +1,11 @@ import * as secp from "@noble/secp256k1"; -import { keccak256 } from "js-sha3"; -import { randomBytes } from "../crypto"; +import { keccak256, randomBytes } from "../crypto"; import { bytesToHex } from "../utils"; import { createNodeId } from "./create"; import { NodeId } from "./types"; -export function hash(input: Uint8Array): Uint8Array { - return new Uint8Array(keccak256.arrayBuffer(input)); -} - export function createPrivateKey(): Uint8Array { return randomBytes(32); } @@ -28,7 +23,7 @@ export async function sign( privKey: Uint8Array, msg: Uint8Array ): Promise { - return secp.sign(hash(msg), privKey, { + return secp.sign(keccak256(msg), privKey, { der: false, }); } @@ -40,7 +35,7 @@ export function verify( ): boolean { try { const _sig = secp.Signature.fromCompact(sig.slice(0, 64)); - return secp.verify(_sig, hash(msg), pubKey); + return secp.verify(_sig, keccak256(msg), pubKey); } catch { return false; } @@ -50,7 +45,7 @@ export function nodeId(pubKey: Uint8Array): NodeId { const publicKey = secp.Point.fromHex(pubKey); const uncompressedPubkey = publicKey.toRawBytes(false); - return createNodeId(hash(uncompressedPubkey.slice(1))); + return createNodeId(keccak256(uncompressedPubkey.slice(1))); } export class ENRKeyPair {