@atcute/cid 2.3.0 → 2.4.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/README.md +16 -57
- package/dist/cid-link.d.ts +1 -1
- package/dist/cid-link.d.ts.map +1 -1
- package/dist/cid-link.js +8 -15
- package/dist/cid-link.js.map +1 -1
- package/dist/codec.d.ts +2 -8
- package/dist/codec.d.ts.map +1 -1
- package/dist/codec.js +12 -53
- package/dist/codec.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/lib/cid-link.ts +8 -19
- package/lib/codec.ts +14 -66
- package/lib/index.ts +4 -2
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -13,74 +13,33 @@ their contents.
|
|
|
13
13
|
|
|
14
14
|
## usage
|
|
15
15
|
|
|
16
|
-
### creating CIDs
|
|
17
|
-
|
|
18
16
|
```ts
|
|
19
17
|
import * as CID from '@atcute/cid';
|
|
18
|
+
import { toCidLink, fromCidLink, isCidLink } from '@atcute/cid';
|
|
20
19
|
|
|
21
|
-
// create a CID
|
|
20
|
+
// create a CID by hashing data (0x71 for DAG-CBOR, 0x55 for raw)
|
|
22
21
|
const cid = await CID.create(0x71, cborBytes);
|
|
23
|
-
// -> { version: 1, codec: 113, digest: {
|
|
24
|
-
|
|
25
|
-
// create from raw data
|
|
26
|
-
const rawCid = await CID.create(0x55, rawBytes);
|
|
27
|
-
|
|
28
|
-
// create an empty CID (zero-length digest)
|
|
29
|
-
const empty = CID.createEmpty(0x71);
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
### parsing CIDs
|
|
33
|
-
|
|
34
|
-
```ts
|
|
35
|
-
import * as CID from '@atcute/cid';
|
|
22
|
+
// -> { version: 1, codec: 113, digest: { codec: 18, contents: Uint8Array(32) }, bytes: Uint8Array(36) }
|
|
36
23
|
|
|
37
24
|
// parse from base32 string
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
// parse from binary (with 0x00 prefix)
|
|
41
|
-
const cid = CID.fromBinary(bytes);
|
|
42
|
-
|
|
43
|
-
// parse from raw CID bytes
|
|
44
|
-
const cid = CID.decode(cidBytes);
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### serializing CIDs
|
|
48
|
-
|
|
49
|
-
```ts
|
|
50
|
-
import * as CID from '@atcute/cid';
|
|
51
|
-
|
|
52
|
-
// to base32 string
|
|
53
|
-
CID.toString(cid);
|
|
54
|
-
// -> "bafyreihffx5a2e7k5uwrmmgofbvzujc5cmw5h4espouwuxt3liqoflx3ee"
|
|
55
|
-
|
|
56
|
-
// to binary (with 0x00 prefix)
|
|
57
|
-
CID.toBinary(cid);
|
|
58
|
-
// -> Uint8Array(37)
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
### comparing CIDs
|
|
62
|
-
|
|
63
|
-
```ts
|
|
64
|
-
import * as CID from '@atcute/cid';
|
|
65
|
-
|
|
66
|
-
CID.equals(cidA, cidB); // true if same content hash
|
|
67
|
-
```
|
|
25
|
+
const parsed = CID.fromString('bafyreihffx5a2e7k5uwrmmgofbvzujc5cmw5h4espouwuxt3liqoflx3ee');
|
|
68
26
|
|
|
69
|
-
|
|
27
|
+
// parse from binary (with 0x00 prefix) or raw CID bytes
|
|
28
|
+
const fromBin = CID.fromBinary(binaryBytes);
|
|
29
|
+
const fromRaw = CID.decode(cidBytes);
|
|
70
30
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
import { toCidLink, fromCidLink, isCidLink } from '@atcute/cid';
|
|
31
|
+
// serialize to base32 string or binary format
|
|
32
|
+
CID.toString(cid); // -> "bafyreihffx5a2e7k5uwrmmgofbvzujc5cmw5h4espouwuxt3liqoflx3ee"
|
|
33
|
+
CID.toBinary(cid); // -> Uint8Array(37) with 0x00 prefix
|
|
75
34
|
|
|
76
|
-
//
|
|
77
|
-
|
|
78
|
-
// -> { $link: "bafyrei..." }
|
|
35
|
+
// compare two CIDs
|
|
36
|
+
CID.equals(cidA, cidB); // -> true if identical
|
|
79
37
|
|
|
80
|
-
// convert
|
|
81
|
-
const
|
|
38
|
+
// convert to CidLink for JSON serialization (atproto data model)
|
|
39
|
+
const link = toCidLink(cid); // -> { $link: "bafyrei..." }
|
|
40
|
+
const back = fromCidLink(link);
|
|
82
41
|
|
|
83
|
-
// type guard
|
|
42
|
+
// type guard for CidLink
|
|
84
43
|
if (isCidLink(value)) {
|
|
85
44
|
console.log(value.$link);
|
|
86
45
|
}
|
package/dist/cid-link.d.ts
CHANGED
package/dist/cid-link.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cid-link.d.ts","sourceRoot":"","sources":["../lib/cid-link.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"cid-link.d.ts","sourceRoot":"","sources":["../lib/cid-link.ts"],"names":[],"mappings":"AAEA,OAAO,EAAsB,KAAK,GAAG,EAAE,MAAM,YAAY,CAAC;AAI1D,MAAM,WAAW,OAAO;IACvB,KAAK,EAAE,MAAM,CAAC;CACd;AAED,qBAAa,cAAe,YAAW,OAAO;IAI7C,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;IAE3B,YAAY,KAAK,EAAE,UAAU,EAE5B;IAED,IAAI,KAAK,IAAI,MAAM,CAQlB;IAED,MAAM,IAAI,OAAO,CAEhB;CACD;AAED,eAAO,MAAM,SAAS,sCAOrB,CAAC;AAEF,eAAO,MAAM,SAAS,uBAErB,CAAC;AAEF,eAAO,MAAM,WAAW,wBAMvB,CAAC"}
|
package/dist/cid-link.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { toBase32 } from '@atcute/multibase';
|
|
2
|
-
import {
|
|
2
|
+
import { decode, fromString } from './codec.js';
|
|
3
3
|
const CID_LINK_SYMBOL = Symbol.for('@atcute/cid-link-wrapper');
|
|
4
|
-
/** @internal */
|
|
5
|
-
export const CIDLINK_STRINGIFY_CACHE = new WeakMap();
|
|
6
4
|
export class CidLinkWrapper {
|
|
7
5
|
/** @internal */
|
|
8
6
|
[CID_LINK_SYMBOL] = true;
|
|
@@ -11,12 +9,12 @@ export class CidLinkWrapper {
|
|
|
11
9
|
this.bytes = bytes;
|
|
12
10
|
}
|
|
13
11
|
get $link() {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
return
|
|
12
|
+
const link = `b${toBase32(this.bytes)}`;
|
|
13
|
+
Object.defineProperty(this, '$link', {
|
|
14
|
+
value: link,
|
|
15
|
+
enumerable: true,
|
|
16
|
+
});
|
|
17
|
+
return link;
|
|
20
18
|
}
|
|
21
19
|
toJSON() {
|
|
22
20
|
return { $link: this.$link };
|
|
@@ -28,12 +26,7 @@ export const isCidLink = (value) => {
|
|
|
28
26
|
(val !== null && typeof val === 'object' && typeof val.$link === 'string'));
|
|
29
27
|
};
|
|
30
28
|
export const toCidLink = (cid) => {
|
|
31
|
-
|
|
32
|
-
const str = CID_STRINGIFY_CACHE.get(cid);
|
|
33
|
-
if (str !== undefined) {
|
|
34
|
-
CIDLINK_STRINGIFY_CACHE.set(inst, str);
|
|
35
|
-
}
|
|
36
|
-
return inst;
|
|
29
|
+
return new CidLinkWrapper(cid.bytes);
|
|
37
30
|
};
|
|
38
31
|
export const fromCidLink = (link) => {
|
|
39
32
|
if (link instanceof CidLinkWrapper) {
|
package/dist/cid-link.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cid-link.js","sourceRoot":"","sources":["../lib/cid-link.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"cid-link.js","sourceRoot":"","sources":["../lib/cid-link.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE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AC,KAAK,CAAC,CAAC;IAC3B,CAAC;IAED,OAAO,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAAA,CAC9B,CAAC"}
|
package/dist/codec.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export declare const CODEC_RAW = 85;
|
|
|
7
7
|
/** multicodec for DAG-CBOR encoded data */
|
|
8
8
|
export declare const CODEC_DCBOR = 113;
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* represents a Content Identifier (CID), in particular, a limited subset of
|
|
11
11
|
* CIDv1 as described by DASL specifications.
|
|
12
12
|
* https://dasl.ing/cid.html
|
|
13
13
|
*/
|
|
@@ -39,13 +39,7 @@ export declare const fromDigest: (codec: 85 | 113, digest: Uint8Array<ArrayBuffe
|
|
|
39
39
|
* @param data raw data to hash
|
|
40
40
|
* @returns CID object
|
|
41
41
|
*/
|
|
42
|
-
export declare const create: (codec: 85 | 113, data: Uint8Array<
|
|
43
|
-
/**
|
|
44
|
-
* creates an empty CID with a zero-length digest
|
|
45
|
-
* @param codec multicodec type for the data
|
|
46
|
-
* @returns CID object with empty digest
|
|
47
|
-
*/
|
|
48
|
-
export declare const createEmpty: (codec: 85 | 113) => Cid;
|
|
42
|
+
export declare const create: (codec: 85 | 113, data: Uint8Array<ArrayBuffer>) => Promise<Cid>;
|
|
49
43
|
/**
|
|
50
44
|
* decodes a CID from bytes, returning the CID and any remaining bytes
|
|
51
45
|
* @param bytes raw CID bytes
|
package/dist/codec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../lib/codec.ts"],"names":[],"mappings":"AAGA,wCAAwC;AACxC,eAAO,MAAM,WAAW,IAAI,CAAC;AAC7B,kCAAkC;AAClC,eAAO,MAAM,WAAW,KAAO,CAAC;AAEhC,qCAAqC;AACrC,eAAO,MAAM,SAAS,KAAO,CAAC;AAC9B,2CAA2C;AAC3C,eAAO,MAAM,WAAW,MAAO,CA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= 0x12;
|
|
|
8
8
|
export const CODEC_RAW = 0x55;
|
|
9
9
|
/** multicodec for DAG-CBOR encoded data */
|
|
10
10
|
export const CODEC_DCBOR = 0x71;
|
|
11
|
-
/** @internal */
|
|
12
|
-
export const CID_STRINGIFY_CACHE = new WeakMap();
|
|
13
11
|
// a SHA-256 CIDv1 is always going to be 36 bytes, that's 4 bytes for the
|
|
14
12
|
// header, and 32 bytes for the digest itself.
|
|
15
13
|
/**
|
|
@@ -48,25 +46,6 @@ export const create = async (codec, data) => {
|
|
|
48
46
|
const digest = await toSha256(data);
|
|
49
47
|
return fromDigest(codec, digest);
|
|
50
48
|
};
|
|
51
|
-
/**
|
|
52
|
-
* creates an empty CID with a zero-length digest
|
|
53
|
-
* @param codec multicodec type for the data
|
|
54
|
-
* @returns CID object with empty digest
|
|
55
|
-
*/
|
|
56
|
-
export const createEmpty = (codec) => {
|
|
57
|
-
const bytes = Uint8Array.from([CID_VERSION, codec, HASH_SHA256, 0]);
|
|
58
|
-
const digest = bytes.subarray(4);
|
|
59
|
-
const cid = {
|
|
60
|
-
version: CID_VERSION,
|
|
61
|
-
codec: codec,
|
|
62
|
-
digest: {
|
|
63
|
-
codec: HASH_SHA256,
|
|
64
|
-
contents: digest,
|
|
65
|
-
},
|
|
66
|
-
bytes: bytes,
|
|
67
|
-
};
|
|
68
|
-
return cid;
|
|
69
|
-
};
|
|
70
49
|
/**
|
|
71
50
|
* decodes a CID from bytes, returning the CID and any remaining bytes
|
|
72
51
|
* @param bytes raw CID bytes
|
|
@@ -74,8 +53,7 @@ export const createEmpty = (codec) => {
|
|
|
74
53
|
* @throws {RangeError} if the bytes are too short or contain invalid values
|
|
75
54
|
*/
|
|
76
55
|
export const decodeFirst = (bytes) => {
|
|
77
|
-
|
|
78
|
-
if (length < 4) {
|
|
56
|
+
if (bytes.length < 36) {
|
|
79
57
|
throw new RangeError(`cid too short`);
|
|
80
58
|
}
|
|
81
59
|
const version = bytes[0];
|
|
@@ -91,22 +69,19 @@ export const decodeFirst = (bytes) => {
|
|
|
91
69
|
if (digestType !== HASH_SHA256) {
|
|
92
70
|
throw new RangeError(`incorrect cid digest codec (got 0x${digestType.toString(16)})`);
|
|
93
71
|
}
|
|
94
|
-
if (digestSize !== 32
|
|
72
|
+
if (digestSize !== 32) {
|
|
95
73
|
throw new RangeError(`incorrect cid digest size (got ${digestSize})`);
|
|
96
74
|
}
|
|
97
|
-
if (length < 4 + digestSize) {
|
|
98
|
-
throw new RangeError(`cid too short`);
|
|
99
|
-
}
|
|
100
75
|
const cid = {
|
|
101
76
|
version: CID_VERSION,
|
|
102
77
|
codec: codec,
|
|
103
78
|
digest: {
|
|
104
79
|
codec: digestType,
|
|
105
|
-
contents: bytes.subarray(4,
|
|
80
|
+
contents: bytes.subarray(4, 36),
|
|
106
81
|
},
|
|
107
|
-
bytes: bytes.subarray(0,
|
|
82
|
+
bytes: bytes.subarray(0, 36),
|
|
108
83
|
};
|
|
109
|
-
return [cid, bytes.subarray(
|
|
84
|
+
return [cid, bytes.subarray(36)];
|
|
110
85
|
};
|
|
111
86
|
/**
|
|
112
87
|
* decodes a CID from bytes, expecting no remainder
|
|
@@ -129,18 +104,12 @@ export const decode = (bytes) => {
|
|
|
129
104
|
* @throws {RangeError} if the string length is invalid
|
|
130
105
|
*/
|
|
131
106
|
export const fromString = (input) => {
|
|
132
|
-
if (input.length < 2 || input[0] !== 'b') {
|
|
133
|
-
throw new SyntaxError(`not a multibase base32 string`);
|
|
134
|
-
}
|
|
135
|
-
// 4 bytes in base32 = 7 characters + 1 character for the prefix
|
|
136
107
|
// 36 bytes in base32 = 58 characters + 1 character for the prefix
|
|
137
|
-
if (input.length !== 59
|
|
138
|
-
throw new
|
|
108
|
+
if (input.length !== 59 || input[0] !== 'b') {
|
|
109
|
+
throw new SyntaxError(`not a valid cid string`);
|
|
139
110
|
}
|
|
140
111
|
const bytes = fromBase32(input.slice(1));
|
|
141
|
-
|
|
142
|
-
CID_STRINGIFY_CACHE.set(cid, input);
|
|
143
|
-
return cid;
|
|
112
|
+
return decode(bytes);
|
|
144
113
|
};
|
|
145
114
|
/**
|
|
146
115
|
* encodes a CID to a multibase base32 string
|
|
@@ -148,12 +117,7 @@ export const fromString = (input) => {
|
|
|
148
117
|
* @returns base32-encoded string with 'b' prefix
|
|
149
118
|
*/
|
|
150
119
|
export const toString = (cid) => {
|
|
151
|
-
|
|
152
|
-
if (str === undefined) {
|
|
153
|
-
str = `b${toBase32(cid.bytes)}`;
|
|
154
|
-
CID_STRINGIFY_CACHE.set(cid, str);
|
|
155
|
-
}
|
|
156
|
-
return str;
|
|
120
|
+
return `b${toBase32(cid.bytes)}`;
|
|
157
121
|
};
|
|
158
122
|
/**
|
|
159
123
|
* parses a CID from binary format (with 0x00 prefix)
|
|
@@ -163,16 +127,11 @@ export const toString = (cid) => {
|
|
|
163
127
|
* @throws {SyntaxError} if the prefix byte is not 0x00
|
|
164
128
|
*/
|
|
165
129
|
export const fromBinary = (input) => {
|
|
166
|
-
// 4 bytes + 1 byte for the 0x00 prefix
|
|
167
130
|
// 36 bytes + 1 byte for the 0x00 prefix
|
|
168
|
-
if (input.length !== 37
|
|
169
|
-
throw new
|
|
131
|
+
if (input.length !== 37 || input[0] !== 0) {
|
|
132
|
+
throw new SyntaxError(`invalid binary cid`);
|
|
170
133
|
}
|
|
171
|
-
|
|
172
|
-
throw new SyntaxError(`incorrect binary cid`);
|
|
173
|
-
}
|
|
174
|
-
const bytes = input.subarray(1);
|
|
175
|
-
return decode(bytes);
|
|
134
|
+
return decode(input.subarray(1));
|
|
176
135
|
};
|
|
177
136
|
/**
|
|
178
137
|
* encodes a CID to binary format (with 0x00 prefix)
|
package/dist/codec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../lib/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAAE,WAAW,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C"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './cid-link.
|
|
2
|
-
export * from './codec.
|
|
1
|
+
export * from './cid-link.ts';
|
|
2
|
+
export * from './codec.ts';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAEA,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,0DAA0D;AAE1D,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC"}
|
package/lib/cid-link.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { toBase32 } from '@atcute/multibase';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { decode, fromString, type Cid } from './codec.ts';
|
|
4
4
|
|
|
5
5
|
const CID_LINK_SYMBOL = Symbol.for('@atcute/cid-link-wrapper');
|
|
6
6
|
|
|
7
|
-
/** @internal */
|
|
8
|
-
export const CIDLINK_STRINGIFY_CACHE = new WeakMap<CidLinkWrapper, string>();
|
|
9
|
-
|
|
10
7
|
export interface CidLink {
|
|
11
8
|
$link: string;
|
|
12
9
|
}
|
|
@@ -22,14 +19,13 @@ export class CidLinkWrapper implements CidLink {
|
|
|
22
19
|
}
|
|
23
20
|
|
|
24
21
|
get $link(): string {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
22
|
+
const link = `b${toBase32(this.bytes)}`;
|
|
23
|
+
Object.defineProperty(this, '$link', {
|
|
24
|
+
value: link,
|
|
25
|
+
enumerable: true,
|
|
26
|
+
});
|
|
31
27
|
|
|
32
|
-
return
|
|
28
|
+
return link;
|
|
33
29
|
}
|
|
34
30
|
|
|
35
31
|
toJSON(): CidLink {
|
|
@@ -47,14 +43,7 @@ export const isCidLink = (value: unknown): value is CidLink => {
|
|
|
47
43
|
};
|
|
48
44
|
|
|
49
45
|
export const toCidLink = (cid: Cid): CidLink => {
|
|
50
|
-
|
|
51
|
-
const str = CID_STRINGIFY_CACHE.get(cid);
|
|
52
|
-
|
|
53
|
-
if (str !== undefined) {
|
|
54
|
-
CIDLINK_STRINGIFY_CACHE.set(inst, str);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return inst;
|
|
46
|
+
return new CidLinkWrapper(cid.bytes);
|
|
58
47
|
};
|
|
59
48
|
|
|
60
49
|
export const fromCidLink = (link: CidLink): Cid => {
|
package/lib/codec.ts
CHANGED
|
@@ -11,11 +11,8 @@ export const CODEC_RAW = 0x55;
|
|
|
11
11
|
/** multicodec for DAG-CBOR encoded data */
|
|
12
12
|
export const CODEC_DCBOR = 0x71;
|
|
13
13
|
|
|
14
|
-
/** @internal */
|
|
15
|
-
export const CID_STRINGIFY_CACHE = new WeakMap<Cid, string>();
|
|
16
|
-
|
|
17
14
|
/**
|
|
18
|
-
*
|
|
15
|
+
* represents a Content Identifier (CID), in particular, a limited subset of
|
|
19
16
|
* CIDv1 as described by DASL specifications.
|
|
20
17
|
* https://dasl.ing/cid.html
|
|
21
18
|
*/
|
|
@@ -75,33 +72,11 @@ export const fromDigest = (codec: 0x55 | 0x71, digest: Uint8Array): Cid => {
|
|
|
75
72
|
* @param data raw data to hash
|
|
76
73
|
* @returns CID object
|
|
77
74
|
*/
|
|
78
|
-
export const create = async (codec: 0x55 | 0x71, data: Uint8Array): Promise<Cid> => {
|
|
75
|
+
export const create = async (codec: 0x55 | 0x71, data: Uint8Array<ArrayBuffer>): Promise<Cid> => {
|
|
79
76
|
const digest = await toSha256(data);
|
|
80
77
|
return fromDigest(codec, digest);
|
|
81
78
|
};
|
|
82
79
|
|
|
83
|
-
/**
|
|
84
|
-
* creates an empty CID with a zero-length digest
|
|
85
|
-
* @param codec multicodec type for the data
|
|
86
|
-
* @returns CID object with empty digest
|
|
87
|
-
*/
|
|
88
|
-
export const createEmpty = (codec: 0x55 | 0x71): Cid => {
|
|
89
|
-
const bytes = Uint8Array.from([CID_VERSION, codec, HASH_SHA256, 0]);
|
|
90
|
-
const digest = bytes.subarray(4);
|
|
91
|
-
|
|
92
|
-
const cid: Cid = {
|
|
93
|
-
version: CID_VERSION,
|
|
94
|
-
codec: codec,
|
|
95
|
-
digest: {
|
|
96
|
-
codec: HASH_SHA256,
|
|
97
|
-
contents: digest,
|
|
98
|
-
},
|
|
99
|
-
bytes: bytes,
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
return cid;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
80
|
/**
|
|
106
81
|
* decodes a CID from bytes, returning the CID and any remaining bytes
|
|
107
82
|
* @param bytes raw CID bytes
|
|
@@ -109,9 +84,7 @@ export const createEmpty = (codec: 0x55 | 0x71): Cid => {
|
|
|
109
84
|
* @throws {RangeError} if the bytes are too short or contain invalid values
|
|
110
85
|
*/
|
|
111
86
|
export const decodeFirst = (bytes: Uint8Array): [decoded: Cid, remainder: Uint8Array] => {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
if (length < 4) {
|
|
87
|
+
if (bytes.length < 36) {
|
|
115
88
|
throw new RangeError(`cid too short`);
|
|
116
89
|
}
|
|
117
90
|
|
|
@@ -132,25 +105,21 @@ export const decodeFirst = (bytes: Uint8Array): [decoded: Cid, remainder: Uint8A
|
|
|
132
105
|
throw new RangeError(`incorrect cid digest codec (got 0x${digestType.toString(16)})`);
|
|
133
106
|
}
|
|
134
107
|
|
|
135
|
-
if (digestSize !== 32
|
|
108
|
+
if (digestSize !== 32) {
|
|
136
109
|
throw new RangeError(`incorrect cid digest size (got ${digestSize})`);
|
|
137
110
|
}
|
|
138
111
|
|
|
139
|
-
if (length < 4 + digestSize) {
|
|
140
|
-
throw new RangeError(`cid too short`);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
112
|
const cid: Cid = {
|
|
144
113
|
version: CID_VERSION,
|
|
145
114
|
codec: codec,
|
|
146
115
|
digest: {
|
|
147
116
|
codec: digestType,
|
|
148
|
-
contents: bytes.subarray(4,
|
|
117
|
+
contents: bytes.subarray(4, 36),
|
|
149
118
|
},
|
|
150
|
-
bytes: bytes.subarray(0,
|
|
119
|
+
bytes: bytes.subarray(0, 36),
|
|
151
120
|
};
|
|
152
121
|
|
|
153
|
-
return [cid, bytes.subarray(
|
|
122
|
+
return [cid, bytes.subarray(36)];
|
|
154
123
|
};
|
|
155
124
|
|
|
156
125
|
/**
|
|
@@ -177,21 +146,13 @@ export const decode = (bytes: Uint8Array): Cid => {
|
|
|
177
146
|
* @throws {RangeError} if the string length is invalid
|
|
178
147
|
*/
|
|
179
148
|
export const fromString = (input: string): Cid => {
|
|
180
|
-
if (input.length < 2 || input[0] !== 'b') {
|
|
181
|
-
throw new SyntaxError(`not a multibase base32 string`);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// 4 bytes in base32 = 7 characters + 1 character for the prefix
|
|
185
149
|
// 36 bytes in base32 = 58 characters + 1 character for the prefix
|
|
186
|
-
if (input.length !== 59
|
|
187
|
-
throw new
|
|
150
|
+
if (input.length !== 59 || input[0] !== 'b') {
|
|
151
|
+
throw new SyntaxError(`not a valid cid string`);
|
|
188
152
|
}
|
|
189
153
|
|
|
190
154
|
const bytes = fromBase32(input.slice(1));
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
CID_STRINGIFY_CACHE.set(cid, input);
|
|
194
|
-
return cid;
|
|
155
|
+
return decode(bytes);
|
|
195
156
|
};
|
|
196
157
|
|
|
197
158
|
/**
|
|
@@ -200,14 +161,7 @@ export const fromString = (input: string): Cid => {
|
|
|
200
161
|
* @returns base32-encoded string with 'b' prefix
|
|
201
162
|
*/
|
|
202
163
|
export const toString = (cid: Cid): string => {
|
|
203
|
-
|
|
204
|
-
if (str === undefined) {
|
|
205
|
-
str = `b${toBase32(cid.bytes)}`;
|
|
206
|
-
|
|
207
|
-
CID_STRINGIFY_CACHE.set(cid, str);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return str;
|
|
164
|
+
return `b${toBase32(cid.bytes)}`;
|
|
211
165
|
};
|
|
212
166
|
|
|
213
167
|
/**
|
|
@@ -218,18 +172,12 @@ export const toString = (cid: Cid): string => {
|
|
|
218
172
|
* @throws {SyntaxError} if the prefix byte is not 0x00
|
|
219
173
|
*/
|
|
220
174
|
export const fromBinary = (input: Uint8Array): Cid => {
|
|
221
|
-
// 4 bytes + 1 byte for the 0x00 prefix
|
|
222
175
|
// 36 bytes + 1 byte for the 0x00 prefix
|
|
223
|
-
if (input.length !== 37
|
|
224
|
-
throw new
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
if (input[0] !== 0) {
|
|
228
|
-
throw new SyntaxError(`incorrect binary cid`);
|
|
176
|
+
if (input.length !== 37 || input[0] !== 0) {
|
|
177
|
+
throw new SyntaxError(`invalid binary cid`);
|
|
229
178
|
}
|
|
230
179
|
|
|
231
|
-
|
|
232
|
-
return decode(bytes);
|
|
180
|
+
return decode(input.subarray(1));
|
|
233
181
|
};
|
|
234
182
|
|
|
235
183
|
/**
|
package/lib/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
1
|
{
|
|
2
|
-
"type": "module",
|
|
3
2
|
"name": "@atcute/cid",
|
|
4
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.1",
|
|
5
4
|
"description": "lightweight DASL CID codec library for AT Protocol",
|
|
6
5
|
"keywords": [
|
|
7
6
|
"atproto",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
7
|
+
"cid",
|
|
8
|
+
"dasl"
|
|
10
9
|
],
|
|
11
10
|
"license": "0BSD",
|
|
12
11
|
"repository": {
|
|
13
12
|
"url": "https://github.com/mary-ext/atcute",
|
|
14
13
|
"directory": "packages/utilities/cid"
|
|
15
14
|
},
|
|
16
|
-
"publishConfig": {
|
|
17
|
-
"access": "public"
|
|
18
|
-
},
|
|
19
15
|
"files": [
|
|
20
16
|
"dist/",
|
|
21
17
|
"lib/",
|
|
22
18
|
"!lib/**/*.bench.ts",
|
|
23
19
|
"!lib/**/*.test.ts"
|
|
24
20
|
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"sideEffects": false,
|
|
25
23
|
"exports": {
|
|
26
24
|
".": "./dist/index.js",
|
|
27
25
|
"./cid-link": "./dist/cid-link.js"
|
|
28
26
|
},
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
"@vitest/coverage-v8": "^4.0.16",
|
|
32
|
-
"vitest": "^4.0.16"
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
33
29
|
},
|
|
34
30
|
"dependencies": {
|
|
35
|
-
"@atcute/multibase": "^1.1.
|
|
36
|
-
"@atcute/uint8array": "^1.
|
|
31
|
+
"@atcute/multibase": "^1.1.8",
|
|
32
|
+
"@atcute/uint8array": "^1.1.1"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
36
|
+
"vitest": "^4.0.18"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsgo --project tsconfig.build.json",
|