Browse Source

Hello Kredits world

tags/v1.0.1
bumi 1 year ago
commit
746149e89a
6 changed files with 2102 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +9
    -0
      README.md
  3. +44
    -0
      index.js
  4. +44
    -0
      lib/ipfs-pinner.js
  5. +1985
    -0
      package-lock.json
  6. +18
    -0
      package.json

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
node_modules


+ 9
- 0
README.md View File

@@ -0,0 +1,9 @@
# Kredits IPFS pinner

This tool pins the IPFS hashes of a Kredits organisation on a IPFS node.


## Usage

$ kredits-ipfs-pinner --help


+ 44
- 0
index.js View File

@@ -0,0 +1,44 @@
#! /usr/bin/env node

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',
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 {
const kredits = await Kredits.for({ network: argv.network, rpcUrl: argv.rpcUrl }, { apm: argv.apm, ipfsConfig }).init();
const ipfsPinner = new IpfsPinner(kredits);

ipfsPinner.pinAll().then(pins => {
console.log('Pinned', JSON.stringify(pins, null, 2));
});
ipfsPinner.monitor((pin) => {
console.log('Pinned', JSON.stringify(pin));
});
console.log(`Subscribed to DAO: ${kredits.Kernel.contract.address}`);
} catch(e) {
console.log(e);
process.exit(1);
}
})();

+ 44
- 0
lib/ipfs-pinner.js View File

@@ -0,0 +1,44 @@
class IpfsPinner {
constructor (kredits, ipfsApi) {
this.kredits = kredits;
this.ipfsApi = ipfsApi || this.kredits.ipfs;
}

pinAll () {
return Promise.all([
this._pinAllFromContract(this.kredits.Contributor),
this._pinAllFromContract(this.kredits.Contribution)
]);
}

monitor (callback) {
this.kredits.Contribution.on('ContributionAdded', (id) => {
this.kredits.Contribution.getData(id)
.then(data => { return this.ipfsApi.pin(data); })
.then(callback);
});
this.kredits.Contributor.on('ContributorAdded', (id) => {
this.kredits.Contribution.getData(id)
.then(data => { return this.ipfsApi.pin(data); })
.then(callback);
});
this.kredits.Contributor.on('ContributorProfileUpdated', (id) => {
this.kredits.Contributor.getData(id)
.then(data => { return this.ipfsApi.pin(data); })
.then(callback);
});
}

_pinAllFromContract (contract) {
return contract.count.then(count => {
let promises = [...Array(count).keys()].map(i => {
let id = i + 1; // 0 => 1 - ids start with 1 and not with 0
return contract.getData(id).then(data => {
return this.ipfsApi.pin(data);
});
});
return Promise.all(promises);
});
}
}
module.exports = IpfsPinner;

+ 1985
- 0
package-lock.json
File diff suppressed because it is too large
View File


+ 18
- 0
package.json View File

@@ -0,0 +1,18 @@
{
"name": "kredits-ipfs-pinner",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"kredits-ipfs-pinner": "index.js"
},
"author": "",
"license": "MIT",
"dependencies": {
"kredits-contracts": "^5.3.0",
"yargs": "^13.2.4"
}
}

Loading…
Cancel
Save