1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | Object.defineProperty(exports, "__esModule", { value: true });
|
26 | exports.isHexString = exports.getKeys = exports.fromAscii = exports.fromUtf8 = exports.toAscii = exports.arrayContainsArray = exports.getBinarySize = exports.padToEven = exports.stripHexPrefix = exports.isHexPrefixed = void 0;
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function isHexPrefixed(str) {
|
34 | if (typeof str !== 'string') {
|
35 | throw new Error(`[isHexPrefixed] input must be type 'string', received type ${typeof str}`);
|
36 | }
|
37 | return str[0] === '0' && str[1] === 'x';
|
38 | }
|
39 | exports.isHexPrefixed = isHexPrefixed;
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | const stripHexPrefix = (str) => {
|
46 | if (typeof str !== 'string')
|
47 | throw new Error(`[stripHexPrefix] input must be type 'string', received ${typeof str}`);
|
48 | return isHexPrefixed(str) ? str.slice(2) : str;
|
49 | };
|
50 | exports.stripHexPrefix = stripHexPrefix;
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | function padToEven(value) {
|
57 | let a = value;
|
58 | if (typeof a !== 'string') {
|
59 | throw new Error(`[padToEven] value must be type 'string', received ${typeof a}`);
|
60 | }
|
61 | if (a.length % 2)
|
62 | a = `0${a}`;
|
63 | return a;
|
64 | }
|
65 | exports.padToEven = padToEven;
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | function getBinarySize(str) {
|
72 | if (typeof str !== 'string') {
|
73 | throw new Error(`[getBinarySize] method requires input type 'string', recieved ${typeof str}`);
|
74 | }
|
75 | return Buffer.byteLength(str, 'utf8');
|
76 | }
|
77 | exports.getBinarySize = getBinarySize;
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function arrayContainsArray(superset, subset, some) {
|
87 | if (Array.isArray(superset) !== true) {
|
88 | throw new Error(`[arrayContainsArray] method requires input 'superset' to be an array, got type '${typeof superset}'`);
|
89 | }
|
90 | if (Array.isArray(subset) !== true) {
|
91 | throw new Error(`[arrayContainsArray] method requires input 'subset' to be an array, got type '${typeof subset}'`);
|
92 | }
|
93 | return subset[some ? 'some' : 'every']((value) => superset.indexOf(value) >= 0);
|
94 | }
|
95 | exports.arrayContainsArray = arrayContainsArray;
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | function toAscii(hex) {
|
103 | let str = '';
|
104 | let i = 0;
|
105 | const l = hex.length;
|
106 | if (hex.substring(0, 2) === '0x')
|
107 | i = 2;
|
108 | for (; i < l; i += 2) {
|
109 | const code = parseInt(hex.substr(i, 2), 16);
|
110 | str += String.fromCharCode(code);
|
111 | }
|
112 | return str;
|
113 | }
|
114 | exports.toAscii = toAscii;
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | function fromUtf8(stringValue) {
|
123 | const str = Buffer.from(stringValue, 'utf8');
|
124 | return `0x${padToEven(str.toString('hex')).replace(/^0+|0+$/g, '')}`;
|
125 | }
|
126 | exports.fromUtf8 = fromUtf8;
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 | function fromAscii(stringValue) {
|
135 | let hex = '';
|
136 | for (let i = 0; i < stringValue.length; i++) {
|
137 | const code = stringValue.charCodeAt(i);
|
138 | const n = code.toString(16);
|
139 | hex += n.length < 2 ? `0${n}` : n;
|
140 | }
|
141 | return `0x${hex}`;
|
142 | }
|
143 | exports.fromAscii = fromAscii;
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | function getKeys(params, key, allowEmpty) {
|
156 | if (!Array.isArray(params)) {
|
157 | throw new Error(`[getKeys] method expects input 'params' to be an array, got ${typeof params}`);
|
158 | }
|
159 | if (typeof key !== 'string') {
|
160 | throw new Error(`[getKeys] method expects input 'key' to be type 'string', got ${typeof params}`);
|
161 | }
|
162 | const result = [];
|
163 | for (let i = 0; i < params.length; i++) {
|
164 | let value = params[i][key];
|
165 | if (allowEmpty && !value) {
|
166 | value = '';
|
167 | }
|
168 | else if (typeof value !== 'string') {
|
169 | throw new Error(`invalid abi - expected type 'string', received ${typeof value}`);
|
170 | }
|
171 | result.push(value);
|
172 | }
|
173 | return result;
|
174 | }
|
175 | exports.getKeys = getKeys;
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 | function isHexString(value, length) {
|
184 | if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/))
|
185 | return false;
|
186 | if (length && value.length !== 2 + 2 * length)
|
187 | return false;
|
188 | return true;
|
189 | }
|
190 | exports.isHexString = isHexString;
|
191 |
|
\ | No newline at end of file |