# IPFS Network

#### &#x20;1. Storage process

Using IPFS Gateway and IPFS remote pinning service, developers can follow below process to upload, store and distribute files：

1. Upload the file to IPFS gateway, and get the file `CID` (a unique identifier generated based on the content of each file)
2. Use `CID` to call IPFS standard pinning service.
3. Obtain and monitor pinning status

#### 2. Dependencies <a href="#id-2-dependencies" id="id-2-dependencies"></a>

The code sample mainly depends on the following libraries:

* [@crustio/type-definitions](https://github.com/crustio/crust.js) Custom data type, used to adapt to Crust network
* [@polkadot/api](https://github.com/polkadot-js/api) The polkadot api library provides a Promise-style interface for performing related operations on the Crust chain
* [ipfs-http-client](https://github.com/ipfs/js-ipfs/tree/master/packages/ipfs-http-client) ipfs http client library, contains all the ipfs apis

### Let's Rock 🤟🏻 <a href="#lets-rock" id="lets-rock"></a>

#### 0. Build web3 authentication header with Crust <a href="#id-0-build-web3-authentication-header-with-crust" id="id-0-build-web3-authentication-header-with-crust"></a>

> This demo only takes Crust(substrate-based chains) as an example, more web3 auth ways can be checked [here](https://wiki.crust.network/docs/en/buildIPFSWeb3AuthGW#usage).

<pre class="language-javascript"><code class="lang-javascript">const { Keyring } = require('@polkadot/keyring');

const seeds = process.argv[2];

// 2. Construct auth header
const keyring = new Keyring();
const pair = keyring.addFromUri(seeds);
const sig = pair.sign(pair.address);
const sigHex = '0x' + Buffer.from(sig).toString('hex');

const authHeader = Buffer.from(`sub-${pair.address}:${sigHex}`).toString('base64');
<strong>Copy
</strong></code></pre>

#### 1. Upload files to IPFS Gateway <a href="#id-1-upload-files-to-ipfs-gateway" id="id-1-upload-files-to-ipfs-gateway"></a>

<pre class="language-javascript"><code class="lang-javascript">const { create, globSource } = require('ipfs-http-client');

// IPFS Web3 Authed Gateway address
const ipfsGateway = 'https://gw.crustfiles.app';

// 3. Create ipfs http client
const ipfs = create({
    url: ipfsGateway + '/api/v0',
    headers: {
        authorization: 'Basic ' + authHeader
    }
});

const { cid } = await ipfs.add(globSource(path, '**/*'));

if (cid) {
    console.log(cid.toV0().toString());
} else {
    throw new Error('IPFS add failed, please try again.');
}
<strong>Copy
</strong></code></pre>

> You can get full list of `ipfsGateway` address [here](https://github.com/crustio/crust-apps/blob/master/packages/apps-config/src/ipfs-gateway-endpoints/index.ts).

#### 2. Pin file through IPFS Pinning Service <a href="#id-2-pin-file-through-ipfs-pinning-service" id="id-2-pin-file-through-ipfs-pinning-service"></a>

<pre class="language-javascript"><code class="lang-javascript">const got = require('got');

const ipfsPinningService = 'https://pin.crustcode.com/psa'; // IPFS Web3 Authed Pinning Service address

// 4. Pin to crust with IPFS standard W3Authed pinning service
const {body} = await got.post(
    ipfsPinningService + '/pins',
    {
        headers: {
            authorization: 'Bearer ' + authHeader
        },
        json: {
            cid: cid.toV0().toString(),
            name: 'crust-demo'
        }
    }
);
<strong>Copy
</strong></code></pre>

> You can get the full list of `ipfsPinningService` [here](https://github.com/crustio/crust-apps/blob/master/packages/apps-config/src/ipfs-pinner/index.ts).

Will return `body` like

<pre class="language-json"><code class="lang-json">{
   "requestId":"c7fc3e00-b303-4a24-8485-71ee7b3ffb78-1631776602227",
   "status":"pinning",
   "created":"2021-09-16T06:55:12+00:00",
   "pin":{
      "cid":"QmYboQmwDrNK6waGjm2VvH7eMGZbo1LUi5X5iMtzetzsnK",
      "name":"crust-demo",
      "meta":null,
      "origins":[
         
      ]
   },
   "delegates":[
      
   ],
   "info":{
      
   }
}
<strong>Copy
</strong></code></pre>

#### 3. Query on-chain pinning status from IPFS Pinning Service <a href="#id-3-query-on-chain-pinning-status-from-ipfs-pinning-service" id="id-3-query-on-chain-pinning-status-from-ipfs-pinning-service"></a>

<pre class="language-javascript"><code class="lang-javascript">if (body) {
    const rid = JSON.parse(body)['requestId'];
    console.log(body, rid);
    // 5. Query pinning status through pinning service
    while (true) {
        const {body: pinningStat} = await got(
            ipfsPinningService + `/pins/${rid}`,
            {
                headers: {
                    authorization: 'Bearer ' + authHeader
                }
            }
        );
        console.log(pinningStat); // After success, you can query the cid on Crust

        await timeout(1000);
    }
} else {
    console.log(body);
    throw new Error('Crust pin failed, please try again.');
}
<strong>Copy
</strong></code></pre>

After pinned success, the demo will return

```json
{
   "requestId":"c7fc3e00-b303-4a24-8485-71ee7b3ffb78-1631776602227",
   "status":"pinned",
   "created":"2021-09-16T06:55:12+00:00",
   "pin":{
      "cid":"QmYboQmwDrNK6waGjm2VvH7eMGZbo1LUi5X5iMtzetzsnK",
      "name":"crust-demo",
      "meta":null,
      "origins":[
         
      ]
   },
   "delegates":[
      
   ],
   "info":{
      
   }
```

\\
