Skip to content

Commit

Permalink
feat: wip pinata pins
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw committed May 20, 2021
1 parent a501f9c commit 19602da
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/cron-pinata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Cron Pinata Sync

on:
schedule:
- cron: '0 */12 * * *'

jobs:
update:
name: Sync with Pinata
runs-on: ubuntu-latest
strategy:
matrix:
env: ['staging', 'production']
timeout-minutes: 60
steps:
- uses: actions/checkout@v2

- name: Setup node
uses: actions/setup-node@v1
with:
node-version: 15

- name: Install dependencies
run: yarn install

- name: Run job
env:
DEBUG: '*'
ENV: ${{ matrix.env }}
CF_ACCOUNT: ${{ secrets.CF_ACCOUNT }}
CF_TOKEN: ${{ secrets.CF_TOKEN }}
PINATA_JWT: ${{ secrets.PINATA_JWT }}
run: yarn --cwd packages/cron start:pinata
Empty file added packages/cron/src/bin/pinata.js
Empty file.
Empty file.
41 changes: 41 additions & 0 deletions packages/cron/src/lib/pinata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { URL } from 'url'
import fetch from 'node-fetch'

const endpoint = 'https://api.pinata.cloud'

export class Pinata {
/**
* @param {{ apiToken: string }} config
*/
constructor({ apiToken }) {
this.apiToken = apiToken
}

/**
* @param {string} cid
* @param {{ pinataOptions?: { hostNodes?: string[] }, pinataMetadata?: { name?: string } }} [options]
* @returns {Promise<{ id: string, ipfsHash: string, status: string, name: string }>}
*/
async pinByHash(cid, options) {
const url = new URL('/pinning/pinByHash', endpoint)

const res = await fetch(url.toString(), {
method: 'POST',
headers: {
Authorization: `Bearer ${this.apiToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ hashToPin: cid, ...(options || {}) }),
})

const text = await res.text()
if (!res.ok) {
throw Object.assign(
new Error(`${res.status} ${res.statusText}: ${text}`),
{ response: res }
)
}

return JSON.parse(text)
}
}
11 changes: 11 additions & 0 deletions packages/cron/src/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Cluster } from '@nftstorage/ipfs-cluster'
import { Cloudflare } from '../lib/cloudflare.js'
import { IPFS } from './ipfs.js'
import { Pinata } from './pinata.js'

/**
* @param {import('./cloudflare').Namespace[]} namespaces
Expand Down Expand Up @@ -55,3 +56,13 @@ export function getClusterIPFSProxy(env) {
headers: { Authorization: `Basic ${basicAuthToken}` },
})
}

/**
* Create a new IPFS client instance from the passed environment variables.
* @param {Record<string, string|undefined>} env
*/
export function getPinata(env) {
const apiToken = env.PINATA_JWT
if (!apiToken) throw new Error('missing Pinata API token')
return new Pinata({ apiToken })
}

0 comments on commit 19602da

Please sign in to comment.