diff --git a/packages/enr/src/enr.spec.ts b/packages/enr/src/enr.spec.ts index 6a61d9a4d2..af9c3737fb 100644 --- a/packages/enr/src/enr.spec.ts +++ b/packages/enr/src/enr.spec.ts @@ -8,7 +8,7 @@ import { equals } from "uint8arrays/equals"; import { ERR_INVALID_ID } from "./constants.js"; import { getPublicKey } from "./crypto.js"; import { ENR } from "./enr.js"; -import { getPrivateKeyFromPeerId } from "./keypair/index.js"; +import { getPrivateKeyFromPeerId } from "./peer_id.js"; describe("ENR", function () { describe("Txt codec", () => { diff --git a/packages/enr/src/enr.ts b/packages/enr/src/enr.ts index 682f7db603..5b3a71312e 100644 --- a/packages/enr/src/enr.ts +++ b/packages/enr/src/enr.ts @@ -24,15 +24,12 @@ import { MAX_RECORD_SIZE, } from "./constants.js"; import { compressPublicKey, keccak256, verifySignature } from "./crypto.js"; -import { - createKeypair, - createPeerIdFromPublicKey, - getPublicKeyFromPeerId, - IKeypair, - KeypairType, -} from "./keypair/index.js"; import { multiaddrFromFields } from "./multiaddr_from_fields.js"; import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js"; +import { + createPeerIdFromPublicKey, + getPublicKeyFromPeerId, +} from "./peer_id.js"; import * as v4 from "./v4.js"; import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js"; @@ -161,15 +158,6 @@ export class ENR extends Map implements IEnr { return bytesToUtf8(id); } - get keypairType(): KeypairType { - switch (this.id) { - case "v4": - return KeypairType.secp256k1; - default: - throw new Error(ERR_INVALID_ID); - } - } - get publicKey(): Uint8Array | undefined { switch (this.id) { case "v4": @@ -179,14 +167,6 @@ export class ENR extends Map implements IEnr { } } - get keypair(): IKeypair | undefined { - if (this.publicKey) { - const publicKey = this.publicKey; - return createKeypair(this.keypairType, undefined, publicKey); - } - return; - } - get nodeId(): NodeId | undefined { switch (this.id) { case "v4": diff --git a/packages/enr/src/index.ts b/packages/enr/src/index.ts index fc8d4c6203..45e2a3bb4e 100644 --- a/packages/enr/src/index.ts +++ b/packages/enr/src/index.ts @@ -1,5 +1,5 @@ export * from "./constants.js"; export * from "./enr.js"; -export * from "./keypair/index.js"; +export * from "./peer_id.js"; export * from "./waku2_codec.js"; export * from "./crypto.js"; diff --git a/packages/enr/src/keypair/secp256k1.ts b/packages/enr/src/keypair/secp256k1.ts deleted file mode 100644 index b658cb5421..0000000000 --- a/packages/enr/src/keypair/secp256k1.ts +++ /dev/null @@ -1,69 +0,0 @@ -import * as secp from "@noble/secp256k1"; - -import { compressPublicKey, randomBytes } from "../crypto.js"; - -import { IKeypair, KeypairType } from "./types.js"; - -export class Secp256k1Keypair implements IKeypair { - readonly type: KeypairType; - _privateKey?: Uint8Array; - readonly _publicKey?: Uint8Array; - - constructor(privateKey?: Uint8Array, publicKey?: Uint8Array) { - let pub = publicKey; - if (pub) { - pub = compressPublicKey(pub); - } - if ((this._privateKey = privateKey) && !this.privateKeyVerify()) { - throw new Error("Invalid private key"); - } - if ((this._publicKey = pub) && !this.publicKeyVerify()) { - throw new Error("Invalid public key"); - } - - this.type = KeypairType.secp256k1; - } - - static async generate(): Promise { - const privateKey = randomBytes(32); - const publicKey = secp.getPublicKey(privateKey); - return new Secp256k1Keypair(privateKey, publicKey); - } - - privateKeyVerify(key = this._privateKey): boolean { - if (key) { - return secp.utils.isValidPrivateKey(key); - } - return true; - } - - publicKeyVerify(key = this._publicKey): boolean { - if (key) { - try { - secp.Point.fromHex(key); - return true; - } catch { - return false; - } - } - return true; - } - - get privateKey(): Uint8Array { - if (!this._privateKey) { - throw new Error(); - } - return this._privateKey; - } - - get publicKey(): Uint8Array { - if (!this._publicKey) { - throw new Error(); - } - return this._publicKey; - } - - hasPrivateKey(): boolean { - return !!this._privateKey; - } -} diff --git a/packages/enr/src/keypair/types.ts b/packages/enr/src/keypair/types.ts deleted file mode 100644 index a24618dadf..0000000000 --- a/packages/enr/src/keypair/types.ts +++ /dev/null @@ -1,14 +0,0 @@ -export enum KeypairType { - rsa = 0, - ed25519 = 1, - secp256k1 = 2, -} - -export interface IKeypair { - type: KeypairType; - privateKey: Uint8Array; - publicKey: Uint8Array; - privateKeyVerify(): boolean; - publicKeyVerify(): boolean; - hasPrivateKey(): boolean; -} diff --git a/packages/enr/src/keypair/index.ts b/packages/enr/src/peer_id.ts similarity index 66% rename from packages/enr/src/keypair/index.ts rename to packages/enr/src/peer_id.ts index b656d65788..db65f14d2a 100644 --- a/packages/enr/src/keypair/index.ts +++ b/packages/enr/src/peer_id.ts @@ -3,13 +3,6 @@ import { supportedKeys } from "@libp2p/crypto/keys"; import type { PeerId } from "@libp2p/interface-peer-id"; import { peerIdFromKeys } from "@libp2p/peer-id"; -import { Secp256k1Keypair } from "./secp256k1.js"; -import { IKeypair, KeypairType } from "./types.js"; - -export const ERR_TYPE_NOT_IMPLEMENTED = "Keypair type not implemented"; -export * from "./types.js"; -export * from "./secp256k1.js"; - export function createPeerIdFromPublicKey( publicKey: Uint8Array ): Promise { @@ -17,19 +10,6 @@ export function createPeerIdFromPublicKey( return peerIdFromKeys(_publicKey.bytes, undefined); } -export function createKeypair( - type: KeypairType, - privateKey?: Uint8Array, - publicKey?: Uint8Array -): IKeypair { - switch (type) { - case KeypairType.secp256k1: - return new Secp256k1Keypair(privateKey, publicKey); - default: - throw new Error(ERR_TYPE_NOT_IMPLEMENTED); - } -} - export function getPublicKeyFromPeerId(peerId: PeerId): Uint8Array { if (peerId.type !== "secp256k1") { throw new Error("Unsupported peer id type");