forked from novasamatech/nova-wallet-metamask-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
62 lines (52 loc) · 1.43 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
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
"use strict";
import { Buffer } from "buffer";
class Utils {
static genId() {
return new Date().getTime() + Math.floor(Math.random() * 1000);
}
static flatMap(array, func) {
return [].concat(...array.map(func));
}
static intRange(from, to) {
if (from >= to) {
return [];
}
return new Array(to - from).fill().map((_, i) => i + from);
}
static hexToInt(hexString) {
if (hexString === undefined || hexString === null) {
return hexString;
}
return Number.parseInt(hexString, 16);
}
static intToHex(int) {
if (int === undefined || int === null) {
return int;
}
let hexString = int.toString(16);
return "0x" + hexString;
}
// message: Bytes | string
static messageToBuffer(message) {
var buffer = Buffer.from([]);
try {
if ((typeof (message) === "string")) {
buffer = Buffer.from(message.replace("0x", ""), "hex");
} else {
buffer = Buffer.from(message);
}
} catch (err) {
console.log(`messageToBuffer error: ${err}`);
}
return buffer;
}
static bufferToHex(buf) {
return "0x" + Buffer.from(buf).toString("hex");
}
}
module.exports = Utils;