@waku/core 0.0.1 → 0.0.2
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.
- package/CHANGELOG.md +9 -0
- package/bundle/{index-691c0be6.js → index-a67d7136.js} +1 -1
- package/bundle/{index-0a4bdddc.js → index-f7e049ad.js} +1 -1
- package/bundle/index.js +24853 -3555
- package/bundle/lib/peer_discovery_static_list.js +18 -4
- package/bundle/lib/predefined_bootstrap_nodes.js +1 -1
- package/bundle/lib/wait_for_remote_peer.js +2 -3
- package/bundle/lib/waku_message/topic_only_message.js +2 -3
- package/bundle/lib/waku_message/version_0.js +317 -4
- package/bundle/{message-e2db79d7.js → message-049c8b67.js} +861 -2
- package/bundle/{topic_only_message-34f36fa6.js → topic_only_message-5ad3a869.js} +1 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -3
- package/dist/index.js.map +1 -1
- package/dist/lib/waku_filter/index.d.ts +2 -2
- package/dist/lib/waku_filter/index.js.map +1 -1
- package/dist/lib/waku_message/version_0.d.ts +3 -2
- package/dist/lib/waku_message/version_0.js +2 -1
- package/dist/lib/waku_message/version_0.js.map +1 -1
- package/dist/lib/waku_relay/index.d.ts +4 -3
- package/dist/lib/waku_relay/index.js.map +1 -1
- package/dist/lib/waku_store/index.d.ts +4 -4
- package/package.json +3 -118
- package/src/index.ts +0 -10
- package/src/lib/waku_filter/index.ts +4 -3
- package/src/lib/waku_message/version_0.ts +5 -3
- package/src/lib/waku_relay/index.ts +4 -3
- package/src/lib/waku_store/index.ts +5 -5
- package/bundle/crypto-8551d579.js +0 -2585
- package/bundle/crypto-b00764b7.js +0 -1772
- package/bundle/enr-564d4a51.js +0 -20785
- package/bundle/enr-9fc5eed8.js +0 -20786
- package/bundle/enr-f6e82a53.js +0 -20785
- package/bundle/events-fcbda4dc.js +0 -76
- package/bundle/index-02d21809.js +0 -20
- package/bundle/index-2ae915be.js +0 -1854
- package/bundle/index-a013a259.js +0 -20
- package/bundle/index-ba42b4fc.js +0 -862
- package/bundle/lib/enr.js +0 -8
- package/bundle/lib/peer_discovery_dns.js +0 -5018
- package/bundle/lib/utils.js +0 -1
- package/bundle/lib/waku_message/version_1.js +0 -463
- package/bundle/multiaddr_to_peer_info-c406b1e1.js +0 -19
- package/bundle/multiaddr_to_peer_info-fd1de516.js +0 -19
- package/bundle/utils-9a3221f2.js +0 -815
- package/bundle/version_0-e6fe440c.js +0 -317
- package/dist/lib/crypto.d.ts +0 -34
- package/dist/lib/crypto.js +0 -79
- package/dist/lib/crypto.js.map +0 -1
- package/dist/lib/enr/constants.d.ts +0 -4
- package/dist/lib/enr/constants.js +0 -8
- package/dist/lib/enr/constants.js.map +0 -1
- package/dist/lib/enr/enr.d.ts +0 -90
- package/dist/lib/enr/enr.js +0 -432
- package/dist/lib/enr/enr.js.map +0 -1
- package/dist/lib/enr/index.d.ts +0 -5
- package/dist/lib/enr/index.js +0 -6
- package/dist/lib/enr/index.js.map +0 -1
- package/dist/lib/enr/keypair/index.d.ts +0 -8
- package/dist/lib/enr/keypair/index.js +0 -53
- package/dist/lib/enr/keypair/index.js.map +0 -1
- package/dist/lib/enr/keypair/secp256k1.d.ts +0 -13
- package/dist/lib/enr/keypair/secp256k1.js +0 -57
- package/dist/lib/enr/keypair/secp256k1.js.map +0 -1
- package/dist/lib/enr/keypair/types.d.ts +0 -13
- package/dist/lib/enr/keypair/types.js +0 -7
- package/dist/lib/enr/keypair/types.js.map +0 -1
- package/dist/lib/enr/multiaddr_from_fields.d.ts +0 -2
- package/dist/lib/enr/multiaddr_from_fields.js +0 -8
- package/dist/lib/enr/multiaddr_from_fields.js.map +0 -1
- package/dist/lib/enr/multiaddrs_codec.d.ts +0 -3
- package/dist/lib/enr/multiaddrs_codec.js +0 -32
- package/dist/lib/enr/multiaddrs_codec.js.map +0 -1
- package/dist/lib/enr/types.d.ts +0 -8
- package/dist/lib/enr/types.js +0 -3
- package/dist/lib/enr/types.js.map +0 -1
- package/dist/lib/enr/v4.d.ts +0 -3
- package/dist/lib/enr/v4.js +0 -14
- package/dist/lib/enr/v4.js.map +0 -1
- package/dist/lib/enr/waku2_codec.d.ts +0 -8
- package/dist/lib/enr/waku2_codec.js +0 -36
- package/dist/lib/enr/waku2_codec.js.map +0 -1
- package/dist/lib/peer_discovery_dns/dns.d.ts +0 -48
- package/dist/lib/peer_discovery_dns/dns.js +0 -158
- package/dist/lib/peer_discovery_dns/dns.js.map +0 -1
- package/dist/lib/peer_discovery_dns/dns_over_https.d.ts +0 -32
- package/dist/lib/peer_discovery_dns/dns_over_https.js +0 -87
- package/dist/lib/peer_discovery_dns/dns_over_https.js.map +0 -1
- package/dist/lib/peer_discovery_dns/enrtree.d.ts +0 -33
- package/dist/lib/peer_discovery_dns/enrtree.js +0 -76
- package/dist/lib/peer_discovery_dns/enrtree.js.map +0 -1
- package/dist/lib/peer_discovery_dns/fetch_nodes.d.ts +0 -14
- package/dist/lib/peer_discovery_dns/fetch_nodes.js +0 -133
- package/dist/lib/peer_discovery_dns/fetch_nodes.js.map +0 -1
- package/dist/lib/peer_discovery_dns/index.d.ts +0 -30
- package/dist/lib/peer_discovery_dns/index.js +0 -54
- package/dist/lib/peer_discovery_dns/index.js.map +0 -1
- package/dist/lib/utils.d.ts +0 -22
- package/dist/lib/utils.js +0 -40
- package/dist/lib/utils.js.map +0 -1
- package/dist/lib/waku_message/constants.d.ts +0 -12
- package/dist/lib/waku_message/constants.js +0 -10
- package/dist/lib/waku_message/constants.js.map +0 -1
- package/dist/lib/waku_message/ecies.d.ts +0 -17
- package/dist/lib/waku_message/ecies.js +0 -126
- package/dist/lib/waku_message/ecies.js.map +0 -1
- package/dist/lib/waku_message/symmetric.d.ts +0 -3
- package/dist/lib/waku_message/symmetric.js +0 -18
- package/dist/lib/waku_message/symmetric.js.map +0 -1
- package/dist/lib/waku_message/version_1.d.ts +0 -93
- package/dist/lib/waku_message/version_1.js +0 -325
- package/dist/lib/waku_message/version_1.js.map +0 -1
- package/src/lib/crypto.ts +0 -100
- package/src/lib/enr/constants.ts +0 -10
- package/src/lib/enr/enr.ts +0 -516
- package/src/lib/enr/index.ts +0 -5
- package/src/lib/enr/keypair/index.ts +0 -76
- package/src/lib/enr/keypair/secp256k1.ts +0 -69
- package/src/lib/enr/keypair/types.ts +0 -14
- package/src/lib/enr/multiaddr_from_fields.ts +0 -18
- package/src/lib/enr/multiaddrs_codec.ts +0 -50
- package/src/lib/enr/types.ts +0 -11
- package/src/lib/enr/v4.ts +0 -22
- package/src/lib/enr/waku2_codec.ts +0 -39
- package/src/lib/peer_discovery_dns/dns.ts +0 -223
- package/src/lib/peer_discovery_dns/dns_over_https.ts +0 -98
- package/src/lib/peer_discovery_dns/enrtree.ts +0 -123
- package/src/lib/peer_discovery_dns/fetch_nodes.ts +0 -180
- package/src/lib/peer_discovery_dns/index.ts +0 -84
- package/src/lib/utils.ts +0 -50
- package/src/lib/waku_message/constants.ts +0 -10
- package/src/lib/waku_message/ecies.ts +0 -194
- package/src/lib/waku_message/symmetric.ts +0 -33
- package/src/lib/waku_message/version_1.ts +0 -457
@@ -1,50 +0,0 @@
|
|
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
|
-
}
|
package/src/lib/enr/types.ts
DELETED
@@ -1,11 +0,0 @@
|
|
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;
|
package/src/lib/enr/v4.ts
DELETED
@@ -1,22 +0,0 @@
|
|
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
|
-
}
|
@@ -1,39 +0,0 @@
|
|
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
|
-
}
|
@@ -1,223 +0,0 @@
|
|
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
|
-
}
|
@@ -1,98 +0,0 @@
|
|
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
|
-
}
|
@@ -1,123 +0,0 @@
|
|
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
|
-
}
|