From a3d21ebb30ab5f1e8cd7e14a39e2e5878b3cea0b Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Wed, 14 Aug 2019 10:12:07 +0200 Subject: [PATCH] Refactor bootstrap node connect, add CLI argument Removes the superfluous wrapper function (as the API already returns a Promise). Also adds a command line argument so you can choose a different bootstrap node. --- index.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index d66e956..9b8e479 100755 --- a/index.js +++ b/index.js @@ -5,7 +5,15 @@ const IpfsPinner = require('./lib/ipfs-pinner'); const debug = require('debug')('ipfs-pinner'); const argv = require('yargs') - .default({ network: 'rinkeby', apm: 'open.aragonpm.eth', host: 'localhost', port: '5001', protocol: 'http', monitor: true }) + .default({ + network: 'rinkeby', + apm: 'open.aragonpm.eth', + host: 'localhost', + port: '5001', + protocol: 'http', + monitor: true, + bootstrapNode: '/ip4/46.4.18.160/tcp/4001/ipfs/QmZ4Lpzhz8bafbTYvEMMCmrbrMM4JfyHDR23WbCSAd9bo7' + }) .boolean('monitor') .describe({ network: 'Ethereum network to connect to', @@ -14,7 +22,8 @@ const argv = require('yargs') host: 'IPFS API host', port: 'IPFS API port', protocol: 'IPFS API protocol', - monitor: 'Monitor contract events for new IPFS documents' + monitor: 'Monitor contract events for new IPFS documents', + bootstrapNode: 'IPFS node address to connect to before fetching documents' }) .example('$0 --network rinkeby --host localhost', 'Pins all existing IPFS documents to the IPFS API running on localhost and monitors for new events.') .argv; @@ -27,15 +36,6 @@ const ipfsConfig = { console.log(`Using IPFS:`, ipfsConfig); -async function swarmConnect (ipfsApi, address) { - return new Promise((resolve, reject) => { - debug(`Connecting to known IPFS node: ${address}`); - ipfsApi.swarm.connect(address, err => { - return err ? reject(err) : resolve(); - }) - }); -} - (async () => { try { const kredits = await Kredits.for( @@ -49,8 +49,8 @@ async function swarmConnect (ipfsApi, address) { await ipfsApi.id(); - // TODO discover address dynamically from somewhere - await swarmConnect(ipfsApi, '/ip4/46.4.18.160/tcp/4001/ipfs/QmZ4Lpzhz8bafbTYvEMMCmrbrMM4JfyHDR23WbCSAd9bo7'); + debug(`Connecting to known IPFS node ${argv.bootstrapNode}`); + await ipfsApi.swarm.connect(argv.bootstrapNode); const ipfsPinner = new IpfsPinner(kredits);