From a10d3ae7d3183a4cede1b2fb0a4e82988cd341ac Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 23 Jun 2022 16:23:27 +1000 Subject: [PATCH] fix: peer id / keypair conversion --- src/lib/enr/keypair/index.ts | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/lib/enr/keypair/index.ts b/src/lib/enr/keypair/index.ts index 41e88652b5..71be1d62ab 100644 --- a/src/lib/enr/keypair/index.ts +++ b/src/lib/enr/keypair/index.ts @@ -1,9 +1,5 @@ -import { - marshalPrivateKey, - marshalPublicKey, - unmarshalPrivateKey, - unmarshalPublicKey, -} from "@libp2p/crypto/keys"; +import { unmarshalPrivateKey, unmarshalPublicKey } from "@libp2p/crypto/keys"; +import { supportedKeys } from "@libp2p/crypto/keys"; import type { PeerId } from "@libp2p/interface-peer-id"; import { peerIdFromKeys } from "@libp2p/peer-id"; @@ -42,15 +38,15 @@ export async function createPeerIdFromKeypair( ): Promise { switch (keypair.type) { case KeypairType.secp256k1: { - // manually create a peer id to avoid expensive ops + const publicKey = new supportedKeys.secp256k1.Secp256k1PublicKey( + keypair.publicKey + ); + const privateKey = keypair.hasPrivateKey() - ? marshalPrivateKey({ bytes: keypair.privateKey }) + ? new supportedKeys.secp256k1.Secp256k1PrivateKey(keypair.privateKey) : undefined; - return peerIdFromKeys( - marshalPublicKey({ bytes: keypair.publicKey }), - privateKey - ); + return peerIdFromKeys(publicKey.bytes, privateKey?.bytes); } default: throw new Error(ERR_TYPE_NOT_IMPLEMENTED);