-
Notifications
You must be signed in to change notification settings - Fork 0
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 #1 from axiom-crypto/feat/ipfs_client
IPFS client
- Loading branch information
Showing
12 changed files
with
799 additions
and
5 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './codec'; | ||
export * from './constants'; | ||
export * from './ipfs'; | ||
export * from './rpc'; | ||
export * from './utils'; |
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,3 @@ | ||
export * from './ipfsClient'; | ||
export * from './pinataIpfsClient'; | ||
export * from './quicknodeIpfsClient'; |
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,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; | ||
} | ||
} |
Oops, something went wrong.