hosting, NFT metadata storing, socialFi, gamingFi and decentralized cloud storage, etc.
Overview
1. Storage process
Put file to IPFS (local IPFS or IPFS gateway), get CID and FileSize back
Send place storage order transaction to Crust with CID and FileSize
Query order status by CID
2. Dependencies
The code sample mainly depends on the following libraries:
Custom data type, used to adapt to Crust network
The polkadot api library provides a Promise-style interface for performing related operations on the Crust chain
ipfs http client library
Let's Rock 🤟🏻
1. Upload file to IPFS
If you want to learn how to upload FOLDERS/FILES into IPFS, please refer this
First, you need to put your file into IPFS, there's 2 ways to upload file to IPFS: local IPFS node or remote IPFS gateway:
With local IPFS node
import { create } from 'ipfs-http-client'
async function addFile(ipfs: IPFS.IPFS, fileContent: any) {
// 1. Create IPFS instant
const ipfs = create({
url: 'http://localhost:5001'
});
// 2. Add file to ipfs
const cid = await ipfs.add(fileContent);
// 3. Get file status from ipfs
const fileStat = await ipfs.files.stat("/ipfs/" + cid.path);
return {
cid: cid.path,
size: fileStat.cumulativeSiz
};
}
Copy
import { create } from 'ipfs-http-client'
import { ethers } from 'ethers';
async function addFile(ipfs: IPFS.IPFS, fileContent: any) {
// 0. Construct web3 authed header
// Now support: ethereum-series, polkadot-series, solana, elrond, flow, near, ...
// Let's take ethereum as example
const pair = ethers.Wallet.createRandom();
const sig = await pair.signMessage(pair.address);
const authHeaderRaw = `eth-${pair.address}:${sig}`;
const authHeader = Buffer.from(authHeaderRaw).toString('base64');
const ipfsW3GW = 'https://crustipfs.xyz';
// 1. Create IPFS instant
const ipfs = create({
url: `${ipfsW3GW}/api/v0`,
headers: {
authorization: `Basic ${authHeader}`
}
});
// 2. Add file to ipfs
const cid = await ipfs.add(fileContent);
// 3. Get file status from ipfs
const fileStat = await ipfs.files.stat("/ipfs/" + cid.path);
return {
cid: cid.path,
size: fileStat.cumulativeSize
};
}
Copy
2. Upload folder
import { create } from 'ipfs-http-client'
async function addFile(ipfs: IPFS.IPFS, rootCid: any) {
...
// Get links from ipfs
for await (const link of this.ipfs.ls(rootCid)) {
console.log(link);
}
...
}
Copy
In addition, after Place storage order, you can use the folder-analyzer service to obtain directly, see the 4 section for details.
3. Place storage order
Next, we need to send a transaction named Place Storage Order on Crust chain, this transaction will dispatch your storage requirement to each Crust IPFS nodes through blockchain. Then the IPFS nodes will start pulling your file with IPFS protocol.
If it's a folder, please set memo = 'folder'
import { ApiPromise, WsProvider } from '@polkadot/api';
import { typesBundleForPolkadot, crustTypes } from '@crustio/type-definitions';
import { Keyring } from '@polkadot/keyring';
import { KeyringPair } from '@polkadot/keyring/types';
// Create global chain instance
const crustChainEndpoint = 'wss://rpc.crust.network';
const api = new ApiPromise({
provider: new WsProvider(crustChainEndpoint),
typesBundle: typesBundleForPolkadot,
});
async function placeStorageOrder() {
// 1. Construct place-storage-order tx
const fileCid = 'Qm123'; // IPFS CID, take `Qm123` as example
const fileSize = 2 * 1024 * 1024 * 1024; // Let's say 2 gb(in byte)
const tips = 0;
// If it's a folder, please set memo = 'folder'
const memo = '';
const tx = api.tx.market.placeStorageOrder(fileCid, fileSize, tips, memo);
// 2. Load seeds(account)
const seeds = 'xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx xxx';
const kr = new Keyring({ type: 'sr25519' });
const krp = kr.addFromUri(seeds);
// 3. Send transaction
await api.isReadyOrError;
return new Promise((resolve, reject) => {
tx.signAndSend(krp, ({events = [], status}) => {
console.log(`💸 Tx status: ${status.type}, nonce: ${tx.nonce}`);
if (status.isInBlock) {
events.forEach(({event: {method, section}}) => {
if (method === 'ExtrinsicSuccess') {
console.log(`✅ Place storage order success!`);
resolve(true);
}
});
} else {
// Pass it
}
}).catch(e => {
reject(e);
})
});
}
Copy
4. Folder information
4.1 Get the cids value contained in the folder
request
curl --request GET 'https://folderanalyzer.crustapps.net/api/v1/cids?root=QmQZYQaq48KkY7nWbpfWh8kyEh21yehwPk5xoofnLFVGtV'
Copy
The default storage time for a single transaction(order) is 6 months. If you want to extend the storage duration, Crust provides an assurance pool for you to customize the file's storage time, it allows you to put some tokens and will automatically extend the file's storage time.
You can find more ipfsW3GW endpoints on . Greatly improve the speed of ipfsW3GW by configuring .
You can also find more authHeader web3 supports on , the following example just takes ethereum as example.
For folder upload, refer to this to build. It is recommended to use this gateway: https://gw.crustfiles.app, or build a local IPFS node with a good network for uploading. At the same time, during the upload process, you can record the cids value of the file in the folder or call the IPFS command to obtain the cids value contained in the folder after the upload is completed. The code is as follows:
You can find more crustChainEndpoint on .
You can create your own account(seeds) on .
If you store a folder in Crust. Through the , you can obtain relevant information about the folder you placed the order in. There is a certain delay in updating this information, which is determined by the network environment.