Skip to content

Commit

Permalink
Merge pull request #1 from axiom-crypto/feat/ipfs_client
Browse files Browse the repository at this point in the history
IPFS client
  • Loading branch information
ytham authored Mar 3, 2024
2 parents 74f1417 + 6e5bd08 commit e32bf0e
Show file tree
Hide file tree
Showing 12 changed files with 799 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ jobs:
export PROVIDER_URI=${{ secrets.PROVIDER_URI }}
export PROVIDER_URI_GOERLI=${{ secrets.PROVIDER_URI_GOERLI }}
export PROVIDER_URI_SEPOLIA=${{ secrets.PROVIDER_URI_SEPOLIA }}
export PINATA_JWT=${{ secrets.PINATA_JWT }}
export QUICKNODE_API_KEY=${{ secrets.QUICKNODE_API_KEY }}
export QUICKNODE_IPFS_URL=${{ secrets.QUICKNODE_IPFS_URL }}
npm test
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
{
"name": "@axiom-crypto/tools",
"version": "2.0.2",
"version": "2.1.0-rc.0",
"description": "Useful data, field, and byte manipulation tools for Axiom.",
"author": "Intrinsic Technologies",
"license": "MIT",
"main": "index.js",
"types": "index.d.ts",
"keywords": ["axiom", "ethereum", "zero knowledge", "zk", "coprocessor", "crypto"],
"keywords": [
"axiom",
"ethereum",
"zero knowledge",
"zk",
"coprocessor",
"crypto"
],
"scripts": {
"build": "rm -rf ./dist/* && tsc && node scripts/postTsc.js",
"test": "jest",
Expand All @@ -17,7 +24,10 @@
"directory": "dist"
},
"dependencies": {
"ethers": "^6.7.0"
"axios": "^1.6.7",
"bs58": "^5.0.0",
"ethers": "^6.7.0",
"form-data": "^4.0.0"
},
"devDependencies": {
"@types/jest": "^29.5.3",
Expand Down
80 changes: 80 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './codec';
export * from './constants';
export * from './ipfs';
export * from './rpc';
export * from './utils';
3 changes: 3 additions & 0 deletions src/ipfs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './ipfsClient';
export * from './pinataIpfsClient';
export * from './quicknodeIpfsClient';
54 changes: 54 additions & 0 deletions src/ipfs/ipfsClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import bs58 from 'bs58';

export interface IpfsResult {
status: number;
value: string | number | boolean | null;
}

export abstract class IpfsClient {
protected name: string;

constructor(name: string) {
this.name = name;
}

// Get the character length of the file
// @param hashOrCid - IPFS hash in bytes32 or CIDv0
// @returns - File size in character length
abstract getSize(hashOrCid: string): Promise<IpfsResult>;

// Read a file from IPFS
// @param hashOrCid - IPFS hash in bytes32 or CIDv0
// @returns - File content as a string
abstract read(hashOrCid: string): Promise<IpfsResult>;

// Write a file and pin it to IPFS
// @param data - File content as a string
// @returns - IPFS hash in bytes32
abstract pin(data: string): Promise<IpfsResult>;

// Unpin a file from IPFS
// @param hashOrCid - IPFS hash in bytes32 or CIDv0
// @returns - Whether the unpinning was successful
abstract unpin(hashOrCid: string): Promise<IpfsResult>;

// Convert bytes32 to CIDv0
// @param bytes32Hex - IPFS hash in bytes32
// @returns - IPFS hash in CIDv0
convertIpfsCidToBytes32(ipfsCid: string) {
return '0x' + Buffer.from(bs58.decode(ipfsCid).slice(2)).toString('hex')
}

// Convert CIDv0 to bytes32
// @param bytes32Hex - IPFS hash in bytes32
// @returns - IPFS hash in CIDv0
convertBytes32ToIpfsCid(bytes32Hex: string) {
return bs58.encode(Buffer.from('1220' + bytes32Hex.slice(2), 'hex'))
}

// Get the name of the IPFS client
// @returns - IPFS client name
getName() {
return this.name;
}
}
Loading

0 comments on commit e32bf0e

Please sign in to comment.