From 41a04d6b53c7f214e6a46cacba56d59972b543da Mon Sep 17 00:00:00 2001 From: bumi Date: Mon, 9 Apr 2018 14:52:39 +0200 Subject: [PATCH] Refactor IPFS hash handling This now uses the multihashes dependency (which is an ipfs dependency) to decode/encode the ipfs hashes. --- app/lib/kredits/contracts/base.js | 22 +++++++++++----------- app/lib/kredits/contracts/contributor.js | 3 +-- app/lib/kredits/contracts/operator.js | 19 ++++++++----------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/app/lib/kredits/contracts/base.js b/app/lib/kredits/contracts/base.js index ffafeaf..6ac690f 100644 --- a/app/lib/kredits/contracts/base.js +++ b/app/lib/kredits/contracts/base.js @@ -1,5 +1,5 @@ import Kredits from '../kredits'; -import { fromBytes32 } from '../utils/multihash'; +import multihashes from 'npm:multihashes'; export default class Base { constructor(contract) { @@ -12,11 +12,11 @@ export default class Base { // TODO: move into utils fetchAndMergeIpfsData(data, Serializer) { - let ipfsHash = data.ipfsHash; - - if (!ipfsHash) { + 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) @@ -26,12 +26,12 @@ export default class Base { }); } - reassembleIpfsHash(data) { - let { ipfsHash: digest, hashFunction, hashSize } = data; - data.ipfsHash = fromBytes32({ digest, hashFunction, hashSize }); - delete data.hashFunction; - delete data.hashSize; - - return data; + 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 6f4c790..9340eff 100644 --- a/app/lib/kredits/contracts/contributor.js +++ b/app/lib/kredits/contracts/contributor.js @@ -26,7 +26,6 @@ export default class Contributor extends Base { id = ethers.utils.bigNumberify(id); return this.contract.functions.getContributorById(id) - .then(this.reassembleIpfsHash) .then((data) => { // TODO: remove as soon as the contract provides the id data.id = id; @@ -51,7 +50,7 @@ export default class Contributor extends Base { .add(new Kredits.ipfs.Buffer(json)) .then((res) => res[0].hash) .then((ipfsHash) => { - Object.assign(attributes, toBytes32(ipfsHash)); + Object.assign(attributes, this.decodeIpfsHash(ipfsHash)); return attributes; }) .then((attributes) => { diff --git a/app/lib/kredits/contracts/operator.js b/app/lib/kredits/contracts/operator.js index 1bff044..e0e860c 100644 --- a/app/lib/kredits/contracts/operator.js +++ b/app/lib/kredits/contracts/operator.js @@ -1,5 +1,6 @@ import ethers from 'npm:ethers'; import RSVP from 'rsvp'; +import multihashes from 'npm:multihashes'; import Kredits from '../kredits'; import ContributionSerializer from '../serializers/contribution'; @@ -26,7 +27,6 @@ export default class Operator extends Base { id = ethers.utils.bigNumberify(id); return this.contract.functions.proposals(id) - .then(this.reassembleIpfsHash) .then((data) => { // TODO: remove as soon as the contract provides the id data.id = id; @@ -51,20 +51,17 @@ export default class Operator extends Base { .add(new Kredits.ipfs.Buffer(json)) .then((res) => res[0].hash) .then((ipfsHash) => { - Object.assign(attributes, toBytes32(ipfsHash)); + Object.assign(attributes, this.decodeIpfsHash(ipfsHash)); return attributes; }) - .then((attributes) => { - let { recipientId, amount, digest, hashFunction, hashSize } = attributes; - + .then((attr) => { let proposal = [ - recipientId, - amount, - digest, - hashFunction, - hashSize, + parseInt(attr.recipientId), + parseInt(attr.amount), + attr.ipfsHash, + attr.hashFunction, + attr.hashSize, ]; - console.log('[kredits] addProposal', ...proposal); return this.contract.functions.addProposal(...proposal); });