-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1970 from privacy-scaling-explorations/feature/su…
…bmit-ipfs-hashes feat(contracts): add ipfs service and prepare parsing ipfs data
- Loading branch information
Showing
8 changed files
with
323 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import chai from "chai"; | ||
import chaiAsPromised from "chai-as-promised"; | ||
|
||
import { IpfsService } from "../ts/ipfs"; | ||
|
||
chai.use(chaiAsPromised); | ||
|
||
const { expect } = chai; | ||
|
||
describe("IpfsService", () => { | ||
let ipfsService: IpfsService; | ||
|
||
beforeEach(() => { | ||
ipfsService = IpfsService.getInstance(); | ||
}); | ||
|
||
it("should read data properly", async () => { | ||
const data = await ipfsService.read("bafybeibro7fxpk7sk2nfvslumxraol437ug35qz4xx2p7ygjctunb2wi3i"); | ||
|
||
expect(data).to.deep.equal({ Title: "sukuna", Description: "gambare gambare 🔥" }); | ||
}); | ||
|
||
it("should throw error if can't read data", async () => { | ||
await expect(ipfsService.read("invalid")).to.eventually.be.rejectedWith("invalid json for cid invalid"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* IPFS Service - A service for interacting with IPFS. | ||
* This service allows reading data from IPFS using a Content Identifier (CID). | ||
* It is designed as a singleton to ensure a single instance is used throughout the application. | ||
*/ | ||
export class IpfsService { | ||
/** | ||
* Singleton instance of the IpfsService. | ||
*/ | ||
private static INSTANCE?: IpfsService; | ||
|
||
/** | ||
* URL of the IPFS gateway to fetch data from. | ||
* Defaults to 'https://ipfs.io/ipfs' if not provided in the environment variables. | ||
*/ | ||
private ipfsGatewayUrl: string; | ||
|
||
/** | ||
* Retrieves the singleton instance of the IpfsService. | ||
* If the instance does not exist, a new one is created and returned. | ||
* | ||
* @returns {IpfsService} The singleton instance of the IpfsService. | ||
*/ | ||
static getInstance(): IpfsService { | ||
if (!IpfsService.INSTANCE) { | ||
IpfsService.INSTANCE = new IpfsService(); | ||
} | ||
|
||
return IpfsService.INSTANCE; | ||
} | ||
|
||
/** | ||
* Private constructor to initialize the service. | ||
* Should not be called directly. | ||
* Use `getInstance()` to access the service. | ||
*/ | ||
private constructor() { | ||
// Initialize the IPFS gateway URL, using an environment variable or a default value. | ||
this.ipfsGatewayUrl = process.env.IPFS_GATEWAY_URL || "https://ipfs.io/ipfs"; | ||
} | ||
|
||
/** | ||
* Fetches data from IPFS using the provided Content Identifier (CID). | ||
* The data is expected to be returned in JSON format, and is parsed accordingly. | ||
* | ||
* @param cid - The Content Identifier (CID) of the IPFS object to retrieve. | ||
* @returns {Promise<T>} A promise that resolves with the fetched data, parsed as the specified type `T`. | ||
* @throws {Error} If the request fails or if the data cannot be parsed as JSON. | ||
* | ||
* @template T - The type of the data expected from the IPFS response. | ||
*/ | ||
async read<T>(cid: string): Promise<T> { | ||
return fetch(`${this.ipfsGatewayUrl}/${cid}`) | ||
.then((res) => | ||
res.json().catch(() => { | ||
throw new Error(`invalid json for cid ${cid}`); | ||
}), | ||
) | ||
.then((res) => res as T); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.