-
Notifications
You must be signed in to change notification settings - Fork 0
/
addCids.mjs
52 lines (40 loc) · 1.18 KB
/
addCids.mjs
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
import { readFileSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import { create } from 'ipfs-http-client'
import { CID } from 'multiformats/cid'
import { pipe } from 'it-pipe'
import all from 'it-all'
import { TypedArrays } from '@react-frontend-developer/buffers'
import { resolve } from 'node:path'
if (!process.argv[2]) {
console.log('No file path provided')
process.exit()
}
const filepath = process.argv[2]
const cids = readFileSync(filepath)
if (!cids) {
console.log(`error reading ${filepath}!`)
process.exitCode = 1
process.exit()
}
const cidsJson = JSON.parse(cids)
if (!cidsJson) {
console.log(`error converting to json!`)
process.exitCode = 1
process.exit()
}
const cidsToPin = Object.keys(cidsJson).filter(
(cid) => cidsJson[cid] === 'hush-hush' || cidsJson[cid].startsWith('did:ipid')
)
const ipfs = create('/ip4/127.0.0.1/tcp/5001')
const status = {}
for (const cid of cidsToPin) {
process.stdout.write(`adding ${cid}...`)
const pinnedCid = await ipfs.pin.add(CID.parse(cid))
console.log('done')
status[pinnedCid.toString()] = 'done'
}
writeFileSync(
`${path.parse(filepath).name}.result.json`,
JSON.stringify(status, undefined, ' ')
)