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