72 lines
2.1 KiB
JavaScript
Executable File
72 lines
2.1 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 argv = require('yargs')
|
|
.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',
|
|
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);
|
|
|
|
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);
|
|
}
|
|
})();
|