Hello Kredits world
Dieser Commit ist enthalten in:
Commit
746149e89a
|
@ -0,0 +1,2 @@
|
|||
node_modules
|
||||
|
|
@ -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
|
||||
|
|
@ -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);
|
||||
}
|
||||
})();
|
|
@ -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;
|
Datei-Diff unterdrückt, da er zu groß ist
Diff laden
|
@ -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"
|
||||
}
|
||||
}
|
Laden…
In neuem Issue referenzieren