@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.
- package/CHANGELOG.md +614 -0
- package/README.md +56 -0
- package/bundle/browser-1e1a2f27.js +722 -0
- package/bundle/crypto-8551d579.js +2585 -0
- package/bundle/crypto-b00764b7.js +1772 -0
- package/bundle/enr-564d4a51.js +20785 -0
- package/bundle/enr-9fc5eed8.js +20786 -0
- package/bundle/enr-f6e82a53.js +20785 -0
- package/bundle/events-158407bb.js +1929 -0
- package/bundle/events-fcbda4dc.js +76 -0
- package/bundle/index-02d21809.js +20 -0
- package/bundle/index-0a4bdddc.js +2976 -0
- package/bundle/index-2ae915be.js +1854 -0
- package/bundle/index-64ce43f0.js +69 -0
- package/bundle/index-691c0be6.js +4059 -0
- package/bundle/index-a013a259.js +20 -0
- package/bundle/index-ba42b4fc.js +862 -0
- package/bundle/index.js +13428 -0
- package/bundle/lib/enr.js +8 -0
- package/bundle/lib/peer_discovery_dns.js +5018 -0
- package/bundle/lib/peer_discovery_static_list.js +75 -0
- package/bundle/lib/predefined_bootstrap_nodes.js +59 -0
- package/bundle/lib/utils.js +1 -0
- package/bundle/lib/wait_for_remote_peer.js +327 -0
- package/bundle/lib/waku_message/topic_only_message.js +4 -0
- package/bundle/lib/waku_message/version_0.js +4 -0
- package/bundle/lib/waku_message/version_1.js +463 -0
- package/bundle/message-e2db79d7.js +8393 -0
- package/bundle/multiaddr_to_peer_info-c406b1e1.js +19 -0
- package/bundle/multiaddr_to_peer_info-fd1de516.js +19 -0
- package/bundle/random_subset-75d1c511.js +26 -0
- package/bundle/topic_only_message-34f36fa6.js +82 -0
- package/bundle/utils-9a3221f2.js +815 -0
- package/bundle/version_0-e6fe440c.js +317 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/constants.d.ts +4 -0
- package/dist/lib/constants.js +5 -0
- package/dist/lib/constants.js.map +1 -0
- package/dist/lib/crypto.d.ts +34 -0
- package/dist/lib/crypto.js +79 -0
- package/dist/lib/crypto.js.map +1 -0
- package/dist/lib/enr/constants.d.ts +4 -0
- package/dist/lib/enr/constants.js +8 -0
- package/dist/lib/enr/constants.js.map +1 -0
- package/dist/lib/enr/enr.d.ts +90 -0
- package/dist/lib/enr/enr.js +432 -0
- package/dist/lib/enr/enr.js.map +1 -0
- package/dist/lib/enr/index.d.ts +5 -0
- package/dist/lib/enr/index.js +6 -0
- package/dist/lib/enr/index.js.map +1 -0
- package/dist/lib/enr/keypair/index.d.ts +8 -0
- package/dist/lib/enr/keypair/index.js +53 -0
- package/dist/lib/enr/keypair/index.js.map +1 -0
- package/dist/lib/enr/keypair/secp256k1.d.ts +13 -0
- package/dist/lib/enr/keypair/secp256k1.js +57 -0
- package/dist/lib/enr/keypair/secp256k1.js.map +1 -0
- package/dist/lib/enr/keypair/types.d.ts +13 -0
- package/dist/lib/enr/keypair/types.js +7 -0
- package/dist/lib/enr/keypair/types.js.map +1 -0
- package/dist/lib/enr/multiaddr_from_fields.d.ts +2 -0
- package/dist/lib/enr/multiaddr_from_fields.js +8 -0
- package/dist/lib/enr/multiaddr_from_fields.js.map +1 -0
- package/dist/lib/enr/multiaddrs_codec.d.ts +3 -0
- package/dist/lib/enr/multiaddrs_codec.js +32 -0
- package/dist/lib/enr/multiaddrs_codec.js.map +1 -0
- package/dist/lib/enr/types.d.ts +8 -0
- package/dist/lib/enr/types.js +3 -0
- package/dist/lib/enr/types.js.map +1 -0
- package/dist/lib/enr/v4.d.ts +3 -0
- package/dist/lib/enr/v4.js +14 -0
- package/dist/lib/enr/v4.js.map +1 -0
- package/dist/lib/enr/waku2_codec.d.ts +8 -0
- package/dist/lib/enr/waku2_codec.js +36 -0
- package/dist/lib/enr/waku2_codec.js.map +1 -0
- package/dist/lib/group_by.d.ts +3 -0
- package/dist/lib/group_by.js +13 -0
- package/dist/lib/group_by.js.map +1 -0
- package/dist/lib/multiaddr_to_peer_info.d.ts +3 -0
- package/dist/lib/multiaddr_to_peer_info.js +15 -0
- package/dist/lib/multiaddr_to_peer_info.js.map +1 -0
- package/dist/lib/peer_discovery_dns/dns.d.ts +48 -0
- package/dist/lib/peer_discovery_dns/dns.js +158 -0
- package/dist/lib/peer_discovery_dns/dns.js.map +1 -0
- package/dist/lib/peer_discovery_dns/dns_over_https.d.ts +32 -0
- package/dist/lib/peer_discovery_dns/dns_over_https.js +87 -0
- package/dist/lib/peer_discovery_dns/dns_over_https.js.map +1 -0
- package/dist/lib/peer_discovery_dns/enrtree.d.ts +33 -0
- package/dist/lib/peer_discovery_dns/enrtree.js +76 -0
- package/dist/lib/peer_discovery_dns/enrtree.js.map +1 -0
- package/dist/lib/peer_discovery_dns/fetch_nodes.d.ts +14 -0
- package/dist/lib/peer_discovery_dns/fetch_nodes.js +133 -0
- package/dist/lib/peer_discovery_dns/fetch_nodes.js.map +1 -0
- package/dist/lib/peer_discovery_dns/index.d.ts +30 -0
- package/dist/lib/peer_discovery_dns/index.js +54 -0
- package/dist/lib/peer_discovery_dns/index.js.map +1 -0
- package/dist/lib/peer_discovery_static_list.d.ts +44 -0
- package/dist/lib/peer_discovery_static_list.js +72 -0
- package/dist/lib/peer_discovery_static_list.js.map +1 -0
- package/dist/lib/predefined_bootstrap_nodes.d.ts +35 -0
- package/dist/lib/predefined_bootstrap_nodes.js +56 -0
- package/dist/lib/predefined_bootstrap_nodes.js.map +1 -0
- package/dist/lib/push_or_init_map.d.ts +1 -0
- package/dist/lib/push_or_init_map.js +9 -0
- package/dist/lib/push_or_init_map.js.map +1 -0
- package/dist/lib/random_subset.d.ts +4 -0
- package/dist/lib/random_subset.js +25 -0
- package/dist/lib/random_subset.js.map +1 -0
- package/dist/lib/select_connection.d.ts +2 -0
- package/dist/lib/select_connection.js +19 -0
- package/dist/lib/select_connection.js.map +1 -0
- package/dist/lib/select_peer.d.ts +15 -0
- package/dist/lib/select_peer.js +59 -0
- package/dist/lib/select_peer.js.map +1 -0
- package/dist/lib/to_proto_message.d.ts +3 -0
- package/dist/lib/to_proto_message.js +11 -0
- package/dist/lib/to_proto_message.js.map +1 -0
- package/dist/lib/utils.d.ts +22 -0
- package/dist/lib/utils.js +40 -0
- package/dist/lib/utils.js.map +1 -0
- package/dist/lib/wait_for_remote_peer.d.ts +22 -0
- package/dist/lib/wait_for_remote_peer.js +113 -0
- package/dist/lib/wait_for_remote_peer.js.map +1 -0
- package/dist/lib/waku.d.ts +61 -0
- package/dist/lib/waku.js +174 -0
- package/dist/lib/waku.js.map +1 -0
- package/dist/lib/waku_filter/filter_rpc.d.ts +25 -0
- package/dist/lib/waku_filter/filter_rpc.js +44 -0
- package/dist/lib/waku_filter/filter_rpc.js.map +1 -0
- package/dist/lib/waku_filter/index.d.ts +50 -0
- package/dist/lib/waku_filter/index.js +181 -0
- package/dist/lib/waku_filter/index.js.map +1 -0
- package/dist/lib/waku_light_push/index.d.ts +38 -0
- package/dist/lib/waku_light_push/index.js +83 -0
- package/dist/lib/waku_light_push/index.js.map +1 -0
- package/dist/lib/waku_light_push/push_rpc.d.ts +11 -0
- package/dist/lib/waku_light_push/push_rpc.js +31 -0
- package/dist/lib/waku_light_push/push_rpc.js.map +1 -0
- package/dist/lib/waku_message/constants.d.ts +12 -0
- package/dist/lib/waku_message/constants.js +10 -0
- package/dist/lib/waku_message/constants.js.map +1 -0
- package/dist/lib/waku_message/ecies.d.ts +17 -0
- package/dist/lib/waku_message/ecies.js +126 -0
- package/dist/lib/waku_message/ecies.js.map +1 -0
- package/dist/lib/waku_message/symmetric.d.ts +3 -0
- package/dist/lib/waku_message/symmetric.js +18 -0
- package/dist/lib/waku_message/symmetric.js.map +1 -0
- package/dist/lib/waku_message/topic_only_message.d.ts +15 -0
- package/dist/lib/waku_message/topic_only_message.js +31 -0
- package/dist/lib/waku_message/topic_only_message.js.map +1 -0
- package/dist/lib/waku_message/version_0.d.ts +26 -0
- package/dist/lib/waku_message/version_0.js +96 -0
- package/dist/lib/waku_message/version_0.js.map +1 -0
- package/dist/lib/waku_message/version_1.d.ts +93 -0
- package/dist/lib/waku_message/version_1.js +325 -0
- package/dist/lib/waku_message/version_1.js.map +1 -0
- package/dist/lib/waku_relay/constants.d.ts +63 -0
- package/dist/lib/waku_relay/constants.js +67 -0
- package/dist/lib/waku_relay/constants.js.map +1 -0
- package/dist/lib/waku_relay/index.d.ts +65 -0
- package/dist/lib/waku_relay/index.js +111 -0
- package/dist/lib/waku_relay/index.js.map +1 -0
- package/dist/lib/waku_store/history_rpc.d.ts +27 -0
- package/dist/lib/waku_store/history_rpc.js +71 -0
- package/dist/lib/waku_store/history_rpc.js.map +1 -0
- package/dist/lib/waku_store/index.d.ts +126 -0
- package/dist/lib/waku_store/index.js +218 -0
- package/dist/lib/waku_store/index.js.map +1 -0
- package/dist/proto/filter.d.ts +65 -0
- package/dist/proto/filter.js +425 -0
- package/dist/proto/filter.js.map +1 -0
- package/dist/proto/light_push.d.ts +57 -0
- package/dist/proto/light_push.js +369 -0
- package/dist/proto/light_push.js.map +1 -0
- package/dist/proto/message.d.ts +29 -0
- package/dist/proto/message.js +215 -0
- package/dist/proto/message.js.map +1 -0
- package/dist/proto/store.d.ts +104 -0
- package/dist/proto/store.js +602 -0
- package/dist/proto/store.js.map +1 -0
- package/dist/proto/topic_only_message.d.ts +10 -0
- package/dist/proto/topic_only_message.js +46 -0
- package/dist/proto/topic_only_message.js.map +1 -0
- package/package.json +292 -0
- package/src/index.ts +33 -0
- package/src/lib/constants.ts +4 -0
- package/src/lib/crypto.ts +100 -0
- package/src/lib/enr/constants.ts +10 -0
- package/src/lib/enr/enr.ts +516 -0
- package/src/lib/enr/index.ts +5 -0
- package/src/lib/enr/keypair/index.ts +76 -0
- package/src/lib/enr/keypair/secp256k1.ts +69 -0
- package/src/lib/enr/keypair/types.ts +14 -0
- package/src/lib/enr/multiaddr_from_fields.ts +18 -0
- package/src/lib/enr/multiaddrs_codec.ts +50 -0
- package/src/lib/enr/types.ts +11 -0
- package/src/lib/enr/v4.ts +22 -0
- package/src/lib/enr/waku2_codec.ts +39 -0
- package/src/lib/group_by.ts +14 -0
- package/src/lib/multiaddr_to_peer_info.ts +17 -0
- package/src/lib/peer_discovery_dns/dns.ts +223 -0
- package/src/lib/peer_discovery_dns/dns_over_https.ts +98 -0
- package/src/lib/peer_discovery_dns/enrtree.ts +123 -0
- package/src/lib/peer_discovery_dns/fetch_nodes.ts +180 -0
- package/src/lib/peer_discovery_dns/index.ts +84 -0
- package/src/lib/peer_discovery_static_list.ts +118 -0
- package/src/lib/predefined_bootstrap_nodes.ts +72 -0
- package/src/lib/push_or_init_map.ts +13 -0
- package/src/lib/random_subset.ts +30 -0
- package/src/lib/select_connection.ts +24 -0
- package/src/lib/select_peer.ts +77 -0
- package/src/lib/to_proto_message.ts +15 -0
- package/src/lib/utils.ts +50 -0
- package/src/lib/wait_for_remote_peer.ts +151 -0
- package/src/lib/waku.ts +258 -0
- package/src/lib/waku_filter/filter_rpc.ts +57 -0
- package/src/lib/waku_filter/index.ts +291 -0
- package/src/lib/waku_light_push/index.ts +137 -0
- package/src/lib/waku_light_push/push_rpc.ts +39 -0
- package/src/lib/waku_message/constants.ts +10 -0
- package/src/lib/waku_message/ecies.ts +194 -0
- package/src/lib/waku_message/symmetric.ts +33 -0
- package/src/lib/waku_message/topic_only_message.ts +40 -0
- package/src/lib/waku_message/version_0.ts +121 -0
- package/src/lib/waku_message/version_1.ts +457 -0
- package/src/lib/waku_relay/constants.ts +77 -0
- package/src/lib/waku_relay/index.ts +189 -0
- package/src/lib/waku_store/history_rpc.ts +94 -0
- package/src/lib/waku_store/index.ts +372 -0
- package/src/proto/filter.ts +602 -0
- package/src/proto/light_push.ts +526 -0
- package/src/proto/message.ts +304 -0
- package/src/proto/store.ts +844 -0
- package/src/proto/topic_only_message.ts +67 -0
@@ -0,0 +1,1929 @@
|
|
1
|
+
var encode_1 = encode$3;
|
2
|
+
|
3
|
+
var MSB = 0x80
|
4
|
+
, REST = 0x7F
|
5
|
+
, MSBALL = ~REST
|
6
|
+
, INT = Math.pow(2, 31);
|
7
|
+
|
8
|
+
function encode$3(num, out, offset) {
|
9
|
+
out = out || [];
|
10
|
+
offset = offset || 0;
|
11
|
+
var oldOffset = offset;
|
12
|
+
|
13
|
+
while(num >= INT) {
|
14
|
+
out[offset++] = (num & 0xFF) | MSB;
|
15
|
+
num /= 128;
|
16
|
+
}
|
17
|
+
while(num & MSBALL) {
|
18
|
+
out[offset++] = (num & 0xFF) | MSB;
|
19
|
+
num >>>= 7;
|
20
|
+
}
|
21
|
+
out[offset] = num | 0;
|
22
|
+
|
23
|
+
encode$3.bytes = offset - oldOffset + 1;
|
24
|
+
|
25
|
+
return out
|
26
|
+
}
|
27
|
+
|
28
|
+
var decode$4 = read;
|
29
|
+
|
30
|
+
var MSB$1 = 0x80
|
31
|
+
, REST$1 = 0x7F;
|
32
|
+
|
33
|
+
function read(buf, offset) {
|
34
|
+
var res = 0
|
35
|
+
, offset = offset || 0
|
36
|
+
, shift = 0
|
37
|
+
, counter = offset
|
38
|
+
, b
|
39
|
+
, l = buf.length;
|
40
|
+
|
41
|
+
do {
|
42
|
+
if (counter >= l) {
|
43
|
+
read.bytes = 0;
|
44
|
+
throw new RangeError('Could not decode varint')
|
45
|
+
}
|
46
|
+
b = buf[counter++];
|
47
|
+
res += shift < 28
|
48
|
+
? (b & REST$1) << shift
|
49
|
+
: (b & REST$1) * Math.pow(2, shift);
|
50
|
+
shift += 7;
|
51
|
+
} while (b >= MSB$1)
|
52
|
+
|
53
|
+
read.bytes = counter - offset;
|
54
|
+
|
55
|
+
return res
|
56
|
+
}
|
57
|
+
|
58
|
+
var N1 = Math.pow(2, 7);
|
59
|
+
var N2 = Math.pow(2, 14);
|
60
|
+
var N3 = Math.pow(2, 21);
|
61
|
+
var N4 = Math.pow(2, 28);
|
62
|
+
var N5 = Math.pow(2, 35);
|
63
|
+
var N6 = Math.pow(2, 42);
|
64
|
+
var N7 = Math.pow(2, 49);
|
65
|
+
var N8 = Math.pow(2, 56);
|
66
|
+
var N9 = Math.pow(2, 63);
|
67
|
+
|
68
|
+
var length = function (value) {
|
69
|
+
return (
|
70
|
+
value < N1 ? 1
|
71
|
+
: value < N2 ? 2
|
72
|
+
: value < N3 ? 3
|
73
|
+
: value < N4 ? 4
|
74
|
+
: value < N5 ? 5
|
75
|
+
: value < N6 ? 6
|
76
|
+
: value < N7 ? 7
|
77
|
+
: value < N8 ? 8
|
78
|
+
: value < N9 ? 9
|
79
|
+
: 10
|
80
|
+
)
|
81
|
+
};
|
82
|
+
|
83
|
+
var varint = {
|
84
|
+
encode: encode_1
|
85
|
+
, decode: decode$4
|
86
|
+
, encodingLength: length
|
87
|
+
};
|
88
|
+
|
89
|
+
var _brrp_varint = varint;
|
90
|
+
|
91
|
+
/**
|
92
|
+
* @param {Uint8Array} data
|
93
|
+
* @param {number} [offset=0]
|
94
|
+
* @returns {[number, number]}
|
95
|
+
*/
|
96
|
+
const decode$3 = (data, offset = 0) => {
|
97
|
+
const code = _brrp_varint.decode(data, offset);
|
98
|
+
return [code, _brrp_varint.decode.bytes]
|
99
|
+
};
|
100
|
+
|
101
|
+
/**
|
102
|
+
* @param {number} int
|
103
|
+
* @param {Uint8Array} target
|
104
|
+
* @param {number} [offset=0]
|
105
|
+
*/
|
106
|
+
const encodeTo = (int, target, offset = 0) => {
|
107
|
+
_brrp_varint.encode(int, target, offset);
|
108
|
+
return target
|
109
|
+
};
|
110
|
+
|
111
|
+
/**
|
112
|
+
* @param {number} int
|
113
|
+
* @returns {number}
|
114
|
+
*/
|
115
|
+
const encodingLength = (int) => {
|
116
|
+
return _brrp_varint.encodingLength(int)
|
117
|
+
};
|
118
|
+
|
119
|
+
/**
|
120
|
+
* @param {Uint8Array} aa
|
121
|
+
* @param {Uint8Array} bb
|
122
|
+
*/
|
123
|
+
const equals$2 = (aa, bb) => {
|
124
|
+
if (aa === bb) return true
|
125
|
+
if (aa.byteLength !== bb.byteLength) {
|
126
|
+
return false
|
127
|
+
}
|
128
|
+
|
129
|
+
for (let ii = 0; ii < aa.byteLength; ii++) {
|
130
|
+
if (aa[ii] !== bb[ii]) {
|
131
|
+
return false
|
132
|
+
}
|
133
|
+
}
|
134
|
+
|
135
|
+
return true
|
136
|
+
};
|
137
|
+
|
138
|
+
/**
|
139
|
+
* @param {ArrayBufferView|ArrayBuffer|Uint8Array} o
|
140
|
+
* @returns {Uint8Array}
|
141
|
+
*/
|
142
|
+
const coerce = o => {
|
143
|
+
if (o instanceof Uint8Array && o.constructor.name === 'Uint8Array') return o
|
144
|
+
if (o instanceof ArrayBuffer) return new Uint8Array(o)
|
145
|
+
if (ArrayBuffer.isView(o)) {
|
146
|
+
return new Uint8Array(o.buffer, o.byteOffset, o.byteLength)
|
147
|
+
}
|
148
|
+
throw new Error('Unknown type, must be binary type')
|
149
|
+
};
|
150
|
+
|
151
|
+
/**
|
152
|
+
* @param {string} str
|
153
|
+
* @returns {Uint8Array}
|
154
|
+
*/
|
155
|
+
const fromString = str => (new TextEncoder()).encode(str);
|
156
|
+
|
157
|
+
/**
|
158
|
+
* @param {Uint8Array} b
|
159
|
+
* @returns {string}
|
160
|
+
*/
|
161
|
+
const toString = b => (new TextDecoder()).decode(b);
|
162
|
+
|
163
|
+
/**
|
164
|
+
* Creates a multihash digest.
|
165
|
+
*
|
166
|
+
* @template {number} Code
|
167
|
+
* @param {Code} code
|
168
|
+
* @param {Uint8Array} digest
|
169
|
+
*/
|
170
|
+
const create = (code, digest) => {
|
171
|
+
const size = digest.byteLength;
|
172
|
+
const sizeOffset = encodingLength(code);
|
173
|
+
const digestOffset = sizeOffset + encodingLength(size);
|
174
|
+
|
175
|
+
const bytes = new Uint8Array(digestOffset + size);
|
176
|
+
encodeTo(code, bytes, 0);
|
177
|
+
encodeTo(size, bytes, sizeOffset);
|
178
|
+
bytes.set(digest, digestOffset);
|
179
|
+
|
180
|
+
return new Digest(code, size, digest, bytes)
|
181
|
+
};
|
182
|
+
|
183
|
+
/**
|
184
|
+
* Turns bytes representation of multihash digest into an instance.
|
185
|
+
*
|
186
|
+
* @param {Uint8Array} multihash
|
187
|
+
* @returns {MultihashDigest}
|
188
|
+
*/
|
189
|
+
const decode$2 = (multihash) => {
|
190
|
+
const bytes = coerce(multihash);
|
191
|
+
const [code, sizeOffset] = decode$3(bytes);
|
192
|
+
const [size, digestOffset] = decode$3(bytes.subarray(sizeOffset));
|
193
|
+
const digest = bytes.subarray(sizeOffset + digestOffset);
|
194
|
+
|
195
|
+
if (digest.byteLength !== size) {
|
196
|
+
throw new Error('Incorrect length')
|
197
|
+
}
|
198
|
+
|
199
|
+
return new Digest(code, size, digest, bytes)
|
200
|
+
};
|
201
|
+
|
202
|
+
/**
|
203
|
+
* @param {MultihashDigest} a
|
204
|
+
* @param {unknown} b
|
205
|
+
* @returns {b is MultihashDigest}
|
206
|
+
*/
|
207
|
+
const equals$1 = (a, b) => {
|
208
|
+
if (a === b) {
|
209
|
+
return true
|
210
|
+
} else {
|
211
|
+
const data = /** @type {{code?:unknown, size?:unknown, bytes?:unknown}} */(b);
|
212
|
+
|
213
|
+
return (
|
214
|
+
a.code === data.code &&
|
215
|
+
a.size === data.size &&
|
216
|
+
data.bytes instanceof Uint8Array &&
|
217
|
+
equals$2(a.bytes, data.bytes)
|
218
|
+
)
|
219
|
+
}
|
220
|
+
};
|
221
|
+
|
222
|
+
/**
|
223
|
+
* @typedef {import('./interface.js').MultihashDigest} MultihashDigest
|
224
|
+
*/
|
225
|
+
|
226
|
+
/**
|
227
|
+
* Represents a multihash digest which carries information about the
|
228
|
+
* hashing alogrithm and an actual hash digest.
|
229
|
+
*
|
230
|
+
* @template {number} Code
|
231
|
+
* @template {number} Size
|
232
|
+
* @class
|
233
|
+
* @implements {MultihashDigest}
|
234
|
+
*/
|
235
|
+
class Digest {
|
236
|
+
/**
|
237
|
+
* Creates a multihash digest.
|
238
|
+
*
|
239
|
+
* @param {Code} code
|
240
|
+
* @param {Size} size
|
241
|
+
* @param {Uint8Array} digest
|
242
|
+
* @param {Uint8Array} bytes
|
243
|
+
*/
|
244
|
+
constructor (code, size, digest, bytes) {
|
245
|
+
this.code = code;
|
246
|
+
this.size = size;
|
247
|
+
this.digest = digest;
|
248
|
+
this.bytes = bytes;
|
249
|
+
}
|
250
|
+
}
|
251
|
+
|
252
|
+
// base-x encoding / decoding
|
253
|
+
// Copyright (c) 2018 base-x contributors
|
254
|
+
// Copyright (c) 2014-2018 The Bitcoin Core developers (base58.cpp)
|
255
|
+
// Distributed under the MIT software license, see the accompanying
|
256
|
+
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
257
|
+
function base (ALPHABET, name) {
|
258
|
+
if (ALPHABET.length >= 255) { throw new TypeError('Alphabet too long') }
|
259
|
+
var BASE_MAP = new Uint8Array(256);
|
260
|
+
for (var j = 0; j < BASE_MAP.length; j++) {
|
261
|
+
BASE_MAP[j] = 255;
|
262
|
+
}
|
263
|
+
for (var i = 0; i < ALPHABET.length; i++) {
|
264
|
+
var x = ALPHABET.charAt(i);
|
265
|
+
var xc = x.charCodeAt(0);
|
266
|
+
if (BASE_MAP[xc] !== 255) { throw new TypeError(x + ' is ambiguous') }
|
267
|
+
BASE_MAP[xc] = i;
|
268
|
+
}
|
269
|
+
var BASE = ALPHABET.length;
|
270
|
+
var LEADER = ALPHABET.charAt(0);
|
271
|
+
var FACTOR = Math.log(BASE) / Math.log(256); // log(BASE) / log(256), rounded up
|
272
|
+
var iFACTOR = Math.log(256) / Math.log(BASE); // log(256) / log(BASE), rounded up
|
273
|
+
function encode (source) {
|
274
|
+
if (source instanceof Uint8Array) ; else if (ArrayBuffer.isView(source)) {
|
275
|
+
source = new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
|
276
|
+
} else if (Array.isArray(source)) {
|
277
|
+
source = Uint8Array.from(source);
|
278
|
+
}
|
279
|
+
if (!(source instanceof Uint8Array)) { throw new TypeError('Expected Uint8Array') }
|
280
|
+
if (source.length === 0) { return '' }
|
281
|
+
// Skip & count leading zeroes.
|
282
|
+
var zeroes = 0;
|
283
|
+
var length = 0;
|
284
|
+
var pbegin = 0;
|
285
|
+
var pend = source.length;
|
286
|
+
while (pbegin !== pend && source[pbegin] === 0) {
|
287
|
+
pbegin++;
|
288
|
+
zeroes++;
|
289
|
+
}
|
290
|
+
// Allocate enough space in big-endian base58 representation.
|
291
|
+
var size = ((pend - pbegin) * iFACTOR + 1) >>> 0;
|
292
|
+
var b58 = new Uint8Array(size);
|
293
|
+
// Process the bytes.
|
294
|
+
while (pbegin !== pend) {
|
295
|
+
var carry = source[pbegin];
|
296
|
+
// Apply "b58 = b58 * 256 + ch".
|
297
|
+
var i = 0;
|
298
|
+
for (var it1 = size - 1; (carry !== 0 || i < length) && (it1 !== -1); it1--, i++) {
|
299
|
+
carry += (256 * b58[it1]) >>> 0;
|
300
|
+
b58[it1] = (carry % BASE) >>> 0;
|
301
|
+
carry = (carry / BASE) >>> 0;
|
302
|
+
}
|
303
|
+
if (carry !== 0) { throw new Error('Non-zero carry') }
|
304
|
+
length = i;
|
305
|
+
pbegin++;
|
306
|
+
}
|
307
|
+
// Skip leading zeroes in base58 result.
|
308
|
+
var it2 = size - length;
|
309
|
+
while (it2 !== size && b58[it2] === 0) {
|
310
|
+
it2++;
|
311
|
+
}
|
312
|
+
// Translate the result into a string.
|
313
|
+
var str = LEADER.repeat(zeroes);
|
314
|
+
for (; it2 < size; ++it2) { str += ALPHABET.charAt(b58[it2]); }
|
315
|
+
return str
|
316
|
+
}
|
317
|
+
function decodeUnsafe (source) {
|
318
|
+
if (typeof source !== 'string') { throw new TypeError('Expected String') }
|
319
|
+
if (source.length === 0) { return new Uint8Array() }
|
320
|
+
var psz = 0;
|
321
|
+
// Skip leading spaces.
|
322
|
+
if (source[psz] === ' ') { return }
|
323
|
+
// Skip and count leading '1's.
|
324
|
+
var zeroes = 0;
|
325
|
+
var length = 0;
|
326
|
+
while (source[psz] === LEADER) {
|
327
|
+
zeroes++;
|
328
|
+
psz++;
|
329
|
+
}
|
330
|
+
// Allocate enough space in big-endian base256 representation.
|
331
|
+
var size = (((source.length - psz) * FACTOR) + 1) >>> 0; // log(58) / log(256), rounded up.
|
332
|
+
var b256 = new Uint8Array(size);
|
333
|
+
// Process the characters.
|
334
|
+
while (source[psz]) {
|
335
|
+
// Decode character
|
336
|
+
var carry = BASE_MAP[source.charCodeAt(psz)];
|
337
|
+
// Invalid character
|
338
|
+
if (carry === 255) { return }
|
339
|
+
var i = 0;
|
340
|
+
for (var it3 = size - 1; (carry !== 0 || i < length) && (it3 !== -1); it3--, i++) {
|
341
|
+
carry += (BASE * b256[it3]) >>> 0;
|
342
|
+
b256[it3] = (carry % 256) >>> 0;
|
343
|
+
carry = (carry / 256) >>> 0;
|
344
|
+
}
|
345
|
+
if (carry !== 0) { throw new Error('Non-zero carry') }
|
346
|
+
length = i;
|
347
|
+
psz++;
|
348
|
+
}
|
349
|
+
// Skip trailing spaces.
|
350
|
+
if (source[psz] === ' ') { return }
|
351
|
+
// Skip leading zeroes in b256.
|
352
|
+
var it4 = size - length;
|
353
|
+
while (it4 !== size && b256[it4] === 0) {
|
354
|
+
it4++;
|
355
|
+
}
|
356
|
+
var vch = new Uint8Array(zeroes + (size - it4));
|
357
|
+
var j = zeroes;
|
358
|
+
while (it4 !== size) {
|
359
|
+
vch[j++] = b256[it4++];
|
360
|
+
}
|
361
|
+
return vch
|
362
|
+
}
|
363
|
+
function decode (string) {
|
364
|
+
var buffer = decodeUnsafe(string);
|
365
|
+
if (buffer) { return buffer }
|
366
|
+
throw new Error(`Non-${name} character`)
|
367
|
+
}
|
368
|
+
return {
|
369
|
+
encode: encode,
|
370
|
+
decodeUnsafe: decodeUnsafe,
|
371
|
+
decode: decode
|
372
|
+
}
|
373
|
+
}
|
374
|
+
var src = base;
|
375
|
+
|
376
|
+
var _brrp__multiformats_scope_baseX = src;
|
377
|
+
|
378
|
+
/**
|
379
|
+
* Class represents both BaseEncoder and MultibaseEncoder meaning it
|
380
|
+
* can be used to encode to multibase or base encode without multibase
|
381
|
+
* prefix.
|
382
|
+
*
|
383
|
+
* @class
|
384
|
+
* @template {string} Base
|
385
|
+
* @template {string} Prefix
|
386
|
+
* @implements {API.MultibaseEncoder<Prefix>}
|
387
|
+
* @implements {API.BaseEncoder}
|
388
|
+
*/
|
389
|
+
class Encoder {
|
390
|
+
/**
|
391
|
+
* @param {Base} name
|
392
|
+
* @param {Prefix} prefix
|
393
|
+
* @param {(bytes:Uint8Array) => string} baseEncode
|
394
|
+
*/
|
395
|
+
constructor (name, prefix, baseEncode) {
|
396
|
+
this.name = name;
|
397
|
+
this.prefix = prefix;
|
398
|
+
this.baseEncode = baseEncode;
|
399
|
+
}
|
400
|
+
|
401
|
+
/**
|
402
|
+
* @param {Uint8Array} bytes
|
403
|
+
* @returns {API.Multibase<Prefix>}
|
404
|
+
*/
|
405
|
+
encode (bytes) {
|
406
|
+
if (bytes instanceof Uint8Array) {
|
407
|
+
return `${this.prefix}${this.baseEncode(bytes)}`
|
408
|
+
} else {
|
409
|
+
throw Error('Unknown type, must be binary type')
|
410
|
+
}
|
411
|
+
}
|
412
|
+
}
|
413
|
+
|
414
|
+
/**
|
415
|
+
* @template {string} Prefix
|
416
|
+
*/
|
417
|
+
/**
|
418
|
+
* Class represents both BaseDecoder and MultibaseDecoder so it could be used
|
419
|
+
* to decode multibases (with matching prefix) or just base decode strings
|
420
|
+
* with corresponding base encoding.
|
421
|
+
*
|
422
|
+
* @class
|
423
|
+
* @template {string} Base
|
424
|
+
* @template {string} Prefix
|
425
|
+
* @implements {API.MultibaseDecoder<Prefix>}
|
426
|
+
* @implements {API.UnibaseDecoder<Prefix>}
|
427
|
+
* @implements {API.BaseDecoder}
|
428
|
+
*/
|
429
|
+
class Decoder {
|
430
|
+
/**
|
431
|
+
* @param {Base} name
|
432
|
+
* @param {Prefix} prefix
|
433
|
+
* @param {(text:string) => Uint8Array} baseDecode
|
434
|
+
*/
|
435
|
+
constructor (name, prefix, baseDecode) {
|
436
|
+
this.name = name;
|
437
|
+
this.prefix = prefix;
|
438
|
+
/* c8 ignore next 3 */
|
439
|
+
if (prefix.codePointAt(0) === undefined) {
|
440
|
+
throw new Error('Invalid prefix character')
|
441
|
+
}
|
442
|
+
/** @private */
|
443
|
+
this.prefixCodePoint = /** @type {number} */ (prefix.codePointAt(0));
|
444
|
+
this.baseDecode = baseDecode;
|
445
|
+
}
|
446
|
+
|
447
|
+
/**
|
448
|
+
* @param {string} text
|
449
|
+
*/
|
450
|
+
decode (text) {
|
451
|
+
if (typeof text === 'string') {
|
452
|
+
if (text.codePointAt(0) !== this.prefixCodePoint) {
|
453
|
+
throw Error(`Unable to decode multibase string ${JSON.stringify(text)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`)
|
454
|
+
}
|
455
|
+
return this.baseDecode(text.slice(this.prefix.length))
|
456
|
+
} else {
|
457
|
+
throw Error('Can only multibase decode strings')
|
458
|
+
}
|
459
|
+
}
|
460
|
+
|
461
|
+
/**
|
462
|
+
* @template {string} OtherPrefix
|
463
|
+
* @param {API.UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
|
464
|
+
* @returns {ComposedDecoder<Prefix|OtherPrefix>}
|
465
|
+
*/
|
466
|
+
or (decoder) {
|
467
|
+
return or(this, decoder)
|
468
|
+
}
|
469
|
+
}
|
470
|
+
|
471
|
+
/**
|
472
|
+
* @template {string} Prefix
|
473
|
+
* @typedef {Record<Prefix, API.UnibaseDecoder<Prefix>>} Decoders
|
474
|
+
*/
|
475
|
+
|
476
|
+
/**
|
477
|
+
* @template {string} Prefix
|
478
|
+
* @implements {API.MultibaseDecoder<Prefix>}
|
479
|
+
* @implements {API.CombobaseDecoder<Prefix>}
|
480
|
+
*/
|
481
|
+
class ComposedDecoder {
|
482
|
+
/**
|
483
|
+
* @param {Decoders<Prefix>} decoders
|
484
|
+
*/
|
485
|
+
constructor (decoders) {
|
486
|
+
this.decoders = decoders;
|
487
|
+
}
|
488
|
+
|
489
|
+
/**
|
490
|
+
* @template {string} OtherPrefix
|
491
|
+
* @param {API.UnibaseDecoder<OtherPrefix>|ComposedDecoder<OtherPrefix>} decoder
|
492
|
+
* @returns {ComposedDecoder<Prefix|OtherPrefix>}
|
493
|
+
*/
|
494
|
+
or (decoder) {
|
495
|
+
return or(this, decoder)
|
496
|
+
}
|
497
|
+
|
498
|
+
/**
|
499
|
+
* @param {string} input
|
500
|
+
* @returns {Uint8Array}
|
501
|
+
*/
|
502
|
+
decode (input) {
|
503
|
+
const prefix = /** @type {Prefix} */ (input[0]);
|
504
|
+
const decoder = this.decoders[prefix];
|
505
|
+
if (decoder) {
|
506
|
+
return decoder.decode(input)
|
507
|
+
} else {
|
508
|
+
throw RangeError(`Unable to decode multibase string ${JSON.stringify(input)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)
|
509
|
+
}
|
510
|
+
}
|
511
|
+
}
|
512
|
+
|
513
|
+
/**
|
514
|
+
* @template {string} L
|
515
|
+
* @template {string} R
|
516
|
+
* @param {API.UnibaseDecoder<L>|API.CombobaseDecoder<L>} left
|
517
|
+
* @param {API.UnibaseDecoder<R>|API.CombobaseDecoder<R>} right
|
518
|
+
* @returns {ComposedDecoder<L|R>}
|
519
|
+
*/
|
520
|
+
const or = (left, right) => new ComposedDecoder(/** @type {Decoders<L|R>} */({
|
521
|
+
...(left.decoders || { [/** @type API.UnibaseDecoder<L> */(left).prefix]: left }),
|
522
|
+
...(right.decoders || { [/** @type API.UnibaseDecoder<R> */(right).prefix]: right })
|
523
|
+
}));
|
524
|
+
|
525
|
+
/**
|
526
|
+
* @class
|
527
|
+
* @template {string} Base
|
528
|
+
* @template {string} Prefix
|
529
|
+
* @implements {API.MultibaseCodec<Prefix>}
|
530
|
+
* @implements {API.MultibaseEncoder<Prefix>}
|
531
|
+
* @implements {API.MultibaseDecoder<Prefix>}
|
532
|
+
* @implements {API.BaseCodec}
|
533
|
+
* @implements {API.BaseEncoder}
|
534
|
+
* @implements {API.BaseDecoder}
|
535
|
+
*/
|
536
|
+
class Codec {
|
537
|
+
/**
|
538
|
+
* @param {Base} name
|
539
|
+
* @param {Prefix} prefix
|
540
|
+
* @param {(bytes:Uint8Array) => string} baseEncode
|
541
|
+
* @param {(text:string) => Uint8Array} baseDecode
|
542
|
+
*/
|
543
|
+
constructor (name, prefix, baseEncode, baseDecode) {
|
544
|
+
this.name = name;
|
545
|
+
this.prefix = prefix;
|
546
|
+
this.baseEncode = baseEncode;
|
547
|
+
this.baseDecode = baseDecode;
|
548
|
+
this.encoder = new Encoder(name, prefix, baseEncode);
|
549
|
+
this.decoder = new Decoder(name, prefix, baseDecode);
|
550
|
+
}
|
551
|
+
|
552
|
+
/**
|
553
|
+
* @param {Uint8Array} input
|
554
|
+
*/
|
555
|
+
encode (input) {
|
556
|
+
return this.encoder.encode(input)
|
557
|
+
}
|
558
|
+
|
559
|
+
/**
|
560
|
+
* @param {string} input
|
561
|
+
*/
|
562
|
+
decode (input) {
|
563
|
+
return this.decoder.decode(input)
|
564
|
+
}
|
565
|
+
}
|
566
|
+
|
567
|
+
/**
|
568
|
+
* @template {string} Base
|
569
|
+
* @template {string} Prefix
|
570
|
+
* @param {object} options
|
571
|
+
* @param {Base} options.name
|
572
|
+
* @param {Prefix} options.prefix
|
573
|
+
* @param {(bytes:Uint8Array) => string} options.encode
|
574
|
+
* @param {(input:string) => Uint8Array} options.decode
|
575
|
+
* @returns {Codec<Base, Prefix>}
|
576
|
+
*/
|
577
|
+
const from$1 = ({ name, prefix, encode, decode }) =>
|
578
|
+
new Codec(name, prefix, encode, decode);
|
579
|
+
|
580
|
+
/**
|
581
|
+
* @template {string} Base
|
582
|
+
* @template {string} Prefix
|
583
|
+
* @param {object} options
|
584
|
+
* @param {Base} options.name
|
585
|
+
* @param {Prefix} options.prefix
|
586
|
+
* @param {string} options.alphabet
|
587
|
+
* @returns {Codec<Base, Prefix>}
|
588
|
+
*/
|
589
|
+
const baseX = ({ prefix, name, alphabet }) => {
|
590
|
+
const { encode, decode } = _brrp__multiformats_scope_baseX(alphabet, name);
|
591
|
+
return from$1({
|
592
|
+
prefix,
|
593
|
+
name,
|
594
|
+
encode,
|
595
|
+
/**
|
596
|
+
* @param {string} text
|
597
|
+
*/
|
598
|
+
decode: text => coerce(decode(text))
|
599
|
+
})
|
600
|
+
};
|
601
|
+
|
602
|
+
/**
|
603
|
+
* @param {string} string
|
604
|
+
* @param {string} alphabet
|
605
|
+
* @param {number} bitsPerChar
|
606
|
+
* @param {string} name
|
607
|
+
* @returns {Uint8Array}
|
608
|
+
*/
|
609
|
+
const decode$1 = (string, alphabet, bitsPerChar, name) => {
|
610
|
+
// Build the character lookup table:
|
611
|
+
/** @type {Record<string, number>} */
|
612
|
+
const codes = {};
|
613
|
+
for (let i = 0; i < alphabet.length; ++i) {
|
614
|
+
codes[alphabet[i]] = i;
|
615
|
+
}
|
616
|
+
|
617
|
+
// Count the padding bytes:
|
618
|
+
let end = string.length;
|
619
|
+
while (string[end - 1] === '=') {
|
620
|
+
--end;
|
621
|
+
}
|
622
|
+
|
623
|
+
// Allocate the output:
|
624
|
+
const out = new Uint8Array((end * bitsPerChar / 8) | 0);
|
625
|
+
|
626
|
+
// Parse the data:
|
627
|
+
let bits = 0; // Number of bits currently in the buffer
|
628
|
+
let buffer = 0; // Bits waiting to be written out, MSB first
|
629
|
+
let written = 0; // Next byte to write
|
630
|
+
for (let i = 0; i < end; ++i) {
|
631
|
+
// Read one character from the string:
|
632
|
+
const value = codes[string[i]];
|
633
|
+
if (value === undefined) {
|
634
|
+
throw new SyntaxError(`Non-${name} character`)
|
635
|
+
}
|
636
|
+
|
637
|
+
// Append the bits to the buffer:
|
638
|
+
buffer = (buffer << bitsPerChar) | value;
|
639
|
+
bits += bitsPerChar;
|
640
|
+
|
641
|
+
// Write out some bits if the buffer has a byte's worth:
|
642
|
+
if (bits >= 8) {
|
643
|
+
bits -= 8;
|
644
|
+
out[written++] = 0xff & (buffer >> bits);
|
645
|
+
}
|
646
|
+
}
|
647
|
+
|
648
|
+
// Verify that we have received just enough bits:
|
649
|
+
if (bits >= bitsPerChar || 0xff & (buffer << (8 - bits))) {
|
650
|
+
throw new SyntaxError('Unexpected end of data')
|
651
|
+
}
|
652
|
+
|
653
|
+
return out
|
654
|
+
};
|
655
|
+
|
656
|
+
/**
|
657
|
+
* @param {Uint8Array} data
|
658
|
+
* @param {string} alphabet
|
659
|
+
* @param {number} bitsPerChar
|
660
|
+
* @returns {string}
|
661
|
+
*/
|
662
|
+
const encode$2 = (data, alphabet, bitsPerChar) => {
|
663
|
+
const pad = alphabet[alphabet.length - 1] === '=';
|
664
|
+
const mask = (1 << bitsPerChar) - 1;
|
665
|
+
let out = '';
|
666
|
+
|
667
|
+
let bits = 0; // Number of bits currently in the buffer
|
668
|
+
let buffer = 0; // Bits waiting to be written out, MSB first
|
669
|
+
for (let i = 0; i < data.length; ++i) {
|
670
|
+
// Slurp data into the buffer:
|
671
|
+
buffer = (buffer << 8) | data[i];
|
672
|
+
bits += 8;
|
673
|
+
|
674
|
+
// Write out as much as we can:
|
675
|
+
while (bits > bitsPerChar) {
|
676
|
+
bits -= bitsPerChar;
|
677
|
+
out += alphabet[mask & (buffer >> bits)];
|
678
|
+
}
|
679
|
+
}
|
680
|
+
|
681
|
+
// Partial character:
|
682
|
+
if (bits) {
|
683
|
+
out += alphabet[mask & (buffer << (bitsPerChar - bits))];
|
684
|
+
}
|
685
|
+
|
686
|
+
// Add padding characters until we hit a byte boundary:
|
687
|
+
if (pad) {
|
688
|
+
while ((out.length * bitsPerChar) & 7) {
|
689
|
+
out += '=';
|
690
|
+
}
|
691
|
+
}
|
692
|
+
|
693
|
+
return out
|
694
|
+
};
|
695
|
+
|
696
|
+
/**
|
697
|
+
* RFC4648 Factory
|
698
|
+
*
|
699
|
+
* @template {string} Base
|
700
|
+
* @template {string} Prefix
|
701
|
+
* @param {object} options
|
702
|
+
* @param {Base} options.name
|
703
|
+
* @param {Prefix} options.prefix
|
704
|
+
* @param {string} options.alphabet
|
705
|
+
* @param {number} options.bitsPerChar
|
706
|
+
*/
|
707
|
+
const rfc4648 = ({ name, prefix, bitsPerChar, alphabet }) => {
|
708
|
+
return from$1({
|
709
|
+
prefix,
|
710
|
+
name,
|
711
|
+
encode (input) {
|
712
|
+
return encode$2(input, alphabet, bitsPerChar)
|
713
|
+
},
|
714
|
+
decode (input) {
|
715
|
+
return decode$1(input, alphabet, bitsPerChar, name)
|
716
|
+
}
|
717
|
+
})
|
718
|
+
};
|
719
|
+
|
720
|
+
const base58btc = baseX({
|
721
|
+
name: 'base58btc',
|
722
|
+
prefix: 'z',
|
723
|
+
alphabet: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
724
|
+
});
|
725
|
+
|
726
|
+
const base58flickr = baseX({
|
727
|
+
name: 'base58flickr',
|
728
|
+
prefix: 'Z',
|
729
|
+
alphabet: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
|
730
|
+
});
|
731
|
+
|
732
|
+
var base58 = /*#__PURE__*/Object.freeze({
|
733
|
+
__proto__: null,
|
734
|
+
base58btc: base58btc,
|
735
|
+
base58flickr: base58flickr
|
736
|
+
});
|
737
|
+
|
738
|
+
const base32 = rfc4648({
|
739
|
+
prefix: 'b',
|
740
|
+
name: 'base32',
|
741
|
+
alphabet: 'abcdefghijklmnopqrstuvwxyz234567',
|
742
|
+
bitsPerChar: 5
|
743
|
+
});
|
744
|
+
|
745
|
+
const base32upper = rfc4648({
|
746
|
+
prefix: 'B',
|
747
|
+
name: 'base32upper',
|
748
|
+
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
|
749
|
+
bitsPerChar: 5
|
750
|
+
});
|
751
|
+
|
752
|
+
const base32pad = rfc4648({
|
753
|
+
prefix: 'c',
|
754
|
+
name: 'base32pad',
|
755
|
+
alphabet: 'abcdefghijklmnopqrstuvwxyz234567=',
|
756
|
+
bitsPerChar: 5
|
757
|
+
});
|
758
|
+
|
759
|
+
const base32padupper = rfc4648({
|
760
|
+
prefix: 'C',
|
761
|
+
name: 'base32padupper',
|
762
|
+
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=',
|
763
|
+
bitsPerChar: 5
|
764
|
+
});
|
765
|
+
|
766
|
+
const base32hex = rfc4648({
|
767
|
+
prefix: 'v',
|
768
|
+
name: 'base32hex',
|
769
|
+
alphabet: '0123456789abcdefghijklmnopqrstuv',
|
770
|
+
bitsPerChar: 5
|
771
|
+
});
|
772
|
+
|
773
|
+
const base32hexupper = rfc4648({
|
774
|
+
prefix: 'V',
|
775
|
+
name: 'base32hexupper',
|
776
|
+
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV',
|
777
|
+
bitsPerChar: 5
|
778
|
+
});
|
779
|
+
|
780
|
+
const base32hexpad = rfc4648({
|
781
|
+
prefix: 't',
|
782
|
+
name: 'base32hexpad',
|
783
|
+
alphabet: '0123456789abcdefghijklmnopqrstuv=',
|
784
|
+
bitsPerChar: 5
|
785
|
+
});
|
786
|
+
|
787
|
+
const base32hexpadupper = rfc4648({
|
788
|
+
prefix: 'T',
|
789
|
+
name: 'base32hexpadupper',
|
790
|
+
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV=',
|
791
|
+
bitsPerChar: 5
|
792
|
+
});
|
793
|
+
|
794
|
+
const base32z = rfc4648({
|
795
|
+
prefix: 'h',
|
796
|
+
name: 'base32z',
|
797
|
+
alphabet: 'ybndrfg8ejkmcpqxot1uwisza345h769',
|
798
|
+
bitsPerChar: 5
|
799
|
+
});
|
800
|
+
|
801
|
+
var base32$1 = /*#__PURE__*/Object.freeze({
|
802
|
+
__proto__: null,
|
803
|
+
base32: base32,
|
804
|
+
base32upper: base32upper,
|
805
|
+
base32pad: base32pad,
|
806
|
+
base32padupper: base32padupper,
|
807
|
+
base32hex: base32hex,
|
808
|
+
base32hexupper: base32hexupper,
|
809
|
+
base32hexpad: base32hexpad,
|
810
|
+
base32hexpadupper: base32hexpadupper,
|
811
|
+
base32z: base32z
|
812
|
+
});
|
813
|
+
|
814
|
+
/**
|
815
|
+
* @template {API.Link<unknown, number, number, API.Version>} T
|
816
|
+
* @template {string} Prefix
|
817
|
+
* @param {T} link
|
818
|
+
* @param {API.MultibaseEncoder<Prefix>} [base]
|
819
|
+
* @returns {API.ToString<T, Prefix>}
|
820
|
+
*/
|
821
|
+
const format = (link, base) => {
|
822
|
+
const { bytes, version } = link;
|
823
|
+
switch (version) {
|
824
|
+
case 0:
|
825
|
+
return toStringV0(
|
826
|
+
bytes,
|
827
|
+
baseCache(link),
|
828
|
+
/** @type {API.MultibaseEncoder<"z">} */ (base) || base58btc.encoder
|
829
|
+
)
|
830
|
+
default:
|
831
|
+
return toStringV1(
|
832
|
+
bytes,
|
833
|
+
baseCache(link),
|
834
|
+
/** @type {API.MultibaseEncoder<Prefix>} */ (base || base32.encoder)
|
835
|
+
)
|
836
|
+
}
|
837
|
+
};
|
838
|
+
|
839
|
+
/** @type {WeakMap<API.UnknownLink, Map<string, string>>} */
|
840
|
+
const cache = new WeakMap();
|
841
|
+
|
842
|
+
/**
|
843
|
+
* @param {API.UnknownLink} cid
|
844
|
+
* @returns {Map<string, string>}
|
845
|
+
*/
|
846
|
+
const baseCache = cid => {
|
847
|
+
const baseCache = cache.get(cid);
|
848
|
+
if (baseCache == null) {
|
849
|
+
const baseCache = new Map();
|
850
|
+
cache.set(cid, baseCache);
|
851
|
+
return baseCache
|
852
|
+
}
|
853
|
+
return baseCache
|
854
|
+
};
|
855
|
+
|
856
|
+
/**
|
857
|
+
* @template {unknown} [Data=unknown]
|
858
|
+
* @template {number} [Format=number]
|
859
|
+
* @template {number} [Alg=number]
|
860
|
+
* @template {API.Version} [Version=API.Version]
|
861
|
+
* @implements {API.Link<Data, Format, Alg, Version>}
|
862
|
+
*/
|
863
|
+
|
864
|
+
class CID {
|
865
|
+
/**
|
866
|
+
* @param {Version} version - Version of the CID
|
867
|
+
* @param {Format} code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
|
868
|
+
* @param {API.MultihashDigest<Alg>} multihash - (Multi)hash of the of the content.
|
869
|
+
* @param {Uint8Array} bytes
|
870
|
+
*
|
871
|
+
*/
|
872
|
+
constructor (version, code, multihash, bytes) {
|
873
|
+
/** @readonly */
|
874
|
+
this.code = code;
|
875
|
+
/** @readonly */
|
876
|
+
this.version = version;
|
877
|
+
/** @readonly */
|
878
|
+
this.multihash = multihash;
|
879
|
+
/** @readonly */
|
880
|
+
this.bytes = bytes;
|
881
|
+
|
882
|
+
// flag to serializers that this is a CID and
|
883
|
+
// should be treated specially
|
884
|
+
/** @readonly */
|
885
|
+
this['/'] = bytes;
|
886
|
+
}
|
887
|
+
|
888
|
+
/**
|
889
|
+
* Signalling `cid.asCID === cid` has been replaced with `cid['/'] === cid.bytes`
|
890
|
+
* please either use `CID.asCID(cid)` or switch to new signalling mechanism
|
891
|
+
*
|
892
|
+
* @deprecated
|
893
|
+
*/
|
894
|
+
get asCID () {
|
895
|
+
return this
|
896
|
+
}
|
897
|
+
|
898
|
+
// ArrayBufferView
|
899
|
+
get byteOffset () {
|
900
|
+
return this.bytes.byteOffset
|
901
|
+
}
|
902
|
+
|
903
|
+
// ArrayBufferView
|
904
|
+
get byteLength () {
|
905
|
+
return this.bytes.byteLength
|
906
|
+
}
|
907
|
+
|
908
|
+
/**
|
909
|
+
* @returns {CID<Data, API.DAG_PB, API.SHA_256, 0>}
|
910
|
+
*/
|
911
|
+
toV0 () {
|
912
|
+
switch (this.version) {
|
913
|
+
case 0: {
|
914
|
+
return /** @type {CID<Data, API.DAG_PB, API.SHA_256, 0>} */ (this)
|
915
|
+
}
|
916
|
+
case 1: {
|
917
|
+
const { code, multihash } = this;
|
918
|
+
|
919
|
+
if (code !== DAG_PB_CODE) {
|
920
|
+
throw new Error('Cannot convert a non dag-pb CID to CIDv0')
|
921
|
+
}
|
922
|
+
|
923
|
+
// sha2-256
|
924
|
+
if (multihash.code !== SHA_256_CODE) {
|
925
|
+
throw new Error('Cannot convert non sha2-256 multihash CID to CIDv0')
|
926
|
+
}
|
927
|
+
|
928
|
+
return /** @type {CID<Data, API.DAG_PB, API.SHA_256, 0>} */ (
|
929
|
+
CID.createV0(
|
930
|
+
/** @type {API.MultihashDigest<API.SHA_256>} */ (multihash)
|
931
|
+
)
|
932
|
+
)
|
933
|
+
}
|
934
|
+
default: {
|
935
|
+
throw Error(
|
936
|
+
`Can not convert CID version ${this.version} to version 0. This is a bug please report`
|
937
|
+
)
|
938
|
+
}
|
939
|
+
}
|
940
|
+
}
|
941
|
+
|
942
|
+
/**
|
943
|
+
* @returns {CID<Data, Format, Alg, 1>}
|
944
|
+
*/
|
945
|
+
toV1 () {
|
946
|
+
switch (this.version) {
|
947
|
+
case 0: {
|
948
|
+
const { code, digest } = this.multihash;
|
949
|
+
const multihash = create(code, digest);
|
950
|
+
return /** @type {CID<Data, Format, Alg, 1>} */ (
|
951
|
+
CID.createV1(this.code, multihash)
|
952
|
+
)
|
953
|
+
}
|
954
|
+
case 1: {
|
955
|
+
return /** @type {CID<Data, Format, Alg, 1>} */ (this)
|
956
|
+
}
|
957
|
+
default: {
|
958
|
+
throw Error(
|
959
|
+
`Can not convert CID version ${this.version} to version 1. This is a bug please report`
|
960
|
+
)
|
961
|
+
}
|
962
|
+
}
|
963
|
+
}
|
964
|
+
|
965
|
+
/**
|
966
|
+
* @param {unknown} other
|
967
|
+
* @returns {other is CID<Data, Format, Alg, Version>}
|
968
|
+
*/
|
969
|
+
equals (other) {
|
970
|
+
return CID.equals(this, other)
|
971
|
+
}
|
972
|
+
|
973
|
+
/**
|
974
|
+
* @template {unknown} Data
|
975
|
+
* @template {number} Format
|
976
|
+
* @template {number} Alg
|
977
|
+
* @template {API.Version} Version
|
978
|
+
* @param {API.Link<Data, Format, Alg, Version>} self
|
979
|
+
* @param {unknown} other
|
980
|
+
* @returns {other is CID}
|
981
|
+
*/
|
982
|
+
static equals (self, other) {
|
983
|
+
const unknown =
|
984
|
+
/** @type {{code?:unknown, version?:unknown, multihash?:unknown}} */ (
|
985
|
+
other
|
986
|
+
);
|
987
|
+
return (
|
988
|
+
unknown &&
|
989
|
+
self.code === unknown.code &&
|
990
|
+
self.version === unknown.version &&
|
991
|
+
equals$1(self.multihash, unknown.multihash)
|
992
|
+
)
|
993
|
+
}
|
994
|
+
|
995
|
+
/**
|
996
|
+
* @param {API.MultibaseEncoder<string>} [base]
|
997
|
+
* @returns {string}
|
998
|
+
*/
|
999
|
+
toString (base) {
|
1000
|
+
return format(this, base)
|
1001
|
+
}
|
1002
|
+
|
1003
|
+
toJSON () {
|
1004
|
+
return {
|
1005
|
+
code: this.code,
|
1006
|
+
version: this.version,
|
1007
|
+
hash: this.multihash.bytes
|
1008
|
+
}
|
1009
|
+
}
|
1010
|
+
|
1011
|
+
link () {
|
1012
|
+
return this
|
1013
|
+
}
|
1014
|
+
|
1015
|
+
get [Symbol.toStringTag] () {
|
1016
|
+
return 'CID'
|
1017
|
+
}
|
1018
|
+
|
1019
|
+
// Legacy
|
1020
|
+
|
1021
|
+
[Symbol.for('nodejs.util.inspect.custom')] () {
|
1022
|
+
return `CID(${this.toString()})`
|
1023
|
+
}
|
1024
|
+
|
1025
|
+
/**
|
1026
|
+
* Takes any input `value` and returns a `CID` instance if it was
|
1027
|
+
* a `CID` otherwise returns `null`. If `value` is instanceof `CID`
|
1028
|
+
* it will return value back. If `value` is not instance of this CID
|
1029
|
+
* class, but is compatible CID it will return new instance of this
|
1030
|
+
* `CID` class. Otherwise returs null.
|
1031
|
+
*
|
1032
|
+
* This allows two different incompatible versions of CID library to
|
1033
|
+
* co-exist and interop as long as binary interface is compatible.
|
1034
|
+
*
|
1035
|
+
* @template {unknown} Data
|
1036
|
+
* @template {number} Format
|
1037
|
+
* @template {number} Alg
|
1038
|
+
* @template {API.Version} Version
|
1039
|
+
* @template {unknown} U
|
1040
|
+
* @param {API.Link<Data, Format, Alg, Version>|U} input
|
1041
|
+
* @returns {CID<Data, Format, Alg, Version>|null}
|
1042
|
+
*/
|
1043
|
+
static asCID (input) {
|
1044
|
+
if (input == null) {
|
1045
|
+
return null
|
1046
|
+
}
|
1047
|
+
|
1048
|
+
const value = /** @type {any} */ (input);
|
1049
|
+
if (value instanceof CID) {
|
1050
|
+
// If value is instance of CID then we're all set.
|
1051
|
+
return value
|
1052
|
+
} else if ((value['/'] != null && value['/'] === value.bytes) || value.asCID === value) {
|
1053
|
+
// If value isn't instance of this CID class but `this.asCID === this` or
|
1054
|
+
// `value['/'] === value.bytes` is true it is CID instance coming from a
|
1055
|
+
// different implementation (diff version or duplicate). In that case we
|
1056
|
+
// rebase it to this `CID` implementation so caller is guaranteed to get
|
1057
|
+
// instance with expected API.
|
1058
|
+
const { version, code, multihash, bytes } = value;
|
1059
|
+
return new CID(
|
1060
|
+
version,
|
1061
|
+
code,
|
1062
|
+
/** @type {API.MultihashDigest<Alg>} */ (multihash),
|
1063
|
+
bytes || encodeCID(version, code, multihash.bytes)
|
1064
|
+
)
|
1065
|
+
} else if (value[cidSymbol] === true) {
|
1066
|
+
// If value is a CID from older implementation that used to be tagged via
|
1067
|
+
// symbol we still rebase it to the this `CID` implementation by
|
1068
|
+
// delegating that to a constructor.
|
1069
|
+
const { version, multihash, code } = value;
|
1070
|
+
const digest =
|
1071
|
+
/** @type {API.MultihashDigest<Alg>} */
|
1072
|
+
(decode$2(multihash));
|
1073
|
+
return CID.create(version, code, digest)
|
1074
|
+
} else {
|
1075
|
+
// Otherwise value is not a CID (or an incompatible version of it) in
|
1076
|
+
// which case we return `null`.
|
1077
|
+
return null
|
1078
|
+
}
|
1079
|
+
}
|
1080
|
+
|
1081
|
+
/**
|
1082
|
+
*
|
1083
|
+
* @template {unknown} Data
|
1084
|
+
* @template {number} Format
|
1085
|
+
* @template {number} Alg
|
1086
|
+
* @template {API.Version} Version
|
1087
|
+
* @param {Version} version - Version of the CID
|
1088
|
+
* @param {Format} code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
|
1089
|
+
* @param {API.MultihashDigest<Alg>} digest - (Multi)hash of the of the content.
|
1090
|
+
* @returns {CID<Data, Format, Alg, Version>}
|
1091
|
+
*/
|
1092
|
+
static create (version, code, digest) {
|
1093
|
+
if (typeof code !== 'number') {
|
1094
|
+
throw new Error('String codecs are no longer supported')
|
1095
|
+
}
|
1096
|
+
|
1097
|
+
if (!(digest.bytes instanceof Uint8Array)) {
|
1098
|
+
throw new Error('Invalid digest')
|
1099
|
+
}
|
1100
|
+
|
1101
|
+
switch (version) {
|
1102
|
+
case 0: {
|
1103
|
+
if (code !== DAG_PB_CODE) {
|
1104
|
+
throw new Error(
|
1105
|
+
`Version 0 CID must use dag-pb (code: ${DAG_PB_CODE}) block encoding`
|
1106
|
+
)
|
1107
|
+
} else {
|
1108
|
+
return new CID(version, code, digest, digest.bytes)
|
1109
|
+
}
|
1110
|
+
}
|
1111
|
+
case 1: {
|
1112
|
+
const bytes = encodeCID(version, code, digest.bytes);
|
1113
|
+
return new CID(version, code, digest, bytes)
|
1114
|
+
}
|
1115
|
+
default: {
|
1116
|
+
throw new Error('Invalid version')
|
1117
|
+
}
|
1118
|
+
}
|
1119
|
+
}
|
1120
|
+
|
1121
|
+
/**
|
1122
|
+
* Simplified version of `create` for CIDv0.
|
1123
|
+
*
|
1124
|
+
* @template {unknown} [T=unknown]
|
1125
|
+
* @param {API.MultihashDigest<typeof SHA_256_CODE>} digest - Multihash.
|
1126
|
+
* @returns {CID<T, typeof DAG_PB_CODE, typeof SHA_256_CODE, 0>}
|
1127
|
+
*/
|
1128
|
+
static createV0 (digest) {
|
1129
|
+
return CID.create(0, DAG_PB_CODE, digest)
|
1130
|
+
}
|
1131
|
+
|
1132
|
+
/**
|
1133
|
+
* Simplified version of `create` for CIDv1.
|
1134
|
+
*
|
1135
|
+
* @template {unknown} Data
|
1136
|
+
* @template {number} Code
|
1137
|
+
* @template {number} Alg
|
1138
|
+
* @param {Code} code - Content encoding format code.
|
1139
|
+
* @param {API.MultihashDigest<Alg>} digest - Miltihash of the content.
|
1140
|
+
* @returns {CID<Data, Code, Alg, 1>}
|
1141
|
+
*/
|
1142
|
+
static createV1 (code, digest) {
|
1143
|
+
return CID.create(1, code, digest)
|
1144
|
+
}
|
1145
|
+
|
1146
|
+
/**
|
1147
|
+
* Decoded a CID from its binary representation. The byte array must contain
|
1148
|
+
* only the CID with no additional bytes.
|
1149
|
+
*
|
1150
|
+
* An error will be thrown if the bytes provided do not contain a valid
|
1151
|
+
* binary representation of a CID.
|
1152
|
+
*
|
1153
|
+
* @template {unknown} Data
|
1154
|
+
* @template {number} Code
|
1155
|
+
* @template {number} Alg
|
1156
|
+
* @template {API.Version} Ver
|
1157
|
+
* @param {API.ByteView<API.Link<Data, Code, Alg, Ver>>} bytes
|
1158
|
+
* @returns {CID<Data, Code, Alg, Ver>}
|
1159
|
+
*/
|
1160
|
+
static decode (bytes) {
|
1161
|
+
const [cid, remainder] = CID.decodeFirst(bytes);
|
1162
|
+
if (remainder.length) {
|
1163
|
+
throw new Error('Incorrect length')
|
1164
|
+
}
|
1165
|
+
return cid
|
1166
|
+
}
|
1167
|
+
|
1168
|
+
/**
|
1169
|
+
* Decoded a CID from its binary representation at the beginning of a byte
|
1170
|
+
* array.
|
1171
|
+
*
|
1172
|
+
* Returns an array with the first element containing the CID and the second
|
1173
|
+
* element containing the remainder of the original byte array. The remainder
|
1174
|
+
* will be a zero-length byte array if the provided bytes only contained a
|
1175
|
+
* binary CID representation.
|
1176
|
+
*
|
1177
|
+
* @template {unknown} T
|
1178
|
+
* @template {number} C
|
1179
|
+
* @template {number} A
|
1180
|
+
* @template {API.Version} V
|
1181
|
+
* @param {API.ByteView<API.Link<T, C, A, V>>} bytes
|
1182
|
+
* @returns {[CID<T, C, A, V>, Uint8Array]}
|
1183
|
+
*/
|
1184
|
+
static decodeFirst (bytes) {
|
1185
|
+
const specs = CID.inspectBytes(bytes);
|
1186
|
+
const prefixSize = specs.size - specs.multihashSize;
|
1187
|
+
const multihashBytes = coerce(
|
1188
|
+
bytes.subarray(prefixSize, prefixSize + specs.multihashSize)
|
1189
|
+
);
|
1190
|
+
if (multihashBytes.byteLength !== specs.multihashSize) {
|
1191
|
+
throw new Error('Incorrect length')
|
1192
|
+
}
|
1193
|
+
const digestBytes = multihashBytes.subarray(
|
1194
|
+
specs.multihashSize - specs.digestSize
|
1195
|
+
);
|
1196
|
+
const digest = new Digest(
|
1197
|
+
specs.multihashCode,
|
1198
|
+
specs.digestSize,
|
1199
|
+
digestBytes,
|
1200
|
+
multihashBytes
|
1201
|
+
);
|
1202
|
+
const cid =
|
1203
|
+
specs.version === 0
|
1204
|
+
? CID.createV0(/** @type {API.MultihashDigest<API.SHA_256>} */ (digest))
|
1205
|
+
: CID.createV1(specs.codec, digest);
|
1206
|
+
return [/** @type {CID<T, C, A, V>} */(cid), bytes.subarray(specs.size)]
|
1207
|
+
}
|
1208
|
+
|
1209
|
+
/**
|
1210
|
+
* Inspect the initial bytes of a CID to determine its properties.
|
1211
|
+
*
|
1212
|
+
* Involves decoding up to 4 varints. Typically this will require only 4 to 6
|
1213
|
+
* bytes but for larger multicodec code values and larger multihash digest
|
1214
|
+
* lengths these varints can be quite large. It is recommended that at least
|
1215
|
+
* 10 bytes be made available in the `initialBytes` argument for a complete
|
1216
|
+
* inspection.
|
1217
|
+
*
|
1218
|
+
* @template {unknown} T
|
1219
|
+
* @template {number} C
|
1220
|
+
* @template {number} A
|
1221
|
+
* @template {API.Version} V
|
1222
|
+
* @param {API.ByteView<API.Link<T, C, A, V>>} initialBytes
|
1223
|
+
* @returns {{ version:V, codec:C, multihashCode:A, digestSize:number, multihashSize:number, size:number }}
|
1224
|
+
*/
|
1225
|
+
static inspectBytes (initialBytes) {
|
1226
|
+
let offset = 0;
|
1227
|
+
const next = () => {
|
1228
|
+
const [i, length] = decode$3(initialBytes.subarray(offset));
|
1229
|
+
offset += length;
|
1230
|
+
return i
|
1231
|
+
};
|
1232
|
+
|
1233
|
+
let version = /** @type {V} */ (next());
|
1234
|
+
let codec = /** @type {C} */ (DAG_PB_CODE);
|
1235
|
+
if (/** @type {number} */(version) === 18) {
|
1236
|
+
// CIDv0
|
1237
|
+
version = /** @type {V} */ (0);
|
1238
|
+
offset = 0;
|
1239
|
+
} else {
|
1240
|
+
codec = /** @type {C} */ (next());
|
1241
|
+
}
|
1242
|
+
|
1243
|
+
if (version !== 0 && version !== 1) {
|
1244
|
+
throw new RangeError(`Invalid CID version ${version}`)
|
1245
|
+
}
|
1246
|
+
|
1247
|
+
const prefixSize = offset;
|
1248
|
+
const multihashCode = /** @type {A} */ (next()); // multihash code
|
1249
|
+
const digestSize = next(); // multihash length
|
1250
|
+
const size = offset + digestSize;
|
1251
|
+
const multihashSize = size - prefixSize;
|
1252
|
+
|
1253
|
+
return { version, codec, multihashCode, digestSize, multihashSize, size }
|
1254
|
+
}
|
1255
|
+
|
1256
|
+
/**
|
1257
|
+
* Takes cid in a string representation and creates an instance. If `base`
|
1258
|
+
* decoder is not provided will use a default from the configuration. It will
|
1259
|
+
* throw an error if encoding of the CID is not compatible with supplied (or
|
1260
|
+
* a default decoder).
|
1261
|
+
*
|
1262
|
+
* @template {string} Prefix
|
1263
|
+
* @template {unknown} Data
|
1264
|
+
* @template {number} Code
|
1265
|
+
* @template {number} Alg
|
1266
|
+
* @template {API.Version} Ver
|
1267
|
+
* @param {API.ToString<API.Link<Data, Code, Alg, Ver>, Prefix>} source
|
1268
|
+
* @param {API.MultibaseDecoder<Prefix>} [base]
|
1269
|
+
* @returns {CID<Data, Code, Alg, Ver>}
|
1270
|
+
*/
|
1271
|
+
static parse (source, base) {
|
1272
|
+
const [prefix, bytes] = parseCIDtoBytes(source, base);
|
1273
|
+
|
1274
|
+
const cid = CID.decode(bytes);
|
1275
|
+
|
1276
|
+
// Cache string representation to avoid computing it on `this.toString()`
|
1277
|
+
baseCache(cid).set(prefix, source);
|
1278
|
+
|
1279
|
+
return cid
|
1280
|
+
}
|
1281
|
+
}
|
1282
|
+
|
1283
|
+
/**
|
1284
|
+
* @template {string} Prefix
|
1285
|
+
* @template {unknown} Data
|
1286
|
+
* @template {number} Code
|
1287
|
+
* @template {number} Alg
|
1288
|
+
* @template {API.Version} Ver
|
1289
|
+
* @param {API.ToString<API.Link<Data, Code, Alg, Ver>, Prefix>} source
|
1290
|
+
* @param {API.MultibaseDecoder<Prefix>} [base]
|
1291
|
+
* @returns {[Prefix, API.ByteView<API.Link<Data, Code, Alg, Ver>>]}
|
1292
|
+
*/
|
1293
|
+
const parseCIDtoBytes = (source, base) => {
|
1294
|
+
switch (source[0]) {
|
1295
|
+
// CIDv0 is parsed differently
|
1296
|
+
case 'Q': {
|
1297
|
+
const decoder = base || base58btc;
|
1298
|
+
return [
|
1299
|
+
/** @type {Prefix} */ (base58btc.prefix),
|
1300
|
+
decoder.decode(`${base58btc.prefix}${source}`)
|
1301
|
+
]
|
1302
|
+
}
|
1303
|
+
case base58btc.prefix: {
|
1304
|
+
const decoder = base || base58btc;
|
1305
|
+
return [/** @type {Prefix} */(base58btc.prefix), decoder.decode(source)]
|
1306
|
+
}
|
1307
|
+
case base32.prefix: {
|
1308
|
+
const decoder = base || base32;
|
1309
|
+
return [/** @type {Prefix} */(base32.prefix), decoder.decode(source)]
|
1310
|
+
}
|
1311
|
+
default: {
|
1312
|
+
if (base == null) {
|
1313
|
+
throw Error(
|
1314
|
+
'To parse non base32 or base58btc encoded CID multibase decoder must be provided'
|
1315
|
+
)
|
1316
|
+
}
|
1317
|
+
return [/** @type {Prefix} */(source[0]), base.decode(source)]
|
1318
|
+
}
|
1319
|
+
}
|
1320
|
+
};
|
1321
|
+
|
1322
|
+
/**
|
1323
|
+
*
|
1324
|
+
* @param {Uint8Array} bytes
|
1325
|
+
* @param {Map<string, string>} cache
|
1326
|
+
* @param {API.MultibaseEncoder<'z'>} base
|
1327
|
+
*/
|
1328
|
+
const toStringV0 = (bytes, cache, base) => {
|
1329
|
+
const { prefix } = base;
|
1330
|
+
if (prefix !== base58btc.prefix) {
|
1331
|
+
throw Error(`Cannot string encode V0 in ${base.name} encoding`)
|
1332
|
+
}
|
1333
|
+
|
1334
|
+
const cid = cache.get(prefix);
|
1335
|
+
if (cid == null) {
|
1336
|
+
const cid = base.encode(bytes).slice(1);
|
1337
|
+
cache.set(prefix, cid);
|
1338
|
+
return cid
|
1339
|
+
} else {
|
1340
|
+
return cid
|
1341
|
+
}
|
1342
|
+
};
|
1343
|
+
|
1344
|
+
/**
|
1345
|
+
* @template {string} Prefix
|
1346
|
+
* @param {Uint8Array} bytes
|
1347
|
+
* @param {Map<string, string>} cache
|
1348
|
+
* @param {API.MultibaseEncoder<Prefix>} base
|
1349
|
+
*/
|
1350
|
+
const toStringV1 = (bytes, cache, base) => {
|
1351
|
+
const { prefix } = base;
|
1352
|
+
const cid = cache.get(prefix);
|
1353
|
+
if (cid == null) {
|
1354
|
+
const cid = base.encode(bytes);
|
1355
|
+
cache.set(prefix, cid);
|
1356
|
+
return cid
|
1357
|
+
} else {
|
1358
|
+
return cid
|
1359
|
+
}
|
1360
|
+
};
|
1361
|
+
|
1362
|
+
const DAG_PB_CODE = 0x70;
|
1363
|
+
const SHA_256_CODE = 0x12;
|
1364
|
+
|
1365
|
+
/**
|
1366
|
+
* @param {API.Version} version
|
1367
|
+
* @param {number} code
|
1368
|
+
* @param {Uint8Array} multihash
|
1369
|
+
* @returns {Uint8Array}
|
1370
|
+
*/
|
1371
|
+
const encodeCID = (version, code, multihash) => {
|
1372
|
+
const codeOffset = encodingLength(version);
|
1373
|
+
const hashOffset = codeOffset + encodingLength(code);
|
1374
|
+
const bytes = new Uint8Array(hashOffset + multihash.byteLength);
|
1375
|
+
encodeTo(version, bytes, 0);
|
1376
|
+
encodeTo(code, bytes, codeOffset);
|
1377
|
+
bytes.set(multihash, hashOffset);
|
1378
|
+
return bytes
|
1379
|
+
};
|
1380
|
+
|
1381
|
+
const cidSymbol = Symbol.for('@ipld/js-cid/CID');
|
1382
|
+
|
1383
|
+
// @ts-check
|
1384
|
+
|
1385
|
+
const identity$2 = from$1({
|
1386
|
+
prefix: '\x00',
|
1387
|
+
name: 'identity',
|
1388
|
+
encode: (buf) => toString(buf),
|
1389
|
+
decode: (str) => fromString(str)
|
1390
|
+
});
|
1391
|
+
|
1392
|
+
var identityBase = /*#__PURE__*/Object.freeze({
|
1393
|
+
__proto__: null,
|
1394
|
+
identity: identity$2
|
1395
|
+
});
|
1396
|
+
|
1397
|
+
// @ts-check
|
1398
|
+
|
1399
|
+
const base2 = rfc4648({
|
1400
|
+
prefix: '0',
|
1401
|
+
name: 'base2',
|
1402
|
+
alphabet: '01',
|
1403
|
+
bitsPerChar: 1
|
1404
|
+
});
|
1405
|
+
|
1406
|
+
var base2$1 = /*#__PURE__*/Object.freeze({
|
1407
|
+
__proto__: null,
|
1408
|
+
base2: base2
|
1409
|
+
});
|
1410
|
+
|
1411
|
+
// @ts-check
|
1412
|
+
|
1413
|
+
const base8 = rfc4648({
|
1414
|
+
prefix: '7',
|
1415
|
+
name: 'base8',
|
1416
|
+
alphabet: '01234567',
|
1417
|
+
bitsPerChar: 3
|
1418
|
+
});
|
1419
|
+
|
1420
|
+
var base8$1 = /*#__PURE__*/Object.freeze({
|
1421
|
+
__proto__: null,
|
1422
|
+
base8: base8
|
1423
|
+
});
|
1424
|
+
|
1425
|
+
const base10 = baseX({
|
1426
|
+
prefix: '9',
|
1427
|
+
name: 'base10',
|
1428
|
+
alphabet: '0123456789'
|
1429
|
+
});
|
1430
|
+
|
1431
|
+
var base10$1 = /*#__PURE__*/Object.freeze({
|
1432
|
+
__proto__: null,
|
1433
|
+
base10: base10
|
1434
|
+
});
|
1435
|
+
|
1436
|
+
// @ts-check
|
1437
|
+
|
1438
|
+
const base16 = rfc4648({
|
1439
|
+
prefix: 'f',
|
1440
|
+
name: 'base16',
|
1441
|
+
alphabet: '0123456789abcdef',
|
1442
|
+
bitsPerChar: 4
|
1443
|
+
});
|
1444
|
+
|
1445
|
+
const base16upper = rfc4648({
|
1446
|
+
prefix: 'F',
|
1447
|
+
name: 'base16upper',
|
1448
|
+
alphabet: '0123456789ABCDEF',
|
1449
|
+
bitsPerChar: 4
|
1450
|
+
});
|
1451
|
+
|
1452
|
+
var base16$1 = /*#__PURE__*/Object.freeze({
|
1453
|
+
__proto__: null,
|
1454
|
+
base16: base16,
|
1455
|
+
base16upper: base16upper
|
1456
|
+
});
|
1457
|
+
|
1458
|
+
const base36 = baseX({
|
1459
|
+
prefix: 'k',
|
1460
|
+
name: 'base36',
|
1461
|
+
alphabet: '0123456789abcdefghijklmnopqrstuvwxyz'
|
1462
|
+
});
|
1463
|
+
|
1464
|
+
const base36upper = baseX({
|
1465
|
+
prefix: 'K',
|
1466
|
+
name: 'base36upper',
|
1467
|
+
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
1468
|
+
});
|
1469
|
+
|
1470
|
+
var base36$1 = /*#__PURE__*/Object.freeze({
|
1471
|
+
__proto__: null,
|
1472
|
+
base36: base36,
|
1473
|
+
base36upper: base36upper
|
1474
|
+
});
|
1475
|
+
|
1476
|
+
// @ts-check
|
1477
|
+
|
1478
|
+
const base64 = rfc4648({
|
1479
|
+
prefix: 'm',
|
1480
|
+
name: 'base64',
|
1481
|
+
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
|
1482
|
+
bitsPerChar: 6
|
1483
|
+
});
|
1484
|
+
|
1485
|
+
const base64pad = rfc4648({
|
1486
|
+
prefix: 'M',
|
1487
|
+
name: 'base64pad',
|
1488
|
+
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
|
1489
|
+
bitsPerChar: 6
|
1490
|
+
});
|
1491
|
+
|
1492
|
+
const base64url = rfc4648({
|
1493
|
+
prefix: 'u',
|
1494
|
+
name: 'base64url',
|
1495
|
+
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
|
1496
|
+
bitsPerChar: 6
|
1497
|
+
});
|
1498
|
+
|
1499
|
+
const base64urlpad = rfc4648({
|
1500
|
+
prefix: 'U',
|
1501
|
+
name: 'base64urlpad',
|
1502
|
+
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=',
|
1503
|
+
bitsPerChar: 6
|
1504
|
+
});
|
1505
|
+
|
1506
|
+
var base64$1 = /*#__PURE__*/Object.freeze({
|
1507
|
+
__proto__: null,
|
1508
|
+
base64: base64,
|
1509
|
+
base64pad: base64pad,
|
1510
|
+
base64url: base64url,
|
1511
|
+
base64urlpad: base64urlpad
|
1512
|
+
});
|
1513
|
+
|
1514
|
+
const alphabet = Array.from('🚀🪐☄🛰🌌🌑🌒🌓🌔🌕🌖🌗🌘🌍🌏🌎🐉☀💻🖥💾💿😂❤😍🤣😊🙏💕😭😘👍😅👏😁🔥🥰💔💖💙😢🤔😆🙄💪😉☺👌🤗💜😔😎😇🌹🤦🎉💞✌✨🤷😱😌🌸🙌😋💗💚😏💛🙂💓🤩😄😀🖤😃💯🙈👇🎶😒🤭❣😜💋👀😪😑💥🙋😞😩😡🤪👊🥳😥🤤👉💃😳✋😚😝😴🌟😬🙃🍀🌷😻😓⭐✅🥺🌈😈🤘💦✔😣🏃💐☹🎊💘😠☝😕🌺🎂🌻😐🖕💝🙊😹🗣💫💀👑🎵🤞😛🔴😤🌼😫⚽🤙☕🏆🤫👈😮🙆🍻🍃🐶💁😲🌿🧡🎁⚡🌞🎈❌✊👋😰🤨😶🤝🚶💰🍓💢🤟🙁🚨💨🤬✈🎀🍺🤓😙💟🌱😖👶🥴▶➡❓💎💸⬇😨🌚🦋😷🕺⚠🙅😟😵👎🤲🤠🤧📌🔵💅🧐🐾🍒😗🤑🌊🤯🐷☎💧😯💆👆🎤🙇🍑❄🌴💣🐸💌📍🥀🤢👅💡💩👐📸👻🤐🤮🎼🥵🚩🍎🍊👼💍📣🥂');
|
1515
|
+
const alphabetBytesToChars = /** @type {string[]} */ (alphabet.reduce((p, c, i) => { p[i] = c; return p }, /** @type {string[]} */([])));
|
1516
|
+
const alphabetCharsToBytes = /** @type {number[]} */ (alphabet.reduce((p, c, i) => { p[/** @type {number} */ (c.codePointAt(0))] = i; return p }, /** @type {number[]} */([])));
|
1517
|
+
|
1518
|
+
/**
|
1519
|
+
* @param {Uint8Array} data
|
1520
|
+
* @returns {string}
|
1521
|
+
*/
|
1522
|
+
function encode$1 (data) {
|
1523
|
+
return data.reduce((p, c) => {
|
1524
|
+
p += alphabetBytesToChars[c];
|
1525
|
+
return p
|
1526
|
+
}, '')
|
1527
|
+
}
|
1528
|
+
|
1529
|
+
/**
|
1530
|
+
* @param {string} str
|
1531
|
+
* @returns {Uint8Array}
|
1532
|
+
*/
|
1533
|
+
function decode (str) {
|
1534
|
+
const byts = [];
|
1535
|
+
for (const char of str) {
|
1536
|
+
const byt = alphabetCharsToBytes[/** @type {number} */ (char.codePointAt(0))];
|
1537
|
+
if (byt === undefined) {
|
1538
|
+
throw new Error(`Non-base256emoji character: ${char}`)
|
1539
|
+
}
|
1540
|
+
byts.push(byt);
|
1541
|
+
}
|
1542
|
+
return new Uint8Array(byts)
|
1543
|
+
}
|
1544
|
+
|
1545
|
+
const base256emoji = from$1({
|
1546
|
+
prefix: '🚀',
|
1547
|
+
name: 'base256emoji',
|
1548
|
+
encode: encode$1,
|
1549
|
+
decode
|
1550
|
+
});
|
1551
|
+
|
1552
|
+
var base256emoji$1 = /*#__PURE__*/Object.freeze({
|
1553
|
+
__proto__: null,
|
1554
|
+
base256emoji: base256emoji
|
1555
|
+
});
|
1556
|
+
|
1557
|
+
/**
|
1558
|
+
* @template {string} Name
|
1559
|
+
* @template {number} Code
|
1560
|
+
* @param {object} options
|
1561
|
+
* @param {Name} options.name
|
1562
|
+
* @param {Code} options.code
|
1563
|
+
* @param {(input: Uint8Array) => Await<Uint8Array>} options.encode
|
1564
|
+
*/
|
1565
|
+
const from = ({ name, code, encode }) => new Hasher(name, code, encode);
|
1566
|
+
|
1567
|
+
/**
|
1568
|
+
* Hasher represents a hashing algorithm implementation that produces as
|
1569
|
+
* `MultihashDigest`.
|
1570
|
+
*
|
1571
|
+
* @template {string} Name
|
1572
|
+
* @template {number} Code
|
1573
|
+
* @class
|
1574
|
+
* @implements {MultihashHasher<Code>}
|
1575
|
+
*/
|
1576
|
+
class Hasher {
|
1577
|
+
/**
|
1578
|
+
*
|
1579
|
+
* @param {Name} name
|
1580
|
+
* @param {Code} code
|
1581
|
+
* @param {(input: Uint8Array) => Await<Uint8Array>} encode
|
1582
|
+
*/
|
1583
|
+
constructor (name, code, encode) {
|
1584
|
+
this.name = name;
|
1585
|
+
this.code = code;
|
1586
|
+
this.encode = encode;
|
1587
|
+
}
|
1588
|
+
|
1589
|
+
/**
|
1590
|
+
* @param {Uint8Array} input
|
1591
|
+
* @returns {Await<Digest.Digest<Code, number>>}
|
1592
|
+
*/
|
1593
|
+
digest (input) {
|
1594
|
+
if (input instanceof Uint8Array) {
|
1595
|
+
const result = this.encode(input);
|
1596
|
+
return result instanceof Uint8Array
|
1597
|
+
? create(this.code, result)
|
1598
|
+
/* c8 ignore next 1 */
|
1599
|
+
: result.then(digest => create(this.code, digest))
|
1600
|
+
} else {
|
1601
|
+
throw Error('Unknown type, must be binary type')
|
1602
|
+
/* c8 ignore next 1 */
|
1603
|
+
}
|
1604
|
+
}
|
1605
|
+
}
|
1606
|
+
|
1607
|
+
/**
|
1608
|
+
* @template {number} Alg
|
1609
|
+
* @typedef {import('./interface.js').MultihashHasher} MultihashHasher
|
1610
|
+
*/
|
1611
|
+
|
1612
|
+
/**
|
1613
|
+
* @template T
|
1614
|
+
* @typedef {Promise<T>|T} Await
|
1615
|
+
*/
|
1616
|
+
|
1617
|
+
/* global crypto */
|
1618
|
+
|
1619
|
+
/**
|
1620
|
+
* @param {AlgorithmIdentifier} name
|
1621
|
+
*/
|
1622
|
+
const sha = name =>
|
1623
|
+
/**
|
1624
|
+
* @param {Uint8Array} data
|
1625
|
+
*/
|
1626
|
+
async data => new Uint8Array(await crypto.subtle.digest(name, data));
|
1627
|
+
|
1628
|
+
const sha256 = from({
|
1629
|
+
name: 'sha2-256',
|
1630
|
+
code: 0x12,
|
1631
|
+
encode: sha('SHA-256')
|
1632
|
+
});
|
1633
|
+
|
1634
|
+
const sha512 = from({
|
1635
|
+
name: 'sha2-512',
|
1636
|
+
code: 0x13,
|
1637
|
+
encode: sha('SHA-512')
|
1638
|
+
});
|
1639
|
+
|
1640
|
+
var sha2 = /*#__PURE__*/Object.freeze({
|
1641
|
+
__proto__: null,
|
1642
|
+
sha256: sha256,
|
1643
|
+
sha512: sha512
|
1644
|
+
});
|
1645
|
+
|
1646
|
+
const code = 0x0;
|
1647
|
+
const name = 'identity';
|
1648
|
+
|
1649
|
+
/** @type {(input:Uint8Array) => Uint8Array} */
|
1650
|
+
const encode = coerce;
|
1651
|
+
|
1652
|
+
/**
|
1653
|
+
* @param {Uint8Array} input
|
1654
|
+
* @returns {Digest.Digest<typeof code, number>}
|
1655
|
+
*/
|
1656
|
+
const digest = (input) => create(code, encode(input));
|
1657
|
+
|
1658
|
+
const identity = { code, name, encode, digest };
|
1659
|
+
|
1660
|
+
var identity$1 = /*#__PURE__*/Object.freeze({
|
1661
|
+
__proto__: null,
|
1662
|
+
identity: identity
|
1663
|
+
});
|
1664
|
+
|
1665
|
+
// @ts-check
|
1666
|
+
|
1667
|
+
/**
|
1668
|
+
* @template T
|
1669
|
+
* @typedef {import('./interface.js').ByteView<T>} ByteView
|
1670
|
+
*/
|
1671
|
+
|
1672
|
+
new TextEncoder();
|
1673
|
+
new TextDecoder();
|
1674
|
+
|
1675
|
+
// @ts-check
|
1676
|
+
|
1677
|
+
const bases = { ...identityBase, ...base2$1, ...base8$1, ...base10$1, ...base16$1, ...base32$1, ...base36$1, ...base58, ...base64$1, ...base256emoji$1 };
|
1678
|
+
({ ...sha2, ...identity$1 });
|
1679
|
+
|
1680
|
+
/**
|
1681
|
+
* Returns true if the two passed Uint8Arrays have the same content
|
1682
|
+
*/
|
1683
|
+
function equals(a, b) {
|
1684
|
+
if (a === b) {
|
1685
|
+
return true;
|
1686
|
+
}
|
1687
|
+
if (a.byteLength !== b.byteLength) {
|
1688
|
+
return false;
|
1689
|
+
}
|
1690
|
+
for (let i = 0; i < a.byteLength; i++) {
|
1691
|
+
if (a[i] !== b[i]) {
|
1692
|
+
return false;
|
1693
|
+
}
|
1694
|
+
}
|
1695
|
+
return true;
|
1696
|
+
}
|
1697
|
+
|
1698
|
+
const symbol = Symbol.for('@libp2p/peer-id');
|
1699
|
+
|
1700
|
+
const baseDecoder = Object
|
1701
|
+
.values(bases)
|
1702
|
+
.map(codec => codec.decoder)
|
1703
|
+
// @ts-expect-error https://github.com/multiformats/js-multiformats/issues/141
|
1704
|
+
.reduce((acc, curr) => acc.or(curr), bases.identity.decoder);
|
1705
|
+
// these values are from https://github.com/multiformats/multicodec/blob/master/table.csv
|
1706
|
+
const LIBP2P_KEY_CODE = 0x72;
|
1707
|
+
const MARSHALLED_ED225519_PUBLIC_KEY_LENGTH = 36;
|
1708
|
+
const MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH = 37;
|
1709
|
+
class PeerIdImpl {
|
1710
|
+
constructor(init) {
|
1711
|
+
this.type = init.type;
|
1712
|
+
this.multihash = init.multihash;
|
1713
|
+
this.privateKey = init.privateKey;
|
1714
|
+
// mark string cache as non-enumerable
|
1715
|
+
Object.defineProperty(this, 'string', {
|
1716
|
+
enumerable: false,
|
1717
|
+
writable: true
|
1718
|
+
});
|
1719
|
+
}
|
1720
|
+
get [Symbol.toStringTag]() {
|
1721
|
+
return `PeerId(${this.toString()})`;
|
1722
|
+
}
|
1723
|
+
get [symbol]() {
|
1724
|
+
return true;
|
1725
|
+
}
|
1726
|
+
toString() {
|
1727
|
+
if (this.string == null) {
|
1728
|
+
this.string = base58btc.encode(this.multihash.bytes).slice(1);
|
1729
|
+
}
|
1730
|
+
return this.string;
|
1731
|
+
}
|
1732
|
+
// return self-describing String representation
|
1733
|
+
// in default format from RFC 0001: https://github.com/libp2p/specs/pull/209
|
1734
|
+
toCID() {
|
1735
|
+
return CID.createV1(LIBP2P_KEY_CODE, this.multihash);
|
1736
|
+
}
|
1737
|
+
toBytes() {
|
1738
|
+
return this.multihash.bytes;
|
1739
|
+
}
|
1740
|
+
/**
|
1741
|
+
* Returns Multiaddr as a JSON encoded object
|
1742
|
+
*/
|
1743
|
+
toJSON() {
|
1744
|
+
return this.toString();
|
1745
|
+
}
|
1746
|
+
/**
|
1747
|
+
* Checks the equality of `this` peer against a given PeerId
|
1748
|
+
*/
|
1749
|
+
equals(id) {
|
1750
|
+
if (id instanceof Uint8Array) {
|
1751
|
+
return equals(this.multihash.bytes, id);
|
1752
|
+
}
|
1753
|
+
else if (typeof id === 'string') {
|
1754
|
+
return peerIdFromString(id).equals(this);
|
1755
|
+
}
|
1756
|
+
else if (id?.multihash?.bytes != null) {
|
1757
|
+
return equals(this.multihash.bytes, id.multihash.bytes);
|
1758
|
+
}
|
1759
|
+
else {
|
1760
|
+
throw new Error('not valid Id');
|
1761
|
+
}
|
1762
|
+
}
|
1763
|
+
}
|
1764
|
+
class RSAPeerIdImpl extends PeerIdImpl {
|
1765
|
+
constructor(init) {
|
1766
|
+
super({ ...init, type: 'RSA' });
|
1767
|
+
this.type = 'RSA';
|
1768
|
+
this.publicKey = init.publicKey;
|
1769
|
+
}
|
1770
|
+
}
|
1771
|
+
class Ed25519PeerIdImpl extends PeerIdImpl {
|
1772
|
+
constructor(init) {
|
1773
|
+
super({ ...init, type: 'Ed25519' });
|
1774
|
+
this.type = 'Ed25519';
|
1775
|
+
this.publicKey = init.multihash.digest;
|
1776
|
+
}
|
1777
|
+
}
|
1778
|
+
class Secp256k1PeerIdImpl extends PeerIdImpl {
|
1779
|
+
constructor(init) {
|
1780
|
+
super({ ...init, type: 'secp256k1' });
|
1781
|
+
this.type = 'secp256k1';
|
1782
|
+
this.publicKey = init.multihash.digest;
|
1783
|
+
}
|
1784
|
+
}
|
1785
|
+
function peerIdFromString(str, decoder) {
|
1786
|
+
if (str.charAt(0) === '1' || str.charAt(0) === 'Q') {
|
1787
|
+
// identity hash ed25519/secp256k1 key or sha2-256 hash of
|
1788
|
+
// rsa public key - base58btc encoded either way
|
1789
|
+
const multihash = decode$2(base58btc.decode(`z${str}`));
|
1790
|
+
if (str.startsWith('12D')) {
|
1791
|
+
return new Ed25519PeerIdImpl({ multihash });
|
1792
|
+
}
|
1793
|
+
else if (str.startsWith('16U')) {
|
1794
|
+
return new Secp256k1PeerIdImpl({ multihash });
|
1795
|
+
}
|
1796
|
+
else {
|
1797
|
+
return new RSAPeerIdImpl({ multihash });
|
1798
|
+
}
|
1799
|
+
}
|
1800
|
+
return peerIdFromBytes(baseDecoder.decode(str));
|
1801
|
+
}
|
1802
|
+
function peerIdFromBytes(buf) {
|
1803
|
+
try {
|
1804
|
+
const multihash = decode$2(buf);
|
1805
|
+
if (multihash.code === identity.code) {
|
1806
|
+
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
1807
|
+
return new Ed25519PeerIdImpl({ multihash });
|
1808
|
+
}
|
1809
|
+
else if (multihash.digest.length === MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH) {
|
1810
|
+
return new Secp256k1PeerIdImpl({ multihash });
|
1811
|
+
}
|
1812
|
+
}
|
1813
|
+
if (multihash.code === sha256.code) {
|
1814
|
+
return new RSAPeerIdImpl({ multihash });
|
1815
|
+
}
|
1816
|
+
}
|
1817
|
+
catch {
|
1818
|
+
return peerIdFromCID(CID.decode(buf));
|
1819
|
+
}
|
1820
|
+
throw new Error('Supplied PeerID CID is invalid');
|
1821
|
+
}
|
1822
|
+
function peerIdFromCID(cid) {
|
1823
|
+
if (cid == null || cid.multihash == null || cid.version == null || (cid.version === 1 && cid.code !== LIBP2P_KEY_CODE)) {
|
1824
|
+
throw new Error('Supplied PeerID CID is invalid');
|
1825
|
+
}
|
1826
|
+
const multihash = cid.multihash;
|
1827
|
+
if (multihash.code === sha256.code) {
|
1828
|
+
return new RSAPeerIdImpl({ multihash: cid.multihash });
|
1829
|
+
}
|
1830
|
+
else if (multihash.code === identity.code) {
|
1831
|
+
if (multihash.digest.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
1832
|
+
return new Ed25519PeerIdImpl({ multihash: cid.multihash });
|
1833
|
+
}
|
1834
|
+
else if (multihash.digest.length === MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH) {
|
1835
|
+
return new Secp256k1PeerIdImpl({ multihash: cid.multihash });
|
1836
|
+
}
|
1837
|
+
}
|
1838
|
+
throw new Error('Supplied PeerID CID is invalid');
|
1839
|
+
}
|
1840
|
+
/**
|
1841
|
+
* @param publicKey - A marshalled public key
|
1842
|
+
* @param privateKey - A marshalled private key
|
1843
|
+
*/
|
1844
|
+
async function peerIdFromKeys(publicKey, privateKey) {
|
1845
|
+
if (publicKey.length === MARSHALLED_ED225519_PUBLIC_KEY_LENGTH) {
|
1846
|
+
return new Ed25519PeerIdImpl({ multihash: create(identity.code, publicKey), privateKey });
|
1847
|
+
}
|
1848
|
+
if (publicKey.length === MARSHALLED_SECP256K1_PUBLIC_KEY_LENGTH) {
|
1849
|
+
return new Secp256k1PeerIdImpl({ multihash: create(identity.code, publicKey), privateKey });
|
1850
|
+
}
|
1851
|
+
return new RSAPeerIdImpl({ multihash: await sha256.digest(publicKey), publicKey, privateKey });
|
1852
|
+
}
|
1853
|
+
|
1854
|
+
var __classPrivateFieldGet = (undefined && undefined.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
1855
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
1856
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
1857
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
1858
|
+
};
|
1859
|
+
var _EventEmitter_listeners;
|
1860
|
+
/**
|
1861
|
+
* Adds types to the EventTarget class. Hopefully this won't be necessary forever.
|
1862
|
+
*
|
1863
|
+
* https://github.com/microsoft/TypeScript/issues/28357
|
1864
|
+
* https://github.com/microsoft/TypeScript/issues/43477
|
1865
|
+
* https://github.com/microsoft/TypeScript/issues/299
|
1866
|
+
* etc
|
1867
|
+
*/
|
1868
|
+
class EventEmitter extends EventTarget {
|
1869
|
+
constructor() {
|
1870
|
+
super(...arguments);
|
1871
|
+
_EventEmitter_listeners.set(this, new Map());
|
1872
|
+
}
|
1873
|
+
listenerCount(type) {
|
1874
|
+
const listeners = __classPrivateFieldGet(this, _EventEmitter_listeners, "f").get(type);
|
1875
|
+
if (listeners == null) {
|
1876
|
+
return 0;
|
1877
|
+
}
|
1878
|
+
return listeners.length;
|
1879
|
+
}
|
1880
|
+
addEventListener(type, listener, options) {
|
1881
|
+
super.addEventListener(type, listener, options);
|
1882
|
+
let list = __classPrivateFieldGet(this, _EventEmitter_listeners, "f").get(type);
|
1883
|
+
if (list == null) {
|
1884
|
+
list = [];
|
1885
|
+
__classPrivateFieldGet(this, _EventEmitter_listeners, "f").set(type, list);
|
1886
|
+
}
|
1887
|
+
list.push({
|
1888
|
+
callback: listener,
|
1889
|
+
once: (options !== true && options !== false && options?.once) ?? false
|
1890
|
+
});
|
1891
|
+
}
|
1892
|
+
removeEventListener(type, listener, options) {
|
1893
|
+
super.removeEventListener(type.toString(), listener ?? null, options);
|
1894
|
+
let list = __classPrivateFieldGet(this, _EventEmitter_listeners, "f").get(type);
|
1895
|
+
if (list == null) {
|
1896
|
+
return;
|
1897
|
+
}
|
1898
|
+
list = list.filter(({ callback }) => callback !== listener);
|
1899
|
+
__classPrivateFieldGet(this, _EventEmitter_listeners, "f").set(type, list);
|
1900
|
+
}
|
1901
|
+
dispatchEvent(event) {
|
1902
|
+
const result = super.dispatchEvent(event);
|
1903
|
+
let list = __classPrivateFieldGet(this, _EventEmitter_listeners, "f").get(event.type);
|
1904
|
+
if (list == null) {
|
1905
|
+
return result;
|
1906
|
+
}
|
1907
|
+
list = list.filter(({ once }) => !once);
|
1908
|
+
__classPrivateFieldGet(this, _EventEmitter_listeners, "f").set(event.type, list);
|
1909
|
+
return result;
|
1910
|
+
}
|
1911
|
+
}
|
1912
|
+
_EventEmitter_listeners = new WeakMap();
|
1913
|
+
/**
|
1914
|
+
* CustomEvent is a standard event but it's not supported by node.
|
1915
|
+
*
|
1916
|
+
* Remove this when https://github.com/nodejs/node/issues/40678 is closed.
|
1917
|
+
*
|
1918
|
+
* Ref: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
|
1919
|
+
*/
|
1920
|
+
class CustomEventPolyfill extends Event {
|
1921
|
+
constructor(message, data) {
|
1922
|
+
super(message, data);
|
1923
|
+
// @ts-expect-error could be undefined
|
1924
|
+
this.detail = data?.detail;
|
1925
|
+
}
|
1926
|
+
}
|
1927
|
+
const CustomEvent = globalThis.CustomEvent ?? CustomEventPolyfill;
|
1928
|
+
|
1929
|
+
export { CustomEvent as C, EventEmitter as E, peerIdFromKeys as a, peerIdFromBytes as b, peerIdFromString as p };
|