ipfs: add pin_metadata.js for fixing dapp hashes

Details: https://github.com/status-im/infra-ipfs/issues/4

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2020-01-31 10:52:36 +01:00
parent 55c0fc6f96
commit f3687c0042
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020
2 changed files with 62 additions and 1 deletions

View File

@ -5,6 +5,8 @@
"license": "MIT",
"dependencies": {
"base64-img": "^1.0.4",
"ipfs-http-client": "32.0.1"
"fetch-timeout": "^0.0.2",
"ipfs-http-client": "32.0.1",
"mongodb": "^3.5.2"
}
}

59
ipfs/pin_metadata.js Executable file
View File

@ -0,0 +1,59 @@
#!/usr/bin/env node
const fs = require('fs')
const fetch = require('fetch-timeout')
const ipfsClient = require('ipfs-http-client')
const MongoClient = require('mongodb').MongoClient
const ipfsUpload = async (ipfs, data) => {
let json = JSON.stringify(data)
let content = Buffer.from(json)
resp = await ipfs.add(content, { pin: false })
return resp[0].hash
}
const updateDapp = async (dbCol, oldHash, newHash) => {
return dbCol.updateOne(
{hash: oldHash},
{'$set': {hash: newHash}}
)
}
const main = async () => {
const mongo = await MongoClient.connect(
process.env.MONGODB_URI,
{ useUnifiedTopology: true }
)
const db = mongo.db()
const dbDapps = db.collection('dappsmetadatas')
const ipfs = ipfsClient(
process.env.IPFS_HOST || 'ipfs.status.im',
process.env.IPFS_PORT || 443,
{ protocol: 'https' }
)
const dapps = await dbDapps.find().toArray()
for (let dapp of dapps) {
let oldHash = dapp.hash
console.log(` * ${dapp.details.name} - ${dapp.details.url}`)
console.log(` - OLD HASH: ${oldHash}`)
let newHash = await ipfsUpload(ipfs, dapp.details)
console.log(` - NEW HASH: ${newHash}`)
if (oldHash == newHash) {
console.log(' - MATCHING')
continue
}
let rval = await updateDapp(dbDapps, oldHash, newHash)
if (rval.result.ok != 1) {
console.log(' ! FAILURE')
}
console.log(' ! UPDATED')
}
await mongo.close()
}
main()