Skip to content

Commit 3b6bc25

Browse files
authored
Merge pull request #30 from iden3/fix/url-with-creds
support urls with credentials (e.g. ipfs url configs)
2 parents 74805ec + 9c20a5b commit 3b6bc25

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-12
lines changed

.github/workflows/ci.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ jobs:
4949
run: npm run build
5050

5151
- name: Run Tests
52+
env:
53+
IPFS_URL: ${{ secrets.IPFS_URL }}
5254
run: npm run test

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@iden3/js-jsonld-merklization",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "json ld merklization library",
55
"main": "dist/cjs/index.js",
66
"module": "dist/esm/index.js",

src/loaders/jsonld-loader.ts

+15
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,15 @@ async function _fetch({ url, method }: { url: string | URL; method?: string }) {
233233
options['method'] = method;
234234
}
235235
try {
236+
url = new URL(url);
237+
if (url.username && url.password) {
238+
options['headers'] = {
239+
...(options['headers'] ?? {}),
240+
authorization: `Basic ${btoa(url.username + ':' + url.password)}`
241+
};
242+
url = removeCredentialsFromURL(url);
243+
}
244+
236245
const res = await fetch(url, options);
237246
if (res.status >= 300 && res.status < 400) {
238247
return { res, body: null };
@@ -256,6 +265,12 @@ async function _fetch({ url, method }: { url: string | URL; method?: string }) {
256265
}
257266
}
258267

268+
function removeCredentialsFromURL(url: string | URL): string {
269+
const urlObj = new URL(url);
270+
urlObj.username = '';
271+
urlObj.password = '';
272+
return urlObj.href;
273+
}
259274
export type DocumentLoader = (url: Url) => Promise<RemoteDocument>;
260275

261276
const ipfsURLPrefix = 'ipfs://';

tests/merklization.test.ts

+21-9
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,10 @@ describe('tests merkelization', () => {
461461
expect(birthDate.getTime()).toEqual(valueD.epochMilliseconds);
462462

463463
const valueMTEntry = await MtValue.mkValueMtEntry(DEFAULT_HASHER, valueD);
464-
const ok = await verifyProof(await mz.mt!.root(), proof, pathMTEntry, valueMTEntry);
464+
if (!mz.mt) {
465+
throw new Error("can't hash mt entry ");
466+
}
467+
const ok = await verifyProof(await mz.mt.root(), proof, pathMTEntry, valueMTEntry);
465468
expect(ok).toBeTruthy();
466469

467470
expect((await mz.root()).hex()).toEqual(
@@ -1025,9 +1028,8 @@ describe('merklize document with ipfs context', () => {
10251028
// node --experimental-vm-modules node_modules/jest/bin/jest.js -t 'set kubo client' tests/merklization.test.ts
10261029

10271030
const ipfsNodeURL = process.env.IPFS_URL ?? null;
1028-
if (ipfsNodeURL === null) {
1029-
console.warn('IPFS_URL is not set, skipping IPFS Node test');
1030-
return;
1031+
if (!ipfsNodeURL) {
1032+
throw new Error('IPFS_URL is not set, skipping IPFS Node test');
10311033
}
10321034

10331035
beforeAll(async () => {
@@ -1036,7 +1038,7 @@ describe('merklize document with ipfs context', () => {
10361038

10371039
it('ipfsNodeURL is set', async () => {
10381040
const mz: Merklizer = await Merklizer.merklizeJSONLD(ipfsDocument, {
1039-
ipfsNodeURL: ipfsNodeURL
1041+
ipfsNodeURL
10401042
});
10411043
expect((await mz.root()).bigInt().toString()).toEqual(
10421044
'19309047812100087948241250053335720576191969395309912987389452441269932261840'
@@ -1045,7 +1047,7 @@ describe('merklize document with ipfs context', () => {
10451047

10461048
it('ipfsGatewayURL is set', async () => {
10471049
const mz: Merklizer = await Merklizer.merklizeJSONLD(ipfsDocument, {
1048-
ipfsGatewayURL: 'http://ipfs.io'
1050+
ipfsNodeURL
10491051
});
10501052
expect((await mz.root()).bigInt().toString()).toEqual(
10511053
'19309047812100087948241250053335720576191969395309912987389452441269932261840'
@@ -1075,10 +1077,20 @@ async function pushSchemasToIPFS(ipfsNodeURL: string): Promise<void> {
10751077
'dir1/dir2/bbs-v2.jsonld'
10761078
);
10771079

1078-
const addURL = normalizeIPFSNodeURL(ipfsNodeURL, 'add');
1079-
const res = await fetch(addURL, {
1080+
let url: string | URL = normalizeIPFSNodeURL(ipfsNodeURL, 'add');
1081+
url = new URL(url);
1082+
let headers = {};
1083+
if (url.username && url.password) {
1084+
headers = {
1085+
authorization: `Basic ${btoa(url.username + ':' + url.password)}`
1086+
};
1087+
url.username = '';
1088+
url.password = '';
1089+
}
1090+
const res = await fetch(url, {
10801091
method: 'POST',
1081-
body: formData
1092+
body: formData,
1093+
headers
10821094
});
10831095

10841096
const resBody = await res.text();

0 commit comments

Comments
 (0)