-
Notifications
You must be signed in to change notification settings - Fork 0
IPFS
Timothy Wang edited this page Jun 22, 2018
·
2 revisions
IPFS uses MultiHash to describe file paths
Currently, they are using sha-256, 32 bytes for their multihash, from which we can get the actual hash using
export function getBytes32FromMultiash(multihash) {
const decoded = bs58.decode(multihash);
return {
digest: `0x${decoded.slice(2).toString('hex')}`,
hashFunction: decoded[0],
size: decoded[1],
};
}where the digest part is the hash.
To convert bytes32 to multihash, apply
export function getMultihashFromBytes32(digest) {
const hashFunction = 18;
const size = 32;
// 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);
multihashBytes[0] = hashFunction;
multihashBytes[1] = size;
multihashBytes.set(hashBytes, 2);
return bs58.encode(multihashBytes);
}See this file for details.
Since the hash function and the size stay unchanged, we only need to store the digest part in a bytes32.