-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipfsPin.js
77 lines (71 loc) · 2.26 KB
/
ipfsPin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
require('dotenv').config();
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const API = 'https://ipfs.infura.io:5001/api/v0';
const IPFS_GATEWAY = process.env.IPFS_GATEWAY || 'https://ipfs.io';
const owner = 'nance-eth';
const repo = 'nance-actions';
const filePath = `CIDs/${process.argv[2]}/README.md`;
const AUTH_HEADER = `Basic ${Buffer.from(
`${process.env.INFURA_IPFS_ID}:${process.env.INFURA_IPFS_SECRET}`,
).toString('base64')}`;
// https://github.com/jbx-protocol/juice-interface/blob/main/src/lib/infura/ipfs.ts
async function dotPin(dataIn, encoding = 'utf-8') {
console.log(`Pinning to IPFS`);
const data = Buffer.from(dataIn, encoding);
const formData = new FormData();
formData.append('file', data);
return axios({
method: 'post',
url: `${API}/add`,
headers: {
'Authorization': AUTH_HEADER,
'Content-Type': 'multipart/form-data',
},
data: formData
}).then((res) => {
const cid = res.data.Hash;
console.log(`IPFS CID: ${cid}`);
return cid;
}).catch((e) => {
console.error(e);
});
}
async function writeCIDToReadme(cid) {
console.log(`Writing CID to ${filePath}`);
const { sha, oldContent } = await axios({
method: 'get',
url: `https://api.github.com/repos/${owner}/${repo}/contents/${filePath}`,
headers: {
'Authorization': `Bearer ${process.env.MY_GITHUB_TOKEN}`,
'Content-Type': 'application/json',
},
}).then((res) => {
return { sha: res.data.sha, oldContent: res.data.content.replace(/\n/g, '') };
}).catch((e) => {
console.error(e);
});
const content = Buffer.from(`${IPFS_GATEWAY}/ipfs/${cid}`).toString('base64')
if (content === oldContent) { console.log('no updates skip readme push'); return; }
return axios({
method: 'put',
url: `https://api.github.com/repos/${owner}/${repo}/contents/${filePath}`,
data: {
message: `update ${filePath} with ${cid}`,
content,
branch: 'main',
sha,
},
headers: {
'Authorization': `Bearer ${process.env.MY_GITHUB_TOKEN}`,
'Content-Type': 'application/json',
},
})
}
async function main() {
const file = fs.readFileSync(process.argv[3]);
const cid = await dotPin(file);
console.log(await writeCIDToReadme(cid));
}
main();