noob-ethereum
Version:
A simple Ethereum library
92 lines (91 loc) • 2.87 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.hexZeroPad = exports.minutes = exports.gweiToEther = exports.toGwei = exports.toISO = exports.decimal = exports.hexify = void 0;
/**
* Convert hex string/Buffer to 0x-prefixed hex string OR convert decimal number to 0x-prefixed hex string
* @param {string | Buffer | number} exp - hex string (e.g. 'a2'), hex Buffer, decimal number (e.g. 22)
* @returns {string} - 0x-prefixed hex string (e.g. 'ab' -> '0xab', 22 -> '0x16')
*/
function hexify(exp) {
if (Buffer.isBuffer(exp)) {
return '0x' + exp.toString('hex');
}
if (typeof exp === 'number') {
return '0x' + exp.toString(16);
}
return '0x' + exp;
}
exports.hexify = hexify;
/**
* Convert hex string/Buffer to decimal number
* @param {string | Buffer | number} exp - hex string (e.g. '02ab' or with 0x-prefix '0x02ab), hex Buffer (e.g. <Buffer 02 ab>)
* @returns {string} - decimal number (e.g. '02ab' -> 683, '0x02ab' -> 683, <Buffer 02 ab> -> 683)
*/
function decimal(exp) {
if (Buffer.isBuffer(exp)) {
return exp.readInt16BE(0);
}
return parseInt(exp, 16);
}
exports.decimal = decimal;
/**
* Convert UNIX timestamp to ISO string including date
* @param epoch - UNIX timestamp (e.g. 1634473161)
* @returns {string} - "2022-03-31T10:58:13.000Z"
*/
function toISO(epoch) {
return new Date(epoch * 1000).toISOString();
}
exports.toISO = toISO;
function gweiToEther(gwei) {
return gwei * Math.pow(10, -9);
}
exports.gweiToEther = gweiToEther;
function toGwei(exp, from) {
if (typeof exp === 'undefined')
return null;
switch (from) {
case 'wei':
default: {
if (typeof exp === 'number') {
return exp / 10 ** 9;
}
else {
const wei = parseInt(exp, 16);
return wei / 10 ** 9;
}
}
case 'ether': {
if (typeof exp === 'number') {
return exp * 10 ** 9;
}
else {
const ether = parseInt(exp, 16);
return ether * 10 ** 9;
}
}
}
}
exports.toGwei = toGwei;
function minutes(ms) {
const m = Math.floor(ms / 60000);
const s = +((ms % 60000) / 1000).toFixed(0);
return `${m}:${s < 10 ? '0' : ''}${s}`;
}
exports.minutes = minutes;
function hexZeroPad(value, length) {
if (typeof value !== 'string') {
value = hexify(value);
}
else if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/)) {
throw new Error('invalid hex string');
}
if (value.length > 2 * length + 2) {
throw new Error('value out of range');
}
while (value.length < 2 * length + 2) {
value = '0x0' + value.substring(2);
}
return value;
}
exports.hexZeroPad = hexZeroPad;
;