@@ -52,6 +52,40 @@ describe('CID', () => {
5252 } )
5353 } )
5454
55+ it ( 'handles Uint8Array multihash' , ( done ) => {
56+ multihashing ( Buffer . from ( 'hello world' ) , 'sha2-256' , ( err , mh ) => {
57+ expect ( err ) . to . not . exist ( )
58+ const mhStr = 'QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'
59+ const mhUint8 = Uint8Array . from ( mh )
60+
61+ const cid = new CID ( mhUint8 )
62+
63+ expect ( cid ) . to . have . property ( 'codec' , 'dag-pb' )
64+ expect ( cid ) . to . have . property ( 'version' , 0 )
65+ expect ( cid ) . to . have . property ( 'multihash' ) . that . eql ( mh )
66+
67+ expect ( cid . toBaseEncodedString ( ) ) . to . eql ( mhStr )
68+ done ( )
69+ } )
70+ } )
71+
72+ it ( 'handles Array multihash' , ( done ) => {
73+ multihashing ( Buffer . from ( 'hello world' ) , 'sha2-256' , ( err , mh ) => {
74+ expect ( err ) . to . not . exist ( )
75+ const mhStr = 'QmaozNR7DZHQK1ZcU9p7QdrshMvXqWK6gpu5rmrkPdT3L4'
76+ const mhArray = Uint8Array . from ( mh )
77+
78+ const cid = new CID ( mhArray )
79+
80+ expect ( cid ) . to . have . property ( 'codec' , 'dag-pb' )
81+ expect ( cid ) . to . have . property ( 'version' , 0 )
82+ expect ( cid ) . to . have . property ( 'multihash' ) . that . eql ( mh )
83+
84+ expect ( cid . toBaseEncodedString ( ) ) . to . eql ( mhStr )
85+ done ( )
86+ } )
87+ } )
88+
5589 it ( 'create by parts' , ( ) => {
5690 const cid = new CID ( 0 , 'dag-pb' , hash )
5791
@@ -125,6 +159,34 @@ describe('CID', () => {
125159 expect ( cid . toBaseEncodedString ( ) ) . to . be . eql ( cidStr )
126160 } )
127161
162+ it ( 'handles CID by Uint8Array' , ( ) => {
163+ const cidStr = 'zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS'
164+ const cidBuf = Buffer . from ( '017012207252523e6591fb8fe553d67ff55a86f84044b46a3e4176e10c58fa529a4aabd5' , 'hex' )
165+ const cidUint8 = Uint8Array . from ( cidBuf )
166+
167+ const cid = new CID ( cidUint8 )
168+
169+ expect ( cid ) . to . have . property ( 'codec' , 'dag-pb' )
170+ expect ( cid ) . to . have . property ( 'version' , 1 )
171+ expect ( cid ) . to . have . property ( 'multihash' )
172+
173+ expect ( cid . toBaseEncodedString ( ) ) . to . be . eql ( cidStr )
174+ } )
175+
176+ it ( 'handles CID by Array' , ( ) => {
177+ const cidStr = 'zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS'
178+ const cidBuf = Buffer . from ( '017012207252523e6591fb8fe553d67ff55a86f84044b46a3e4176e10c58fa529a4aabd5' , 'hex' )
179+ const cidArray = Array . from ( cidBuf )
180+
181+ const cid = new CID ( cidArray )
182+
183+ expect ( cid ) . to . have . property ( 'codec' , 'dag-pb' )
184+ expect ( cid ) . to . have . property ( 'version' , 1 )
185+ expect ( cid ) . to . have . property ( 'multihash' )
186+
187+ expect ( cid . toBaseEncodedString ( ) ) . to . be . eql ( cidStr )
188+ } )
189+
128190 it ( 'create by parts' , ( ) => {
129191 const cid = new CID ( 1 , 'dag-cbor' , hash )
130192
0 commit comments