@waku/enr 0.0.4 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/CHANGELOG.md +46 -2
  2. package/bundle/index.js +5367 -5359
  3. package/dist/.tsbuildinfo +1 -0
  4. package/dist/creator.d.ts +7 -0
  5. package/dist/creator.js +26 -0
  6. package/dist/creator.js.map +1 -0
  7. package/dist/crypto.d.ts +0 -7
  8. package/dist/crypto.js +1 -7
  9. package/dist/crypto.js.map +1 -1
  10. package/dist/decoder.d.ts +5 -0
  11. package/dist/decoder.js +62 -0
  12. package/dist/decoder.js.map +1 -0
  13. package/dist/encoder.d.ts +7 -0
  14. package/dist/encoder.js +38 -0
  15. package/dist/encoder.js.map +1 -0
  16. package/dist/enr.d.ts +19 -68
  17. package/dist/enr.js +43 -336
  18. package/dist/enr.js.map +1 -1
  19. package/dist/get_multiaddr.d.ts +3 -0
  20. package/dist/get_multiaddr.js +31 -0
  21. package/dist/get_multiaddr.js.map +1 -0
  22. package/dist/index.d.ts +3 -1
  23. package/dist/index.js +3 -1
  24. package/dist/index.js.map +1 -1
  25. package/dist/peer_id.d.ts +4 -0
  26. package/dist/peer_id.js +25 -0
  27. package/dist/peer_id.js.map +1 -0
  28. package/dist/raw_enr.d.ts +56 -0
  29. package/dist/raw_enr.js +142 -0
  30. package/dist/raw_enr.js.map +1 -0
  31. package/dist/v4.js +1 -1
  32. package/dist/v4.js.map +1 -1
  33. package/package.json +39 -28
  34. package/src/creator.ts +36 -0
  35. package/src/crypto.ts +1 -9
  36. package/src/decoder.ts +84 -0
  37. package/src/encoder.ts +50 -0
  38. package/src/enr.ts +50 -411
  39. package/src/get_multiaddr.ts +47 -0
  40. package/src/index.ts +3 -1
  41. package/src/peer_id.ts +34 -0
  42. package/src/raw_enr.ts +204 -0
  43. package/src/v4.ts +1 -2
  44. package/dist/keypair/index.d.ts +0 -8
  45. package/dist/keypair/index.js +0 -53
  46. package/dist/keypair/index.js.map +0 -1
  47. package/dist/keypair/secp256k1.d.ts +0 -13
  48. package/dist/keypair/secp256k1.js +0 -57
  49. package/dist/keypair/secp256k1.js.map +0 -1
  50. package/dist/keypair/types.d.ts +0 -13
  51. package/dist/keypair/types.js +0 -7
  52. package/dist/keypair/types.js.map +0 -1
  53. package/src/keypair/index.ts +0 -76
  54. package/src/keypair/secp256k1.ts +0 -69
  55. package/src/keypair/types.ts +0 -14
package/src/enr.ts CHANGED
@@ -1,64 +1,39 @@
1
- import * as RLP from "@ethersproject/rlp";
2
1
  import type { PeerId } from "@libp2p/interface-peer-id";
3
- import { Multiaddr } from "@multiformats/multiaddr";
4
- import {
5
- convertToBytes,
6
- convertToString,
7
- } from "@multiformats/multiaddr/convert";
8
- import {
9
- bytesToHex,
10
- bytesToUtf8,
11
- hexToBytes,
12
- utf8ToBytes,
13
- } from "@waku/byte-utils";
2
+ import type { PeerInfo } from "@libp2p/interface-peer-info";
3
+ import type { Multiaddr } from "@multiformats/multiaddr";
14
4
  import type {
15
5
  ENRKey,
16
6
  ENRValue,
17
7
  IEnr,
18
8
  NodeId,
19
9
  SequenceNumber,
20
- Waku2,
21
10
  } from "@waku/interfaces";
22
11
  import debug from "debug";
23
- import { fromString } from "uint8arrays/from-string";
24
- import { toString } from "uint8arrays/to-string";
25
12
 
26
- import {
27
- ERR_INVALID_ID,
28
- ERR_NO_SIGNATURE,
29
- MAX_RECORD_SIZE,
30
- } from "./constants.js";
31
- import { compressPublicKey, keccak256, verifySignature } from "./crypto.js";
32
- import {
33
- createKeypair,
34
- createKeypairFromPeerId,
35
- createPeerIdFromKeypair,
36
- IKeypair,
37
- KeypairType,
38
- } from "./keypair/index.js";
39
- import { multiaddrFromFields } from "./multiaddr_from_fields.js";
40
- import { decodeMultiaddrs, encodeMultiaddrs } from "./multiaddrs_codec.js";
13
+ import { ERR_INVALID_ID } from "./constants.js";
14
+ import { keccak256, verifySignature } from "./crypto.js";
15
+ import { locationMultiaddrFromEnrFields } from "./get_multiaddr.js";
16
+ import { createPeerIdFromPublicKey } from "./peer_id.js";
17
+ import { RawEnr } from "./raw_enr.js";
41
18
  import * as v4 from "./v4.js";
42
- import { decodeWaku2, encodeWaku2 } from "./waku2_codec.js";
43
19
 
44
20
  const log = debug("waku:enr");
45
21
 
46
- export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
22
+ export enum TransportProtocol {
23
+ TCP = "tcp",
24
+ UDP = "udp",
25
+ }
26
+ export enum TransportProtocolPerIpVersion {
27
+ TCP4 = "tcp4",
28
+ UDP4 = "udp4",
29
+ TCP6 = "tcp6",
30
+ UDP6 = "udp6",
31
+ }
32
+
33
+ export class ENR extends RawEnr implements IEnr {
47
34
  public static readonly RECORD_PREFIX = "enr:";
48
- public seq: SequenceNumber;
49
- public signature?: Uint8Array;
50
35
  public peerId?: PeerId;
51
36
 
52
- private constructor(
53
- kvs: Record<ENRKey, ENRValue> = {},
54
- seq: SequenceNumber = BigInt(1),
55
- signature?: Uint8Array
56
- ) {
57
- super(Object.entries(kvs));
58
- this.seq = seq;
59
- this.signature = signature;
60
- }
61
-
62
37
  static async create(
63
38
  kvs: Record<ENRKey, ENRValue> = {},
64
39
  seq: SequenceNumber = BigInt(1),
@@ -68,8 +43,7 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
68
43
  try {
69
44
  const publicKey = enr.publicKey;
70
45
  if (publicKey) {
71
- const keypair = createKeypair(enr.keypairType, undefined, publicKey);
72
- enr.peerId = await createPeerIdFromKeypair(keypair);
46
+ enr.peerId = await createPeerIdFromPublicKey(publicKey);
73
47
  }
74
48
  } catch (e) {
75
49
  log("Could not calculate peer id for ENR", e);
@@ -78,122 +52,6 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
78
52
  return enr;
79
53
  }
80
54
 
81
- static createV4(
82
- publicKey: Uint8Array,
83
- kvs: Record<ENRKey, ENRValue> = {}
84
- ): Promise<ENR> {
85
- // EIP-778 specifies that the key must be in compressed format, 33 bytes
86
- if (publicKey.length !== 33) {
87
- publicKey = compressPublicKey(publicKey);
88
- }
89
- return ENR.create({
90
- ...kvs,
91
- id: utf8ToBytes("v4"),
92
- secp256k1: publicKey,
93
- });
94
- }
95
-
96
- static async createFromPeerId(
97
- peerId: PeerId,
98
- kvs: Record<ENRKey, ENRValue> = {}
99
- ): Promise<ENR> {
100
- const keypair = await createKeypairFromPeerId(peerId);
101
- switch (keypair.type) {
102
- case KeypairType.secp256k1:
103
- return ENR.createV4(keypair.publicKey, kvs);
104
- default:
105
- throw new Error();
106
- }
107
- }
108
-
109
- static async decodeFromValues(decoded: Uint8Array[]): Promise<ENR> {
110
- if (!Array.isArray(decoded)) {
111
- throw new Error("Decoded ENR must be an array");
112
- }
113
- if (decoded.length % 2 !== 0) {
114
- throw new Error("Decoded ENR must have an even number of elements");
115
- }
116
- const [signature, seq, ...kvs] = decoded;
117
- if (!signature || Array.isArray(signature)) {
118
- throw new Error("Decoded ENR invalid signature: must be a byte array");
119
- }
120
- if (!seq || Array.isArray(seq)) {
121
- throw new Error(
122
- "Decoded ENR invalid sequence number: must be a byte array"
123
- );
124
- }
125
- const obj: Record<ENRKey, ENRValue> = {};
126
- for (let i = 0; i < kvs.length; i += 2) {
127
- try {
128
- obj[bytesToUtf8(kvs[i])] = kvs[i + 1];
129
- } catch (e) {
130
- log("Failed to decode ENR key to UTF-8, skipping it", kvs[i], e);
131
- }
132
- }
133
- // If seq is an empty array, translate as value 0
134
- const hexSeq = "0x" + (seq.length ? bytesToHex(seq) : "00");
135
-
136
- const enr = await ENR.create(obj, BigInt(hexSeq), signature);
137
-
138
- const rlpEncodedBytes = hexToBytes(RLP.encode([seq, ...kvs]));
139
- if (!enr.verify(rlpEncodedBytes, signature)) {
140
- throw new Error("Unable to verify ENR signature");
141
- }
142
- return enr;
143
- }
144
-
145
- static decode(encoded: Uint8Array): Promise<ENR> {
146
- const decoded = RLP.decode(encoded).map(hexToBytes);
147
- return ENR.decodeFromValues(decoded);
148
- }
149
-
150
- static decodeTxt(encoded: string): Promise<ENR> {
151
- if (!encoded.startsWith(this.RECORD_PREFIX)) {
152
- throw new Error(
153
- `"string encoded ENR must start with '${this.RECORD_PREFIX}'`
154
- );
155
- }
156
- return ENR.decode(fromString(encoded.slice(4), "base64url"));
157
- }
158
-
159
- set(k: ENRKey, v: ENRValue): this {
160
- this.signature = undefined;
161
- this.seq++;
162
- return super.set(k, v);
163
- }
164
-
165
- get id(): string {
166
- const id = this.get("id");
167
- if (!id) throw new Error("id not found.");
168
- return bytesToUtf8(id);
169
- }
170
-
171
- get keypairType(): KeypairType {
172
- switch (this.id) {
173
- case "v4":
174
- return KeypairType.secp256k1;
175
- default:
176
- throw new Error(ERR_INVALID_ID);
177
- }
178
- }
179
-
180
- get publicKey(): Uint8Array | undefined {
181
- switch (this.id) {
182
- case "v4":
183
- return this.get("secp256k1");
184
- default:
185
- throw new Error(ERR_INVALID_ID);
186
- }
187
- }
188
-
189
- get keypair(): IKeypair | undefined {
190
- if (this.publicKey) {
191
- const publicKey = this.publicKey;
192
- return createKeypair(this.keypairType, undefined, publicKey);
193
- }
194
- return;
195
- }
196
-
197
55
  get nodeId(): NodeId | undefined {
198
56
  switch (this.id) {
199
57
  case "v4":
@@ -202,193 +60,9 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
202
60
  throw new Error(ERR_INVALID_ID);
203
61
  }
204
62
  }
205
-
206
- get ip(): string | undefined {
207
- const raw = this.get("ip");
208
- if (raw) {
209
- return convertToString("ip4", raw) as string;
210
- } else {
211
- return undefined;
212
- }
213
- }
214
-
215
- set ip(ip: string | undefined) {
216
- if (ip) {
217
- this.set("ip", convertToBytes("ip4", ip));
218
- } else {
219
- this.delete("ip");
220
- }
221
- }
222
-
223
- get tcp(): number | undefined {
224
- const raw = this.get("tcp");
225
- if (raw) {
226
- return Number(convertToString("tcp", raw));
227
- } else {
228
- return undefined;
229
- }
230
- }
231
-
232
- set tcp(port: number | undefined) {
233
- if (port === undefined) {
234
- this.delete("tcp");
235
- } else {
236
- this.set("tcp", convertToBytes("tcp", port.toString(10)));
237
- }
238
- }
239
-
240
- get udp(): number | undefined {
241
- const raw = this.get("udp");
242
- if (raw) {
243
- return Number(convertToString("udp", raw));
244
- } else {
245
- return undefined;
246
- }
247
- }
248
-
249
- set udp(port: number | undefined) {
250
- if (port === undefined) {
251
- this.delete("udp");
252
- } else {
253
- this.set("udp", convertToBytes("udp", port.toString(10)));
254
- }
255
- }
256
-
257
- get ip6(): string | undefined {
258
- const raw = this.get("ip6");
259
- if (raw) {
260
- return convertToString("ip6", raw) as string;
261
- } else {
262
- return undefined;
263
- }
264
- }
265
-
266
- set ip6(ip: string | undefined) {
267
- if (ip) {
268
- this.set("ip6", convertToBytes("ip6", ip));
269
- } else {
270
- this.delete("ip6");
271
- }
272
- }
273
-
274
- get tcp6(): number | undefined {
275
- const raw = this.get("tcp6");
276
- if (raw) {
277
- return Number(convertToString("tcp", raw));
278
- } else {
279
- return undefined;
280
- }
281
- }
282
-
283
- set tcp6(port: number | undefined) {
284
- if (port === undefined) {
285
- this.delete("tcp6");
286
- } else {
287
- this.set("tcp6", convertToBytes("tcp", port.toString(10)));
288
- }
289
- }
290
-
291
- get udp6(): number | undefined {
292
- const raw = this.get("udp6");
293
- if (raw) {
294
- return Number(convertToString("udp", raw));
295
- } else {
296
- return undefined;
297
- }
298
- }
299
-
300
- set udp6(port: number | undefined) {
301
- if (port === undefined) {
302
- this.delete("udp6");
303
- } else {
304
- this.set("udp6", convertToBytes("udp", port.toString(10)));
305
- }
306
- }
307
-
308
- /**
309
- * Get the `multiaddrs` field from ENR.
310
- *
311
- * This field is used to store multiaddresses that cannot be stored with the current ENR pre-defined keys.
312
- * These can be a multiaddresses that include encapsulation (e.g. wss) or do not use `ip4` nor `ip6` for the host
313
- * address (e.g. `dns4`, `dnsaddr`, etc)..
314
- *
315
- * If the peer information only contains information that can be represented with the ENR pre-defined keys
316
- * (ip, tcp, etc) then the usage of { @link getLocationMultiaddr } should be preferred.
317
- *
318
- * The multiaddresses stored in this field are expected to be location multiaddresses, ie, peer id less.
319
- */
320
- get multiaddrs(): Multiaddr[] | undefined {
321
- const raw = this.get("multiaddrs");
322
-
323
- if (raw) return decodeMultiaddrs(raw);
324
-
325
- return;
326
- }
327
-
328
- /**
329
- * Set the `multiaddrs` field on the ENR.
330
- *
331
- * This field is used to store multiaddresses that cannot be stored with the current ENR pre-defined keys.
332
- * These can be a multiaddresses that include encapsulation (e.g. wss) or do not use `ip4` nor `ip6` for the host
333
- * address (e.g. `dns4`, `dnsaddr`, etc)..
334
- *
335
- * If the peer information only contains information that can be represented with the ENR pre-defined keys
336
- * (ip, tcp, etc) then the usage of { @link setLocationMultiaddr } should be preferred.
337
- * The multiaddresses stored in this field must be location multiaddresses,
338
- * ie, without a peer id.
339
- */
340
- set multiaddrs(multiaddrs: Multiaddr[] | undefined) {
341
- if (multiaddrs === undefined) {
342
- this.delete("multiaddrs");
343
- } else {
344
- const multiaddrsBuf = encodeMultiaddrs(multiaddrs);
345
- this.set("multiaddrs", multiaddrsBuf);
346
- }
347
- }
348
-
349
- getLocationMultiaddr(
350
- protocol: "udp" | "udp4" | "udp6" | "tcp" | "tcp4" | "tcp6"
351
- ): Multiaddr | undefined {
352
- if (protocol === "udp") {
353
- return (
354
- this.getLocationMultiaddr("udp4") || this.getLocationMultiaddr("udp6")
355
- );
356
- }
357
- if (protocol === "tcp") {
358
- return (
359
- this.getLocationMultiaddr("tcp4") || this.getLocationMultiaddr("tcp6")
360
- );
361
- }
362
- const isIpv6 = protocol.endsWith("6");
363
- const ipVal = this.get(isIpv6 ? "ip6" : "ip");
364
- if (!ipVal) {
365
- return;
366
- }
367
-
368
- const isUdp = protocol.startsWith("udp");
369
- const isTcp = protocol.startsWith("tcp");
370
- let protoName, protoVal;
371
- if (isUdp) {
372
- protoName = "udp";
373
- protoVal = isIpv6 ? this.get("udp6") : this.get("udp");
374
- } else if (isTcp) {
375
- protoName = "tcp";
376
- protoVal = isIpv6 ? this.get("tcp6") : this.get("tcp");
377
- } else {
378
- return;
379
- }
380
-
381
- if (!protoVal) {
382
- return;
383
- }
384
-
385
- return multiaddrFromFields(
386
- isIpv6 ? "ip6" : "ip4",
387
- protoName,
388
- ipVal,
389
- protoVal
390
- );
391
- }
63
+ getLocationMultiaddr: (
64
+ protocol: TransportProtocol | TransportProtocolPerIpVersion
65
+ ) => Multiaddr | undefined = locationMultiaddrFromEnrFields.bind({}, this);
392
66
 
393
67
  setLocationMultiaddr(multiaddr: Multiaddr): void {
394
68
  const protoNames = multiaddr.protoNames();
@@ -414,6 +88,32 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
414
88
  }
415
89
  }
416
90
 
91
+ getAllLocationMultiaddrs(): Multiaddr[] {
92
+ const multiaddrs = [];
93
+
94
+ for (const protocol of Object.values(TransportProtocolPerIpVersion)) {
95
+ const ma = this.getLocationMultiaddr(
96
+ protocol as TransportProtocolPerIpVersion
97
+ );
98
+ if (ma) multiaddrs.push(ma);
99
+ }
100
+
101
+ const _multiaddrs = this.multiaddrs ?? [];
102
+ multiaddrs.concat(_multiaddrs);
103
+
104
+ return multiaddrs;
105
+ }
106
+
107
+ get peerInfo(): PeerInfo | undefined {
108
+ const id = this.peerId;
109
+ if (!id) return;
110
+ return {
111
+ id,
112
+ multiaddrs: this.getAllLocationMultiaddrs(),
113
+ protocols: [],
114
+ };
115
+ }
116
+
417
117
  /**
418
118
  * Returns the full multiaddr from the ENR fields matching the provided
419
119
  * `protocol` parameter.
@@ -423,7 +123,7 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
423
123
  * @param protocol
424
124
  */
425
125
  getFullMultiaddr(
426
- protocol: "udp" | "udp4" | "udp6" | "tcp" | "tcp4" | "tcp6"
126
+ protocol: TransportProtocol | TransportProtocolPerIpVersion
427
127
  ): Multiaddr | undefined {
428
128
  if (this.peerId) {
429
129
  const locationMultiaddr = this.getLocationMultiaddr(protocol);
@@ -447,28 +147,6 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
447
147
  return [];
448
148
  }
449
149
 
450
- /**
451
- * Get the `waku2` field from ENR.
452
- */
453
- get waku2(): Waku2 | undefined {
454
- const raw = this.get("waku2");
455
- if (raw) return decodeWaku2(raw[0]);
456
-
457
- return;
458
- }
459
-
460
- /**
461
- * Set the `waku2` field on the ENR.
462
- */
463
- set waku2(waku2: Waku2 | undefined) {
464
- if (waku2 === undefined) {
465
- this.delete("waku2");
466
- } else {
467
- const byte = encodeWaku2(waku2);
468
- this.set("waku2", new Uint8Array([byte]));
469
- }
470
- }
471
-
472
150
  verify(data: Uint8Array, signature: Uint8Array): boolean {
473
151
  if (!this.get("id") || this.id !== "v4") {
474
152
  throw new Error(ERR_INVALID_ID);
@@ -489,43 +167,4 @@ export class ENR extends Map<ENRKey, ENRValue> implements IEnr {
489
167
  }
490
168
  return this.signature;
491
169
  }
492
-
493
- async encodeToValues(
494
- privateKey?: Uint8Array
495
- ): Promise<(ENRKey | ENRValue | number[])[]> {
496
- // sort keys and flatten into [k, v, k, v, ...]
497
- const content: Array<ENRKey | ENRValue | number[]> = Array.from(this.keys())
498
- .sort((a, b) => a.localeCompare(b))
499
- .map((k) => [k, this.get(k)] as [ENRKey, ENRValue])
500
- .map(([k, v]) => [utf8ToBytes(k), v])
501
- .flat();
502
- content.unshift(new Uint8Array([Number(this.seq)]));
503
- if (privateKey) {
504
- content.unshift(
505
- await this.sign(hexToBytes(RLP.encode(content)), privateKey)
506
- );
507
- } else {
508
- if (!this.signature) {
509
- throw new Error(ERR_NO_SIGNATURE);
510
- }
511
- content.unshift(this.signature);
512
- }
513
- return content;
514
- }
515
-
516
- async encode(privateKey?: Uint8Array): Promise<Uint8Array> {
517
- const encoded = hexToBytes(
518
- RLP.encode(await this.encodeToValues(privateKey))
519
- );
520
- if (encoded.length >= MAX_RECORD_SIZE) {
521
- throw new Error("ENR must be less than 300 bytes");
522
- }
523
- return encoded;
524
- }
525
-
526
- async encodeTxt(privateKey?: Uint8Array): Promise<string> {
527
- return (
528
- ENR.RECORD_PREFIX + toString(await this.encode(privateKey), "base64url")
529
- );
530
- }
531
170
  }
@@ -0,0 +1,47 @@
1
+ import { Multiaddr } from "@multiformats/multiaddr";
2
+ import type { IEnr } from "@waku/interfaces";
3
+
4
+ import { multiaddrFromFields } from "./multiaddr_from_fields.js";
5
+
6
+ export function locationMultiaddrFromEnrFields(
7
+ enr: IEnr,
8
+ protocol: "udp" | "udp4" | "udp6" | "tcp" | "tcp4" | "tcp6"
9
+ ): Multiaddr | undefined {
10
+ switch (protocol) {
11
+ case "udp":
12
+ return (
13
+ locationMultiaddrFromEnrFields(enr, "udp4") ||
14
+ locationMultiaddrFromEnrFields(enr, "udp6")
15
+ );
16
+ case "tcp":
17
+ return (
18
+ locationMultiaddrFromEnrFields(enr, "tcp4") ||
19
+ locationMultiaddrFromEnrFields(enr, "tcp6")
20
+ );
21
+ }
22
+ const isIpv6 = protocol.endsWith("6");
23
+ const ipVal = enr.get(isIpv6 ? "ip6" : "ip");
24
+ if (!ipVal) return;
25
+
26
+ const protoName = protocol.slice(0, 3);
27
+ let protoVal;
28
+ switch (protoName) {
29
+ case "udp":
30
+ protoVal = isIpv6 ? enr.get("udp6") : enr.get("udp");
31
+ break;
32
+ case "tcp":
33
+ protoVal = isIpv6 ? enr.get("tcp6") : enr.get("tcp");
34
+ break;
35
+ default:
36
+ return;
37
+ }
38
+
39
+ if (!protoVal) return;
40
+
41
+ return multiaddrFromFields(
42
+ isIpv6 ? "ip6" : "ip4",
43
+ protoName,
44
+ ipVal,
45
+ protoVal
46
+ );
47
+ }
package/src/index.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from "./constants.js";
2
+ export * from "./creator.js";
3
+ export * from "./decoder.js";
2
4
  export * from "./enr.js";
3
- export * from "./keypair/index.js";
5
+ export * from "./peer_id.js";
4
6
  export * from "./waku2_codec.js";
5
7
  export * from "./crypto.js";
package/src/peer_id.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { unmarshalPrivateKey, unmarshalPublicKey } from "@libp2p/crypto/keys";
2
+ import { supportedKeys } from "@libp2p/crypto/keys";
3
+ import type { PeerId } from "@libp2p/interface-peer-id";
4
+ import { peerIdFromKeys } from "@libp2p/peer-id";
5
+
6
+ export function createPeerIdFromPublicKey(
7
+ publicKey: Uint8Array
8
+ ): Promise<PeerId> {
9
+ const _publicKey = new supportedKeys.secp256k1.Secp256k1PublicKey(publicKey);
10
+ return peerIdFromKeys(_publicKey.bytes, undefined);
11
+ }
12
+
13
+ export function getPublicKeyFromPeerId(peerId: PeerId): Uint8Array {
14
+ if (peerId.type !== "secp256k1") {
15
+ throw new Error("Unsupported peer id type");
16
+ }
17
+
18
+ return unmarshalPublicKey(peerId.publicKey).marshal();
19
+ }
20
+
21
+ // Only used in tests
22
+ export async function getPrivateKeyFromPeerId(
23
+ peerId: PeerId
24
+ ): Promise<Uint8Array> {
25
+ if (peerId.type !== "secp256k1") {
26
+ throw new Error("Unsupported peer id type");
27
+ }
28
+ if (!peerId.privateKey) {
29
+ throw new Error("Private key not present on peer id");
30
+ }
31
+
32
+ const privateKey = await unmarshalPrivateKey(peerId.privateKey);
33
+ return privateKey.marshal();
34
+ }