@atcute/cid 2.3.0 → 2.4.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/dist/codec.d.ts +1 -7
- package/dist/codec.d.ts.map +1 -1
- package/dist/codec.js +10 -42
- package/dist/codec.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/lib/codec.ts +11 -50
- package/lib/index.ts +2 -0
- package/package.json +1 -1
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
|
*/
|
|
@@ -40,12 +40,6 @@ export declare const fromDigest: (codec: 85 | 113, digest: Uint8Array<ArrayBuffe
|
|
|
40
40
|
* @returns CID object
|
|
41
41
|
*/
|
|
42
42
|
export declare const create: (codec: 85 | 113, data: Uint8Array<ArrayBufferLike>) => Promise<Cid>;
|
|
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;
|
|
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;AA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AAE,UAAU,CAAC;KAC9B,CAAC;IACF,oBAAoB;IACpB,QAAQ,CAAC,KAAK,EAAE,UAAU,CAAC;CAC3B;AAKD;;;;;GAKG;AACH,eAAO,MAAM,UAAU,+DAuBtB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,MAAM,sEAGlB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,WAAW,gGAqCvB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,MAAM,6CAQlB,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,wBAWtB,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,QAAQ,sBASpB,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,6CAOtB,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,QAAQ,2CAMpB,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,MAAM,6BAElB,CAAC"}
|
package/dist/codec.js
CHANGED
|
@@ -48,25 +48,6 @@ export const create = async (codec, data) => {
|
|
|
48
48
|
const digest = await toSha256(data);
|
|
49
49
|
return fromDigest(codec, digest);
|
|
50
50
|
};
|
|
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
51
|
/**
|
|
71
52
|
* decodes a CID from bytes, returning the CID and any remaining bytes
|
|
72
53
|
* @param bytes raw CID bytes
|
|
@@ -74,8 +55,7 @@ export const createEmpty = (codec) => {
|
|
|
74
55
|
* @throws {RangeError} if the bytes are too short or contain invalid values
|
|
75
56
|
*/
|
|
76
57
|
export const decodeFirst = (bytes) => {
|
|
77
|
-
|
|
78
|
-
if (length < 4) {
|
|
58
|
+
if (bytes.length < 36) {
|
|
79
59
|
throw new RangeError(`cid too short`);
|
|
80
60
|
}
|
|
81
61
|
const version = bytes[0];
|
|
@@ -91,22 +71,19 @@ export const decodeFirst = (bytes) => {
|
|
|
91
71
|
if (digestType !== HASH_SHA256) {
|
|
92
72
|
throw new RangeError(`incorrect cid digest codec (got 0x${digestType.toString(16)})`);
|
|
93
73
|
}
|
|
94
|
-
if (digestSize !== 32
|
|
74
|
+
if (digestSize !== 32) {
|
|
95
75
|
throw new RangeError(`incorrect cid digest size (got ${digestSize})`);
|
|
96
76
|
}
|
|
97
|
-
if (length < 4 + digestSize) {
|
|
98
|
-
throw new RangeError(`cid too short`);
|
|
99
|
-
}
|
|
100
77
|
const cid = {
|
|
101
78
|
version: CID_VERSION,
|
|
102
79
|
codec: codec,
|
|
103
80
|
digest: {
|
|
104
81
|
codec: digestType,
|
|
105
|
-
contents: bytes.subarray(4,
|
|
82
|
+
contents: bytes.subarray(4, 36),
|
|
106
83
|
},
|
|
107
|
-
bytes: bytes.subarray(0,
|
|
84
|
+
bytes: bytes.subarray(0, 36),
|
|
108
85
|
};
|
|
109
|
-
return [cid, bytes.subarray(
|
|
86
|
+
return [cid, bytes.subarray(36)];
|
|
110
87
|
};
|
|
111
88
|
/**
|
|
112
89
|
* decodes a CID from bytes, expecting no remainder
|
|
@@ -129,13 +106,9 @@ export const decode = (bytes) => {
|
|
|
129
106
|
* @throws {RangeError} if the string length is invalid
|
|
130
107
|
*/
|
|
131
108
|
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
109
|
// 36 bytes in base32 = 58 characters + 1 character for the prefix
|
|
137
|
-
if (input.length !== 59
|
|
138
|
-
throw new
|
|
110
|
+
if (input.length !== 59 || input[0] !== 'b') {
|
|
111
|
+
throw new SyntaxError(`not a valid cid string`);
|
|
139
112
|
}
|
|
140
113
|
const bytes = fromBase32(input.slice(1));
|
|
141
114
|
const cid = decode(bytes);
|
|
@@ -163,16 +136,11 @@ export const toString = (cid) => {
|
|
|
163
136
|
* @throws {SyntaxError} if the prefix byte is not 0x00
|
|
164
137
|
*/
|
|
165
138
|
export const fromBinary = (input) => {
|
|
166
|
-
// 4 bytes + 1 byte for the 0x00 prefix
|
|
167
139
|
// 36 bytes + 1 byte for the 0x00 prefix
|
|
168
|
-
if (input.length !== 37
|
|
169
|
-
throw new
|
|
170
|
-
}
|
|
171
|
-
if (input[0] !== 0) {
|
|
172
|
-
throw new SyntaxError(`incorrect binary cid`);
|
|
140
|
+
if (input.length !== 37 || input[0] !== 0) {
|
|
141
|
+
throw new SyntaxError(`invalid binary cid`);
|
|
173
142
|
}
|
|
174
|
-
|
|
175
|
-
return decode(bytes);
|
|
143
|
+
return decode(input.subarray(1));
|
|
176
144
|
};
|
|
177
145
|
/**
|
|
178
146
|
* 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,EAAE,QAAQ,EAAE,MAAM,IAAI,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEpF,wCAAwC;AACxC,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,CAAC;AAC7B,kCAAkC;AAClC,MAAM,CAAC,MAAM,WAAW,GAAG,I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AClD,OAAO,aAAa,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;AAAA,CACvC,CAAC"}
|
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/codec.ts
CHANGED
|
@@ -15,7 +15,7 @@ export const CODEC_DCBOR = 0x71;
|
|
|
15
15
|
export const CID_STRINGIFY_CACHE = new WeakMap<Cid, string>();
|
|
16
16
|
|
|
17
17
|
/**
|
|
18
|
-
*
|
|
18
|
+
* represents a Content Identifier (CID), in particular, a limited subset of
|
|
19
19
|
* CIDv1 as described by DASL specifications.
|
|
20
20
|
* https://dasl.ing/cid.html
|
|
21
21
|
*/
|
|
@@ -80,28 +80,6 @@ export const create = async (codec: 0x55 | 0x71, data: Uint8Array): Promise<Cid>
|
|
|
80
80
|
return fromDigest(codec, digest);
|
|
81
81
|
};
|
|
82
82
|
|
|
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
83
|
/**
|
|
106
84
|
* decodes a CID from bytes, returning the CID and any remaining bytes
|
|
107
85
|
* @param bytes raw CID bytes
|
|
@@ -109,9 +87,7 @@ export const createEmpty = (codec: 0x55 | 0x71): Cid => {
|
|
|
109
87
|
* @throws {RangeError} if the bytes are too short or contain invalid values
|
|
110
88
|
*/
|
|
111
89
|
export const decodeFirst = (bytes: Uint8Array): [decoded: Cid, remainder: Uint8Array] => {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
if (length < 4) {
|
|
90
|
+
if (bytes.length < 36) {
|
|
115
91
|
throw new RangeError(`cid too short`);
|
|
116
92
|
}
|
|
117
93
|
|
|
@@ -132,25 +108,21 @@ export const decodeFirst = (bytes: Uint8Array): [decoded: Cid, remainder: Uint8A
|
|
|
132
108
|
throw new RangeError(`incorrect cid digest codec (got 0x${digestType.toString(16)})`);
|
|
133
109
|
}
|
|
134
110
|
|
|
135
|
-
if (digestSize !== 32
|
|
111
|
+
if (digestSize !== 32) {
|
|
136
112
|
throw new RangeError(`incorrect cid digest size (got ${digestSize})`);
|
|
137
113
|
}
|
|
138
114
|
|
|
139
|
-
if (length < 4 + digestSize) {
|
|
140
|
-
throw new RangeError(`cid too short`);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
115
|
const cid: Cid = {
|
|
144
116
|
version: CID_VERSION,
|
|
145
117
|
codec: codec,
|
|
146
118
|
digest: {
|
|
147
119
|
codec: digestType,
|
|
148
|
-
contents: bytes.subarray(4,
|
|
120
|
+
contents: bytes.subarray(4, 36),
|
|
149
121
|
},
|
|
150
|
-
bytes: bytes.subarray(0,
|
|
122
|
+
bytes: bytes.subarray(0, 36),
|
|
151
123
|
};
|
|
152
124
|
|
|
153
|
-
return [cid, bytes.subarray(
|
|
125
|
+
return [cid, bytes.subarray(36)];
|
|
154
126
|
};
|
|
155
127
|
|
|
156
128
|
/**
|
|
@@ -177,14 +149,9 @@ export const decode = (bytes: Uint8Array): Cid => {
|
|
|
177
149
|
* @throws {RangeError} if the string length is invalid
|
|
178
150
|
*/
|
|
179
151
|
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
152
|
// 36 bytes in base32 = 58 characters + 1 character for the prefix
|
|
186
|
-
if (input.length !== 59
|
|
187
|
-
throw new
|
|
153
|
+
if (input.length !== 59 || input[0] !== 'b') {
|
|
154
|
+
throw new SyntaxError(`not a valid cid string`);
|
|
188
155
|
}
|
|
189
156
|
|
|
190
157
|
const bytes = fromBase32(input.slice(1));
|
|
@@ -218,18 +185,12 @@ export const toString = (cid: Cid): string => {
|
|
|
218
185
|
* @throws {SyntaxError} if the prefix byte is not 0x00
|
|
219
186
|
*/
|
|
220
187
|
export const fromBinary = (input: Uint8Array): Cid => {
|
|
221
|
-
// 4 bytes + 1 byte for the 0x00 prefix
|
|
222
188
|
// 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`);
|
|
189
|
+
if (input.length !== 37 || input[0] !== 0) {
|
|
190
|
+
throw new SyntaxError(`invalid binary cid`);
|
|
229
191
|
}
|
|
230
192
|
|
|
231
|
-
|
|
232
|
-
return decode(bytes);
|
|
193
|
+
return decode(input.subarray(1));
|
|
233
194
|
};
|
|
234
195
|
|
|
235
196
|
/**
|
package/lib/index.ts
CHANGED