-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathabstract-coder.ts
188 lines (150 loc) · 5.9 KB
/
abstract-coder.ts
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
"use strict";
import { arrayify, BytesLike, concat, hexlify } from "@ethersproject/bytes";
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
import { defineReadOnly } from "@ethersproject/properties";
import { Logger } from "@ethersproject/logger";
import { version } from "../_version";
const logger = new Logger(version);
export interface Result extends ReadonlyArray<any> {
readonly [key: string]: any;
}
export function checkResultErrors(result: Result): Array<{ path: Array<string | number>, error: Error }> {
// Find the first error (if any)
const errors: Array<{ path: Array<string | number>, error: Error }> = [ ];
const checkErrors = function(path: Array<string | number>, object: any): void {
if (!Array.isArray(object)) { return; }
for (let key in object) {
const childPath = path.slice();
childPath.push(key);
try {
checkErrors(childPath, object[key]);
} catch (error) {
errors.push({ path: childPath, error: error });
}
}
}
checkErrors([ ], result);
return errors;
}
export type CoerceFunc = (type: string, value: any) => any;
export abstract class Coder {
// The coder name:
// - address, uint256, tuple, array, etc.
readonly name: string;
// The fully expanded type, including composite types:
// - address, uint256, tuple(address,bytes), uint256[3][4][], etc.
readonly type: string;
// The localName bound in the signature, in this example it is "baz":
// - tuple(address foo, uint bar) baz
readonly localName: string;
// Whether this type is dynamic:
// - Dynamic: bytes, string, address[], tuple(boolean[]), etc.
// - Not Dynamic: address, uint256, boolean[3], tuple(address, uint8)
readonly dynamic: boolean;
constructor(name: string, type: string, localName: string, dynamic: boolean) {
// @TODO: defineReadOnly these
this.name = name;
this.type = type;
this.localName = localName;
this.dynamic = dynamic;
}
_throwError(message: string, value: any): void {
logger.throwArgumentError(message, this.localName, value);
}
abstract encode(writer: Writer, value: any): number;
abstract decode(reader: Reader): any;
}
export class Writer {
readonly wordSize: number;
_data: Uint8Array;
_padding: Uint8Array;
constructor(wordSize?: number) {
defineReadOnly(this, "wordSize", wordSize || 32);
this._data = arrayify([ ]);
this._padding = new Uint8Array(wordSize);
}
get data(): string { return hexlify(this._data); }
get length(): number { return this._data.length; }
_writeData(data: Uint8Array): number {
this._data = concat([ this._data, data ]);
return data.length;
}
// Arrayish items; padded on the right to wordSize
writeBytes(value: BytesLike): number {
let bytes = arrayify(value);
if (bytes.length % this.wordSize) {
bytes = concat([ bytes, this._padding.slice(bytes.length % this.wordSize) ])
}
return this._writeData(bytes);
}
_getValue(value: BigNumberish): Uint8Array {
let bytes = arrayify(BigNumber.from(value));
if (bytes.length > this.wordSize) {
logger.throwError("value out-of-bounds", Logger.errors.BUFFER_OVERRUN, {
length: this.wordSize,
offset: bytes.length
});
}
if (bytes.length % this.wordSize) {
bytes = concat([ this._padding.slice(bytes.length % this.wordSize), bytes ]);
}
return bytes;
}
// BigNumberish items; padded on the left to wordSize
writeValue(value: BigNumberish): number {
return this._writeData(this._getValue(value));
}
writeUpdatableValue(): (value: BigNumberish) => void {
let offset = this.length;
this.writeValue(0);
return (value: BigNumberish) => {
this._data.set(this._getValue(value), offset);
};
}
}
export class Reader {
readonly wordSize: number;
readonly _data: Uint8Array;
readonly _coerceFunc: CoerceFunc;
_offset: number;
constructor(data: BytesLike, wordSize?: number, coerceFunc?: CoerceFunc) {
defineReadOnly(this, "_data", arrayify(data));
defineReadOnly(this, "wordSize", wordSize || 32);
defineReadOnly(this, "_coerceFunc", coerceFunc);
this._offset = 0;
}
get data(): string { return hexlify(this._data); }
get consumed(): number { return this._offset; }
// The default Coerce function
static coerce(name: string, value: any): any {
let match = name.match("^u?int([0-9]+)$");
if (match && parseInt(match[1]) <= 48) { value = value.toNumber(); }
return value;
}
coerce(name: string, value: any): any {
if (this._coerceFunc) { return this._coerceFunc(name, value); }
return Reader.coerce(name, value);
}
_peekBytes(offset: number, length: number): Uint8Array {
let alignedLength = Math.ceil(length / this.wordSize) * this.wordSize;
if (this._offset + alignedLength > this._data.length) {
logger.throwError("data out-of-bounds", Logger.errors.BUFFER_OVERRUN, {
length: this._data.length,
offset: this._offset + alignedLength
});
}
return this._data.slice(this._offset, this._offset + alignedLength)
}
subReader(offset: number): Reader {
return new Reader(this._data.slice(this._offset + offset), this.wordSize, this._coerceFunc);
}
readBytes(length: number): Uint8Array {
let bytes = this._peekBytes(0, length);
this._offset += bytes.length;
// @TODO: Make sure the length..end bytes are all 0?
return bytes.slice(0, length);
}
readValue(): BigNumber {
return BigNumber.from(this.readBytes(this.wordSize));
}
}