@@ -1144,8 +1144,14 @@ describe('merklize document with ipfs context', () => {
1144
1144
// node --experimental-vm-modules node_modules/jest/bin/jest.js -t 'set kubo client' tests/merklization.test.ts
1145
1145
1146
1146
const ipfsNodeURL = process . env . IPFS_URL ?? null ;
1147
+ const ipfsGatewayURL = process . env . IPFS_GATEWAY_URL ?? null ;
1148
+
1147
1149
if ( ! ipfsNodeURL ) {
1148
- throw new Error ( 'IPFS_URL is not set, skipping IPFS Node test' ) ;
1150
+ throw new Error ( 'IPFS_URL is not set' ) ;
1151
+ }
1152
+
1153
+ if ( ! ipfsGatewayURL ) {
1154
+ throw new Error ( 'IPFS_GATEWAY_URL is not set' ) ;
1149
1155
}
1150
1156
1151
1157
beforeAll ( async ( ) => {
@@ -1168,7 +1174,7 @@ describe('merklize document with ipfs context', () => {
1168
1174
it ( 'ipfsGatewayURL is set' , async ( ) => {
1169
1175
const opts = {
1170
1176
documentLoader : cacheLoader ( {
1171
- ipfsNodeURL
1177
+ ipfsGatewayURL
1172
1178
} )
1173
1179
} ;
1174
1180
const mz : Merklizer = await Merklizer . merklizeJSONLD ( ipfsDocument , opts ) ;
@@ -1186,7 +1192,7 @@ describe('merklize document with ipfs context', () => {
1186
1192
it ( 'TestExistenceProofIPFS' , async ( ) => {
1187
1193
const opts = {
1188
1194
documentLoader : cacheLoader ( {
1189
- ipfsGatewayURL : 'https://ipfs.io'
1195
+ ipfsGatewayURL
1190
1196
} )
1191
1197
} ;
1192
1198
const mz = await Merklizer . merklizeJSONLD ( testDocumentIPFS , opts ) ;
@@ -1206,42 +1212,68 @@ describe('merklize document with ipfs context', () => {
1206
1212
} ) ;
1207
1213
1208
1214
async function pushSchemasToIPFS ( ipfsNodeURL : string ) : Promise < void > {
1209
- const citizenshipData = await readFile ( 'tests/testdata/citizenship-v1.jsonld' ) ;
1210
- const bbsData = await readFile ( 'tests/testdata/dir1/dir2/bbs-v2.jsonld' ) ;
1211
- const formData = new FormData ( ) ;
1212
- formData . append (
1213
- 'file' ,
1214
- new Blob ( [ citizenshipData ] , { type : 'application/octet-stream' } ) ,
1215
- 'citizenship-v1.jsonld'
1216
- ) ;
1217
- formData . append (
1218
- 'file' ,
1219
- new Blob ( [ bbsData ] , { type : 'application/octet-stream' } ) ,
1220
- 'dir1/dir2/bbs-v2.jsonld'
1221
- ) ;
1222
- let url : string | URL = normalizeIPFSNodeURL ( ipfsNodeURL , 'add' ) ;
1223
- url = new URL ( url ) ;
1224
- let headers = { } ;
1225
- if ( url . username && url . password ) {
1226
- headers = {
1227
- authorization : `Basic ${ btoa ( url . username + ':' + url . password ) } `
1228
- } ;
1215
+ const getUrl = ( uri : string , method : string ) : { url : string ; headers : unknown } => {
1216
+ const url : string | URL = new URL ( normalizeIPFSNodeURL ( uri , method ) ) ;
1217
+
1218
+ const headers =
1219
+ url . username && url . password
1220
+ ? {
1221
+ authorization : `Basic ${ btoa ( url . username + ':' + url . password ) } `
1222
+ }
1223
+ : { } ;
1229
1224
url . username = '' ;
1230
1225
url . password = '' ;
1226
+
1227
+ return { url : url . toString ( ) , headers } ;
1228
+ } ;
1229
+
1230
+ const { url : catUrl , headers } = getUrl ( ipfsNodeURL , 'cat' ) ;
1231
+ const catOpts = { headers, method : 'POST' } ;
1232
+
1233
+ try {
1234
+ const cat = await Promise . all ( [
1235
+ fetch ( `${ catUrl } ?arg=QmdP4MZkESEabRVB322r2xWm7TCi7LueMNWMJawYmSy7hp` , {
1236
+ ...catOpts
1237
+ } as RequestInit ) . then ( ( r ) => r . text ( ) ) ,
1238
+ fetch ( `${ catUrl } ?arg=Qmbp4kwoHULnmK71abrxdksjPH5sAjxSAXU5PEp2XRMFNw/dir2/bbs-v2.jsonld` , {
1239
+ ...catOpts
1240
+ } as RequestInit ) . then ( ( r ) => r . text ( ) )
1241
+ ] ) ;
1242
+ const records = cat . map ( ( r ) => JSON . parse ( r ) [ '@context' ] ) . filter ( Boolean ) ;
1243
+ if ( records . length !== 2 ) {
1244
+ throw new Error ( 'IPFS records not found' ) ;
1245
+ }
1246
+ } catch ( e ) {
1247
+ console . warn ( 'try to upload document' , e ) ;
1248
+ const citizenshipData = await readFile ( 'tests/testdata/citizenship-v1.jsonld' ) ;
1249
+ const bbsData = await readFile ( 'tests/testdata/dir1/dir2/bbs-v2.jsonld' ) ;
1250
+ const formData = new FormData ( ) ;
1251
+ formData . append (
1252
+ 'file' ,
1253
+ new Blob ( [ citizenshipData ] , { type : 'application/octet-stream' } ) ,
1254
+ 'citizenship-v1.jsonld'
1255
+ ) ;
1256
+ formData . append (
1257
+ 'file' ,
1258
+ new Blob ( [ bbsData ] , { type : 'application/octet-stream' } ) ,
1259
+ 'dir1/dir2/bbs-v2.jsonld'
1260
+ ) ;
1261
+ const { url : addUrl , headers : addHeaders } = getUrl ( ipfsNodeURL , 'add' ) ;
1262
+
1263
+ const res = await fetch ( addUrl , {
1264
+ method : 'POST' ,
1265
+ body : formData ,
1266
+ headers : addHeaders as HeadersInit
1267
+ } ) ;
1268
+ const resBody = await res . text ( ) ;
1269
+ const records = resBody
1270
+ . split ( '\n' )
1271
+ . filter ( ( l ) => l . trim ( ) . length > 0 )
1272
+ . map ( ( l ) => JSON . parse ( l ) . Hash ) ;
1273
+ // Check that URLs from ipfsDocument are uploaded to IPFS
1274
+ expect ( records ) . toContain ( 'QmdP4MZkESEabRVB322r2xWm7TCi7LueMNWMJawYmSy7hp' ) ;
1275
+ expect ( records ) . toContain ( 'Qmbp4kwoHULnmK71abrxdksjPH5sAjxSAXU5PEp2XRMFNw' ) ;
1231
1276
}
1232
- const res = await fetch ( url , {
1233
- method : 'POST' ,
1234
- body : formData ,
1235
- headers
1236
- } ) ;
1237
- const resBody = await res . text ( ) ;
1238
- const records = resBody
1239
- . split ( '\n' )
1240
- . filter ( ( l ) => l . trim ( ) . length > 0 )
1241
- . map ( ( l ) => JSON . parse ( l ) . Hash ) ;
1242
- // Check that URLs from ipfsDocument are uploaded to IPFS
1243
- expect ( records ) . toContain ( 'QmdP4MZkESEabRVB322r2xWm7TCi7LueMNWMJawYmSy7hp' ) ;
1244
- expect ( records ) . toContain ( 'Qmbp4kwoHULnmK71abrxdksjPH5sAjxSAXU5PEp2XRMFNw' ) ;
1245
1277
}
1246
1278
1247
1279
function strHash ( str : string ) : string {
0 commit comments