@waku/rln 0.1.8-e224c05.0 → 0.1.8-e800af3.0
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/bundle/_virtual/utils.js +2 -2
- package/bundle/_virtual/utils2.js +2 -2
- package/bundle/index.js +6 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/_sha2.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/hmac.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/pbkdf2.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/scrypt.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha256.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/sha512.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/@noble/hashes/utils.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +1 -1
- package/bundle/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +2 -2
- package/bundle/node_modules/@chainsafe/is-ip/lib/is-ip.js +12 -0
- package/bundle/node_modules/@chainsafe/is-ip/lib/parse.js +26 -0
- package/bundle/node_modules/@chainsafe/is-ip/lib/parser.js +202 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/constants.js +43 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/errors.js +17 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/registry.js +245 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/utils.js +191 -0
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/validation.js +30 -0
- package/bundle/node_modules/@noble/hashes/esm/hmac.js +88 -0
- package/bundle/node_modules/@noble/hashes/esm/sha3.js +1 -1
- package/bundle/node_modules/@noble/hashes/esm/utils.js +8 -1
- package/bundle/node_modules/@waku/zerokit-rln-wasm/rln_wasm.js +517 -255
- package/bundle/node_modules/it-length-prefixed/dist/src/decode.js +6 -0
- package/bundle/node_modules/multiformats/dist/src/bases/base10.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/bases/base16.js +4 -2
- package/bundle/node_modules/multiformats/dist/src/bases/base2.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/bases/base256emoji.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/bases/base32.js +11 -9
- package/bundle/node_modules/multiformats/dist/src/bases/base36.js +4 -2
- package/bundle/node_modules/multiformats/dist/src/bases/base58.js +4 -2
- package/bundle/node_modules/multiformats/dist/src/bases/base64.js +6 -4
- package/bundle/node_modules/multiformats/dist/src/bases/base8.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/bases/identity.js +3 -1
- package/bundle/node_modules/multiformats/dist/src/basics.js +15 -0
- package/bundle/node_modules/multiformats/dist/src/bytes.js +15 -1
- package/bundle/node_modules/multiformats/dist/src/cid.js +371 -0
- package/bundle/node_modules/multiformats/dist/src/hashes/digest.js +62 -0
- package/bundle/node_modules/multiformats/dist/src/varint.js +15 -0
- package/bundle/node_modules/multiformats/dist/src/vendor/varint.js +78 -0
- package/bundle/node_modules/protons-runtime/dist/src/codec.js +20 -0
- package/bundle/node_modules/protons-runtime/dist/src/codecs/enum.js +24 -0
- package/bundle/node_modules/protons-runtime/dist/src/codecs/message.js +7 -0
- package/bundle/node_modules/protons-runtime/dist/src/decode.js +8 -0
- package/bundle/node_modules/protons-runtime/dist/src/encode.js +11 -0
- package/bundle/node_modules/protons-runtime/dist/src/index.js +30 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/float.js +54 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/longbits.js +175 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/pool.js +28 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/reader.js +367 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/utf8.js +99 -0
- package/bundle/node_modules/protons-runtime/dist/src/utils/writer.js +438 -0
- package/bundle/node_modules/uint8-varint/dist/src/index.js +124 -0
- package/bundle/node_modules/uint8arrays/dist/src/alloc.js +17 -0
- package/bundle/node_modules/uint8arrays/dist/src/concat.js +20 -0
- package/bundle/node_modules/uint8arrays/dist/src/from-string.js +19 -0
- package/bundle/node_modules/uint8arrays/dist/src/to-string.js +19 -0
- package/bundle/node_modules/uint8arrays/dist/src/util/as-uint8array.js +9 -0
- package/bundle/node_modules/uint8arrays/dist/src/util/bases.js +49 -0
- package/bundle/packages/core/dist/lib/connection_manager/connection_limiter.js +18 -0
- package/bundle/packages/core/dist/lib/connection_manager/connection_manager.js +24 -0
- package/bundle/packages/core/dist/lib/connection_manager/dialer.js +14 -0
- package/bundle/packages/core/dist/lib/connection_manager/discovery_dialer.js +14 -0
- package/bundle/packages/core/dist/lib/connection_manager/keep_alive_manager.js +15 -0
- package/bundle/packages/core/dist/lib/connection_manager/shard_reader.js +14 -0
- package/bundle/packages/core/dist/lib/filter/filter.js +28 -0
- package/bundle/packages/core/dist/lib/light_push/light_push.js +28 -0
- package/bundle/packages/core/dist/lib/message/version_0.js +172 -0
- package/bundle/packages/core/dist/lib/metadata/metadata.js +28 -0
- package/bundle/packages/core/dist/lib/store/store.js +24 -0
- package/bundle/packages/interfaces/dist/connection_manager.js +9 -0
- package/bundle/packages/interfaces/dist/health_status.js +17 -0
- package/bundle/packages/interfaces/dist/protocols.js +92 -0
- package/bundle/packages/interfaces/dist/waku.js +7 -0
- package/bundle/packages/proto/dist/generated/filter.js +447 -0
- package/bundle/packages/proto/dist/generated/filter_v2.js +426 -0
- package/bundle/packages/proto/dist/generated/light_push.js +550 -0
- package/bundle/packages/proto/dist/generated/message.js +215 -0
- package/bundle/packages/proto/dist/generated/metadata.js +132 -0
- package/bundle/packages/proto/dist/generated/peer_exchange.js +211 -0
- package/bundle/packages/proto/dist/generated/sds_message.js +172 -0
- package/bundle/packages/proto/dist/generated/store_v3.js +492 -0
- package/bundle/packages/proto/dist/generated/topic_only_message.js +63 -0
- package/bundle/packages/rln/dist/codec.js +92 -0
- package/bundle/packages/rln/dist/contract/constants.js +14 -7
- package/bundle/packages/rln/dist/contract/rln_base_contract.js +2 -1
- package/bundle/packages/rln/dist/contract/rln_contract.js +109 -0
- package/bundle/packages/rln/dist/credentials_manager.js +45 -4
- package/bundle/packages/rln/dist/identity.js +2 -1
- package/bundle/packages/rln/dist/keystore/keystore.js +30 -9
- package/bundle/packages/rln/dist/message.js +59 -0
- package/bundle/packages/rln/dist/proof.js +54 -0
- package/bundle/packages/rln/dist/resources/verification_key.js +112 -0
- package/bundle/packages/rln/dist/resources/witness_calculator.js +1 -1
- package/bundle/packages/rln/dist/rln.js +36 -4
- package/bundle/packages/rln/dist/root_tracker.js +76 -0
- package/bundle/packages/rln/dist/utils/bytes.js +70 -31
- package/bundle/packages/rln/dist/utils/epoch.js +23 -1
- package/bundle/packages/rln/dist/utils/hash.js +10 -0
- package/bundle/packages/rln/dist/zerokit.js +99 -2
- package/bundle/packages/utils/dist/bytes/index.js +31 -0
- package/bundle/resources/rln.wasm +0 -0
- package/bundle/resources/rln_final.zkey +0 -0
- package/bundle/resources/verification_key.d.ts +13 -0
- package/bundle/resources/verification_key.js +112 -0
- package/bundle/resources/witness_calculator.d.ts +7 -21
- package/bundle/resources/witness_calculator.js +1 -1
- package/dist/.tsbuildinfo +1 -1
- package/dist/codec.d.ts +40 -0
- package/dist/codec.js +79 -0
- package/dist/codec.js.map +1 -0
- package/dist/codec.test-utils.d.ts +37 -0
- package/dist/codec.test-utils.js +61 -0
- package/dist/codec.test-utils.js.map +1 -0
- package/dist/contract/constants.d.ts +10 -3
- package/dist/contract/constants.js +13 -6
- package/dist/contract/constants.js.map +1 -1
- package/dist/contract/index.d.ts +1 -0
- package/dist/contract/index.js +1 -0
- package/dist/contract/index.js.map +1 -1
- package/dist/contract/rln_base_contract.js +2 -1
- package/dist/contract/rln_base_contract.js.map +1 -1
- package/dist/contract/rln_contract.d.ts +17 -0
- package/dist/contract/rln_contract.js +107 -0
- package/dist/contract/rln_contract.js.map +1 -0
- package/dist/contract/test_setup.d.ts +26 -0
- package/dist/contract/test_setup.js +56 -0
- package/dist/contract/test_setup.js.map +1 -0
- package/dist/contract/test_utils.d.ts +39 -0
- package/dist/contract/test_utils.js +118 -0
- package/dist/contract/test_utils.js.map +1 -0
- package/dist/credentials_manager.d.ts +14 -2
- package/dist/credentials_manager.js +45 -4
- package/dist/credentials_manager.js.map +1 -1
- package/dist/identity.js +2 -1
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/keystore/keystore.js +30 -9
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/message.d.ts +19 -0
- package/dist/message.js +51 -0
- package/dist/message.js.map +1 -0
- package/dist/proof.d.ts +21 -0
- package/dist/proof.js +50 -0
- package/dist/proof.js.map +1 -0
- package/dist/resources/rln.wasm +0 -0
- package/dist/resources/rln_final.zkey +0 -0
- package/dist/resources/verification_key.d.ts +13 -0
- package/dist/resources/verification_key.js +112 -0
- package/dist/resources/witness_calculator.d.ts +7 -21
- package/dist/resources/witness_calculator.js +1 -1
- package/dist/rln.d.ts +9 -0
- package/dist/rln.js +32 -4
- package/dist/rln.js.map +1 -1
- package/dist/root_tracker.d.ts +10 -0
- package/dist/root_tracker.js +75 -0
- package/dist/root_tracker.js.map +1 -0
- package/dist/utils/bytes.d.ts +31 -9
- package/dist/utils/bytes.js +70 -31
- package/dist/utils/bytes.js.map +1 -1
- package/dist/zerokit.d.ts +11 -0
- package/dist/zerokit.js +97 -1
- package/dist/zerokit.js.map +1 -1
- package/package.json +1 -1
- package/src/codec.test-utils.ts +88 -0
- package/src/codec.ts +138 -0
- package/src/contract/constants.ts +16 -6
- package/src/contract/index.ts +1 -0
- package/src/contract/rln_base_contract.ts +2 -1
- package/src/contract/rln_contract.ts +147 -0
- package/src/contract/test_setup.ts +86 -0
- package/src/contract/test_utils.ts +179 -0
- package/src/credentials_manager.ts +72 -8
- package/src/identity.ts +2 -1
- package/src/index.ts +11 -1
- package/src/keystore/keystore.ts +32 -9
- package/src/message.ts +73 -0
- package/src/proof.ts +69 -0
- package/src/resources/verification_key.d.ts +13 -0
- package/src/resources/witness_calculator.d.ts +7 -21
- package/src/rln.ts +65 -5
- package/src/root_tracker.ts +92 -0
- package/src/utils/bytes.ts +73 -36
- package/src/zerokit.ts +217 -1
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { rfc4648 } from './base.js';
|
|
2
2
|
|
|
3
|
-
rfc4648({
|
|
3
|
+
const base16 = rfc4648({
|
|
4
4
|
prefix: 'f',
|
|
5
5
|
name: 'base16',
|
|
6
6
|
alphabet: '0123456789abcdef',
|
|
7
7
|
bitsPerChar: 4
|
|
8
8
|
});
|
|
9
|
-
rfc4648({
|
|
9
|
+
const base16upper = rfc4648({
|
|
10
10
|
prefix: 'F',
|
|
11
11
|
name: 'base16upper',
|
|
12
12
|
alphabet: '0123456789ABCDEF',
|
|
13
13
|
bitsPerChar: 4
|
|
14
14
|
});
|
|
15
|
+
|
|
16
|
+
export { base16, base16upper };
|
|
@@ -1,56 +1,58 @@
|
|
|
1
1
|
import { rfc4648 } from './base.js';
|
|
2
2
|
|
|
3
|
-
rfc4648({
|
|
3
|
+
const base32 = rfc4648({
|
|
4
4
|
prefix: 'b',
|
|
5
5
|
name: 'base32',
|
|
6
6
|
alphabet: 'abcdefghijklmnopqrstuvwxyz234567',
|
|
7
7
|
bitsPerChar: 5
|
|
8
8
|
});
|
|
9
|
-
rfc4648({
|
|
9
|
+
const base32upper = rfc4648({
|
|
10
10
|
prefix: 'B',
|
|
11
11
|
name: 'base32upper',
|
|
12
12
|
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
|
|
13
13
|
bitsPerChar: 5
|
|
14
14
|
});
|
|
15
|
-
rfc4648({
|
|
15
|
+
const base32pad = rfc4648({
|
|
16
16
|
prefix: 'c',
|
|
17
17
|
name: 'base32pad',
|
|
18
18
|
alphabet: 'abcdefghijklmnopqrstuvwxyz234567=',
|
|
19
19
|
bitsPerChar: 5
|
|
20
20
|
});
|
|
21
|
-
rfc4648({
|
|
21
|
+
const base32padupper = rfc4648({
|
|
22
22
|
prefix: 'C',
|
|
23
23
|
name: 'base32padupper',
|
|
24
24
|
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=',
|
|
25
25
|
bitsPerChar: 5
|
|
26
26
|
});
|
|
27
|
-
rfc4648({
|
|
27
|
+
const base32hex = rfc4648({
|
|
28
28
|
prefix: 'v',
|
|
29
29
|
name: 'base32hex',
|
|
30
30
|
alphabet: '0123456789abcdefghijklmnopqrstuv',
|
|
31
31
|
bitsPerChar: 5
|
|
32
32
|
});
|
|
33
|
-
rfc4648({
|
|
33
|
+
const base32hexupper = rfc4648({
|
|
34
34
|
prefix: 'V',
|
|
35
35
|
name: 'base32hexupper',
|
|
36
36
|
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV',
|
|
37
37
|
bitsPerChar: 5
|
|
38
38
|
});
|
|
39
|
-
rfc4648({
|
|
39
|
+
const base32hexpad = rfc4648({
|
|
40
40
|
prefix: 't',
|
|
41
41
|
name: 'base32hexpad',
|
|
42
42
|
alphabet: '0123456789abcdefghijklmnopqrstuv=',
|
|
43
43
|
bitsPerChar: 5
|
|
44
44
|
});
|
|
45
|
-
rfc4648({
|
|
45
|
+
const base32hexpadupper = rfc4648({
|
|
46
46
|
prefix: 'T',
|
|
47
47
|
name: 'base32hexpadupper',
|
|
48
48
|
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUV=',
|
|
49
49
|
bitsPerChar: 5
|
|
50
50
|
});
|
|
51
|
-
rfc4648({
|
|
51
|
+
const base32z = rfc4648({
|
|
52
52
|
prefix: 'h',
|
|
53
53
|
name: 'base32z',
|
|
54
54
|
alphabet: 'ybndrfg8ejkmcpqxot1uwisza345h769',
|
|
55
55
|
bitsPerChar: 5
|
|
56
56
|
});
|
|
57
|
+
|
|
58
|
+
export { base32, base32hex, base32hexpad, base32hexpadupper, base32hexupper, base32pad, base32padupper, base32upper, base32z };
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { baseX } from './base.js';
|
|
2
2
|
|
|
3
|
-
baseX({
|
|
3
|
+
const base36 = baseX({
|
|
4
4
|
prefix: 'k',
|
|
5
5
|
name: 'base36',
|
|
6
6
|
alphabet: '0123456789abcdefghijklmnopqrstuvwxyz'
|
|
7
7
|
});
|
|
8
|
-
baseX({
|
|
8
|
+
const base36upper = baseX({
|
|
9
9
|
prefix: 'K',
|
|
10
10
|
name: 'base36upper',
|
|
11
11
|
alphabet: '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
12
12
|
});
|
|
13
|
+
|
|
14
|
+
export { base36, base36upper };
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { baseX } from './base.js';
|
|
2
2
|
|
|
3
|
-
baseX({
|
|
3
|
+
const base58btc = baseX({
|
|
4
4
|
name: 'base58btc',
|
|
5
5
|
prefix: 'z',
|
|
6
6
|
alphabet: '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
|
|
7
7
|
});
|
|
8
|
-
baseX({
|
|
8
|
+
const base58flickr = baseX({
|
|
9
9
|
name: 'base58flickr',
|
|
10
10
|
prefix: 'Z',
|
|
11
11
|
alphabet: '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ'
|
|
12
12
|
});
|
|
13
|
+
|
|
14
|
+
export { base58btc, base58flickr };
|
|
@@ -1,26 +1,28 @@
|
|
|
1
1
|
import { rfc4648 } from './base.js';
|
|
2
2
|
|
|
3
|
-
rfc4648({
|
|
3
|
+
const base64 = rfc4648({
|
|
4
4
|
prefix: 'm',
|
|
5
5
|
name: 'base64',
|
|
6
6
|
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
|
|
7
7
|
bitsPerChar: 6
|
|
8
8
|
});
|
|
9
|
-
rfc4648({
|
|
9
|
+
const base64pad = rfc4648({
|
|
10
10
|
prefix: 'M',
|
|
11
11
|
name: 'base64pad',
|
|
12
12
|
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
|
|
13
13
|
bitsPerChar: 6
|
|
14
14
|
});
|
|
15
|
-
rfc4648({
|
|
15
|
+
const base64url = rfc4648({
|
|
16
16
|
prefix: 'u',
|
|
17
17
|
name: 'base64url',
|
|
18
18
|
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
|
|
19
19
|
bitsPerChar: 6
|
|
20
20
|
});
|
|
21
|
-
rfc4648({
|
|
21
|
+
const base64urlpad = rfc4648({
|
|
22
22
|
prefix: 'U',
|
|
23
23
|
name: 'base64urlpad',
|
|
24
24
|
alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=',
|
|
25
25
|
bitsPerChar: 6
|
|
26
26
|
});
|
|
27
|
+
|
|
28
|
+
export { base64, base64pad, base64url, base64urlpad };
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { toString, fromString } from '../bytes.js';
|
|
2
2
|
import { from } from './base.js';
|
|
3
3
|
|
|
4
|
-
from({
|
|
4
|
+
const identity = from({
|
|
5
5
|
prefix: '\x00',
|
|
6
6
|
name: 'identity',
|
|
7
7
|
encode: (buf) => toString(buf),
|
|
8
8
|
decode: (str) => fromString(str)
|
|
9
9
|
});
|
|
10
|
+
|
|
11
|
+
export { identity };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as base10 from './bases/base10.js';
|
|
2
|
+
import * as base16 from './bases/base16.js';
|
|
3
|
+
import * as base2 from './bases/base2.js';
|
|
4
|
+
import * as base256emoji from './bases/base256emoji.js';
|
|
5
|
+
import * as base32 from './bases/base32.js';
|
|
6
|
+
import * as base36 from './bases/base36.js';
|
|
7
|
+
import * as base58 from './bases/base58.js';
|
|
8
|
+
import * as base64 from './bases/base64.js';
|
|
9
|
+
import * as base8 from './bases/base8.js';
|
|
10
|
+
import * as identity from './bases/identity.js';
|
|
11
|
+
import './codecs/json.js';
|
|
12
|
+
|
|
13
|
+
const bases = { ...identity, ...base2, ...base8, ...base10, ...base16, ...base32, ...base36, ...base58, ...base64, ...base256emoji };
|
|
14
|
+
|
|
15
|
+
export { bases };
|
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
function equals(aa, bb) {
|
|
2
|
+
if (aa === bb) {
|
|
3
|
+
return true;
|
|
4
|
+
}
|
|
5
|
+
if (aa.byteLength !== bb.byteLength) {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
for (let ii = 0; ii < aa.byteLength; ii++) {
|
|
9
|
+
if (aa[ii] !== bb[ii]) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
1
15
|
function coerce(o) {
|
|
2
16
|
if (o instanceof Uint8Array && o.constructor.name === 'Uint8Array') {
|
|
3
17
|
return o;
|
|
@@ -17,4 +31,4 @@ function toString(b) {
|
|
|
17
31
|
return new TextDecoder().decode(b);
|
|
18
32
|
}
|
|
19
33
|
|
|
20
|
-
export { coerce, fromString, toString };
|
|
34
|
+
export { coerce, equals, fromString, toString };
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import { base32 } from './bases/base32.js';
|
|
2
|
+
import { base36 } from './bases/base36.js';
|
|
3
|
+
import { base58btc } from './bases/base58.js';
|
|
4
|
+
import { coerce } from './bytes.js';
|
|
5
|
+
import { create, equals, decode, Digest } from './hashes/digest.js';
|
|
6
|
+
import { decode as decode$1, encodeTo, encodingLength } from './varint.js';
|
|
7
|
+
|
|
8
|
+
function format(link, base) {
|
|
9
|
+
const { bytes, version } = link;
|
|
10
|
+
switch (version) {
|
|
11
|
+
case 0:
|
|
12
|
+
return toStringV0(bytes, baseCache(link), base ?? base58btc.encoder);
|
|
13
|
+
default:
|
|
14
|
+
return toStringV1(bytes, baseCache(link), (base ?? base32.encoder));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const cache = new WeakMap();
|
|
18
|
+
function baseCache(cid) {
|
|
19
|
+
const baseCache = cache.get(cid);
|
|
20
|
+
if (baseCache == null) {
|
|
21
|
+
const baseCache = new Map();
|
|
22
|
+
cache.set(cid, baseCache);
|
|
23
|
+
return baseCache;
|
|
24
|
+
}
|
|
25
|
+
return baseCache;
|
|
26
|
+
}
|
|
27
|
+
class CID {
|
|
28
|
+
code;
|
|
29
|
+
version;
|
|
30
|
+
multihash;
|
|
31
|
+
bytes;
|
|
32
|
+
'/';
|
|
33
|
+
/**
|
|
34
|
+
* @param version - Version of the CID
|
|
35
|
+
* @param code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
|
|
36
|
+
* @param multihash - (Multi)hash of the of the content.
|
|
37
|
+
*/
|
|
38
|
+
constructor(version, code, multihash, bytes) {
|
|
39
|
+
this.code = code;
|
|
40
|
+
this.version = version;
|
|
41
|
+
this.multihash = multihash;
|
|
42
|
+
this.bytes = bytes;
|
|
43
|
+
// flag to serializers that this is a CID and
|
|
44
|
+
// should be treated specially
|
|
45
|
+
this['/'] = bytes;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Signalling `cid.asCID === cid` has been replaced with `cid['/'] === cid.bytes`
|
|
49
|
+
* please either use `CID.asCID(cid)` or switch to new signalling mechanism
|
|
50
|
+
*
|
|
51
|
+
* @deprecated
|
|
52
|
+
*/
|
|
53
|
+
get asCID() {
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
// ArrayBufferView
|
|
57
|
+
get byteOffset() {
|
|
58
|
+
return this.bytes.byteOffset;
|
|
59
|
+
}
|
|
60
|
+
// ArrayBufferView
|
|
61
|
+
get byteLength() {
|
|
62
|
+
return this.bytes.byteLength;
|
|
63
|
+
}
|
|
64
|
+
toV0() {
|
|
65
|
+
switch (this.version) {
|
|
66
|
+
case 0: {
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
case 1: {
|
|
70
|
+
const { code, multihash } = this;
|
|
71
|
+
if (code !== DAG_PB_CODE) {
|
|
72
|
+
throw new Error('Cannot convert a non dag-pb CID to CIDv0');
|
|
73
|
+
}
|
|
74
|
+
// sha2-256
|
|
75
|
+
if (multihash.code !== SHA_256_CODE) {
|
|
76
|
+
throw new Error('Cannot convert non sha2-256 multihash CID to CIDv0');
|
|
77
|
+
}
|
|
78
|
+
return (CID.createV0(multihash));
|
|
79
|
+
}
|
|
80
|
+
default: {
|
|
81
|
+
throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
toV1() {
|
|
86
|
+
switch (this.version) {
|
|
87
|
+
case 0: {
|
|
88
|
+
const { code, digest } = this.multihash;
|
|
89
|
+
const multihash = create(code, digest);
|
|
90
|
+
return (CID.createV1(this.code, multihash));
|
|
91
|
+
}
|
|
92
|
+
case 1: {
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
default: {
|
|
96
|
+
throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
equals(other) {
|
|
101
|
+
return CID.equals(this, other);
|
|
102
|
+
}
|
|
103
|
+
static equals(self, other) {
|
|
104
|
+
const unknown = other;
|
|
105
|
+
return (unknown != null &&
|
|
106
|
+
self.code === unknown.code &&
|
|
107
|
+
self.version === unknown.version &&
|
|
108
|
+
equals(self.multihash, unknown.multihash));
|
|
109
|
+
}
|
|
110
|
+
toString(base) {
|
|
111
|
+
return format(this, base);
|
|
112
|
+
}
|
|
113
|
+
toJSON() {
|
|
114
|
+
return { '/': format(this) };
|
|
115
|
+
}
|
|
116
|
+
link() {
|
|
117
|
+
return this;
|
|
118
|
+
}
|
|
119
|
+
[Symbol.toStringTag] = 'CID';
|
|
120
|
+
// Legacy
|
|
121
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
122
|
+
return `CID(${this.toString()})`;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Takes any input `value` and returns a `CID` instance if it was
|
|
126
|
+
* a `CID` otherwise returns `null`. If `value` is instanceof `CID`
|
|
127
|
+
* it will return value back. If `value` is not instance of this CID
|
|
128
|
+
* class, but is compatible CID it will return new instance of this
|
|
129
|
+
* `CID` class. Otherwise returns null.
|
|
130
|
+
*
|
|
131
|
+
* This allows two different incompatible versions of CID library to
|
|
132
|
+
* co-exist and interop as long as binary interface is compatible.
|
|
133
|
+
*/
|
|
134
|
+
static asCID(input) {
|
|
135
|
+
if (input == null) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
const value = input;
|
|
139
|
+
if (value instanceof CID) {
|
|
140
|
+
// If value is instance of CID then we're all set.
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
else if ((value['/'] != null && value['/'] === value.bytes) || value.asCID === value) {
|
|
144
|
+
// If value isn't instance of this CID class but `this.asCID === this` or
|
|
145
|
+
// `value['/'] === value.bytes` is true it is CID instance coming from a
|
|
146
|
+
// different implementation (diff version or duplicate). In that case we
|
|
147
|
+
// rebase it to this `CID` implementation so caller is guaranteed to get
|
|
148
|
+
// instance with expected API.
|
|
149
|
+
const { version, code, multihash, bytes } = value;
|
|
150
|
+
return new CID(version, code, multihash, bytes ?? encodeCID(version, code, multihash.bytes));
|
|
151
|
+
}
|
|
152
|
+
else if (value[cidSymbol] === true) {
|
|
153
|
+
// If value is a CID from older implementation that used to be tagged via
|
|
154
|
+
// symbol we still rebase it to the this `CID` implementation by
|
|
155
|
+
// delegating that to a constructor.
|
|
156
|
+
const { version, multihash, code } = value;
|
|
157
|
+
const digest = decode(multihash);
|
|
158
|
+
return CID.create(version, code, digest);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
// Otherwise value is not a CID (or an incompatible version of it) in
|
|
162
|
+
// which case we return `null`.
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* @param version - Version of the CID
|
|
168
|
+
* @param code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
|
|
169
|
+
* @param digest - (Multi)hash of the of the content.
|
|
170
|
+
*/
|
|
171
|
+
static create(version, code, digest) {
|
|
172
|
+
if (typeof code !== 'number') {
|
|
173
|
+
throw new Error('String codecs are no longer supported');
|
|
174
|
+
}
|
|
175
|
+
if (!(digest.bytes instanceof Uint8Array)) {
|
|
176
|
+
throw new Error('Invalid digest');
|
|
177
|
+
}
|
|
178
|
+
switch (version) {
|
|
179
|
+
case 0: {
|
|
180
|
+
if (code !== DAG_PB_CODE) {
|
|
181
|
+
throw new Error(`Version 0 CID must use dag-pb (code: ${DAG_PB_CODE}) block encoding`);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
return new CID(version, code, digest, digest.bytes);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
case 1: {
|
|
188
|
+
const bytes = encodeCID(version, code, digest.bytes);
|
|
189
|
+
return new CID(version, code, digest, bytes);
|
|
190
|
+
}
|
|
191
|
+
default: {
|
|
192
|
+
throw new Error('Invalid version');
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Simplified version of `create` for CIDv0.
|
|
198
|
+
*/
|
|
199
|
+
static createV0(digest) {
|
|
200
|
+
return CID.create(0, DAG_PB_CODE, digest);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Simplified version of `create` for CIDv1.
|
|
204
|
+
*
|
|
205
|
+
* @param code - Content encoding format code.
|
|
206
|
+
* @param digest - Multihash of the content.
|
|
207
|
+
*/
|
|
208
|
+
static createV1(code, digest) {
|
|
209
|
+
return CID.create(1, code, digest);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Decoded a CID from its binary representation. The byte array must contain
|
|
213
|
+
* only the CID with no additional bytes.
|
|
214
|
+
*
|
|
215
|
+
* An error will be thrown if the bytes provided do not contain a valid
|
|
216
|
+
* binary representation of a CID.
|
|
217
|
+
*/
|
|
218
|
+
static decode(bytes) {
|
|
219
|
+
const [cid, remainder] = CID.decodeFirst(bytes);
|
|
220
|
+
if (remainder.length !== 0) {
|
|
221
|
+
throw new Error('Incorrect length');
|
|
222
|
+
}
|
|
223
|
+
return cid;
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Decoded a CID from its binary representation at the beginning of a byte
|
|
227
|
+
* array.
|
|
228
|
+
*
|
|
229
|
+
* Returns an array with the first element containing the CID and the second
|
|
230
|
+
* element containing the remainder of the original byte array. The remainder
|
|
231
|
+
* will be a zero-length byte array if the provided bytes only contained a
|
|
232
|
+
* binary CID representation.
|
|
233
|
+
*/
|
|
234
|
+
static decodeFirst(bytes) {
|
|
235
|
+
const specs = CID.inspectBytes(bytes);
|
|
236
|
+
const prefixSize = specs.size - specs.multihashSize;
|
|
237
|
+
const multihashBytes = coerce(bytes.subarray(prefixSize, prefixSize + specs.multihashSize));
|
|
238
|
+
if (multihashBytes.byteLength !== specs.multihashSize) {
|
|
239
|
+
throw new Error('Incorrect length');
|
|
240
|
+
}
|
|
241
|
+
const digestBytes = multihashBytes.subarray(specs.multihashSize - specs.digestSize);
|
|
242
|
+
const digest = new Digest(specs.multihashCode, specs.digestSize, digestBytes, multihashBytes);
|
|
243
|
+
const cid = specs.version === 0
|
|
244
|
+
? CID.createV0(digest)
|
|
245
|
+
: CID.createV1(specs.codec, digest);
|
|
246
|
+
return [cid, bytes.subarray(specs.size)];
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Inspect the initial bytes of a CID to determine its properties.
|
|
250
|
+
*
|
|
251
|
+
* Involves decoding up to 4 varints. Typically this will require only 4 to 6
|
|
252
|
+
* bytes but for larger multicodec code values and larger multihash digest
|
|
253
|
+
* lengths these varints can be quite large. It is recommended that at least
|
|
254
|
+
* 10 bytes be made available in the `initialBytes` argument for a complete
|
|
255
|
+
* inspection.
|
|
256
|
+
*/
|
|
257
|
+
static inspectBytes(initialBytes) {
|
|
258
|
+
let offset = 0;
|
|
259
|
+
const next = () => {
|
|
260
|
+
const [i, length] = decode$1(initialBytes.subarray(offset));
|
|
261
|
+
offset += length;
|
|
262
|
+
return i;
|
|
263
|
+
};
|
|
264
|
+
let version = next();
|
|
265
|
+
let codec = DAG_PB_CODE;
|
|
266
|
+
if (version === 18) {
|
|
267
|
+
// CIDv0
|
|
268
|
+
version = 0;
|
|
269
|
+
offset = 0;
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
codec = next();
|
|
273
|
+
}
|
|
274
|
+
if (version !== 0 && version !== 1) {
|
|
275
|
+
throw new RangeError(`Invalid CID version ${version}`);
|
|
276
|
+
}
|
|
277
|
+
const prefixSize = offset;
|
|
278
|
+
const multihashCode = next(); // multihash code
|
|
279
|
+
const digestSize = next(); // multihash length
|
|
280
|
+
const size = offset + digestSize;
|
|
281
|
+
const multihashSize = size - prefixSize;
|
|
282
|
+
return { version, codec, multihashCode, digestSize, multihashSize, size };
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Takes cid in a string representation and creates an instance. If `base`
|
|
286
|
+
* decoder is not provided will use a default from the configuration. It will
|
|
287
|
+
* throw an error if encoding of the CID is not compatible with supplied (or
|
|
288
|
+
* a default decoder).
|
|
289
|
+
*/
|
|
290
|
+
static parse(source, base) {
|
|
291
|
+
const [prefix, bytes] = parseCIDtoBytes(source, base);
|
|
292
|
+
const cid = CID.decode(bytes);
|
|
293
|
+
if (cid.version === 0 && source[0] !== 'Q') {
|
|
294
|
+
throw Error('Version 0 CID string must not include multibase prefix');
|
|
295
|
+
}
|
|
296
|
+
// Cache string representation to avoid computing it on `this.toString()`
|
|
297
|
+
baseCache(cid).set(prefix, source);
|
|
298
|
+
return cid;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
function parseCIDtoBytes(source, base) {
|
|
302
|
+
switch (source[0]) {
|
|
303
|
+
// CIDv0 is parsed differently
|
|
304
|
+
case 'Q': {
|
|
305
|
+
const decoder = base ?? base58btc;
|
|
306
|
+
return [
|
|
307
|
+
base58btc.prefix,
|
|
308
|
+
decoder.decode(`${base58btc.prefix}${source}`)
|
|
309
|
+
];
|
|
310
|
+
}
|
|
311
|
+
case base58btc.prefix: {
|
|
312
|
+
const decoder = base ?? base58btc;
|
|
313
|
+
return [base58btc.prefix, decoder.decode(source)];
|
|
314
|
+
}
|
|
315
|
+
case base32.prefix: {
|
|
316
|
+
const decoder = base ?? base32;
|
|
317
|
+
return [base32.prefix, decoder.decode(source)];
|
|
318
|
+
}
|
|
319
|
+
case base36.prefix: {
|
|
320
|
+
const decoder = base ?? base36;
|
|
321
|
+
return [base36.prefix, decoder.decode(source)];
|
|
322
|
+
}
|
|
323
|
+
default: {
|
|
324
|
+
if (base == null) {
|
|
325
|
+
throw Error('To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided');
|
|
326
|
+
}
|
|
327
|
+
return [source[0], base.decode(source)];
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
function toStringV0(bytes, cache, base) {
|
|
332
|
+
const { prefix } = base;
|
|
333
|
+
if (prefix !== base58btc.prefix) {
|
|
334
|
+
throw Error(`Cannot string encode V0 in ${base.name} encoding`);
|
|
335
|
+
}
|
|
336
|
+
const cid = cache.get(prefix);
|
|
337
|
+
if (cid == null) {
|
|
338
|
+
const cid = base.encode(bytes).slice(1);
|
|
339
|
+
cache.set(prefix, cid);
|
|
340
|
+
return cid;
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
return cid;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
function toStringV1(bytes, cache, base) {
|
|
347
|
+
const { prefix } = base;
|
|
348
|
+
const cid = cache.get(prefix);
|
|
349
|
+
if (cid == null) {
|
|
350
|
+
const cid = base.encode(bytes);
|
|
351
|
+
cache.set(prefix, cid);
|
|
352
|
+
return cid;
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
return cid;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
const DAG_PB_CODE = 0x70;
|
|
359
|
+
const SHA_256_CODE = 0x12;
|
|
360
|
+
function encodeCID(version, code, multihash) {
|
|
361
|
+
const codeOffset = encodingLength(version);
|
|
362
|
+
const hashOffset = codeOffset + encodingLength(code);
|
|
363
|
+
const bytes = new Uint8Array(hashOffset + multihash.byteLength);
|
|
364
|
+
encodeTo(version, bytes, 0);
|
|
365
|
+
encodeTo(code, bytes, codeOffset);
|
|
366
|
+
bytes.set(multihash, hashOffset);
|
|
367
|
+
return bytes;
|
|
368
|
+
}
|
|
369
|
+
const cidSymbol = Symbol.for('@ipld/js-cid/CID');
|
|
370
|
+
|
|
371
|
+
export { CID, format };
|