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.
This commit is contained in:
Basti 2019-08-14 10:12:07 +02:00
parent b11dbb9fde
commit a3d21ebb30
No known key found for this signature in database
GPG Key ID: BE4634D632D39B67
1 changed files with 13 additions and 13 deletions

View File

@ -5,7 +5,15 @@ const IpfsPinner = require('./lib/ipfs-pinner');
const debug = require('debug')('ipfs-pinner'); const debug = require('debug')('ipfs-pinner');
const argv = require('yargs') 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') .boolean('monitor')
.describe({ .describe({
network: 'Ethereum network to connect to', network: 'Ethereum network to connect to',
@ -14,7 +22,8 @@ const argv = require('yargs')
host: 'IPFS API host', host: 'IPFS API host',
port: 'IPFS API port', port: 'IPFS API port',
protocol: 'IPFS API protocol', 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.') .example('$0 --network rinkeby --host localhost', 'Pins all existing IPFS documents to the IPFS API running on localhost and monitors for new events.')
.argv; .argv;
@ -27,15 +36,6 @@ const ipfsConfig = {
console.log(`Using IPFS:`, 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 () => { (async () => {
try { try {
const kredits = await Kredits.for( const kredits = await Kredits.for(
@ -49,8 +49,8 @@ async function swarmConnect (ipfsApi, address) {
await ipfsApi.id(); await ipfsApi.id();
// TODO discover address dynamically from somewhere debug(`Connecting to known IPFS node ${argv.bootstrapNode}`);
await swarmConnect(ipfsApi, '/ip4/46.4.18.160/tcp/4001/ipfs/QmZ4Lpzhz8bafbTYvEMMCmrbrMM4JfyHDR23WbCSAd9bo7'); await ipfsApi.swarm.connect(argv.bootstrapNode);
const ipfsPinner = new IpfsPinner(kredits); const ipfsPinner = new IpfsPinner(kredits);