Connect to ipfs.kosmos.org node on start

This ensures that the local IPFS node is always connected to a known
Kosmos node before attempting to fetch documents. Vastly improves
performance.

closes #4
This commit is contained in:
Basti 2019-08-13 10:07:25 +02:00
parent 2a3c021737
commit f2f884a317
No known key found for this signature in database
GPG Key ID: BE4634D632D39B67
1 changed files with 16 additions and 1 deletions

View File

@ -2,6 +2,7 @@
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 })
@ -26,6 +27,15 @@ const 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 () => {
try {
const kredits = await Kredits.for(
@ -35,7 +45,12 @@ console.log(`Using IPFS:`, ipfsConfig);
// check the connection to the IPFS client
// TODO redesign IPFS wrapper API and do not use an internal attribute
await kredits.ipfs._ipfsAPI.id();
const ipfsApi = kredits.ipfs._ipfsAPI;
await ipfsApi.id();
// TODO discover address dynamically from somewhere
await swarmConnect(ipfsApi, '/ip4/46.4.18.160/tcp/4001/ipfs/QmZ4Lpzhz8bafbTYvEMMCmrbrMM4JfyHDR23WbCSAd9bo7');
const ipfsPinner = new IpfsPinner(kredits);