mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-01-25 19:33:08 +00:00
* 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
35 lines
1.1 KiB
TypeScript
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();
|
|
}
|