-
Notifications
You must be signed in to change notification settings - Fork 54
/
cid.js
557 lines (513 loc) · 15.9 KB
/
cid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
import * as varint from './varint.js'
import * as Digest from './hashes/digest.js'
import { base58btc } from './bases/base58.js'
import { base32 } from './bases/base32.js'
import { coerce } from './bytes.js'
// Linter can see that API is used in types.
// eslint-disable-next-line
import * as API from "./link/interface.js"
// This way TS will also expose all the types from module
export * from './link/interface.js'
/**
* @template {API.Link<unknown, number, number, API.Version>} T
* @template {string} Prefix
* @param {T} link
* @param {API.MultibaseEncoder<Prefix>} [base]
* @returns {API.ToString<T, Prefix>}
*/
export const format = (link, base) => {
const { bytes, version } = link
switch (version) {
case 0:
return toStringV0(
bytes,
baseCache(link),
/** @type {API.MultibaseEncoder<"z">} */ (base) || base58btc.encoder
)
default:
return toStringV1(
bytes,
baseCache(link),
/** @type {API.MultibaseEncoder<Prefix>} */ (base || base32.encoder)
)
}
}
/** @type {WeakMap<API.UnknownLink, Map<string, string>>} */
const cache = new WeakMap()
/**
* @param {API.UnknownLink} cid
* @returns {Map<string, string>}
*/
const baseCache = cid => {
const baseCache = cache.get(cid)
if (baseCache == null) {
const baseCache = new Map()
cache.set(cid, baseCache)
return baseCache
}
return baseCache
}
/**
* @template {unknown} [Data=unknown]
* @template {number} [Format=number]
* @template {number} [Alg=number]
* @template {API.Version} [Version=API.Version]
* @implements {API.Link<Data, Format, Alg, Version>}
*/
export class CID {
/**
* @param {Version} version - Version of the CID
* @param {Format} code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
* @param {API.MultihashDigest<Alg>} multihash - (Multi)hash of the of the content.
* @param {Uint8Array} bytes
*
*/
constructor (version, code, multihash, bytes) {
/** @readonly */
this.code = code
/** @readonly */
this.version = version
/** @readonly */
this.multihash = multihash
/** @readonly */
this.bytes = bytes
// ArrayBufferView
/** @readonly */
this.byteOffset = bytes.byteOffset
/** @readonly */
this.byteLength = bytes.byteLength
// Circular reference
/** @readonly */
this.asCID = this
}
/**
* @returns {CID<Data, API.DAG_PB, API.SHA_256, 0>}
*/
toV0 () {
switch (this.version) {
case 0: {
return /** @type {CID<Data, API.DAG_PB, API.SHA_256, 0>} */ (this)
}
case 1: {
const { code, multihash } = this
if (code !== DAG_PB_CODE) {
throw new Error('Cannot convert a non dag-pb CID to CIDv0')
}
// sha2-256
if (multihash.code !== SHA_256_CODE) {
throw new Error('Cannot convert non sha2-256 multihash CID to CIDv0')
}
return /** @type {CID<Data, API.DAG_PB, API.SHA_256, 0>} */ (
CID.createV0(
/** @type {API.MultihashDigest<API.SHA_256>} */ (multihash)
)
)
}
default: {
throw Error(
`Can not convert CID version ${this.version} to version 0. This is a bug please report`
)
}
}
}
/**
* @returns {CID<Data, Format, Alg, 1>}
*/
toV1 () {
switch (this.version) {
case 0: {
const { code, digest } = this.multihash
const multihash = Digest.create(code, digest)
return /** @type {CID<Data, Format, Alg, 1>} */ (
CID.createV1(this.code, multihash)
)
}
case 1: {
return /** @type {CID<Data, Format, Alg, 1>} */ (this)
}
default: {
throw Error(
`Can not convert CID version ${this.version} to version 1. This is a bug please report`
)
}
}
}
/**
* @param {unknown} other
* @returns {other is CID<Data, Format, Alg, Version>}
*/
equals (other) {
return CID.equals(this, other)
}
/**
* @template {unknown} Data
* @template {number} Format
* @template {number} Alg
* @template {API.Version} Version
* @param {API.Link<Data, Format, Alg, Version>} self
* @param {unknown} other
* @returns {other is CID}
*/
static equals (self, other) {
const unknown =
/** @type {{code?:unknown, version?:unknown, multihash?:unknown}} */ (
other
)
return (
unknown &&
self.code === unknown.code &&
self.version === unknown.version &&
Digest.equals(self.multihash, unknown.multihash)
)
}
/**
* @param {API.MultibaseEncoder<string>} [base]
* @returns {string}
*/
toString (base) {
return format(this, base)
}
toJSON () {
return {
code: this.code,
version: this.version,
hash: this.multihash.bytes
}
}
link () {
return this
}
get [Symbol.toStringTag] () {
return 'CID'
}
// Legacy
[Symbol.for('nodejs.util.inspect.custom')] () {
return `CID(${this.toString()})`
}
/**
* Takes any input `value` and returns a `CID` instance if it was
* a `CID` otherwise returns `null`. If `value` is instanceof `CID`
* it will return value back. If `value` is not instance of this CID
* class, but is compatible CID it will return new instance of this
* `CID` class. Otherwise returs null.
*
* This allows two different incompatible versions of CID library to
* co-exist and interop as long as binary interface is compatible.
*
* @template {unknown} Data
* @template {number} Format
* @template {number} Alg
* @template {API.Version} Version
* @template {unknown} U
* @param {API.Link<Data, Format, Alg, Version>|U} input
* @returns {CID<Data, Format, Alg, Version>|null}
*/
static asCID (input) {
const value = /** @type {any} */ (input)
if (value instanceof CID) {
// If value is instance of CID then we're all set.
return value
} else if (value != null && value.asCID === value) {
// If value isn't instance of this CID class but `this.asCID === this` is
// true it is CID instance coming from a different implementation (diff
// version or duplicate). In that case we rebase it to this `CID`
// implementation so caller is guaranteed to get instance with expected
// API.
const { version, code, multihash, bytes } = value
return new CID(
version,
code,
/** @type {API.MultihashDigest<Alg>} */ (multihash),
bytes || encodeCID(version, code, multihash.bytes)
)
} else if (value != null && value[cidSymbol] === true) {
// If value is a CID from older implementation that used to be tagged via
// symbol we still rebase it to the this `CID` implementation by
// delegating that to a constructor.
const { version, multihash, code } = value
const digest =
/** @type {API.MultihashDigest<Alg>} */
(Digest.decode(multihash))
return CID.create(version, code, digest)
} else {
// Otherwise value is not a CID (or an incompatible version of it) in
// which case we return `null`.
return null
}
}
/**
*
* @template {unknown} Data
* @template {number} Format
* @template {number} Alg
* @template {API.Version} Version
* @param {Version} version - Version of the CID
* @param {Format} code - Code of the codec content is encoded in, see https://github.com/multiformats/multicodec/blob/master/table.csv
* @param {API.MultihashDigest<Alg>} digest - (Multi)hash of the of the content.
* @returns {CID<Data, Format, Alg, Version>}
*/
static create (version, code, digest) {
if (typeof code !== 'number') {
throw new Error('String codecs are no longer supported')
}
switch (version) {
case 0: {
if (code !== DAG_PB_CODE) {
throw new Error(
`Version 0 CID must use dag-pb (code: ${DAG_PB_CODE}) block encoding`
)
} else {
return new CID(version, code, digest, digest.bytes)
}
}
case 1: {
const bytes = encodeCID(version, code, digest.bytes)
return new CID(version, code, digest, bytes)
}
default: {
throw new Error('Invalid version')
}
}
}
/**
* Simplified version of `create` for CIDv0.
*
* @template {unknown} [T=unknown]
* @param {API.MultihashDigest<typeof SHA_256_CODE>} digest - Multihash.
* @returns {CID<T, typeof DAG_PB_CODE, typeof SHA_256_CODE, 0>}
*/
static createV0 (digest) {
return CID.create(0, DAG_PB_CODE, digest)
}
/**
* Simplified version of `create` for CIDv1.
*
* @template {unknown} Data
* @template {number} Code
* @template {number} Alg
* @param {Code} code - Content encoding format code.
* @param {API.MultihashDigest<Alg>} digest - Miltihash of the content.
* @returns {CID<Data, Code, Alg, 1>}
*/
static createV1 (code, digest) {
return CID.create(1, code, digest)
}
/**
* Decoded a CID from its binary representation. The byte array must contain
* only the CID with no additional bytes.
*
* An error will be thrown if the bytes provided do not contain a valid
* binary representation of a CID.
*
* @template {unknown} Data
* @template {number} Code
* @template {number} Alg
* @template {API.Version} Ver
* @param {API.ByteView<API.Link<Data, Code, Alg, Ver>>} bytes
* @returns {CID<Data, Code, Alg, Ver>}
*/
static decode (bytes) {
const [cid, remainder] = CID.decodeFirst(bytes)
if (remainder.length) {
throw new Error('Incorrect length')
}
return cid
}
/**
* Decoded a CID from its binary representation at the beginning of a byte
* array.
*
* Returns an array with the first element containing the CID and the second
* element containing the remainder of the original byte array. The remainder
* will be a zero-length byte array if the provided bytes only contained a
* binary CID representation.
*
* @template {unknown} T
* @template {number} C
* @template {number} A
* @template {API.Version} V
* @param {API.ByteView<API.Link<T, C, A, V>>} bytes
* @returns {[CID<T, C, A, V>, Uint8Array]}
*/
static decodeFirst (bytes) {
const specs = CID.inspectBytes(bytes)
const prefixSize = specs.size - specs.multihashSize
const multihashBytes = coerce(
bytes.subarray(prefixSize, prefixSize + specs.multihashSize)
)
if (multihashBytes.byteLength !== specs.multihashSize) {
throw new Error('Incorrect length')
}
const digestBytes = multihashBytes.subarray(
specs.multihashSize - specs.digestSize
)
const digest = new Digest.Digest(
specs.multihashCode,
specs.digestSize,
digestBytes,
multihashBytes
)
const cid =
specs.version === 0
? CID.createV0(/** @type {API.MultihashDigest<API.SHA_256>} */ (digest))
: CID.createV1(specs.codec, digest)
return [/** @type {CID<T, C, A, V>} */(cid), bytes.subarray(specs.size)]
}
/**
* Inspect the initial bytes of a CID to determine its properties.
*
* Involves decoding up to 4 varints. Typically this will require only 4 to 6
* bytes but for larger multicodec code values and larger multihash digest
* lengths these varints can be quite large. It is recommended that at least
* 10 bytes be made available in the `initialBytes` argument for a complete
* inspection.
*
* @template {unknown} T
* @template {number} C
* @template {number} A
* @template {API.Version} V
* @param {API.ByteView<API.Link<T, C, A, V>>} initialBytes
* @returns {{ version:V, codec:C, multihashCode:A, digestSize:number, multihashSize:number, size:number }}
*/
static inspectBytes (initialBytes) {
let offset = 0
const next = () => {
const [i, length] = varint.decode(initialBytes.subarray(offset))
offset += length
return i
}
let version = /** @type {V} */ (next())
let codec = /** @type {C} */ (DAG_PB_CODE)
if (/** @type {number} */(version) === 18) {
// CIDv0
version = /** @type {V} */ (0)
offset = 0
} else {
codec = /** @type {C} */ (next())
}
if (version !== 0 && version !== 1) {
throw new RangeError(`Invalid CID version ${version}`)
}
const prefixSize = offset
const multihashCode = /** @type {A} */ (next()) // multihash code
const digestSize = next() // multihash length
const size = offset + digestSize
const multihashSize = size - prefixSize
return { version, codec, multihashCode, digestSize, multihashSize, size }
}
/**
* Takes cid in a string representation and creates an instance. If `base`
* decoder is not provided will use a default from the configuration. It will
* throw an error if encoding of the CID is not compatible with supplied (or
* a default decoder).
*
* @template {string} Prefix
* @template {unknown} Data
* @template {number} Code
* @template {number} Alg
* @template {API.Version} Ver
* @param {API.ToString<API.Link<Data, Code, Alg, Ver>, Prefix>} source
* @param {API.MultibaseDecoder<Prefix>} [base]
* @returns {CID<Data, Code, Alg, Ver>}
*/
static parse (source, base) {
const [prefix, bytes] = parseCIDtoBytes(source, base)
const cid = CID.decode(bytes)
// Cache string representation to avoid computing it on `this.toString()`
baseCache(cid).set(prefix, source)
return cid
}
}
/**
* @template {string} Prefix
* @template {unknown} Data
* @template {number} Code
* @template {number} Alg
* @template {API.Version} Ver
* @param {API.ToString<API.Link<Data, Code, Alg, Ver>, Prefix>} source
* @param {API.MultibaseDecoder<Prefix>} [base]
* @returns {[Prefix, API.ByteView<API.Link<Data, Code, Alg, Ver>>]}
*/
const parseCIDtoBytes = (source, base) => {
switch (source[0]) {
// CIDv0 is parsed differently
case 'Q': {
const decoder = base || base58btc
return [
/** @type {Prefix} */ (base58btc.prefix),
decoder.decode(`${base58btc.prefix}${source}`)
]
}
case base58btc.prefix: {
const decoder = base || base58btc
return [/** @type {Prefix} */(base58btc.prefix), decoder.decode(source)]
}
case base32.prefix: {
const decoder = base || base32
return [/** @type {Prefix} */(base32.prefix), decoder.decode(source)]
}
default: {
if (base == null) {
throw Error(
'To parse non base32 or base58btc encoded CID multibase decoder must be provided'
)
}
return [/** @type {Prefix} */(source[0]), base.decode(source)]
}
}
}
/**
*
* @param {Uint8Array} bytes
* @param {Map<string, string>} cache
* @param {API.MultibaseEncoder<'z'>} base
*/
const toStringV0 = (bytes, cache, base) => {
const { prefix } = base
if (prefix !== base58btc.prefix) {
throw Error(`Cannot string encode V0 in ${base.name} encoding`)
}
const cid = cache.get(prefix)
if (cid == null) {
const cid = base.encode(bytes).slice(1)
cache.set(prefix, cid)
return cid
} else {
return cid
}
}
/**
* @template {string} Prefix
* @param {Uint8Array} bytes
* @param {Map<string, string>} cache
* @param {API.MultibaseEncoder<Prefix>} base
*/
const toStringV1 = (bytes, cache, base) => {
const { prefix } = base
const cid = cache.get(prefix)
if (cid == null) {
const cid = base.encode(bytes)
cache.set(prefix, cid)
return cid
} else {
return cid
}
}
const DAG_PB_CODE = 0x70
const SHA_256_CODE = 0x12
/**
* @param {API.Version} version
* @param {number} code
* @param {Uint8Array} multihash
* @returns {Uint8Array}
*/
const encodeCID = (version, code, multihash) => {
const codeOffset = varint.encodingLength(version)
const hashOffset = codeOffset + varint.encodingLength(code)
const bytes = new Uint8Array(hashOffset + multihash.byteLength)
varint.encodeTo(version, bytes, 0)
varint.encodeTo(code, bytes, codeOffset)
bytes.set(multihash, hashOffset)
return bytes
}
const cidSymbol = Symbol.for('@ipld/js-cid/CID')