File tree Expand file tree Collapse file tree 2 files changed +42
-6
lines changed Expand file tree Collapse file tree 2 files changed +42
-6
lines changed Original file line number Diff line number Diff line change @@ -78,17 +78,21 @@ export class CID {
78
78
/** @readonly */
79
79
this . bytes = bytes
80
80
81
- // ArrayBufferView
82
- /** @readonly */
83
- this . byteOffset = bytes . byteOffset
84
- /** @readonly */
85
- this . byteLength = bytes . byteLength
86
-
87
81
// Circular reference
88
82
/** @readonly */
89
83
this . asCID = this
90
84
}
91
85
86
+ // ArrayBufferView
87
+ get byteOffset ( ) {
88
+ return this . bytes . byteOffset
89
+ }
90
+
91
+ // ArrayBufferView
92
+ get byteLength ( ) {
93
+ return this . bytes . byteLength
94
+ }
95
+
92
96
/**
93
97
* @returns {CID<Data, API.DAG_PB, API.SHA_256, 0> }
94
98
*/
@@ -274,6 +278,10 @@ export class CID {
274
278
throw new Error ( 'String codecs are no longer supported' )
275
279
}
276
280
281
+ if ( digest . bytes == null ) {
282
+ throw new Error ( 'Invalid digest' )
283
+ }
284
+
277
285
switch ( version ) {
278
286
case 0 : {
279
287
if ( code !== DAG_PB_CODE ) {
Original file line number Diff line number Diff line change @@ -708,4 +708,32 @@ describe('CID', () => {
708
708
sender . close ( )
709
709
receiver . close ( )
710
710
} )
711
+
712
+ describe ( 'decode' , ( ) => {
713
+ const tests = {
714
+ v0 : 'QmTFHZL5CkgNz19MdPnSuyLAi6AVq9fFp81zmPpaL2amED' ,
715
+ v1 : 'bafybeif2pall7dybz7vecqka3zo24irdwabwdi4wc55jznaq75q7eaavvu'
716
+ }
717
+
718
+ Object . entries ( tests ) . forEach ( ( [ version , cidString ] ) => {
719
+ it ( `decode ${ version } from bytes` , ( ) => {
720
+ const cid1 = CID . parse ( cidString )
721
+ const cid2 = CID . decode ( cid1 . bytes )
722
+
723
+ assert . deepStrictEqual ( cid1 , cid2 )
724
+ } )
725
+
726
+ it ( `decode ${ version } from subarray` , ( ) => {
727
+ const cid1 = CID . parse ( cidString )
728
+ // a byte array with an extra byte at the start and end
729
+ const bytes = new Uint8Array ( cid1 . bytes . length + 2 )
730
+ bytes . set ( cid1 . bytes , 1 )
731
+ // slice the cid bytes out of the middle to have a subarray with a non-zero .byteOffset
732
+ const subarray = bytes . subarray ( 1 , cid1 . bytes . length + 1 )
733
+ const cid2 = CID . decode ( subarray )
734
+
735
+ assert . deepStrictEqual ( cid1 , cid2 )
736
+ } )
737
+ } )
738
+ } )
711
739
} )
You can’t perform that action at this time.
0 commit comments