Extract multihash functions

This commit was merged in pull request #33.
This commit is contained in:
2018-04-07 19:24:45 +02:00
committed by Michael Bumann
parent 939baec1a8
commit 6e262537a4
3 changed files with 48 additions and 45 deletions
+4 -1
View File
@@ -1,5 +1,8 @@
import ContributorSerializer from './serializers/contributor'; import ContributorSerializer from './serializers/contributor';
import { fromBytes32, toBytes32 } from './utils/multihash';
export { export {
ContributorSerializer ContributorSerializer,
fromBytes32,
toBytes32
}; };
+33
View File
@@ -0,0 +1,33 @@
import bs58 from 'npm:bs58';
import NpmBuffer from 'npm:buffer';
const Buffer = NpmBuffer.Buffer;
function fromBytes32({ digest, hashFunction, hashSize }) {
if (hashSize === 0) {
return;
}
const hashBytes = Buffer.from(digest.slice(2), 'hex');
const multiHashBytes = new (hashBytes.constructor)(2 + hashBytes.length);
multiHashBytes[0] = hashFunction;
multiHashBytes[1] = hashSize;
multiHashBytes.set(hashBytes, 2);
return bs58.encode(multiHashBytes);
}
function toBytes32(string) {
const decoded = bs58.decode(string);
return {
digest: `0x${decoded.slice(2).toString('hex')}`,
hashFunction: decoded[0],
hashSize: decoded[1],
};
}
export {
fromBytes32,
toBytes32
};
+11 -44
View File
@@ -1,6 +1,4 @@
import ethers from 'npm:ethers'; import ethers from 'npm:ethers';
import bs58 from 'npm:bs58';
import NpmBuffer from 'npm:buffer';
import RSVP from 'rsvp'; import RSVP from 'rsvp';
import Ember from 'ember'; import Ember from 'ember';
@@ -14,7 +12,11 @@ import Proposal from 'kredits-web/models/proposal';
import abis from 'contracts/abis'; import abis from 'contracts/abis';
import addresses from 'contracts/addresses'; import addresses from 'contracts/addresses';
import { ContributorSerializer } from 'kredits-web/lib/kredits'; import {
ContributorSerializer,
fromBytes32,
toBytes32
} from 'kredits-web/lib/kredits';
const { const {
getOwner, getOwner,
@@ -25,7 +27,6 @@ const {
} }
} = Ember; } = Ember;
const Buffer = NpmBuffer.Buffer;
export default Service.extend({ export default Service.extend({
@@ -127,11 +128,7 @@ export default Service.extend({
}) => { }) => {
let isCurrentUser = this.get('currentUserAccounts').includes(address); let isCurrentUser = this.get('currentUserAccounts').includes(address);
let profileHash = this.getMultihashFromBytes32({ let profileHash = fromBytes32({ digest, hashFunction, hashSize });
digest,
hashFunction,
size: hashSize
});
return { return {
id, id,
@@ -206,7 +203,8 @@ export default Service.extend({
return this.get('kreditsContract') return this.get('kreditsContract')
.then((contract) => contract.proposals(i)) .then((contract) => contract.proposals(i))
.then(p => { .then(p => {
let contributionIpfsHash = this.getMultihashFromBytes32({ digest: p.ipfsHash, hashFunction: p.hashFunction, size: p.hashSize }); let { ipfsHash: digest, hashFunction, hashSize } = p;
let ipfsHash = fromBytes32({ digest, hashFunction, hashSize });
let proposal = Proposal.create({ let proposal = Proposal.create({
id : i, id : i,
@@ -216,7 +214,7 @@ export default Service.extend({
votesNeeded : p.votesNeeded.toNumber(), votesNeeded : p.votesNeeded.toNumber(),
amount : p.amount.toNumber(), amount : p.amount.toNumber(),
executed : p.executed, executed : p.executed,
ipfsHash : contributionIpfsHash ipfsHash
}); });
if (proposal.get('ipfsHash')) { if (proposal.get('ipfsHash')) {
@@ -256,35 +254,6 @@ export default Service.extend({
}); });
}, },
// TODO: move into utils
getMultihashFromBytes32(multihash) {
const { digest, hashFunction, size } = multihash;
if (size === 0) {
return;
}
const hashBytes = Buffer.from(digest.slice(2), 'hex');
const multiHashBytes = new (hashBytes.constructor)(2 + hashBytes.length);
multiHashBytes[0] = hashFunction; //contributorData[2];
multiHashBytes[1] = size; //contributorData[3];
multiHashBytes.set(hashBytes, 2);
return bs58.encode(multiHashBytes);
},
getBytes32FromMultihash(multihash) {
const decoded = bs58.decode(multihash);
return {
digest: `0x${decoded.slice(2).toString('hex')}`,
hashFunction: decoded[0],
size: decoded[1],
};
},
// TODO: extract common logic to module // TODO: extract common logic to module
addContributor(attributes) { addContributor(attributes) {
debug('[kredits] add contributor', attributes); debug('[kredits] add contributor', attributes);
@@ -302,15 +271,13 @@ export default Service.extend({
return this.get('kreditsContract') return this.get('kreditsContract')
.then((contract) => { .then((contract) => {
let { address, isCore, profileHash } = attributes; let { address, isCore, profileHash } = attributes;
let { let { digest, hashFunction, hashSize } = toBytes32(profileHash);
digest, hashFunction, size
} = this.getBytes32FromMultihash(profileHash);
let contributor = [ let contributor = [
address, address,
digest, digest,
hashFunction, hashFunction,
size, hashSize,
isCore, isCore,
]; ];
debug('[kredits] addContributor', ...contributor); debug('[kredits] addContributor', ...contributor);