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 { 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
};
}
}
+1 -2
View File
@@ -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) => {
+8 -11
View File
@@ -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);
});