From b93c87604398d5fea76280a596b39f3959345469 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Fri, 20 May 2022 11:42:01 +1000 Subject: [PATCH] Consolidate compress public key functions --- src/lib/crypto.ts | 8 ++++++++ src/lib/enr/enr.ts | 2 +- src/lib/enr/keypair/secp256k1.ts | 29 ++--------------------------- src/lib/enr/v4.ts | 5 ----- 4 files changed, 11 insertions(+), 33 deletions(-) diff --git a/src/lib/crypto.ts b/src/lib/crypto.ts index d37a8ab7ae..32a330e278 100644 --- a/src/lib/crypto.ts +++ b/src/lib/crypto.ts @@ -72,3 +72,11 @@ export async function sign( export function keccak256(input: Uint8Array): Uint8Array { return new Uint8Array(sha3.keccak256.arrayBuffer(input)); } + +export function compressPublicKey(publicKey: Uint8Array): Uint8Array { + if (publicKey.length === 64) { + publicKey = concat([[4], publicKey], 65); + } + const point = secp.Point.fromHex(publicKey); + return point.toRawBytes(true); +} diff --git a/src/lib/enr/enr.ts b/src/lib/enr/enr.ts index 517ad530b7..e0632b83b8 100644 --- a/src/lib/enr/enr.ts +++ b/src/lib/enr/enr.ts @@ -9,6 +9,7 @@ import { fromString } from "uint8arrays/from-string"; import { toString } from "uint8arrays/to-string"; import { encode as varintEncode } from "varint"; +import { compressPublicKey } from "../crypto"; import { bytesToHex, bytesToUtf8, hexToBytes, utf8ToBytes } from "../utils"; import { ERR_INVALID_ID, ERR_NO_SIGNATURE, MAX_RECORD_SIZE } from "./constants"; @@ -22,7 +23,6 @@ import { import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec"; import { ENRKey, ENRValue, NodeId, SequenceNumber } from "./types"; import * as v4 from "./v4"; -import { compressPublicKey } from "./v4"; import { decodeWaku2, encodeWaku2, Waku2 } from "./waku2_codec"; const dbg = debug("waku:enr"); diff --git a/src/lib/enr/keypair/secp256k1.ts b/src/lib/enr/keypair/secp256k1.ts index 7abef54d98..8c6a83448e 100644 --- a/src/lib/enr/keypair/secp256k1.ts +++ b/src/lib/enr/keypair/secp256k1.ts @@ -1,34 +1,9 @@ import * as secp from "@noble/secp256k1"; -import { concat } from "uint8arrays/concat"; -import { randomBytes } from "../../crypto"; +import { compressPublicKey, randomBytes } from "../../crypto"; import { IKeypair, IKeypairClass, KeypairType } from "./types"; -export function secp256k1PublicKeyToCompressed( - publicKey: Uint8Array -): Uint8Array { - if (publicKey.length === 64) { - publicKey = concat([[4], publicKey], 65); - } - const point = secp.Point.fromHex(publicKey); - return point.toRawBytes(true); -} - -export function secp256k1PublicKeyToFull(publicKey: Uint8Array): Uint8Array { - if (publicKey.length === 64) { - publicKey = concat([[4], publicKey], 65); - } - const point = secp.Point.fromHex(publicKey); - - return point.toRawBytes(false); -} - -export function secp256k1PublicKeyToRaw(publicKey: Uint8Array): Uint8Array { - const point = secp.Point.fromHex(publicKey); - return point.toRawBytes(false).slice(1); -} - export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair implements IKeypair { @@ -39,7 +14,7 @@ export const Secp256k1Keypair: IKeypairClass = class Secp256k1Keypair constructor(privateKey?: Uint8Array, publicKey?: Uint8Array) { let pub = publicKey; if (pub) { - pub = secp256k1PublicKeyToCompressed(pub); + pub = compressPublicKey(pub); } if ((this._privateKey = privateKey) && !this.privateKeyVerify()) { throw new Error("Invalid private key"); diff --git a/src/lib/enr/v4.ts b/src/lib/enr/v4.ts index 06afaa0320..6555de58ba 100644 --- a/src/lib/enr/v4.ts +++ b/src/lib/enr/v4.ts @@ -5,11 +5,6 @@ import { bytesToHex } from "../utils"; import { NodeId } from "./types"; -export function compressPublicKey(publicKey: Uint8Array): Uint8Array { - const point = secp.Point.fromHex(bytesToHex(publicKey)); - return point.toRawBytes(true); -} - export async function sign( privKey: Uint8Array, msg: Uint8Array