Compare commits
3 Commits
77c2cea4a4
...
feature/ip
| Author | SHA1 | Date | |
|---|---|---|---|
|
ef1ae5add7
|
|||
|
9c34e4c913
|
|||
|
ba5f67861f
|
@@ -1,6 +1,6 @@
|
|||||||
# Kredits IPFS Pinner
|
# Kredits IPFS Pinner
|
||||||
|
|
||||||
This tool pins the IPFS documents of a Kredits organization on any IPFS node.
|
This tool pins the IPFS hashes of a Kredits organisation on an IPFS node.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
@@ -9,11 +9,11 @@ config flags, in case it is not running on localhost with default ports.)
|
|||||||
|
|
||||||
### With NPX magic
|
### With NPX magic
|
||||||
|
|
||||||
npx @kredits/ipfs-pinner
|
npx @kosmos/kredits-ipfs-pinner
|
||||||
|
|
||||||
### Global install
|
### Global install
|
||||||
|
|
||||||
npm install -g @kredits/ipfs-pinner
|
npm install -g @kosmos/kredits-ipfs-pinner
|
||||||
kredits-ipfs-pinner
|
kredits-ipfs-pinner
|
||||||
|
|
||||||
### From repo
|
### From repo
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"ID": "QmRw21aC3TroRVdZhrE2Qh4W6PBA67kbE8p3fNfsVcfW8D",
|
|
||||||
"Addrs": ["/dns4/draco.kosmos.org/tcp/4001"]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
66
index.js
66
index.js
@@ -1,30 +1,38 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const fs = require('fs');
|
const Kredits = require('kredits-contracts');
|
||||||
const debug = require('debug')('ipfs-pinner');
|
|
||||||
const Kredits = require('@kredits/contracts');
|
|
||||||
const IpfsPinner = require('./lib/ipfs-pinner');
|
const IpfsPinner = require('./lib/ipfs-pinner');
|
||||||
const defaultPeers = JSON.parse(fs.readFileSync('./config/peers.json'));
|
const debug = require('debug')('ipfs-pinner');
|
||||||
|
const { inspect } = require('util');
|
||||||
|
|
||||||
|
// TODO make configurable
|
||||||
|
const peerId = "QmbqZCZ2RzVr4r1UEdFak6ra76kHxtGmfV9r3e1Ev6Tj5D";
|
||||||
|
const peerAddrs = [ "/dns4/barnard.kosmos.org/tcp/4001" ];
|
||||||
|
const bootstrapNode = `${peerAddrs[0]}/ipfs/${peerId}`;
|
||||||
|
const peerConfig = { "ID": peerId, "Addrs": [ peerAddrs ] };
|
||||||
|
|
||||||
const argv = require('yargs')
|
const argv = require('yargs')
|
||||||
.default({
|
.default({
|
||||||
rpcUrl: 'https://rsk-testnet.kosmos.org',
|
network: 'rinkeby',
|
||||||
|
apm: 'open.aragonpm.eth',
|
||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
port: '5001',
|
port: '5001',
|
||||||
protocol: 'http',
|
protocol: 'http',
|
||||||
monitor: true,
|
monitor: true,
|
||||||
bootstrapNode: `${defaultPeers[0].Addrs[0]}/ipfs/${defaultPeers[0].ID}`
|
bootstrapNode: bootstrapNode
|
||||||
})
|
})
|
||||||
.boolean('monitor')
|
.boolean('monitor')
|
||||||
.describe({
|
.describe({
|
||||||
rpcUrl: 'Web3/EVM node RPC URL; alternative to --network',
|
network: 'Ethereum network to connect to',
|
||||||
|
rpcUrl: 'Ethereum node RPC URL; alternative to --network',
|
||||||
|
daoAddress: 'Optional Kredits DAO address',
|
||||||
host: 'IPFS API host',
|
host: 'IPFS API host',
|
||||||
port: 'IPFS API port',
|
port: 'IPFS API port',
|
||||||
protocol: 'IPFS API protocol',
|
protocol: 'IPFS API protocol',
|
||||||
monitor: 'Monitor contract events for new IPFS documents',
|
monitor: 'Monitor contract events for new IPFS documents',
|
||||||
bootstrapNode: 'IPFS node address to connect to before fetching documents'
|
bootstrapNode: 'IPFS node address to connect to before fetching documents'
|
||||||
})
|
})
|
||||||
.example('$0 --host localhost', 'Pins all existing IPFS documents to the IPFS API running on localhost and monitors for new events')
|
.example('$0 --network rinkeby --host localhost', 'Pins all existing IPFS documents to the IPFS API running on localhost and monitors for new events.')
|
||||||
.argv;
|
.argv;
|
||||||
|
|
||||||
const ipfsConfig = {
|
const ipfsConfig = {
|
||||||
@@ -38,31 +46,45 @@ console.log(`Using IPFS:`, ipfsConfig);
|
|||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const kredits = await Kredits.for(
|
const kredits = await Kredits.for(
|
||||||
{ rpcUrl: argv.rpcUrl },
|
{ network: argv.network, rpcUrl: argv.rpcUrl },
|
||||||
{ ipfsConfig: ipfsConfig }
|
{ apm: argv.apm, ipfsConfig: ipfsConfig, addresses: { Kernel: argv.daoAddress } }
|
||||||
).init();
|
).init();
|
||||||
|
|
||||||
// Check the connection to the IPFS client
|
// check the connection to the IPFS client
|
||||||
// TODO redesign IPFS wrapper API and do not use an internal attribute
|
// TODO redesign IPFS wrapper API and do not use an internal attribute
|
||||||
const ipfsApi = kredits.ipfs._ipfsAPI;
|
const ipfsApi = kredits.ipfs._ipfsAPI;
|
||||||
|
|
||||||
await ipfsApi.id();
|
await ipfsApi.id();
|
||||||
|
|
||||||
debug(`Connecting to known IPFS node ${argv.bootstrapNode}`);
|
// debug(`Connecting to known IPFS node ${argv.bootstrapNode}`);
|
||||||
await ipfsApi.swarm.connect(argv.bootstrapNode);
|
// await ipfsApi.swarm.connect(argv.bootstrapNode);
|
||||||
|
|
||||||
const ipfsPinner = new IpfsPinner(kredits);
|
let peers;
|
||||||
|
|
||||||
ipfsPinner.pinAll().then(pins => {
|
try {
|
||||||
console.log(`Pinned ${pins.length} existing documents`);
|
peers = await ipfsApi.config.get('Peering.Peers');
|
||||||
});
|
} catch(e) { /* No peers configured */ }
|
||||||
|
|
||||||
ipfsPinner.monitor(pin => {
|
if (peers) {
|
||||||
console.log('Pinned a new document:', pin[0]["hash"]);
|
// TODO check for kosmos peer
|
||||||
});
|
debug('IPFS peers configured:');
|
||||||
|
debug(inspect(peers.map(p => p.ID)));
|
||||||
|
} else {
|
||||||
|
debug(`Configuring persistent IPFS peer: ${peerId}`);
|
||||||
|
await ipfsApi.config.set('Peering', { "Peers": [ peerConfig ]});
|
||||||
|
}
|
||||||
|
|
||||||
// TODO Add new deployment/DAO/org ID or all contract proxy addresses
|
// const ipfsPinner = new IpfsPinner(kredits);
|
||||||
// console.log(`Subscribed to DAO: ${kredits.Kernel.contract.address}`);
|
|
||||||
|
// 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) {
|
} catch(e) {
|
||||||
console.log('Failed to start');
|
console.log('Failed to start');
|
||||||
console.log(e);
|
console.log(e);
|
||||||
|
|||||||
@@ -1,16 +1,5 @@
|
|||||||
const debug = require('debug')('ipfs-pinner');
|
const debug = require('debug')('ipfs-pinner');
|
||||||
|
|
||||||
async function promiseAllInBatches(task, items, batchSize) {
|
|
||||||
let position = 0;
|
|
||||||
let results = [];
|
|
||||||
while (position < items.length) {
|
|
||||||
const itemsForBatch = items.slice(position, position + batchSize);
|
|
||||||
results = [...results, ...await Promise.allSettled(itemsForBatch.map(item => task(item)))];
|
|
||||||
position += batchSize;
|
|
||||||
}
|
|
||||||
return results;
|
|
||||||
}
|
|
||||||
|
|
||||||
class IpfsPinner {
|
class IpfsPinner {
|
||||||
constructor (kredits, ipfsApi) {
|
constructor (kredits, ipfsApi) {
|
||||||
this.kredits = kredits;
|
this.kredits = kredits;
|
||||||
@@ -18,12 +7,12 @@ class IpfsPinner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async pinAll () {
|
async pinAll () {
|
||||||
const contributorHashes = await this._pinAllFromContract(this.kredits.Contributor);
|
const contributorHashes = await this._pinAllFromContract(this.kredits.Contributor);
|
||||||
const contributionHashes = await this._pinAllFromContract(this.kredits.Contribution);
|
const contributionHashes = await this._pinAllFromContract(this.kredits.Contribution);
|
||||||
const reimbursementHashes = await this._pinAllFromContract(this.kredits.Reimbursement);
|
const proposalHashes = await this._pinAllFromContract(this.kredits.Proposal);
|
||||||
|
|
||||||
return contributorHashes.concat(contributionHashes)
|
return contributorHashes.concat(contributionHashes)
|
||||||
.concat(reimbursementHashes);
|
.concat(proposalHashes);
|
||||||
}
|
}
|
||||||
|
|
||||||
monitor (callback) {
|
monitor (callback) {
|
||||||
@@ -44,25 +33,19 @@ class IpfsPinner {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async _pinAllFromContract (contract) {
|
_pinAllFromContract (contract) {
|
||||||
debug(`Pinning data from ${contract.constructor.name}...`);
|
debug(`Pinning data from ${contract.constructor.name}...`);
|
||||||
const count = await contract.count;
|
return contract.count.then(count => {
|
||||||
debug('Item count:', count);
|
let promises = [...Array(count).keys()].map(i => {
|
||||||
const ids = [...Array(count).keys()].map(i => i+1);
|
let id = i + 1; // 0 => 1 - ids start with 1 and not with 0
|
||||||
const cids = [];
|
debug(`Loading ${contract.constructor.name} #${id}`);
|
||||||
|
return contract.getData(id).then(data => {
|
||||||
async function loadAndPin (id) {
|
debug(`Pinning ${contract.constructor.name} #${id}`);
|
||||||
debug(`Loading ${contract.constructor.name} #${id}`);
|
return this.ipfsApi.pin(data);
|
||||||
return contract.getData(id).then(data => {
|
});
|
||||||
debug(`Pinning ${contract.constructor.name} #${id}`);
|
|
||||||
return this.ipfsApi.pin(data).then(cid => cids.push(cid));
|
|
||||||
});
|
});
|
||||||
}
|
return Promise.all(promises);
|
||||||
|
});
|
||||||
await promiseAllInBatches(loadAndPin.bind(this), ids, 100);
|
|
||||||
|
|
||||||
return cids;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = IpfsPinner;
|
module.exports = IpfsPinner;
|
||||||
|
|||||||
4646
package-lock.json
generated
4646
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -20,9 +20,9 @@
|
|||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@kredits/contracts": "git+https://gitea.kosmos.org/kredits/contracts#6e0ec87",
|
"debug": "^4.1.1",
|
||||||
"debug": "^4.3.4",
|
"kredits-contracts": "^5.4.0",
|
||||||
"yargs": "^17.6.0"
|
"yargs": "^13.2.4"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|||||||
Reference in New Issue
Block a user