forked from ethers-io/ethers.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes.ts
More file actions
323 lines (251 loc) · 8.73 KB
/
Copy pathbytes.ts
File metadata and controls
323 lines (251 loc) · 8.73 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* Conversion Utilities
*
*/
import * as errors from '../errors';
///////////////////////////////
// Imported Types
import { Arrayish } from './bytes';
///////////////////////////////
// Exported Types
export type Arrayish = string | ArrayLike<number>;
export interface Hexable {
toHexString(): string;
}
export interface Signature {
r: string;
s: string;
/* At least one of the following MUST be specified; the other will be derived */
recoveryParam?: number;
v?: number;
}
///////////////////////////////
export function isHexable(value: any): value is Hexable {
return !!(value.toHexString);
}
function addSlice(array: Uint8Array): Uint8Array {
if (array.slice) { return array; }
array.slice = function() {
var args = Array.prototype.slice.call(arguments);
return addSlice(new Uint8Array(Array.prototype.slice.apply(array, args)));
}
return array;
}
export function isArrayish(value: any): value is Arrayish {
if (!value || parseInt(String(value.length)) != value.length || typeof(value) === 'string') {
return false;
}
for (var i = 0; i < value.length; i++) {
var v = value[i];
if (v < 0 || v >= 256 || parseInt(String(v)) != v) {
return false;
}
}
return true;
}
export function arrayify(value: Arrayish | Hexable): Uint8Array {
if (value == null) {
errors.throwError('cannot convert null value to array', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
}
if (isHexable(value)) {
value = value.toHexString();
}
if (typeof(value) === 'string') {
let match = value.match(/^(0x)?[0-9a-fA-F]*$/);
if (!match) {
errors.throwError('invalid hexidecimal string', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
}
if (match[1] !== '0x') {
errors.throwError('hex string must have 0x prefix', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
}
value = value.substring(2);
if (value.length % 2) { value = '0' + value; }
var result = [];
for (var i = 0; i < value.length; i += 2) {
result.push(parseInt(value.substr(i, 2), 16));
}
return addSlice(new Uint8Array(result));
}
if (isArrayish(value)) {
return addSlice(new Uint8Array(value));
}
errors.throwError('invalid arrayify value', null, { arg: 'value', value: value, type: typeof(value) });
return null;
}
export function concat(objects: Array<Arrayish>): Uint8Array {
var arrays = [];
var length = 0;
for (var i = 0; i < objects.length; i++) {
var object = arrayify(objects[i])
arrays.push(object);
length += object.length;
}
var result = new Uint8Array(length);
var offset = 0;
for (var i = 0; i < arrays.length; i++) {
result.set(arrays[i], offset);
offset += arrays[i].length;
}
return addSlice(result);
}
export function stripZeros(value: Arrayish): Uint8Array {
let result: Uint8Array = arrayify(value);
if (result.length === 0) { return result; }
// Find the first non-zero entry
var start = 0;
while (result[start] === 0) { start++ }
// If we started with zeros, strip them
if (start) {
result = result.slice(start);
}
return result;
}
export function padZeros(value: Arrayish, length: number): Uint8Array {
value = arrayify(value);
if (length < value.length) { throw new Error('cannot pad'); }
var result = new Uint8Array(length);
result.set(value, length - value.length);
return addSlice(result);
}
export function isHexString(value: any, length?: number): boolean {
if (typeof(value) !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/)) {
return false
}
if (length && value.length !== 2 + 2 * length) { return false; }
return true;
}
const HexCharacters: string = '0123456789abcdef';
export function hexlify(value: Arrayish | Hexable | number): string {
if (isHexable(value)) {
return value.toHexString();
}
if (typeof(value) === 'number') {
if (value < 0) {
errors.throwError('cannot hexlify negative value', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
}
// @TODO: Roll this into the above error as a numeric fault (overflow); next version, not backward compatible
// We can about (value == MAX_INT) to as well, since that may indicate we underflowed already
if (value >= 9007199254740991) {
errors.throwError("out-of-range", errors.NUMERIC_FAULT, {
operartion: "hexlify",
fault: "out-of-safe-range"
});
}
var hex = '';
while (value) {
hex = HexCharacters[value & 0x0f] + hex;
value = Math.floor(value / 16);
}
if (hex.length) {
if (hex.length % 2) { hex = '0' + hex; }
return '0x' + hex;
}
return '0x00';
}
if (typeof(value) === 'string') {
let match = value.match(/^(0x)?[0-9a-fA-F]*$/);
if (!match) {
errors.throwError('invalid hexidecimal string', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
}
if (match[1] !== '0x') {
errors.throwError('hex string must have 0x prefix', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
}
if (value.length % 2) {
value = '0x0' + value.substring(2);
}
return value;
}
if (isArrayish(value)) {
var result = [];
for (var i = 0; i < value.length; i++) {
var v = value[i];
result.push(HexCharacters[(v & 0xf0) >> 4] + HexCharacters[v & 0x0f]);
}
return '0x' + result.join('');
}
errors.throwError('invalid hexlify value', null, { arg: 'value', value: value });
return 'never';
}
export function hexDataLength(data: string) {
if (!isHexString(data) || (data.length % 2) !== 0) {
return null;
}
return (data.length - 2) / 2;
}
export function hexDataSlice(data: string, offset: number, endOffset?: number): string {
if (!isHexString(data)) {
errors.throwError('invalid hex data', errors.INVALID_ARGUMENT, { arg: 'value', value: data });
}
if ((data.length % 2) !== 0) {
errors.throwError('hex data length must be even', errors.INVALID_ARGUMENT, { arg: 'value', value: data });
}
offset = 2 + 2 * offset;
if (endOffset != null) {
return '0x' + data.substring(offset, 2 + 2 * endOffset);
}
return '0x' + data.substring(offset);
}
export function hexStripZeros(value: string): string {
if (!isHexString(value)) {
errors.throwError('invalid hex string', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
}
while (value.length > 3 && value.substring(0, 3) === '0x0') {
value = '0x' + value.substring(3);
}
return value;
}
export function hexZeroPad(value: string, length: number): string {
if (!isHexString(value)) {
errors.throwError('invalid hex string', errors.INVALID_ARGUMENT, { arg: 'value', value: value });
}
while (value.length < 2 * length + 2) {
value = '0x0' + value.substring(2);
}
return value;
}
function isSignature(value: any): value is Signature {
return (value && value.r != null && value.s != null);
}
export function splitSignature(signature: Arrayish | Signature): Signature {
let v = 0;
let r = '0x', s = '0x';
if (isSignature(signature)) {
if (signature.v == null && signature.recoveryParam == null) {
errors.throwError('at least on of recoveryParam or v must be specified', errors.INVALID_ARGUMENT, { argument: 'signature', value: signature });
}
r = hexZeroPad(signature.r, 32);
s = hexZeroPad(signature.s, 32);
v = signature.v;
if (typeof(v) === 'string') { v = parseInt(v, 16); }
let recoveryParam = signature.recoveryParam;
if (recoveryParam == null && signature.v != null) {
recoveryParam = 1 - (v % 2);
}
v = 27 + recoveryParam;
} else {
let bytes: Uint8Array = arrayify(signature);
if (bytes.length !== 65) {
throw new Error('invalid signature');
}
r = hexlify(bytes.slice(0, 32));
s = hexlify(bytes.slice(32, 64));
v = bytes[64];
if (v !== 27 && v !== 28) {
v = 27 + (v % 2);
}
}
return {
r: r,
s: s,
recoveryParam: (v - 27),
v: v
}
}
export function joinSignature(signature: Signature): string {
signature = splitSignature(signature);
return hexlify(concat([
signature.r,
signature.s,
(signature.recoveryParam ? '0x1c': '0x1b')
]));
}