wallet/Safari Shared/Inpage Provider/utils.js
2024-01-23 14:02:43 +03:00

60 lines
1.4 KiB
JavaScript

// ∅ 2024 lil org
// Rewrite of utils.js from trust-web3-provider.
"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;