diff --git a/app/lib/kredits/contracts/base.js b/app/lib/kredits/contracts/base.js index 6ac690f..a7a35aa 100644 --- a/app/lib/kredits/contracts/base.js +++ b/app/lib/kredits/contracts/base.js @@ -1,6 +1,3 @@ -import Kredits from '../kredits'; -import multihashes from 'npm:multihashes'; - export default class Base { constructor(contract) { this.contract = contract; @@ -10,28 +7,4 @@ export default class Base { 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 - }; - } } diff --git a/app/lib/kredits/contracts/contributor.js b/app/lib/kredits/contracts/contributor.js index 65cad84..4e859ff 100644 --- a/app/lib/kredits/contracts/contributor.js +++ b/app/lib/kredits/contracts/contributor.js @@ -35,30 +35,23 @@ export default class Contributor extends Base { }) // Fetch IPFS data if available .then((data) => { - return this.fetchAndMergeIpfsData(data, ContributorSerializer); + return Kredits.ipfs.catAndMerge(data, ContributorSerializer.deserialize); }); } - add(attributes) { - console.log(attributes); - - let json = ContributorSerializer.serialize(attributes); + add(contributorAttr) { + let json = ContributorSerializer.serialize(contributorAttr); // TODO: validate against schema return Kredits.ipfs - .add(new Kredits.ipfs.Buffer(json)) - .then((res) => res[0].hash) - .then((ipfsHash) => { - Object.assign(attributes, this.decodeIpfsHash(ipfsHash)); - return attributes; - }) - .then((attr) => { + .add(json) + .then((ipfsHashAttr) => { let contributor = [ - attr.address, - attr.ipfsHash, - attr.hashFunction, - attr.hashSize, - attr.isCore, + contributorAttr.address, + ipfsHashAttr.ipfsHash, + ipfsHashAttr.hashFunction, + ipfsHashAttr.hashSize, + contributorAttr.isCore, ]; console.log('[kredits] addContributor', ...contributor); diff --git a/app/lib/kredits/contracts/operator.js b/app/lib/kredits/contracts/operator.js index 841c421..c62eb9c 100644 --- a/app/lib/kredits/contracts/operator.js +++ b/app/lib/kredits/contracts/operator.js @@ -35,30 +35,23 @@ export default class Operator extends Base { }) // Fetch IPFS data if available .then((data) => { - return this.fetchAndMergeIpfsData(data, ContributionSerializer); + return Kredits.ipfs.catAndMerge(data, ContributionSerializer.deserialize); }); } - addProposal(attributes) { - console.log(attributes); - - let json = ContributionSerializer.serialize(attributes); + addProposal(proposalAttr) { + let json = ContributionSerializer.serialize(proposalAttr); // TODO: validate against schema return Kredits.ipfs - .add(new Kredits.ipfs.Buffer(json)) - .then((res) => res[0].hash) - .then((ipfsHash) => { - Object.assign(attributes, this.decodeIpfsHash(ipfsHash)); - return attributes; - }) - .then((attr) => { + .add(json) + .then((ipfsHashAttr) => { let proposal = [ - attr.recipientId, - attr.amount, - attr.ipfsHash, - attr.hashFunction, - attr.hashSize, + proposalAttr.recipientId, + proposalAttr.amount, + ipfsHashAttr.ipfsHash, + ipfsHashAttr.hashFunction, + ipfsHashAttr.hashSize, ]; console.log('[kredits] addProposal', ...proposal); diff --git a/app/lib/kredits/kredits.js b/app/lib/kredits/kredits.js index 1531a0b..b087371 100644 --- a/app/lib/kredits/kredits.js +++ b/app/lib/kredits/kredits.js @@ -1,11 +1,11 @@ import ethers from 'npm:ethers'; -import ipfsAPI from 'npm:ipfs-api'; import RSVP from 'rsvp'; import abis from 'contracts/abis'; import addresses from 'contracts/addresses'; import contracts from './contracts'; +import IPFS from './utils/ipfs'; // Helpers function capitalize(word) { @@ -25,7 +25,7 @@ export default class Kredits { static setup(provider, signer, ipfsConfig) { this.ipfsConfig = ipfsConfig; - this.ipfs = ipfsAPI(ipfsConfig); + this.ipfs = new IPFS(ipfsConfig); let registryContract = this.initRegistryContract(provider); diff --git a/app/lib/kredits/utils/ipfs.js b/app/lib/kredits/utils/ipfs.js new file mode 100644 index 0000000..9485c12 --- /dev/null +++ b/app/lib/kredits/utils/ipfs.js @@ -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); + } + + +}