-
Notifications
You must be signed in to change notification settings - Fork 31
/
multihash.js
71 lines (62 loc) · 1.96 KB
/
multihash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import bs58 from 'bs58';
/**
* @typedef {Object} Multihash
* @property {string} digest The digest output of hash function in hex with prepended '0x'
* @property {number} hashFunction The hash function code for the function used
* @property {number} size The length of digest
*/
/**
* Partition multihash string into object representing multihash
*
* @param {string} multihash A base58 encoded multihash string
* @returns {Multihash}
*/
export function getBytes32FromMultiash(multihash) {
const decoded = bs58.decode(multihash);
return {
digest: `0x${decoded.slice(2).toString('hex')}`,
hashFunction: decoded[0],
size: decoded[1],
};
}
/**
* Encode a multihash structure into base58 encoded multihash string
*
* @param {Multihash} multihash
* @returns {(string|null)} base58 encoded multihash string
*/
export 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);
multihashBytes[0] = hashFunction;
multihashBytes[1] = size;
multihashBytes.set(hashBytes, 2);
return bs58.encode(multihashBytes);
}
/**
* Parse Solidity response in array to a Multihash object
*
* @param {array} response Response array from Solidity
* @returns {Multihash} multihash object
*/
export function parseContractResponse(response) {
const [digest, hashFunction, size] = response;
return {
digest,
hashFunction: hashFunction.toNumber(),
size: size.toNumber(),
};
}
/**
* Parse Solidity response in array to a base58 encoded multihash string
*
* @param {array} response Response array from Solidity
* @returns {string} base58 encoded multihash string
*/
export function getMultihashFromContractResponse(response) {
return getMultihashFromBytes32(parseContractResponse(response));
}