UNPKG

3.05 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.hexUtils = void 0;
4const crypto = require("crypto");
5const ethUtil = require("ethereumjs-util");
6const configured_bignumber_1 = require("./configured_bignumber");
7// tslint:disable:custom-no-magic-numbers
8const WORD_LENGTH = 32;
9const WORD_CEIL = new configured_bignumber_1.BigNumber(2).pow(WORD_LENGTH * 8);
10exports.hexUtils = {
11 concat,
12 random,
13 leftPad,
14 rightPad,
15 invert,
16 slice,
17 hash,
18 size,
19 toHex,
20 isHex,
21};
22/**
23 * Concatenate all arguments as a hex string.
24 */
25function concat(...args) {
26 return ethUtil.bufferToHex(Buffer.concat(args.map(h => ethUtil.toBuffer(h))));
27}
28/**
29 * Generate a random hex string.
30 */
31function random(_size = WORD_LENGTH) {
32 return ethUtil.bufferToHex(crypto.randomBytes(_size));
33}
34/**
35 * Left-pad a hex number to a number of bytes.
36 */
37function leftPad(n, _size = WORD_LENGTH) {
38 return ethUtil.bufferToHex(ethUtil.setLengthLeft(ethUtil.toBuffer(exports.hexUtils.toHex(n)), _size));
39}
40/**
41 * Right-pad a hex number to a number of bytes.
42 */
43function rightPad(n, _size = WORD_LENGTH) {
44 return ethUtil.bufferToHex(ethUtil.setLengthRight(ethUtil.toBuffer(exports.hexUtils.toHex(n)), _size));
45}
46/**
47 * Inverts a hex word.
48 */
49function invert(n, _size = WORD_LENGTH) {
50 const buf = ethUtil.setLengthLeft(ethUtil.toBuffer(exports.hexUtils.toHex(n)), _size);
51 // tslint:disable-next-line: no-bitwise
52 return ethUtil.bufferToHex(Buffer.from(buf.map(b => ~b)));
53}
54/**
55 * Slices a hex number.
56 */
57function slice(n, start, end) {
58 const hex = exports.hexUtils.toHex(n).substr(2);
59 const sliceStart = start >= 0 ? start * 2 : Math.max(0, hex.length + start * 2);
60 let sliceEnd = hex.length;
61 if (end !== undefined) {
62 sliceEnd = end >= 0 ? end * 2 : Math.max(0, hex.length + end * 2);
63 }
64 return '0x'.concat(hex.substring(sliceStart, sliceEnd));
65}
66/**
67 * Get the keccak hash of some data.
68 */
69function hash(n) {
70 const buf = Buffer.isBuffer(n) ? n : ethUtil.toBuffer(exports.hexUtils.toHex(n));
71 return ethUtil.bufferToHex(ethUtil.keccak256(buf));
72}
73/**
74 * Get the length, in bytes, of a hex string.
75 */
76function size(hex) {
77 return Math.ceil((hex.length - 2) / 2);
78}
79/**
80 * Convert a string, a number, a Buffer, or a BigNumber into a hex string.
81 * Works with negative numbers, as well.
82 */
83function toHex(n, _size = WORD_LENGTH) {
84 if (Buffer.isBuffer(n)) {
85 return `0x${n.toString('hex')}`;
86 }
87 if (typeof n === 'string' && isHex(n)) {
88 // Already a hex.
89 return n;
90 }
91 let _n = new configured_bignumber_1.BigNumber(n);
92 if (_n.isNegative()) {
93 // Perform two's-complement.
94 // prettier-ignore
95 _n = new configured_bignumber_1.BigNumber(invert(toHex(_n.abs()), _size).substr(2), 16).plus(1).mod(WORD_CEIL);
96 }
97 return `0x${_n.toString(16)}`;
98}
99/**
100 * Check if a string is a hex string.
101 */
102function isHex(s) {
103 return /^0x[0-9a-f]*$/i.test(s);
104}
105//# sourceMappingURL=hex_utils.js.map
\No newline at end of file