Refactor IPFS hash handling

This now uses the multihashes dependency (which is an ipfs dependency)
to decode/encode the ipfs hashes.
This commit is contained in:
2018-04-09 14:52:39 +02:00
parent e527099b00
commit 41a04d6b53
3 changed files with 20 additions and 24 deletions
+11 -11
View File
@@ -1,5 +1,5 @@
import Kredits from '../kredits'; import Kredits from '../kredits';
import { fromBytes32 } from '../utils/multihash'; import multihashes from 'npm:multihashes';
export default class Base { export default class Base {
constructor(contract) { constructor(contract) {
@@ -12,11 +12,11 @@ export default class Base {
// TODO: move into utils // TODO: move into utils
fetchAndMergeIpfsData(data, Serializer) { fetchAndMergeIpfsData(data, Serializer) {
let ipfsHash = data.ipfsHash; if (!data.hashSize || data.hashSize === 0) {
if (!ipfsHash) {
return data; 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 return Kredits.ipfs
.cat(ipfsHash) .cat(ipfsHash)
@@ -26,12 +26,12 @@ export default class Base {
}); });
} }
reassembleIpfsHash(data) { decodeIpfsHash(ipfsHash) {
let { ipfsHash: digest, hashFunction, hashSize } = data; let multihash = multihashes.decode(multihashes.fromB58String(ipfsHash));
data.ipfsHash = fromBytes32({ digest, hashFunction, hashSize }); return {
delete data.hashFunction; ipfsHash: '0x' + multihashes.toHexString(multihash.digest),
delete data.hashSize; hashSize: multihash.length,
hashFunction: multihash.code
return data; };
} }
} }
+1 -2
View File
@@ -26,7 +26,6 @@ export default class Contributor extends Base {
id = ethers.utils.bigNumberify(id); id = ethers.utils.bigNumberify(id);
return this.contract.functions.getContributorById(id) return this.contract.functions.getContributorById(id)
.then(this.reassembleIpfsHash)
.then((data) => { .then((data) => {
// TODO: remove as soon as the contract provides the id // TODO: remove as soon as the contract provides the id
data.id = id; data.id = id;
@@ -51,7 +50,7 @@ export default class Contributor extends Base {
.add(new Kredits.ipfs.Buffer(json)) .add(new Kredits.ipfs.Buffer(json))
.then((res) => res[0].hash) .then((res) => res[0].hash)
.then((ipfsHash) => { .then((ipfsHash) => {
Object.assign(attributes, toBytes32(ipfsHash)); Object.assign(attributes, this.decodeIpfsHash(ipfsHash));
return attributes; return attributes;
}) })
.then((attributes) => { .then((attributes) => {
+8 -11
View File
@@ -1,5 +1,6 @@
import ethers from 'npm:ethers'; import ethers from 'npm:ethers';
import RSVP from 'rsvp'; import RSVP from 'rsvp';
import multihashes from 'npm:multihashes';
import Kredits from '../kredits'; import Kredits from '../kredits';
import ContributionSerializer from '../serializers/contribution'; import ContributionSerializer from '../serializers/contribution';
@@ -26,7 +27,6 @@ export default class Operator extends Base {
id = ethers.utils.bigNumberify(id); id = ethers.utils.bigNumberify(id);
return this.contract.functions.proposals(id) return this.contract.functions.proposals(id)
.then(this.reassembleIpfsHash)
.then((data) => { .then((data) => {
// TODO: remove as soon as the contract provides the id // TODO: remove as soon as the contract provides the id
data.id = id; data.id = id;
@@ -51,20 +51,17 @@ export default class Operator extends Base {
.add(new Kredits.ipfs.Buffer(json)) .add(new Kredits.ipfs.Buffer(json))
.then((res) => res[0].hash) .then((res) => res[0].hash)
.then((ipfsHash) => { .then((ipfsHash) => {
Object.assign(attributes, toBytes32(ipfsHash)); Object.assign(attributes, this.decodeIpfsHash(ipfsHash));
return attributes; return attributes;
}) })
.then((attributes) => { .then((attr) => {
let { recipientId, amount, digest, hashFunction, hashSize } = attributes;
let proposal = [ let proposal = [
recipientId, parseInt(attr.recipientId),
amount, parseInt(attr.amount),
digest, attr.ipfsHash,
hashFunction, attr.hashFunction,
hashSize, attr.hashSize,
]; ];
console.log('[kredits] addProposal', ...proposal); console.log('[kredits] addProposal', ...proposal);
return this.contract.functions.addProposal(...proposal); return this.contract.functions.addProposal(...proposal);
}); });