diff --git a/packages/enr/src/enr.ts b/packages/enr/src/enr.ts index 337bb06a43..8b690e66da 100644 --- a/packages/enr/src/enr.ts +++ b/packages/enr/src/enr.ts @@ -27,7 +27,7 @@ import { compressPublicKey, keccak256, verifySignature } from "./crypto.js"; import { createKeypair, createKeypairFromPeerId, - createPeerIdFromKeypair, + createPeerIdFromPublicKey, IKeypair, KeypairType, } from "./keypair/index.js"; @@ -63,8 +63,7 @@ export class ENR extends Map implements IEnr { try { const publicKey = enr.publicKey; if (publicKey) { - const keypair = createKeypair(enr.keypairType, undefined, publicKey); - enr.peerId = await createPeerIdFromKeypair(keypair); + enr.peerId = await createPeerIdFromPublicKey(publicKey); } } catch (e) { log("Could not calculate peer id for ENR", e); diff --git a/packages/enr/src/keypair/index.ts b/packages/enr/src/keypair/index.ts index addd1dad34..7f76c0465f 100644 --- a/packages/enr/src/keypair/index.ts +++ b/packages/enr/src/keypair/index.ts @@ -10,6 +10,13 @@ export const ERR_TYPE_NOT_IMPLEMENTED = "Keypair type not implemented"; export * from "./types.js"; export * from "./secp256k1.js"; +export function createPeerIdFromPublicKey( + publicKey: Uint8Array +): Promise { + const _publicKey = new supportedKeys.secp256k1.Secp256k1PublicKey(publicKey); + return peerIdFromKeys(_publicKey.bytes, undefined); +} + export function createKeypair( type: KeypairType, privateKey?: Uint8Array, @@ -23,26 +30,6 @@ export function createKeypair( } } -export async function createPeerIdFromKeypair( - keypair: IKeypair -): Promise { - switch (keypair.type) { - case KeypairType.secp256k1: { - const publicKey = new supportedKeys.secp256k1.Secp256k1PublicKey( - keypair.publicKey - ); - - const privateKey = keypair.hasPrivateKey() - ? new supportedKeys.secp256k1.Secp256k1PrivateKey(keypair.privateKey) - : undefined; - - return peerIdFromKeys(publicKey.bytes, privateKey?.bytes); - } - default: - throw new Error(ERR_TYPE_NOT_IMPLEMENTED); - } -} - export async function createKeypairFromPeerId( peerId: PeerId ): Promise {