forked from aave/aip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aip-uploader.js
64 lines (57 loc) · 1.9 KB
/
aip-uploader.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
#!/usr/bin/env node
const bs58 = require('bs58');
const fs = require('fs')
const fetch = require('node-fetch')
const { exec, spawn } = require('child_process');
const { PINATA_KEY, PINATA_SECRET} = process.env;
const pinataEndpoint = 'https://api.pinata.cloud/pinning/pinJSONToIPFS'
jsonAips = JSON.parse(fs.readFileSync('./content/ipfs-aips/all-aips.json').toString());
Object.keys(jsonAips).forEach((id) => {
delete Object.assign(jsonAips[id], {'description': jsonAips[id]['content'] })['content'];
fetch(pinataEndpoint, {
method: 'POST',
body: JSON.stringify({
pinataOptions: { cidVersion: 0 },
pinataContent: jsonAips[id],
}),
headers: {
'Content-Type': 'application/json',
pinata_api_key: PINATA_KEY,
pinata_secret_api_key: PINATA_SECRET,
},
})
.then(res => {
return res.json()
}).then(result => {
if (result.error) throw { message: result.error }
const hash = result.IpfsHash
const encodedHash = `0x${
bs58
.decode(hash)
.slice(2)
.toString('hex')
}`
jsonAips[id].ipfsHash = hash
jsonAips[id].encodeIpfsHash = encodedHash
console.log('✅ Success!')
console.log(` IPFS hash: ${hash}`)
console.log(` Encoded IPFS hash (for proposal creation): ${encodedHash}`)
console.log(` See the file here: https://gateway.pinata.cloud/ipfs/${hash}`)
fs.writeFileSync(
`./content/ipfs-aips/${id}-Ipfs-hashes.json`,
JSON.stringify({aip: id, hash, encodedHash}, null, 2)
)
fs.writeFileSync(
`./content/ipfs-aips/all-aips.json`,
JSON.stringify(jsonAips, null, 2)
)
exec(`curl https://gateway.pinata.cloud/ipfs/${hash} > tmp && curl -sF file='@./tmp' https://api.thegraph.com/ipfs/api/v0/add`, (err, stdout, stderr) => {
if(err) {
throw new Error(stderr)
}
console.log(stdout)
});
}).catch((err) => {
console.log('error:', err);
})
})