2020-01-31 09:52:36 +00:00
|
|
|
#!/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
|
|
|
|
}
|
|
|
|
|
2020-02-12 12:44:47 +00:00
|
|
|
const updateDapp = async (dbCol, legacyHash, newHash) => {
|
2020-01-31 09:52:36 +00:00
|
|
|
return dbCol.updateOne(
|
2020-02-12 12:44:47 +00:00
|
|
|
{hash: legacyHash},
|
2020-02-12 12:22:28 +00:00
|
|
|
{'$set': {ipfsHash: newHash}}
|
2020-01-31 09:52:36 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-02-12 12:44:47 +00:00
|
|
|
let legacyHash = dapp.hash
|
|
|
|
let oldHash = dapp.ipfsHash
|
2020-01-31 09:52:36 +00:00
|
|
|
console.log(` * ${dapp.details.name} - ${dapp.details.url}`)
|
2020-02-12 12:44:47 +00:00
|
|
|
console.log(` - LEG HASH: ${legacyHash}`)
|
2020-01-31 09:52:36 +00:00
|
|
|
console.log(` - OLD HASH: ${oldHash}`)
|
|
|
|
let newHash = await ipfsUpload(ipfs, dapp.details)
|
|
|
|
console.log(` - NEW HASH: ${newHash}`)
|
|
|
|
if (oldHash == newHash) {
|
|
|
|
console.log(' - MATCHING')
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-12 12:44:47 +00:00
|
|
|
let rval = await updateDapp(dbDapps, legacyHash, newHash)
|
2020-01-31 09:52:36 +00:00
|
|
|
if (rval.result.ok != 1) {
|
|
|
|
console.log(' ! FAILURE')
|
|
|
|
}
|
|
|
|
console.log(' ! UPDATED')
|
|
|
|
}
|
|
|
|
|
|
|
|
await mongo.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|