mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-02-27 03:23:20 +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
37 lines
1000 B
TypeScript
37 lines
1000 B
TypeScript
import type { PeerId } from "@libp2p/interface/peer-id";
|
|
import type { ENRKey, ENRValue } from "@waku/interfaces";
|
|
import { utf8ToBytes } from "@waku/utils/bytes";
|
|
|
|
import { compressPublicKey } from "./crypto.js";
|
|
import { ENR } from "./enr.js";
|
|
import { getPublicKeyFromPeerId } from "./peer_id.js";
|
|
|
|
export class EnrCreator {
|
|
static fromPublicKey(
|
|
publicKey: Uint8Array,
|
|
kvs: Record<ENRKey, ENRValue> = {}
|
|
): Promise<ENR> {
|
|
// EIP-778 specifies that the key must be in compressed format, 33 bytes
|
|
if (publicKey.length !== 33) {
|
|
publicKey = compressPublicKey(publicKey);
|
|
}
|
|
return ENR.create({
|
|
...kvs,
|
|
id: utf8ToBytes("v4"),
|
|
secp256k1: publicKey
|
|
});
|
|
}
|
|
|
|
static async fromPeerId(
|
|
peerId: PeerId,
|
|
kvs: Record<ENRKey, ENRValue> = {}
|
|
): Promise<ENR> {
|
|
switch (peerId.type) {
|
|
case "secp256k1":
|
|
return EnrCreator.fromPublicKey(getPublicKeyFromPeerId(peerId), kvs);
|
|
default:
|
|
throw new Error();
|
|
}
|
|
}
|
|
}
|