ipfs-pinner/index.js
Sebastian Kippe ef1ae5add7
Use DNS multiaddr
Makes it much easier to identify the node you're connecting to
2020-10-26 09:16:00 +01:00

94 lines
2.8 KiB
JavaScript
Executable File

#!/usr/bin/env node
const Kredits = require('kredits-contracts');
const IpfsPinner = require('./lib/ipfs-pinner');
const debug = require('debug')('ipfs-pinner');
const { inspect } = require('util');
// TODO make configurable
const peerId = "QmbqZCZ2RzVr4r1UEdFak6ra76kHxtGmfV9r3e1Ev6Tj5D";
const peerAddrs = [ "/dns4/barnard.kosmos.org/tcp/4001" ];
const bootstrapNode = `${peerAddrs[0]}/ipfs/${peerId}`;
const peerConfig = { "ID": peerId, "Addrs": [ peerAddrs ] };
const argv = require('yargs')
.default({
network: 'rinkeby',
apm: 'open.aragonpm.eth',
host: 'localhost',
port: '5001',
protocol: 'http',
monitor: true,
bootstrapNode: bootstrapNode
})
.boolean('monitor')
.describe({
network: 'Ethereum network to connect to',
rpcUrl: 'Ethereum node RPC URL; alternative to --network',
daoAddress: 'Optional Kredits DAO address',
host: 'IPFS API host',
port: 'IPFS API port',
protocol: 'IPFS API protocol',
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;
const ipfsConfig = {
host: argv.host,
port: argv.port,
protocol: argv.protocol
};
console.log(`Using IPFS:`, ipfsConfig);
(async () => {
try {
const kredits = await Kredits.for(
{ network: argv.network, rpcUrl: argv.rpcUrl },
{ apm: argv.apm, ipfsConfig: ipfsConfig, addresses: { Kernel: argv.daoAddress } }
).init();
// check the connection to the IPFS client
// TODO redesign IPFS wrapper API and do not use an internal attribute
const ipfsApi = kredits.ipfs._ipfsAPI;
await ipfsApi.id();
// debug(`Connecting to known IPFS node ${argv.bootstrapNode}`);
// await ipfsApi.swarm.connect(argv.bootstrapNode);
let peers;
try {
peers = await ipfsApi.config.get('Peering.Peers');
} catch(e) { /* No peers configured */ }
if (peers) {
// TODO check for kosmos peer
debug('IPFS peers configured:');
debug(inspect(peers.map(p => p.ID)));
} else {
debug(`Configuring persistent IPFS peer: ${peerId}`);
await ipfsApi.config.set('Peering', { "Peers": [ peerConfig ]});
}
// const ipfsPinner = new IpfsPinner(kredits);
// ipfsPinner.pinAll().then(pins => {
// console.log(`Pinned ${pins.length} existing documents`);
// });
//
// ipfsPinner.monitor(pin => {
// console.log('Pinned a new document:', pin[0]["hash"]);
// });
console.log(`Subscribed to DAO: ${kredits.Kernel.contract.address}`);
} catch(e) {
console.log('Failed to start');
console.log(e);
process.exit(1);
}
})();