Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit 529f4fb

Browse files
committed
fix(bugs): index.mjs and added IPFS gateway
1 parent 519e842 commit 529f4fb

File tree

8 files changed

+60
-30
lines changed

8 files changed

+60
-30
lines changed

contracts/RELEASE.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# 1.1.1
2+
3+
- Improve ESM support by exporting `index.mjs` instead of `index.js`. @baumstern
4+
5+
## New contributors
6+
7+
Welcoming @baumstern as a new contributor to the hypercerts codebase!
8+
19
# 1.1.0
210

311
- Added Sepolia marketplace deployment

contracts/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@hypercerts-org/contracts",
33
"description": "EVM compatible protocol for managing impact claims",
4-
"version": "1.1.0",
4+
"version": "1.1.1-alpha.0",
55
"author": {
66
"name": "Hypercerts Foundation",
77
"url": "https://github.com/hypercerts-org/hypercerts"
@@ -18,9 +18,9 @@
1818
"types": "./dist/index.d.ts",
1919
"exports": {
2020
".": {
21+
"types": "./dist/index.d.ts",
2122
"require": "./dist/cjs/index.js",
22-
"import": "./dist/esm/index.mjs",
23-
"types": "./dist/index.d.ts"
23+
"import": "./dist/esm/index.mjs"
2424
}
2525
},
2626
"files": [

pnpm-lock.yaml

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/RELEASE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Release notes
22

3+
## 1.4.2
4+
5+
- Improve ESM support by exporting `index.mjs` instead of `index.js`. @baumstern
6+
- Added dweb IPFS gateway links and use `Promise.any` call to try and fetch data from multiple gateways.
7+
8+
## New contributors
9+
10+
Welcoming @baumstern as a new contributor to the hypercerts codebase!
11+
312
## 1.4.0
413

514
- Added all deployments from `@hypercerts-org/contracts` to the SDK under deploments.

sdk/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hypercerts-org/sdk",
3-
"version": "1.4.1",
3+
"version": "1.4.2-alpha.0",
44
"description": "SDK for hypercerts protocol",
55
"repository": "[email protected]:hypercerts-org/hypercerts.git",
66
"author": "Hypercerts team",
@@ -11,9 +11,9 @@
1111
"module": "./dist/esm/index.mjs",
1212
"types": "./dist/index.d.ts",
1313
"exports": {
14+
"types": "./dist/index.d.ts",
1415
"require": "./dist/cjs/index.js",
15-
"import": "./dist/esm/index.mjs",
16-
"types": "./dist/index.d.ts"
16+
"import": "./dist/esm/index.mjs"
1717
},
1818
"files": [
1919
"dist",
@@ -24,7 +24,7 @@
2424
"@ethereum-attestation-service/eas-sdk": "1.3.7",
2525
"@ethersproject/abstract-signer": "^5.7.0",
2626
"@graphql-typed-document-node/core": "^3.2.0",
27-
"@hypercerts-org/contracts": "1.1.0",
27+
"@hypercerts-org/contracts": "1.1.1-alpha.0",
2828
"@openzeppelin/merkle-tree": "^1.0.5",
2929
"@urql/core": "^4.2.0",
3030
"@whatwg-node/fetch": "^0.9.13",

sdk/src/utils/fetchers.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { logger } from "./logger";
33
import axios from "axios";
44

55
/**
6-
* Fetches data from IPFS using either the NFT Storage gateway or the Web3Up gateway.
6+
* Fetches data from IPFS using either the DWeb IPFS, NFT Storage, or the Web3Up gateway.
77
*
8-
* This function attempts to fetch data from the NFT Storage gateway first. If the request times out, it then tries to fetch the data from the Web3Up gateway.
9-
* If the data cannot be fetched from either gateway, it throws a `StorageError`.
8+
* This function attempts to fetch data from all gateways at the same time and returns on the first on to resolve.
9+
* If the data cannot be fetched from any gateway, it throws a `StorageError`.
1010
*
1111
* @param {string} cidOrIpfsUri - The CID or IPFS URI of the data to fetch.
1212
* @param {number} [timeout=10000] - The timeout for the fetch request in milliseconds. Defaults to 10000ms.
@@ -15,15 +15,20 @@ import axios from "axios";
1515
* @async
1616
*/
1717
const getFromIPFS = async (cidOrIpfsUri: string, timeout: number = 10000): Promise<unknown> => {
18-
const nftStorageGatewayLink = getNftStorageGatewayUri(cidOrIpfsUri);
19-
const web3upGatewayLink = getWeb3UpGatewayUri(cidOrIpfsUri);
20-
logger.debug(`Getting metadata ${cidOrIpfsUri} at ${nftStorageGatewayLink}`);
18+
const requests = [
19+
axios.get(getDwebLinkGatewayUri(cidOrIpfsUri), { timeout }),
20+
axios.get(getNftStorageGatewayUri(cidOrIpfsUri), { timeout }),
21+
axios.get(getWeb3UpGatewayUri(cidOrIpfsUri), { timeout }),
22+
];
2123

22-
const res = await axios.get(nftStorageGatewayLink, { timeout }).catch(() => {
23-
logger.debug(`${nftStorageGatewayLink} timed out.`);
24-
logger.debug(`Getting metadata ${cidOrIpfsUri} at ${web3upGatewayLink}`);
25-
return axios.get(web3upGatewayLink, { timeout });
26-
});
24+
logger.debug(`Getting metadata for ${cidOrIpfsUri}`);
25+
26+
const res = await Promise.any(requests)
27+
.then()
28+
.catch((err) => {
29+
logger.error(err);
30+
throw new StorageError(`Failed to get ${cidOrIpfsUri}`, { error: err });
31+
});
2732

2833
if (!res || !res.data) {
2934
throw new StorageError(`Failed to get ${cidOrIpfsUri}`);
@@ -34,6 +39,11 @@ const getFromIPFS = async (cidOrIpfsUri: string, timeout: number = 10000): Promi
3439

3540
const getCid = (cidOrIpfsUri: string) => cidOrIpfsUri.replace("ipfs://", "");
3641

42+
const getDwebLinkGatewayUri = (cidOrIpfsUri: string) => {
43+
const DWEB_LINK_IPFS_GATEWAY = "https://{cid}.ipfs.dweb.link/";
44+
return DWEB_LINK_IPFS_GATEWAY.replace("{cid}", getCid(cidOrIpfsUri));
45+
};
46+
3747
const getNftStorageGatewayUri = (cidOrIpfsUri: string) => {
3848
const NFT_STORAGE_IPFS_GATEWAY = "https://nftstorage.link/ipfs/{cid}";
3949
return NFT_STORAGE_IPFS_GATEWAY.replace("{cid}", getCid(cidOrIpfsUri));

sdk/test/utils/allowlist.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ describe("Fetchers", () => {
4747

4848
expect(res?.proof).to.deep.equal(tree.getProof(0));
4949
expect(res?.root).to.deep.equal(tree.root);
50-
expect(stub.calledOnce).to.be.true;
50+
expect(stub.calledThrice).to.be.true;
5151
});
5252
});

sdk/test/utils/fetchers.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,22 @@ describe("Fetchers", () => {
2020

2121
const res = await getFromIPFS("test");
2222
expect(res).to.equal(validResponse.data);
23-
expect(axiosStub.calledOnce).to.be.true;
23+
expect(axiosStub.calledThrice).to.be.true;
2424
});
2525

26-
it("IPFS: should try another endpoint after the first fails", async () => {
26+
it("IPFS: should try multiple endpoint and resolves when a valid response is returned from any", async () => {
2727
const validResponse = { data: "TEST_PASSED" };
2828
const axiosStub = sinon
2929
.stub(axios, "get")
3030
.onFirstCall()
3131
.rejects()
3232
.onSecondCall()
33-
.resolves(Promise.resolve(validResponse));
33+
.resolves(Promise.resolve(validResponse))
34+
.onThirdCall()
35+
.rejects();
3436

3537
const res = await getFromIPFS("test");
3638
expect(res).to.equal(validResponse.data);
37-
expect(axiosStub.calledTwice).to.be.true;
39+
expect(axiosStub.calledThrice).to.be.true;
3840
});
3941
});

0 commit comments

Comments
 (0)