forked from LIT-Protocol/LitNodeContracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
130 lines (115 loc) · 3.45 KB
/
utils.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const bs58 = require("bs58");
const ethers = require("ethers");
function int2ip(ipInt) {
return (
(ipInt >>> 24) +
"." +
((ipInt >> 16) & 255) +
"." +
((ipInt >> 8) & 255) +
"." +
(ipInt & 255)
);
}
function ip2int(ip) {
return (
ip.split(".").reduce(function (ipInt, octet) {
return (ipInt << 8) + parseInt(octet, 10);
}, 0) >>> 0
);
}
// the below functions are from https://github.com/saurfang/ipfs-multihash-on-solidity
/**
* @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}
*/
function getBytes32FromMultihash(multihash) {
const decoded = bs58.decode(multihash);
return {
digest: `0x${Buffer.from(decoded.slice(2)).toString("hex")}`,
hashFunction: decoded[0],
size: decoded[1],
};
}
/**
* Partition multihash string into object representing multihash
*
* @param {string} multihash A base58 encoded multihash string
* @returns {Multihash}
*/
function getBytesFromMultihash(multihash) {
const decoded = bs58.decode(multihash);
return `0x${Buffer.from(decoded).toString("hex")}`;
}
/**
* Encode a multihash structure into base58 encoded multihash string
*
* @param {Multihash} multihash
* @returns {(string|null)} base58 encoded multihash string
*/
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
*/
function parseMultihashContractResponse(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
*/
function getMultihashFromContractResponse(response) {
return getMultihashFromBytes32(parseMultihashContractResponse(response));
}
function ipfsIdToIpfsIdHash(ipfsId) {
const multihashStruct = getBytes32FromMultihash(ipfsId);
// console.log("multihashStruct", multihashStruct);
const packed = ethers.utils.solidityPack(
["bytes32", "uint8", "uint8"],
[
multihashStruct.digest,
multihashStruct.hashFunction,
multihashStruct.size,
]
);
return ethers.utils.keccak256(packed);
}
module.exports = {
int2ip,
ip2int,
getBytes32FromMultihash,
getMultihashFromBytes32,
parseMultihashContractResponse,
getMultihashFromContractResponse,
ipfsIdToIpfsIdHash,
getBytesFromMultihash,
};