Move IPFS handling in utils class

This commit is contained in:
2018-04-09 19:28:49 +02:00
parent 0e330a6529
commit 1a5d33b1f0
5 changed files with 77 additions and 63 deletions
-27
View File
@@ -1,6 +1,3 @@
import Kredits from '../kredits';
import multihashes from 'npm:multihashes';
export default class Base { export default class Base {
constructor(contract) { constructor(contract) {
this.contract = contract; this.contract = contract;
@@ -10,28 +7,4 @@ export default class Base {
return this.contract.functions; return this.contract.functions;
} }
// TODO: move into utils
fetchAndMergeIpfsData(data, Serializer) {
if (!data.hashSize || data.hashSize === 0) {
return data;
}
let digest = Kredits.ipfs.Buffer.from(data.ipfsHash.slice(2), 'hex');
let ipfsHash = multihashes.encode(digest, data.hashFunction, data.hashSize);
return Kredits.ipfs
.cat(ipfsHash)
.then(Serializer.deserialize)
.then((attributes) => {
return Object.assign({}, data, attributes);
});
}
decodeIpfsHash(ipfsHash) {
let multihash = multihashes.decode(multihashes.fromB58String(ipfsHash));
return {
ipfsHash: '0x' + multihashes.toHexString(multihash.digest),
hashSize: multihash.length,
hashFunction: multihash.code
};
}
} }
+10 -17
View File
@@ -35,30 +35,23 @@ export default class Contributor extends Base {
}) })
// Fetch IPFS data if available // Fetch IPFS data if available
.then((data) => { .then((data) => {
return this.fetchAndMergeIpfsData(data, ContributorSerializer); return Kredits.ipfs.catAndMerge(data, ContributorSerializer.deserialize);
}); });
} }
add(attributes) { add(contributorAttr) {
console.log(attributes); let json = ContributorSerializer.serialize(contributorAttr);
let json = ContributorSerializer.serialize(attributes);
// TODO: validate against schema // TODO: validate against schema
return Kredits.ipfs return Kredits.ipfs
.add(new Kredits.ipfs.Buffer(json)) .add(json)
.then((res) => res[0].hash) .then((ipfsHashAttr) => {
.then((ipfsHash) => {
Object.assign(attributes, this.decodeIpfsHash(ipfsHash));
return attributes;
})
.then((attr) => {
let contributor = [ let contributor = [
attr.address, contributorAttr.address,
attr.ipfsHash, ipfsHashAttr.ipfsHash,
attr.hashFunction, ipfsHashAttr.hashFunction,
attr.hashSize, ipfsHashAttr.hashSize,
attr.isCore, contributorAttr.isCore,
]; ];
console.log('[kredits] addContributor', ...contributor); console.log('[kredits] addContributor', ...contributor);
+10 -17
View File
@@ -35,30 +35,23 @@ export default class Operator extends Base {
}) })
// Fetch IPFS data if available // Fetch IPFS data if available
.then((data) => { .then((data) => {
return this.fetchAndMergeIpfsData(data, ContributionSerializer); return Kredits.ipfs.catAndMerge(data, ContributionSerializer.deserialize);
}); });
} }
addProposal(attributes) { addProposal(proposalAttr) {
console.log(attributes); let json = ContributionSerializer.serialize(proposalAttr);
let json = ContributionSerializer.serialize(attributes);
// TODO: validate against schema // TODO: validate against schema
return Kredits.ipfs return Kredits.ipfs
.add(new Kredits.ipfs.Buffer(json)) .add(json)
.then((res) => res[0].hash) .then((ipfsHashAttr) => {
.then((ipfsHash) => {
Object.assign(attributes, this.decodeIpfsHash(ipfsHash));
return attributes;
})
.then((attr) => {
let proposal = [ let proposal = [
attr.recipientId, proposalAttr.recipientId,
attr.amount, proposalAttr.amount,
attr.ipfsHash, ipfsHashAttr.ipfsHash,
attr.hashFunction, ipfsHashAttr.hashFunction,
attr.hashSize, ipfsHashAttr.hashSize,
]; ];
console.log('[kredits] addProposal', ...proposal); console.log('[kredits] addProposal', ...proposal);
+2 -2
View File
@@ -1,11 +1,11 @@
import ethers from 'npm:ethers'; import ethers from 'npm:ethers';
import ipfsAPI from 'npm:ipfs-api';
import RSVP from 'rsvp'; import RSVP from 'rsvp';
import abis from 'contracts/abis'; import abis from 'contracts/abis';
import addresses from 'contracts/addresses'; import addresses from 'contracts/addresses';
import contracts from './contracts'; import contracts from './contracts';
import IPFS from './utils/ipfs';
// Helpers // Helpers
function capitalize(word) { function capitalize(word) {
@@ -25,7 +25,7 @@ export default class Kredits {
static setup(provider, signer, ipfsConfig) { static setup(provider, signer, ipfsConfig) {
this.ipfsConfig = ipfsConfig; this.ipfsConfig = ipfsConfig;
this.ipfs = ipfsAPI(ipfsConfig); this.ipfs = new IPFS(ipfsConfig);
let registryContract = this.initRegistryContract(provider); let registryContract = this.initRegistryContract(provider);
+55
View File
@@ -0,0 +1,55 @@
import ipfsAPI from 'npm:ipfs-api';
import multihashes from 'npm:multihashes';
export default class IPFS {
constructor(config) {
this._ipfsAPI = ipfsAPI(config);
this._config = config;
}
catAndMerge(data, deserializer) {
// if no hash details are found simply return the data; nothing to merge
if (!data.hashSize || data.hashSize === 0) {
return data;
}
return this.cat(data)
.then(deserializer)
.then((attributes) => {
return Object.assign({}, data, attributes);
});
}
add(data) {
return this._ipfsAPI
.add(new this._ipfsAPI.Buffer(data))
.then((res) => {
return this.decodeHash(res[0].hash);
});
}
cat(hashData) {
let ipfsHash = hashData; // default - if it is a string
if (hashData.hasOwnProperty('hashSize')) {
ipfsHash = this.encodeHash(hashData);
}
return this._ipfsAPI.cat(ipfsHash);
}
decodeHash(ipfsHash) {
let multihash = multihashes.decode(multihashes.fromB58String(ipfsHash));
return {
ipfsHash: '0x' + multihashes.toHexString(multihash.digest),
hashSize: multihash.length,
hashFunction: multihash.code,
sourceHash: ipfsHash
};
}
encodeHash(hashData) {
let digest = this._ipfsAPI.Buffer.from(hashData.ipfsHash.slice(2), 'hex');
return multihashes.encode(digest, hashData.hashFunction, hashData.hashSize);
}
}