3
3
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
4
4
import * as dagPB from '@ipld/dag-pb'
5
5
import * as dagCBOR from '@ipld/dag-cbor'
6
+ import * as dagJOSE from 'dag-jose'
6
7
import { importer } from 'ipfs-unixfs-importer'
7
8
import { UnixFS } from 'ipfs-unixfs'
8
9
import all from 'it-all'
9
10
import { CID } from 'multiformats/cid'
10
11
import { sha256 } from 'multiformats/hashes/sha2'
11
12
import { base32 } from 'multiformats/bases/base32'
13
+ import { base64url } from 'multiformats/bases/base64'
12
14
import { expect } from 'aegir/utils/chai.js'
13
15
import { getDescribe , getIt } from '../utils/mocha.js'
14
16
import testTimeout from '../utils/test-timeout.js'
15
17
import { identity } from 'multiformats/hashes/identity'
16
18
import blockstore from '../utils/blockstore-adapter.js'
19
+ import { ES256KSigner , createJWS } from 'did-jwt'
17
20
18
21
/**
19
22
* @typedef {import('ipfsd-ctl').Factory } Factory
@@ -42,6 +45,10 @@ export function testGet (factory, options) {
42
45
* @type {any }
43
46
*/
44
47
let cborNode
48
+ /**
49
+ * @type {dagJOSE.DagJWE }
50
+ */
51
+ let joseNode
45
52
/**
46
53
* @type {dagPB.PBNode }
47
54
*/
@@ -50,6 +57,10 @@ export function testGet (factory, options) {
50
57
* @type {any }
51
58
*/
52
59
let nodeCbor
60
+ /**
61
+ * @type {string }
62
+ */
63
+ let nodeJose
53
64
/**
54
65
* @type {CID }
55
66
*/
@@ -58,6 +69,10 @@ export function testGet (factory, options) {
58
69
* @type {CID }
59
70
*/
60
71
let cidCbor
72
+ /**
73
+ * @type {CID }
74
+ */
75
+ let cidJose
61
76
62
77
before ( async ( ) => {
63
78
const someData = uint8ArrayFromString ( 'some other data' )
@@ -68,6 +83,12 @@ export function testGet (factory, options) {
68
83
cborNode = {
69
84
data : someData
70
85
}
86
+ joseNode = {
87
+ protected : 'eyJhbGciOiJkaXIiLCJlbmMiOiJYQzIwUCJ9' ,
88
+ iv : 'DhVb9URR_o_85MOl-hCellwPTtQ_dj6d' ,
89
+ ciphertext : 'EtUsNJcKzEKdFM9DW5Ua5tVyaQRCKsAD' ,
90
+ tag : '-vG17pRSVB2Vycf2MZRgBA'
91
+ }
71
92
72
93
nodePb = {
73
94
Data : uint8ArrayFromString ( 'I am inside a Protobuf' ) ,
@@ -83,6 +104,11 @@ export function testGet (factory, options) {
83
104
84
105
await ipfs . dag . put ( nodePb , { storeCodec : 'dag-pb' , hashAlg : 'sha2-256' } )
85
106
await ipfs . dag . put ( nodeCbor , { storeCodec : 'dag-cbor' , hashAlg : 'sha2-256' } )
107
+
108
+ const signer = ES256KSigner ( '278a5de700e29faae8e40e366ec5012b5ec63d36ec77e8a2417154cc1d25383f' )
109
+ nodeJose = await createJWS ( base64url . encode ( cidCbor . bytes ) . slice ( 1 ) , signer )
110
+ cidJose = CID . createV1 ( dagJOSE . code , await sha256 . digest ( dagJOSE . encode ( nodeJose ) ) )
111
+ await ipfs . dag . put ( nodeJose , { storeCodec : dagJOSE . name , hashAlg : 'sha2-256' } )
86
112
} )
87
113
88
114
it ( 'should respect timeout option when getting a DAG node' , ( ) => {
@@ -158,6 +184,14 @@ export function testGet (factory, options) {
158
184
it . skip ( 'should get dag-cbor node value two levels deep' , ( done ) => { } )
159
185
it . skip ( 'should get dag-cbor value via dag-pb node' , ( done ) => { } )
160
186
187
+ it ( 'should get only a CID, due to resolving locally only' , async function ( ) {
188
+ const result = await ipfs . dag . get ( cidCbor , {
189
+ path : 'pb/Data' ,
190
+ localResolve : true
191
+ } )
192
+ expect ( result . value . equals ( cidPb ) ) . to . be . true ( )
193
+ } )
194
+
161
195
it ( 'should get dag-pb value via dag-cbor node' , async function ( ) {
162
196
const result = await ipfs . dag . get ( cidCbor , {
163
197
path : 'pb/Data'
@@ -305,5 +339,51 @@ export function testGet (factory, options) {
305
339
306
340
expect ( atPath ) . to . have . deep . property ( 'value' ) . that . is . an . instanceOf ( CID )
307
341
} )
342
+
343
+ it ( 'should get a dag-jose node' , async ( ) => {
344
+ const cid = await ipfs . dag . put ( joseNode , {
345
+ storeCodec : 'dag-jose' ,
346
+ hashAlg : 'sha2-256'
347
+ } )
348
+
349
+ const result = await ipfs . dag . get ( cid )
350
+
351
+ const node = result . value
352
+ expect ( joseNode ) . to . eql ( node )
353
+ } )
354
+
355
+ it ( 'should get a dag-jose node with path' , async ( ) => {
356
+ const result = await ipfs . dag . get ( cidJose , {
357
+ path : '/'
358
+ } )
359
+
360
+ const node = result . value
361
+
362
+ const cid = CID . createV1 ( dagJOSE . code , await sha256 . digest ( dagJOSE . encode ( node ) ) )
363
+ expect ( cid . equals ( cidJose ) ) . to . be . true ( )
364
+ } )
365
+
366
+ it ( 'should get a dag-jose node local value' , async ( ) => {
367
+ const result = await ipfs . dag . get ( cidJose , {
368
+ path : 'payload'
369
+ } )
370
+ const converted = dagJOSE . toGeneral ( nodeJose )
371
+ expect ( result . value ) . to . eql ( 'payload' in converted && converted . payload )
372
+ } )
373
+
374
+ it ( 'should get dag-cbor value via dag-jose node' , async function ( ) {
375
+ const result = await ipfs . dag . get ( cidJose , {
376
+ path : 'link/someData'
377
+ } )
378
+ expect ( result . value ) . to . eql ( 'I am inside a Cbor object' )
379
+ } )
380
+
381
+ it ( 'should get dag-cbor cid via dag-jose node if local resolve' , async function ( ) {
382
+ const result = await ipfs . dag . get ( cidJose , {
383
+ path : 'link' ,
384
+ localResolve : true
385
+ } )
386
+ expect ( result . value ) . to . eql ( cidCbor )
387
+ } )
308
388
} )
309
389
}
0 commit comments