95 lines
2.8 KiB
JavaScript
Executable File
95 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
|
|
// barnard.kosmos.org
|
|
const peerId = "QmbqZCZ2RzVr4r1UEdFak6ra76kHxtGmfV9r3e1Ev6Tj5D";
|
|
const peerAddrs = [ "/ip4/104.248.95.16/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);
|
|
}
|
|
})();
|