-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.js
272 lines (218 loc) · 7.37 KB
/
common.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
* Copyright ©️ 2019 GaltProject Society Construction and Terraforming Company
* (Founded by [Nikolai Popeka](https://github.com/npopeka)
*
* Copyright ©️ 2019 Galt•Core Blockchain Company
* (Founded by [Nikolai Popeka](https://github.com/npopeka) by
* [Basic Agreement](ipfs/QmaCiXUmSrP16Gz8Jdzq6AJESY1EAANmmwha15uR3c1bsS)).
*/
const BN = require("bn.js");
const bs58 = require('bs58');
const web3Abi = require('web3-eth-abi');
const base32 = '0123456789bcdefghjkmnpqrstuvwxyz';
const base32Array = base32.split('');
const GEOHASH_MASK = new BN('0100000000000000000000000000000000000000000000000000000000000000', 16);
const PACK_MASK = new BN('0200000000000000000000000000000000000000000000000000000000000000', 16);
const Z_RESERVED_MASK = new BN('0000000000000000000000000000000ffffffffffffffffffffffffffffffff', 16);
const Z_HEIGHT_MASK = new BN('0000000000000000000000000000000ffffffff000000000000000000000000', 16);
const Z_GEOHASH5_MASK = new BN('000000000000000000000000000000000000000ffffffffffffffffffffffff', 16);
// 12 symbols each of 5 bits length = 60 bits
const limit = new BN('1 152 921 504 606 846 975');
// -2_147_483_648
const Z_MIN = -2147483648;
// 2_147_483_647
const Z_MAX = 2147483647;
let decodeMap = {};
for (let i = 0; i < base32.length; i++) {
decodeMap[base32[i]] = i;
}
/**
* Convert geohash string representation into numeric where each symbol
* encoded into 5 bits.
*
* For ex. 'qwerqwerqwer' will be converted into BN of `824642203853484471`
*
* @param {string} input geohash to encode, for ex. 'sezu06`
* @returns {BN} bignumber for given geohash
*/
function geohashToNumber(input) {
if (typeof input !== "string") {
throw new TypeError("Geohash should be a string");
}
if (input === "") {
throw new TypeError("Geohash shouldn't be empty");
}
const output = new BN('0');
for (let i = 0; i < input.length; i++) {
let v = input[i];
if (base32Array.indexOf(v) === -1) {
throw new RangeError(`Character '${v}' no allowed`);
}
output.ior(new BN(decodeMap[v]));
if (i !== input.length - 1) {
output.ishln(5);
}
}
return output;
}
/**
* Convert a numerical representation of geohash into a string one.
*
* For ex. '824642203853484471' will be converted into `qwerqwerqwer`
*
* @param {string} input bignumber as a string
* @returns {string} geohash
*/
function numberToGeohash(input) {
const num = new BN(input.toString(10));
if (num.gt(limit)) {
throw new Error("Geohash number is greater than limit");
}
const output = [];
const fiveBits = new BN(31);
while (!num.isZero()) {
// get right 5 bytes
const d = num.and(fiveBits);
output.push(base32[d]);
num.ishrn(5);
}
output.reverse();
return output.join('');
}
/**
* Convert geohash string representation into numeric with toString(10) calling.
* @param geohash
* @returns {string}
*/
function geohashToGeohash5(geohash) {
return geohashToNumber(geohash).toString(10);
}
function geohash5ToTokenId(geohash) {
return (new BN(geohash.toString(10))).xor(GEOHASH_MASK).toString(10);
}
function geohashToTokenId(geohash) {
return geohash5ToTokenId(geohashToGeohash5(geohash));
}
function geohashToTokenIdHex(geohash) {
return tokenIdToHex(geohashToTokenId(geohash));
}
function tokenIdToGeohash5(tokenId) {
return (new BN(tokenId.toString(10))).xor(GEOHASH_MASK).toString(10);
}
function tokenIdToGeohash(tokenId) {
return numberToGeohash(tokenIdToGeohash5(tokenId));
}
function tokenIdHexToTokenId(tokenIdHex) {
return new BN(tokenIdHex.replace("0x", ""), 16).toString(10);
}
function tokenIdHexToGeohash5(tokenIdHex) {
return tokenIdToGeohash5(tokenIdHexToTokenId(tokenIdHex));
}
function tokenIdHexToGeohash(tokenIdHex) {
return numberToGeohash(tokenIdHexToGeohash5(tokenIdHex));
}
function tokenIdToHex(tokenId) {
const geohash16 = (new BN(tokenId.toString(10))).toString(16);
let hex;
if(geohash16.length === 64) {
hex = geohash16;
} else {
hex = "0".repeat(64 - geohash16.length) + geohash16;
}
return "0x" + hex;
}
function isPack(tokenId) {
return (new BN(tokenId.toString(10))).and(PACK_MASK).eq(PACK_MASK);
}
function isGeohash(tokenId) {
return (new BN(tokenId.toString(10))).and(GEOHASH_MASK).eq(GEOHASH_MASK);
}
// https://ethereum.stackexchange.com/a/39961/20417
function ipfsHashToBytes32(ipfsHash) {
if (typeof ipfsHash !== "string") {
throw new TypeError("IPFS hash should be a string");
}
if (ipfsHash === "") {
throw new TypeError("IPFS hash shouldn't be empty");
}
if (ipfsHash.length !== 46) {
throw new TypeError("IPFS hash should have exactly 46 symbols");
}
if (!ipfsHash.startsWith("Qm")) {
throw new TypeError("IPFS hash should start with 'Qm'");
}
const bytes = bs58.decode(ipfsHash);
const hexString = bytes.toString("hex");
return "0x" + hexString.substr(4);
}
function bytes32ToIpfsHash(bytes32) {
if (typeof bytes32 !== "string") {
throw new TypeError("bytes32 should be a string");
}
if (bytes32 === "") {
throw new TypeError("bytes32 shouldn't be empty");
}
if (bytes32.length !== 66) {
throw new TypeError("bytes32 should have exactly 66 symbols (with 0x)");
}
if (!(bytes32.startsWith("0x") || bytes32.startsWith("0X"))) {
throw new TypeError("bytes32 hash should start with '0x'");
}
const hexString = "1220" + bytes32.substr(2);
const bytes = Buffer.from(hexString, 'hex');
return bs58.encode(bytes);
}
function roundToDecimal(number, decimal = 4) {
return Math.ceil(number * Math.pow(10, decimal)) / Math.pow(10, decimal);
}
function geohash5ToGeohash5z(height, geohash5) {
if ((typeof height !== "number") && (typeof height !== "string")) {
throw new TypeError("height should be either a number or a string");
}
height = parseInt(height, 10);
requireHeightValid(height);
if (typeof geohash5 !== "string") {
throw new TypeError("geohash5 should be a stringifed number");
}
if (geohash5 === "") {
throw new TypeError("geohash5 shouldn't be empty");
}
height = web3Abi.decodeParameter('uint256', web3Abi.encodeParameter('int256', height));
height = (new BN(height)).ishln(96);
geohash5 = (new BN(geohash5));
return (geohash5.or(height)).and(Z_RESERVED_MASK);
}
function geohash5zToGeohash5(geohash5z) {
geohash5z = new BN(geohash5z);
const height = geohash5z.and(Z_HEIGHT_MASK).shrn(96);
const geohash5 = geohash5z.and(Z_GEOHASH5_MASK);
const encoded = web3Abi.encodeParameter('uint256', height.toString(10));
const decoded = web3Abi.decodeParameter('int32', encoded);
return { height: parseInt(decoded, 10), geohash5 };
}
function requireHeightValid(height) {
if (!(Z_MIN <= height && height <= Z_MAX)) {
throw new RangeError("Height overflow");
}
}
module.exports = {
geohashToNumber,
numberToGeohash,
geohashToGeohash5,
geohash5ToTokenId,
geohashToTokenId,
geohashToTokenIdHex,
geohash5ToGeohash5z,
geohash5zToGeohash5,
tokenIdToGeohash5,
tokenIdToGeohash,
tokenIdHexToGeohash5,
tokenIdHexToGeohash,
tokenIdToHex,
tokenIdHexToTokenId,
ipfsHashToBytes32,
bytes32ToIpfsHash,
isPack,
isGeohash,
roundToDecimal
};