2024-01-15 16:12:01 -08:00
|
|
|
import type { PeerId } from "@libp2p/interface";
|
2023-03-03 13:34:04 +11:00
|
|
|
import type { ENRKey, ENRValue } from "@waku/interfaces";
|
2023-03-14 10:10:38 +05:30
|
|
|
import { utf8ToBytes } from "@waku/utils/bytes";
|
2023-03-03 13:34:04 +11:00
|
|
|
|
|
|
|
|
import { compressPublicKey } from "./crypto.js";
|
|
|
|
|
import { ENR } from "./enr.js";
|
|
|
|
|
|
|
|
|
|
export class EnrCreator {
|
2024-07-19 15:58:17 +05:30
|
|
|
public static fromPublicKey(
|
2023-03-03 13:34:04 +11:00
|
|
|
publicKey: Uint8Array,
|
2023-08-16 20:18:13 +05:30
|
|
|
kvs: Record<ENRKey, ENRValue> = {}
|
2024-10-21 16:43:24 +05:30
|
|
|
): ENR {
|
2023-03-03 13:34:04 +11:00
|
|
|
// 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"),
|
2023-08-16 20:18:13 +05:30
|
|
|
secp256k1: publicKey
|
2023-03-03 13:34:04 +11:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-19 15:58:17 +05:30
|
|
|
public static async fromPeerId(
|
2023-03-03 13:34:04 +11:00
|
|
|
peerId: PeerId,
|
2023-08-16 20:18:13 +05:30
|
|
|
kvs: Record<ENRKey, ENRValue> = {}
|
2023-03-03 13:34:04 +11:00
|
|
|
): Promise<ENR> {
|
|
|
|
|
switch (peerId.type) {
|
|
|
|
|
case "secp256k1":
|
2024-10-21 16:43:24 +05:30
|
|
|
return EnrCreator.fromPublicKey(peerId.publicKey.raw, kvs);
|
2023-03-03 13:34:04 +11:00
|
|
|
default:
|
|
|
|
|
throw new Error();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|