ipfs-pinner/index.js

56 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-07-02 16:41:48 +00:00
const debug = require('debug')('ipfs-pinner');
2019-07-02 14:20:52 +00:00
const Kredits = require('kredits-contracts');
const IpfsPinner = require('./lib/ipfs-pinner');
const argv = require('yargs')
.default({ network: 'rinkeby', apm: 'open.aragonpm.eth', host: 'localhost', port: '5001', protocol: 'http', monitor: true })
.boolean('monitor')
.describe({
network: 'Ethereum network to connect to',
rpcUrl: 'Ethereum node RPC URL; alternative to --network',
2019-07-02 15:27:51 +00:00
daoAddress: 'Optional Kredits DAO address',
2019-07-02 14:20:52 +00:00
host: 'IPFS API host',
port: 'IPFS API port',
protocol: 'IPFS API protocol',
monitor: 'Monitor contract events for new IPFS 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 {
2019-07-02 15:27:51 +00:00
const kredits = await Kredits.for(
{ network: argv.network, rpcUrl: argv.rpcUrl },
{ apm: argv.apm, ipfsConfig: ipfsConfig, addresses: { Kernel: argv.daoAddress } }
).init();
2019-07-02 16:41:48 +00:00
// check the connection to the IPFS client
// TODO redesign IPFS wrapper API and do not use an internal attribute
await kredits.ipfs._ipfsAPI.id();
2019-07-02 14:20:52 +00:00
const ipfsPinner = new IpfsPinner(kredits);
ipfsPinner.pinAll().then(pins => {
2019-07-02 16:41:48 +00:00
console.log(`Pinned ${pins.length} existing documents`);
2019-07-02 14:20:52 +00:00
});
2019-07-02 16:41:48 +00:00
ipfsPinner.monitor(pin => {
console.log('Pinned a new document:', pin[0]["hash"]);
2019-07-02 14:20:52 +00:00
});
2019-07-02 16:41:48 +00:00
2019-07-02 14:20:52 +00:00
console.log(`Subscribed to DAO: ${kredits.Kernel.contract.address}`);
} catch(e) {
2019-07-02 16:41:48 +00:00
console.log('Failed to start');
2019-07-02 14:20:52 +00:00
console.log(e);
process.exit(1);
}
})();