@waku/core 0.0.1

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 (235) hide show
  1. package/CHANGELOG.md +614 -0
  2. package/README.md +56 -0
  3. package/bundle/browser-1e1a2f27.js +722 -0
  4. package/bundle/crypto-8551d579.js +2585 -0
  5. package/bundle/crypto-b00764b7.js +1772 -0
  6. package/bundle/enr-564d4a51.js +20785 -0
  7. package/bundle/enr-9fc5eed8.js +20786 -0
  8. package/bundle/enr-f6e82a53.js +20785 -0
  9. package/bundle/events-158407bb.js +1929 -0
  10. package/bundle/events-fcbda4dc.js +76 -0
  11. package/bundle/index-02d21809.js +20 -0
  12. package/bundle/index-0a4bdddc.js +2976 -0
  13. package/bundle/index-2ae915be.js +1854 -0
  14. package/bundle/index-64ce43f0.js +69 -0
  15. package/bundle/index-691c0be6.js +4059 -0
  16. package/bundle/index-a013a259.js +20 -0
  17. package/bundle/index-ba42b4fc.js +862 -0
  18. package/bundle/index.js +13428 -0
  19. package/bundle/lib/enr.js +8 -0
  20. package/bundle/lib/peer_discovery_dns.js +5018 -0
  21. package/bundle/lib/peer_discovery_static_list.js +75 -0
  22. package/bundle/lib/predefined_bootstrap_nodes.js +59 -0
  23. package/bundle/lib/utils.js +1 -0
  24. package/bundle/lib/wait_for_remote_peer.js +327 -0
  25. package/bundle/lib/waku_message/topic_only_message.js +4 -0
  26. package/bundle/lib/waku_message/version_0.js +4 -0
  27. package/bundle/lib/waku_message/version_1.js +463 -0
  28. package/bundle/message-e2db79d7.js +8393 -0
  29. package/bundle/multiaddr_to_peer_info-c406b1e1.js +19 -0
  30. package/bundle/multiaddr_to_peer_info-fd1de516.js +19 -0
  31. package/bundle/random_subset-75d1c511.js +26 -0
  32. package/bundle/topic_only_message-34f36fa6.js +82 -0
  33. package/bundle/utils-9a3221f2.js +815 -0
  34. package/bundle/version_0-e6fe440c.js +317 -0
  35. package/dist/index.d.ts +16 -0
  36. package/dist/index.js +17 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/lib/constants.d.ts +4 -0
  39. package/dist/lib/constants.js +5 -0
  40. package/dist/lib/constants.js.map +1 -0
  41. package/dist/lib/crypto.d.ts +34 -0
  42. package/dist/lib/crypto.js +79 -0
  43. package/dist/lib/crypto.js.map +1 -0
  44. package/dist/lib/enr/constants.d.ts +4 -0
  45. package/dist/lib/enr/constants.js +8 -0
  46. package/dist/lib/enr/constants.js.map +1 -0
  47. package/dist/lib/enr/enr.d.ts +90 -0
  48. package/dist/lib/enr/enr.js +432 -0
  49. package/dist/lib/enr/enr.js.map +1 -0
  50. package/dist/lib/enr/index.d.ts +5 -0
  51. package/dist/lib/enr/index.js +6 -0
  52. package/dist/lib/enr/index.js.map +1 -0
  53. package/dist/lib/enr/keypair/index.d.ts +8 -0
  54. package/dist/lib/enr/keypair/index.js +53 -0
  55. package/dist/lib/enr/keypair/index.js.map +1 -0
  56. package/dist/lib/enr/keypair/secp256k1.d.ts +13 -0
  57. package/dist/lib/enr/keypair/secp256k1.js +57 -0
  58. package/dist/lib/enr/keypair/secp256k1.js.map +1 -0
  59. package/dist/lib/enr/keypair/types.d.ts +13 -0
  60. package/dist/lib/enr/keypair/types.js +7 -0
  61. package/dist/lib/enr/keypair/types.js.map +1 -0
  62. package/dist/lib/enr/multiaddr_from_fields.d.ts +2 -0
  63. package/dist/lib/enr/multiaddr_from_fields.js +8 -0
  64. package/dist/lib/enr/multiaddr_from_fields.js.map +1 -0
  65. package/dist/lib/enr/multiaddrs_codec.d.ts +3 -0
  66. package/dist/lib/enr/multiaddrs_codec.js +32 -0
  67. package/dist/lib/enr/multiaddrs_codec.js.map +1 -0
  68. package/dist/lib/enr/types.d.ts +8 -0
  69. package/dist/lib/enr/types.js +3 -0
  70. package/dist/lib/enr/types.js.map +1 -0
  71. package/dist/lib/enr/v4.d.ts +3 -0
  72. package/dist/lib/enr/v4.js +14 -0
  73. package/dist/lib/enr/v4.js.map +1 -0
  74. package/dist/lib/enr/waku2_codec.d.ts +8 -0
  75. package/dist/lib/enr/waku2_codec.js +36 -0
  76. package/dist/lib/enr/waku2_codec.js.map +1 -0
  77. package/dist/lib/group_by.d.ts +3 -0
  78. package/dist/lib/group_by.js +13 -0
  79. package/dist/lib/group_by.js.map +1 -0
  80. package/dist/lib/multiaddr_to_peer_info.d.ts +3 -0
  81. package/dist/lib/multiaddr_to_peer_info.js +15 -0
  82. package/dist/lib/multiaddr_to_peer_info.js.map +1 -0
  83. package/dist/lib/peer_discovery_dns/dns.d.ts +48 -0
  84. package/dist/lib/peer_discovery_dns/dns.js +158 -0
  85. package/dist/lib/peer_discovery_dns/dns.js.map +1 -0
  86. package/dist/lib/peer_discovery_dns/dns_over_https.d.ts +32 -0
  87. package/dist/lib/peer_discovery_dns/dns_over_https.js +87 -0
  88. package/dist/lib/peer_discovery_dns/dns_over_https.js.map +1 -0
  89. package/dist/lib/peer_discovery_dns/enrtree.d.ts +33 -0
  90. package/dist/lib/peer_discovery_dns/enrtree.js +76 -0
  91. package/dist/lib/peer_discovery_dns/enrtree.js.map +1 -0
  92. package/dist/lib/peer_discovery_dns/fetch_nodes.d.ts +14 -0
  93. package/dist/lib/peer_discovery_dns/fetch_nodes.js +133 -0
  94. package/dist/lib/peer_discovery_dns/fetch_nodes.js.map +1 -0
  95. package/dist/lib/peer_discovery_dns/index.d.ts +30 -0
  96. package/dist/lib/peer_discovery_dns/index.js +54 -0
  97. package/dist/lib/peer_discovery_dns/index.js.map +1 -0
  98. package/dist/lib/peer_discovery_static_list.d.ts +44 -0
  99. package/dist/lib/peer_discovery_static_list.js +72 -0
  100. package/dist/lib/peer_discovery_static_list.js.map +1 -0
  101. package/dist/lib/predefined_bootstrap_nodes.d.ts +35 -0
  102. package/dist/lib/predefined_bootstrap_nodes.js +56 -0
  103. package/dist/lib/predefined_bootstrap_nodes.js.map +1 -0
  104. package/dist/lib/push_or_init_map.d.ts +1 -0
  105. package/dist/lib/push_or_init_map.js +9 -0
  106. package/dist/lib/push_or_init_map.js.map +1 -0
  107. package/dist/lib/random_subset.d.ts +4 -0
  108. package/dist/lib/random_subset.js +25 -0
  109. package/dist/lib/random_subset.js.map +1 -0
  110. package/dist/lib/select_connection.d.ts +2 -0
  111. package/dist/lib/select_connection.js +19 -0
  112. package/dist/lib/select_connection.js.map +1 -0
  113. package/dist/lib/select_peer.d.ts +15 -0
  114. package/dist/lib/select_peer.js +59 -0
  115. package/dist/lib/select_peer.js.map +1 -0
  116. package/dist/lib/to_proto_message.d.ts +3 -0
  117. package/dist/lib/to_proto_message.js +11 -0
  118. package/dist/lib/to_proto_message.js.map +1 -0
  119. package/dist/lib/utils.d.ts +22 -0
  120. package/dist/lib/utils.js +40 -0
  121. package/dist/lib/utils.js.map +1 -0
  122. package/dist/lib/wait_for_remote_peer.d.ts +22 -0
  123. package/dist/lib/wait_for_remote_peer.js +113 -0
  124. package/dist/lib/wait_for_remote_peer.js.map +1 -0
  125. package/dist/lib/waku.d.ts +61 -0
  126. package/dist/lib/waku.js +174 -0
  127. package/dist/lib/waku.js.map +1 -0
  128. package/dist/lib/waku_filter/filter_rpc.d.ts +25 -0
  129. package/dist/lib/waku_filter/filter_rpc.js +44 -0
  130. package/dist/lib/waku_filter/filter_rpc.js.map +1 -0
  131. package/dist/lib/waku_filter/index.d.ts +50 -0
  132. package/dist/lib/waku_filter/index.js +181 -0
  133. package/dist/lib/waku_filter/index.js.map +1 -0
  134. package/dist/lib/waku_light_push/index.d.ts +38 -0
  135. package/dist/lib/waku_light_push/index.js +83 -0
  136. package/dist/lib/waku_light_push/index.js.map +1 -0
  137. package/dist/lib/waku_light_push/push_rpc.d.ts +11 -0
  138. package/dist/lib/waku_light_push/push_rpc.js +31 -0
  139. package/dist/lib/waku_light_push/push_rpc.js.map +1 -0
  140. package/dist/lib/waku_message/constants.d.ts +12 -0
  141. package/dist/lib/waku_message/constants.js +10 -0
  142. package/dist/lib/waku_message/constants.js.map +1 -0
  143. package/dist/lib/waku_message/ecies.d.ts +17 -0
  144. package/dist/lib/waku_message/ecies.js +126 -0
  145. package/dist/lib/waku_message/ecies.js.map +1 -0
  146. package/dist/lib/waku_message/symmetric.d.ts +3 -0
  147. package/dist/lib/waku_message/symmetric.js +18 -0
  148. package/dist/lib/waku_message/symmetric.js.map +1 -0
  149. package/dist/lib/waku_message/topic_only_message.d.ts +15 -0
  150. package/dist/lib/waku_message/topic_only_message.js +31 -0
  151. package/dist/lib/waku_message/topic_only_message.js.map +1 -0
  152. package/dist/lib/waku_message/version_0.d.ts +26 -0
  153. package/dist/lib/waku_message/version_0.js +96 -0
  154. package/dist/lib/waku_message/version_0.js.map +1 -0
  155. package/dist/lib/waku_message/version_1.d.ts +93 -0
  156. package/dist/lib/waku_message/version_1.js +325 -0
  157. package/dist/lib/waku_message/version_1.js.map +1 -0
  158. package/dist/lib/waku_relay/constants.d.ts +63 -0
  159. package/dist/lib/waku_relay/constants.js +67 -0
  160. package/dist/lib/waku_relay/constants.js.map +1 -0
  161. package/dist/lib/waku_relay/index.d.ts +65 -0
  162. package/dist/lib/waku_relay/index.js +111 -0
  163. package/dist/lib/waku_relay/index.js.map +1 -0
  164. package/dist/lib/waku_store/history_rpc.d.ts +27 -0
  165. package/dist/lib/waku_store/history_rpc.js +71 -0
  166. package/dist/lib/waku_store/history_rpc.js.map +1 -0
  167. package/dist/lib/waku_store/index.d.ts +126 -0
  168. package/dist/lib/waku_store/index.js +218 -0
  169. package/dist/lib/waku_store/index.js.map +1 -0
  170. package/dist/proto/filter.d.ts +65 -0
  171. package/dist/proto/filter.js +425 -0
  172. package/dist/proto/filter.js.map +1 -0
  173. package/dist/proto/light_push.d.ts +57 -0
  174. package/dist/proto/light_push.js +369 -0
  175. package/dist/proto/light_push.js.map +1 -0
  176. package/dist/proto/message.d.ts +29 -0
  177. package/dist/proto/message.js +215 -0
  178. package/dist/proto/message.js.map +1 -0
  179. package/dist/proto/store.d.ts +104 -0
  180. package/dist/proto/store.js +602 -0
  181. package/dist/proto/store.js.map +1 -0
  182. package/dist/proto/topic_only_message.d.ts +10 -0
  183. package/dist/proto/topic_only_message.js +46 -0
  184. package/dist/proto/topic_only_message.js.map +1 -0
  185. package/package.json +292 -0
  186. package/src/index.ts +33 -0
  187. package/src/lib/constants.ts +4 -0
  188. package/src/lib/crypto.ts +100 -0
  189. package/src/lib/enr/constants.ts +10 -0
  190. package/src/lib/enr/enr.ts +516 -0
  191. package/src/lib/enr/index.ts +5 -0
  192. package/src/lib/enr/keypair/index.ts +76 -0
  193. package/src/lib/enr/keypair/secp256k1.ts +69 -0
  194. package/src/lib/enr/keypair/types.ts +14 -0
  195. package/src/lib/enr/multiaddr_from_fields.ts +18 -0
  196. package/src/lib/enr/multiaddrs_codec.ts +50 -0
  197. package/src/lib/enr/types.ts +11 -0
  198. package/src/lib/enr/v4.ts +22 -0
  199. package/src/lib/enr/waku2_codec.ts +39 -0
  200. package/src/lib/group_by.ts +14 -0
  201. package/src/lib/multiaddr_to_peer_info.ts +17 -0
  202. package/src/lib/peer_discovery_dns/dns.ts +223 -0
  203. package/src/lib/peer_discovery_dns/dns_over_https.ts +98 -0
  204. package/src/lib/peer_discovery_dns/enrtree.ts +123 -0
  205. package/src/lib/peer_discovery_dns/fetch_nodes.ts +180 -0
  206. package/src/lib/peer_discovery_dns/index.ts +84 -0
  207. package/src/lib/peer_discovery_static_list.ts +118 -0
  208. package/src/lib/predefined_bootstrap_nodes.ts +72 -0
  209. package/src/lib/push_or_init_map.ts +13 -0
  210. package/src/lib/random_subset.ts +30 -0
  211. package/src/lib/select_connection.ts +24 -0
  212. package/src/lib/select_peer.ts +77 -0
  213. package/src/lib/to_proto_message.ts +15 -0
  214. package/src/lib/utils.ts +50 -0
  215. package/src/lib/wait_for_remote_peer.ts +151 -0
  216. package/src/lib/waku.ts +258 -0
  217. package/src/lib/waku_filter/filter_rpc.ts +57 -0
  218. package/src/lib/waku_filter/index.ts +291 -0
  219. package/src/lib/waku_light_push/index.ts +137 -0
  220. package/src/lib/waku_light_push/push_rpc.ts +39 -0
  221. package/src/lib/waku_message/constants.ts +10 -0
  222. package/src/lib/waku_message/ecies.ts +194 -0
  223. package/src/lib/waku_message/symmetric.ts +33 -0
  224. package/src/lib/waku_message/topic_only_message.ts +40 -0
  225. package/src/lib/waku_message/version_0.ts +121 -0
  226. package/src/lib/waku_message/version_1.ts +457 -0
  227. package/src/lib/waku_relay/constants.ts +77 -0
  228. package/src/lib/waku_relay/index.ts +189 -0
  229. package/src/lib/waku_store/history_rpc.ts +94 -0
  230. package/src/lib/waku_store/index.ts +372 -0
  231. package/src/proto/filter.ts +602 -0
  232. package/src/proto/light_push.ts +526 -0
  233. package/src/proto/message.ts +304 -0
  234. package/src/proto/store.ts +844 -0
  235. package/src/proto/topic_only_message.ts +67 -0
@@ -0,0 +1,50 @@
1
+ import { multiaddr } from "@multiformats/multiaddr";
2
+ import type { Multiaddr } from "@multiformats/multiaddr";
3
+
4
+ import { MULTIADDR_LENGTH_SIZE } from "./constants";
5
+
6
+ export function decodeMultiaddrs(bytes: Uint8Array): Multiaddr[] {
7
+ const multiaddrs = [];
8
+
9
+ let index = 0;
10
+
11
+ while (index < bytes.length) {
12
+ const sizeDataView = new DataView(
13
+ bytes.buffer,
14
+ index,
15
+ MULTIADDR_LENGTH_SIZE
16
+ );
17
+ const size = sizeDataView.getUint16(0);
18
+ index += MULTIADDR_LENGTH_SIZE;
19
+
20
+ const multiaddrBytes = bytes.slice(index, index + size);
21
+ index += size;
22
+
23
+ multiaddrs.push(multiaddr(multiaddrBytes));
24
+ }
25
+ return multiaddrs;
26
+ }
27
+
28
+ export function encodeMultiaddrs(multiaddrs: Multiaddr[]): Uint8Array {
29
+ const totalLength = multiaddrs.reduce(
30
+ (acc, ma) => acc + MULTIADDR_LENGTH_SIZE + ma.bytes.length,
31
+ 0
32
+ );
33
+ const bytes = new Uint8Array(totalLength);
34
+ const dataView = new DataView(bytes.buffer);
35
+
36
+ let index = 0;
37
+ multiaddrs.forEach((multiaddr) => {
38
+ if (multiaddr.getPeerId())
39
+ throw new Error("`multiaddr` field MUST not contain peer id");
40
+
41
+ // Prepend the size of the next entry
42
+ dataView.setUint16(index, multiaddr.bytes.length);
43
+ index += MULTIADDR_LENGTH_SIZE;
44
+
45
+ bytes.set(multiaddr.bytes, index);
46
+ index += multiaddr.bytes.length;
47
+ });
48
+
49
+ return bytes;
50
+ }
@@ -0,0 +1,11 @@
1
+ // Custom and aliased types for ENRs
2
+
3
+ /**
4
+ * We represent NodeId as a hex string, since node equality is used very heavily
5
+ * and it is convenient to index data by NodeId
6
+ */
7
+ export type NodeId = string;
8
+ export type SequenceNumber = bigint;
9
+
10
+ export type ENRKey = string;
11
+ export type ENRValue = Uint8Array;
@@ -0,0 +1,22 @@
1
+ import * as secp from "@noble/secp256k1";
2
+
3
+ import { keccak256 } from "../crypto";
4
+ import { bytesToHex } from "../utils";
5
+
6
+ import { NodeId } from "./types";
7
+
8
+ export async function sign(
9
+ privKey: Uint8Array,
10
+ msg: Uint8Array
11
+ ): Promise<Uint8Array> {
12
+ return secp.sign(keccak256(msg), privKey, {
13
+ der: false,
14
+ });
15
+ }
16
+
17
+ export function nodeId(pubKey: Uint8Array): NodeId {
18
+ const publicKey = secp.Point.fromHex(pubKey);
19
+ const uncompressedPubkey = publicKey.toRawBytes(false);
20
+
21
+ return bytesToHex(keccak256(uncompressedPubkey.slice(1)));
22
+ }
@@ -0,0 +1,39 @@
1
+ export interface Waku2 {
2
+ relay: boolean;
3
+ store: boolean;
4
+ filter: boolean;
5
+ lightPush: boolean;
6
+ }
7
+
8
+ export function encodeWaku2(protocols: Waku2): number {
9
+ let byte = 0;
10
+
11
+ if (protocols.lightPush) byte += 1;
12
+ byte = byte << 1;
13
+ if (protocols.filter) byte += 1;
14
+ byte = byte << 1;
15
+ if (protocols.store) byte += 1;
16
+ byte = byte << 1;
17
+ if (protocols.relay) byte += 1;
18
+
19
+ return byte;
20
+ }
21
+
22
+ export function decodeWaku2(byte: number): Waku2 {
23
+ const waku2 = {
24
+ relay: false,
25
+ store: false,
26
+ filter: false,
27
+ lightPush: false,
28
+ };
29
+
30
+ if (byte % 2) waku2.relay = true;
31
+ byte = byte >> 1;
32
+ if (byte % 2) waku2.store = true;
33
+ byte = byte >> 1;
34
+ if (byte % 2) waku2.filter = true;
35
+ byte = byte >> 1;
36
+ if (byte % 2) waku2.lightPush = true;
37
+
38
+ return waku2;
39
+ }
@@ -0,0 +1,14 @@
1
+ export function groupByContentTopic<T extends { contentTopic: string }>(
2
+ values: T[]
3
+ ): Map<string, Array<T>> {
4
+ const groupedDecoders = new Map();
5
+ values.forEach((value) => {
6
+ let decs = groupedDecoders.get(value.contentTopic);
7
+ if (!decs) {
8
+ groupedDecoders.set(value.contentTopic, []);
9
+ decs = groupedDecoders.get(value.contentTopic);
10
+ }
11
+ decs.push(value);
12
+ });
13
+ return groupedDecoders;
14
+ }
@@ -0,0 +1,17 @@
1
+ import { PeerInfo } from "@libp2p/interface-peer-info";
2
+ import { peerIdFromString } from "@libp2p/peer-id";
3
+ import { Multiaddr } from "@multiformats/multiaddr";
4
+
5
+ export function multiaddrsToPeerInfo(mas: Multiaddr[]): PeerInfo[] {
6
+ return mas
7
+ .map((ma) => {
8
+ const peerIdStr = ma.getPeerId();
9
+ const protocols: string[] = [];
10
+ return {
11
+ id: peerIdStr ? peerIdFromString(peerIdStr) : null,
12
+ multiaddrs: [ma.decapsulateCode(421)],
13
+ protocols,
14
+ };
15
+ })
16
+ .filter((peerInfo): peerInfo is PeerInfo => peerInfo.id !== null);
17
+ }
@@ -0,0 +1,223 @@
1
+ import debug from "debug";
2
+
3
+ import { ENR } from "../enr";
4
+
5
+ import { DnsOverHttps } from "./dns_over_https";
6
+ import { ENRTree } from "./enrtree";
7
+ import {
8
+ fetchNodesUntilCapabilitiesFulfilled,
9
+ yieldNodesUntilCapabilitiesFulfilled,
10
+ } from "./fetch_nodes";
11
+
12
+ const log = debug("waku:discovery:dns");
13
+
14
+ export type SearchContext = {
15
+ domain: string;
16
+ publicKey: string;
17
+ visits: { [key: string]: boolean };
18
+ };
19
+
20
+ export interface DnsClient {
21
+ resolveTXT: (domain: string) => Promise<string[]>;
22
+ }
23
+
24
+ export interface NodeCapabilityCount {
25
+ relay: number;
26
+ store: number;
27
+ filter: number;
28
+ lightPush: number;
29
+ }
30
+
31
+ export class DnsNodeDiscovery {
32
+ private readonly dns: DnsClient;
33
+ private readonly _DNSTreeCache: { [key: string]: string };
34
+ private readonly _errorTolerance: number = 10;
35
+
36
+ public static dnsOverHttp(dnsClient?: DnsClient): DnsNodeDiscovery {
37
+ if (!dnsClient) {
38
+ dnsClient = new DnsOverHttps();
39
+ }
40
+ return new DnsNodeDiscovery(dnsClient);
41
+ }
42
+
43
+ /**
44
+ * Returns a list of verified peers listed in an EIP-1459 DNS tree. Method may
45
+ * return fewer peers than requested if [[wantedNodeCapabilityCount]] requires
46
+ * larger quantity of peers than available or the number of errors/duplicate
47
+ * peers encountered by randomized search exceeds the sum of the fields of
48
+ * [[wantedNodeCapabilityCount]] plus the [[_errorTolerance]] factor.
49
+ */
50
+ async getPeers(
51
+ enrTreeUrls: string[],
52
+ wantedNodeCapabilityCount: Partial<NodeCapabilityCount>
53
+ ): Promise<ENR[]> {
54
+ const networkIndex = Math.floor(Math.random() * enrTreeUrls.length);
55
+ const { publicKey, domain } = ENRTree.parseTree(enrTreeUrls[networkIndex]);
56
+ const context: SearchContext = {
57
+ domain,
58
+ publicKey,
59
+ visits: {},
60
+ };
61
+
62
+ const peers = await fetchNodesUntilCapabilitiesFulfilled(
63
+ wantedNodeCapabilityCount,
64
+ this._errorTolerance,
65
+ () => this._search(domain, context)
66
+ );
67
+ log(
68
+ "retrieved peers: ",
69
+ peers.map((peer) => {
70
+ return {
71
+ id: peer.peerId?.toString(),
72
+ multiaddrs: peer.multiaddrs?.map((ma) => ma.toString()),
73
+ };
74
+ })
75
+ );
76
+ return peers;
77
+ }
78
+
79
+ public constructor(dns: DnsClient) {
80
+ this._DNSTreeCache = {};
81
+ this.dns = dns;
82
+ }
83
+
84
+ /**
85
+ * {@docInherit getPeers}
86
+ */
87
+ async *getNextPeer(
88
+ enrTreeUrls: string[],
89
+ wantedNodeCapabilityCount: Partial<NodeCapabilityCount>
90
+ ): AsyncGenerator<ENR> {
91
+ const networkIndex = Math.floor(Math.random() * enrTreeUrls.length);
92
+ const { publicKey, domain } = ENRTree.parseTree(enrTreeUrls[networkIndex]);
93
+ const context: SearchContext = {
94
+ domain,
95
+ publicKey,
96
+ visits: {},
97
+ };
98
+
99
+ for await (const peer of yieldNodesUntilCapabilitiesFulfilled(
100
+ wantedNodeCapabilityCount,
101
+ this._errorTolerance,
102
+ () => this._search(domain, context)
103
+ )) {
104
+ yield peer;
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Runs a recursive, randomized descent of the DNS tree to retrieve a single
110
+ * ENR record as an ENR. Returns null if parsing or DNS resolution fails.
111
+ */
112
+ private async _search(
113
+ subdomain: string,
114
+ context: SearchContext
115
+ ): Promise<ENR | null> {
116
+ try {
117
+ const entry = await this._getTXTRecord(subdomain, context);
118
+ context.visits[subdomain] = true;
119
+
120
+ let next: string;
121
+ let branches: string[];
122
+
123
+ const entryType = getEntryType(entry);
124
+ try {
125
+ switch (entryType) {
126
+ case ENRTree.ROOT_PREFIX:
127
+ next = ENRTree.parseAndVerifyRoot(entry, context.publicKey);
128
+ return await this._search(next, context);
129
+ case ENRTree.BRANCH_PREFIX:
130
+ branches = ENRTree.parseBranch(entry);
131
+ next = selectRandomPath(branches, context);
132
+ return await this._search(next, context);
133
+ case ENRTree.RECORD_PREFIX:
134
+ return ENR.decodeTxt(entry);
135
+ default:
136
+ return null;
137
+ }
138
+ } catch (error) {
139
+ log(
140
+ `Failed to search DNS tree ${entryType} at subdomain ${subdomain}: ${error}`
141
+ );
142
+ return null;
143
+ }
144
+ } catch (error) {
145
+ log(`Failed to retrieve TXT record at subdomain ${subdomain}: ${error}`);
146
+ return null;
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Retrieves the TXT record stored at a location from either
152
+ * this DNS tree cache or via DNS query.
153
+ *
154
+ * @throws if the TXT Record contains non-UTF-8 values.
155
+ */
156
+ private async _getTXTRecord(
157
+ subdomain: string,
158
+ context: SearchContext
159
+ ): Promise<string> {
160
+ if (this._DNSTreeCache[subdomain]) {
161
+ return this._DNSTreeCache[subdomain];
162
+ }
163
+
164
+ // Location is either the top level tree entry host or a subdomain of it.
165
+ const location =
166
+ subdomain !== context.domain
167
+ ? `${subdomain}.${context.domain}`
168
+ : context.domain;
169
+
170
+ const response = await this.dns.resolveTXT(location);
171
+
172
+ if (!response.length)
173
+ throw new Error("Received empty result array while fetching TXT record");
174
+ if (!response[0].length) throw new Error("Received empty TXT record");
175
+
176
+ // Branch entries can be an array of strings of comma delimited subdomains, with
177
+ // some subdomain strings split across the array elements
178
+ const result = response.join("");
179
+
180
+ this._DNSTreeCache[subdomain] = result;
181
+ return result;
182
+ }
183
+ }
184
+
185
+ function getEntryType(entry: string): string {
186
+ if (entry.startsWith(ENRTree.ROOT_PREFIX)) return ENRTree.ROOT_PREFIX;
187
+ if (entry.startsWith(ENRTree.BRANCH_PREFIX)) return ENRTree.BRANCH_PREFIX;
188
+ if (entry.startsWith(ENRTree.RECORD_PREFIX)) return ENRTree.RECORD_PREFIX;
189
+
190
+ return "";
191
+ }
192
+
193
+ /**
194
+ * Returns a randomly selected subdomain string from the list provided by a branch
195
+ * entry record.
196
+ *
197
+ * The client must track subdomains which are already resolved to avoid
198
+ * going into an infinite loop b/c branch entries can contain
199
+ * circular references. It’s in the client’s best interest to traverse the
200
+ * tree in random order.
201
+ */
202
+ function selectRandomPath(branches: string[], context: SearchContext): string {
203
+ // Identify domains already visited in this traversal of the DNS tree.
204
+ // Then filter against them to prevent cycles.
205
+ const circularRefs: { [key: number]: boolean } = {};
206
+ for (const [idx, subdomain] of branches.entries()) {
207
+ if (context.visits[subdomain]) {
208
+ circularRefs[idx] = true;
209
+ }
210
+ }
211
+ // If all possible paths are circular...
212
+ if (Object.keys(circularRefs).length === branches.length) {
213
+ throw new Error("Unresolvable circular path detected");
214
+ }
215
+
216
+ // Randomly select a viable path
217
+ let index;
218
+ do {
219
+ index = Math.floor(Math.random() * branches.length);
220
+ } while (circularRefs[index]);
221
+
222
+ return branches[index];
223
+ }
@@ -0,0 +1,98 @@
1
+ import debug from "debug";
2
+ import { Endpoint, query, toEndpoint } from "dns-query";
3
+
4
+ import { bytesToUtf8 } from "../utils";
5
+
6
+ import { DnsClient } from "./dns";
7
+
8
+ const log = debug("waku:dns-over-https");
9
+
10
+ export class DnsOverHttps implements DnsClient {
11
+ /**
12
+ * Default endpoints to use for DNS queries.
13
+ * Taken from https://github.com/martinheidegger/dns-query as static data
14
+ * to avoid dynamic queries.
15
+ *
16
+ * To dynamically retrieve other endpoints, use https://github.com/martinheidegger/dns-query#well-known-endpoints
17
+ */
18
+ static DefaultEndpoints: Endpoint[] = [
19
+ toEndpoint({
20
+ name: "cisco-doh",
21
+ protocol: "https:",
22
+ host: "doh.opendns.com",
23
+ ipv4: "146.112.41.2",
24
+ }),
25
+ toEndpoint({
26
+ name: "cloudflare",
27
+ protocol: "https:",
28
+ host: "dns.cloudflare.com",
29
+ ipv4: "1.0.0.1",
30
+ }),
31
+ ];
32
+
33
+ /**
34
+ * Create new Dns-Over-Http DNS client.
35
+ *
36
+ * @param endpoints The endpoints for Dns-Over-Https queries;
37
+ * Defaults to [[DnsOverHttps.DefaultEndpoints]].
38
+ * @param retries Retries if a given endpoint fails.
39
+ *
40
+ * @throws {code: string} If DNS query fails.
41
+ */
42
+ public constructor(
43
+ private endpoints: Endpoint[] = DnsOverHttps.DefaultEndpoints,
44
+ private retries: number = 3
45
+ ) {}
46
+
47
+ /**
48
+ * Resolves a TXT record
49
+ *
50
+ * @param domain The domain name
51
+ *
52
+ * @throws if the query fails
53
+ */
54
+ async resolveTXT(domain: string): Promise<string[]> {
55
+ let answers;
56
+ try {
57
+ const res = await query(
58
+ {
59
+ question: { type: "TXT", name: domain },
60
+ },
61
+ {
62
+ endpoints: this.endpoints,
63
+ retries: this.retries,
64
+ }
65
+ );
66
+ answers = res.answers;
67
+ } catch (error) {
68
+ log("query failed: ", error);
69
+ throw new Error("DNS query failed");
70
+ }
71
+
72
+ if (!answers) throw new Error(`Could not resolve ${domain}`);
73
+
74
+ const data = answers.map((a) => a.data) as
75
+ | Array<string | Uint8Array>
76
+ | Array<Array<string | Uint8Array>>;
77
+
78
+ const result: string[] = [];
79
+
80
+ data.forEach((d) => {
81
+ if (typeof d === "string") {
82
+ result.push(d);
83
+ } else if (Array.isArray(d)) {
84
+ d.forEach((sd) => {
85
+ if (typeof sd === "string") {
86
+ result.push(sd);
87
+ } else {
88
+ result.push(bytesToUtf8(sd));
89
+ }
90
+ });
91
+ } else {
92
+ result.push(bytesToUtf8(d));
93
+ }
94
+ });
95
+
96
+ return result;
97
+ }
98
+ }
@@ -0,0 +1,123 @@
1
+ import base32 from "hi-base32";
2
+ import { fromString } from "uint8arrays/from-string";
3
+
4
+ import { keccak256, verifySignature } from "../crypto";
5
+ import { ENR } from "../enr";
6
+ import { utf8ToBytes } from "../utils";
7
+
8
+ export type ENRRootValues = {
9
+ eRoot: string;
10
+ lRoot: string;
11
+ seq: number;
12
+ signature: string;
13
+ };
14
+
15
+ export type ENRTreeValues = {
16
+ publicKey: string;
17
+ domain: string;
18
+ };
19
+
20
+ export class ENRTree {
21
+ public static readonly RECORD_PREFIX = ENR.RECORD_PREFIX;
22
+ public static readonly TREE_PREFIX = "enrtree:";
23
+ public static readonly BRANCH_PREFIX = "enrtree-branch:";
24
+ public static readonly ROOT_PREFIX = "enrtree-root:";
25
+
26
+ /**
27
+ * Extracts the branch subdomain referenced by a DNS tree root string after verifying
28
+ * the root record signature with its base32 compressed public key.
29
+ */
30
+ static parseAndVerifyRoot(root: string, publicKey: string): string {
31
+ if (!root.startsWith(this.ROOT_PREFIX))
32
+ throw new Error(
33
+ `ENRTree root entry must start with '${this.ROOT_PREFIX}'`
34
+ );
35
+
36
+ const rootValues = ENRTree.parseRootValues(root);
37
+ const decodedPublicKey = base32.decode.asBytes(publicKey);
38
+
39
+ // The signature is a 65-byte secp256k1 over the keccak256 hash
40
+ // of the record content, excluding the `sig=` part, encoded as URL-safe base64 string
41
+ // (Trailing recovery bit must be trimmed to pass `ecdsaVerify` method)
42
+ const signedComponent = root.split(" sig")[0];
43
+ const signedComponentBuffer = utf8ToBytes(signedComponent);
44
+ const signatureBuffer = fromString(rootValues.signature, "base64url").slice(
45
+ 0,
46
+ 64
47
+ );
48
+
49
+ const isVerified = verifySignature(
50
+ signatureBuffer,
51
+ keccak256(signedComponentBuffer),
52
+ new Uint8Array(decodedPublicKey)
53
+ );
54
+
55
+ if (!isVerified) throw new Error("Unable to verify ENRTree root signature");
56
+
57
+ return rootValues.eRoot;
58
+ }
59
+
60
+ static parseRootValues(txt: string): ENRRootValues {
61
+ const matches = txt.match(
62
+ /^enrtree-root:v1 e=([^ ]+) l=([^ ]+) seq=(\d+) sig=([^ ]+)$/
63
+ );
64
+
65
+ if (!Array.isArray(matches))
66
+ throw new Error("Could not parse ENRTree root entry");
67
+
68
+ matches.shift(); // The first entry is the full match
69
+ const [eRoot, lRoot, seq, signature] = matches;
70
+
71
+ if (!eRoot)
72
+ throw new Error("Could not parse 'e' value from ENRTree root entry");
73
+ if (!lRoot)
74
+ throw new Error("Could not parse 'l' value from ENRTree root entry");
75
+
76
+ if (!seq)
77
+ throw new Error("Could not parse 'seq' value from ENRTree root entry");
78
+ if (!signature)
79
+ throw new Error("Could not parse 'sig' value from ENRTree root entry");
80
+
81
+ return { eRoot, lRoot, seq: Number(seq), signature };
82
+ }
83
+
84
+ /**
85
+ * Returns the public key and top level domain of an ENR tree entry.
86
+ * The domain is the starting point for traversing a set of linked DNS TXT records
87
+ * and the public key is used to verify the root entry record
88
+ */
89
+ static parseTree(tree: string): ENRTreeValues {
90
+ if (!tree.startsWith(this.TREE_PREFIX))
91
+ throw new Error(
92
+ `ENRTree tree entry must start with '${this.TREE_PREFIX}'`
93
+ );
94
+
95
+ const matches = tree.match(/^enrtree:\/\/([^@]+)@(.+)$/);
96
+
97
+ if (!Array.isArray(matches))
98
+ throw new Error("Could not parse ENRTree tree entry");
99
+
100
+ matches.shift(); // The first entry is the full match
101
+ const [publicKey, domain] = matches;
102
+
103
+ if (!publicKey)
104
+ throw new Error("Could not parse public key from ENRTree tree entry");
105
+ if (!domain)
106
+ throw new Error("Could not parse domain from ENRTree tree entry");
107
+
108
+ return { publicKey, domain };
109
+ }
110
+
111
+ /**
112
+ * Returns subdomains listed in an ENR branch entry. These in turn lead to
113
+ * either further branch entries or ENR records.
114
+ */
115
+ static parseBranch(branch: string): string[] {
116
+ if (!branch.startsWith(this.BRANCH_PREFIX))
117
+ throw new Error(
118
+ `ENRTree branch entry must start with '${this.BRANCH_PREFIX}'`
119
+ );
120
+
121
+ return branch.split(this.BRANCH_PREFIX)[1].split(",");
122
+ }
123
+ }