Danish Arora 87717981eb
chore: upgrade libp2p and related deps (#1482)
* chore: update noise

* update: package.lock

* update: @chainsafe/libp2p-gossipsub

* rm unwanted libp2p interface deps & bump up libp2p

* refactor code for new deps

* update: new package.lock

* setup prettier, refactor eslint  and rm trailing commas

* update package.lock

* fix build

* import type for interface

* fix imports for merge

* update typedoc exports

* add: CustomEvent import

* use new libp2p interface

* add aegir as dev dep for tests
2023-08-16 20:18:13 +05:30

35 lines
1.1 KiB
TypeScript

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";
export function createPeerIdFromPublicKey(
publicKey: Uint8Array
): Promise<PeerId> {
const _publicKey = new supportedKeys.secp256k1.Secp256k1PublicKey(publicKey);
return peerIdFromKeys(_publicKey.bytes, undefined);
}
export function getPublicKeyFromPeerId(peerId: PeerId): Uint8Array {
if (peerId.type !== "secp256k1") {
throw new Error("Unsupported peer id type");
}
return unmarshalPublicKey(peerId.publicKey).marshal();
}
// Only used in tests
export async function getPrivateKeyFromPeerId(
peerId: PeerId
): Promise<Uint8Array> {
if (peerId.type !== "secp256k1") {
throw new Error("Unsupported peer id type");
}
if (!peerId.privateKey) {
throw new Error("Private key not present on peer id");
}
const privateKey = await unmarshalPrivateKey(peerId.privateKey);
return privateKey.marshal();
}