From c8dca0eb4638ef112411747c06786cc6d7577bfe Mon Sep 17 00:00:00 2001 From: yann300 Date: Sat, 21 Sep 2019 22:38:05 +0200 Subject: [PATCH] push on several ipfs nodes --- src/lib/publishOnIpfs.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/lib/publishOnIpfs.js b/src/lib/publishOnIpfs.js index 8bdd9fedac..03d562ea1d 100644 --- a/src/lib/publishOnIpfs.js +++ b/src/lib/publishOnIpfs.js @@ -2,8 +2,11 @@ const async = require('async') const IpfsClient = require('ipfs-mini') -const host = 'ipfs.komputing.org' -const ipfs = new IpfsClient({ host, port: 443, protocol: 'https' }) + +const ipfsNodes = [ + new IpfsClient({ host: 'ipfs.komputing.org', port: 443, protocol: 'https' }), + new IpfsClient({ host: 'ipfs.infura.io', port: 5001, protocol: 'https' }) +] module.exports = (contract, fileManager, cb, ipfsVerifiedPublishCallBack) => { // gather list of files to publish @@ -89,7 +92,7 @@ module.exports = (contract, fileManager, cb, ipfsVerifiedPublishCallBack) => { async function ipfsVerifiedPublish (content, expectedHash, cb) { try { - const results = await ipfs.add(content) + const results = await severalGatewaysPush(content) if (results !== expectedHash) { cb(null, { message: 'hash mismatch between solidity bytecode and uploaded content.', url: 'dweb:/ipfs/' + results, hash: results }) } else { @@ -99,3 +102,9 @@ async function ipfsVerifiedPublish (content, expectedHash, cb) { cb(error) } } + +function severalGatewaysPush (content) { + const invert = p => new Promise((resolve, reject) => p.then(reject).catch(resolve)) // Invert res and rej + const promises = ipfsNodes.map((node) => invert(node.add(content))) + return invert(Promise.all(promises)) +}