2019-08-25 02:39:20 -04:00
|
|
|
"use strict";
|
2019-05-14 18:25:46 -04:00
|
|
|
|
2020-10-05 15:34:00 -04:00
|
|
|
import { arrayify, BytesLike, concat, hexConcat, hexlify } from "@ethersproject/bytes";
|
2019-05-14 18:25:46 -04:00
|
|
|
import { BigNumber, BigNumberish } from "@ethersproject/bignumber";
|
|
|
|
import { defineReadOnly } from "@ethersproject/properties";
|
|
|
|
|
2019-08-01 18:04:06 -04:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "../_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
2020-03-30 15:11:23 -04:00
|
|
|
export interface Result extends ReadonlyArray<any> {
|
|
|
|
readonly [key: string]: any;
|
2020-01-07 19:46:51 -05:00
|
|
|
}
|
|
|
|
|
2020-04-25 03:25:42 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
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) {
|
2020-04-25 03:25:42 -04:00
|
|
|
// @TODO: defineReadOnly these
|
2019-05-14 18:25:46 -04:00
|
|
|
this.name = name;
|
|
|
|
this.type = type;
|
|
|
|
this.localName = localName;
|
|
|
|
this.dynamic = dynamic;
|
|
|
|
}
|
|
|
|
|
|
|
|
_throwError(message: string, value: any): void {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwArgumentError(message, this.localName, value);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract encode(writer: Writer, value: any): number;
|
|
|
|
abstract decode(reader: Reader): any;
|
2020-11-23 00:59:44 -05:00
|
|
|
|
|
|
|
abstract defaultValue(): any;
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export class Writer {
|
|
|
|
readonly wordSize: number;
|
|
|
|
|
2020-10-05 15:34:00 -04:00
|
|
|
_data: Array<Uint8Array>;
|
|
|
|
_dataLength: number;
|
2019-05-14 18:25:46 -04:00
|
|
|
_padding: Uint8Array;
|
|
|
|
|
|
|
|
constructor(wordSize?: number) {
|
|
|
|
defineReadOnly(this, "wordSize", wordSize || 32);
|
2020-10-05 15:34:00 -04:00
|
|
|
this._data = [ ];
|
|
|
|
this._dataLength = 0;
|
2019-05-14 18:25:46 -04:00
|
|
|
this._padding = new Uint8Array(wordSize);
|
|
|
|
}
|
|
|
|
|
2020-10-05 15:34:00 -04:00
|
|
|
get data(): string {
|
|
|
|
return hexConcat(this._data);
|
|
|
|
}
|
|
|
|
get length(): number { return this._dataLength; }
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
_writeData(data: Uint8Array): number {
|
2020-10-05 15:34:00 -04:00
|
|
|
this._data.push(data);
|
|
|
|
this._dataLength += data.length;
|
2019-05-14 18:25:46 -04:00
|
|
|
return data.length;
|
|
|
|
}
|
|
|
|
|
2020-10-05 15:34:00 -04:00
|
|
|
appendWriter(writer: Writer): number {
|
|
|
|
return this._writeData(concat(writer._data));
|
|
|
|
}
|
|
|
|
|
2019-05-14 18:25:46 -04:00
|
|
|
// Arrayish items; padded on the right to wordSize
|
|
|
|
writeBytes(value: BytesLike): number {
|
|
|
|
let bytes = arrayify(value);
|
2020-10-05 15:34:00 -04:00
|
|
|
const paddingOffset = bytes.length % this.wordSize;
|
|
|
|
if (paddingOffset) {
|
|
|
|
bytes = concat([ bytes, this._padding.slice(paddingOffset) ])
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
return this._writeData(bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getValue(value: BigNumberish): Uint8Array {
|
|
|
|
let bytes = arrayify(BigNumber.from(value));
|
|
|
|
if (bytes.length > this.wordSize) {
|
2019-08-01 18:04:06 -04:00
|
|
|
logger.throwError("value out-of-bounds", Logger.errors.BUFFER_OVERRUN, {
|
2019-05-14 18:25:46 -04:00
|
|
|
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 {
|
2020-10-05 15:34:00 -04:00
|
|
|
const offset = this._data.length;
|
|
|
|
this._data.push(this._padding);
|
|
|
|
this._dataLength += this.wordSize;
|
2019-05-14 18:25:46 -04:00
|
|
|
return (value: BigNumberish) => {
|
2020-10-05 15:34:00 -04:00
|
|
|
this._data[offset] = this._getValue(value);
|
2019-05-14 18:25:46 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Reader {
|
|
|
|
readonly wordSize: number;
|
2020-09-07 19:35:37 -04:00
|
|
|
readonly allowLoose: boolean;
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
readonly _data: Uint8Array;
|
|
|
|
readonly _coerceFunc: CoerceFunc;
|
|
|
|
|
|
|
|
_offset: number;
|
|
|
|
|
2020-09-07 19:35:37 -04:00
|
|
|
constructor(data: BytesLike, wordSize?: number, coerceFunc?: CoerceFunc, allowLoose?: boolean) {
|
2019-05-14 18:25:46 -04:00
|
|
|
defineReadOnly(this, "_data", arrayify(data));
|
|
|
|
defineReadOnly(this, "wordSize", wordSize || 32);
|
|
|
|
defineReadOnly(this, "_coerceFunc", coerceFunc);
|
2020-09-07 19:35:37 -04:00
|
|
|
defineReadOnly(this, "allowLoose", allowLoose);
|
2019-05-14 18:25:46 -04:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-09-07 19:35:37 -04:00
|
|
|
_peekBytes(offset: number, length: number, loose?: boolean): Uint8Array {
|
2019-05-14 18:25:46 -04:00
|
|
|
let alignedLength = Math.ceil(length / this.wordSize) * this.wordSize;
|
|
|
|
if (this._offset + alignedLength > this._data.length) {
|
2020-09-07 19:35:37 -04:00
|
|
|
if (this.allowLoose && loose && this._offset + length <= this._data.length) {
|
|
|
|
alignedLength = length;
|
|
|
|
} else {
|
|
|
|
logger.throwError("data out-of-bounds", Logger.errors.BUFFER_OVERRUN, {
|
|
|
|
length: this._data.length,
|
|
|
|
offset: this._offset + alignedLength
|
|
|
|
});
|
|
|
|
}
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
return this._data.slice(this._offset, this._offset + alignedLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
subReader(offset: number): Reader {
|
2020-09-07 19:35:37 -04:00
|
|
|
return new Reader(this._data.slice(this._offset + offset), this.wordSize, this._coerceFunc, this.allowLoose);
|
2019-05-14 18:25:46 -04:00
|
|
|
}
|
|
|
|
|
2020-09-07 19:35:37 -04:00
|
|
|
readBytes(length: number, loose?: boolean): Uint8Array {
|
|
|
|
let bytes = this._peekBytes(0, length, !!loose);
|
2019-05-14 18:25:46 -04:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|