contracts/scripts/multihash.js
bumi 78b6b2e14f Add seeds script
Allows to seed the smart contract with development data specified in
config/seeds.js

Usage:

truffle exec scripts/seeds.js

or

npm run bootstrap which runs them all:
* truffle migrate --reset
* truffle exec scripts/seeds.js
* npm run build-json
2018-04-02 13:25:03 +02:00

36 lines
881 B
JavaScript

var bs58 = require('bs58');
function getBytes32FromMultiash(multihash) {
const decoded = bs58.decode(multihash);
return {
digest: `0x${decoded.slice(2).toString('hex')}`,
hashFunction: decoded[0],
size: decoded[1],
};
}
function getMultihashFromBytes32(multihash) {
const { digest, hashFunction, size } = multihash;
if (size === 0) return null;
// cut off leading "0x"
const hashBytes = Buffer.from(digest.slice(2), 'hex');
// prepend hashFunction and digest size
//const multihashBytes = new (hashBytes.constructor)(2 + hashBytes.length);
const multihashBytes = new Buffer(2 + hashBytes.length);
console.log(hashBytes.constructor);
multihashBytes[0] = hashFunction;
multihashBytes[1] = size;
multihashBytes.set(hashBytes, 2);
return bs58.encode(multihashBytes);
}
var m = getBytes32FromMultiash(process.argv[2]);
console.log(m)