2019-05-15 01:25:46 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
|
|
|
|
|
|
|
|
import { arrayify, BytesLike } from "@ethersproject/bytes";
|
|
|
|
import { defineReadOnly } from "@ethersproject/properties";
|
|
|
|
|
2019-08-02 01:04:06 +03:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
import { Coder, Reader, Writer } from "./coders/abstract-coder";
|
|
|
|
import { AddressCoder } from "./coders/address";
|
|
|
|
import { ArrayCoder } from "./coders/array";
|
|
|
|
import { BooleanCoder } from "./coders/boolean";
|
|
|
|
import { BytesCoder } from "./coders/bytes";
|
|
|
|
import { FixedBytesCoder } from "./coders/fixed-bytes";
|
|
|
|
import { NullCoder } from "./coders/null";
|
|
|
|
import { NumberCoder } from "./coders/number";
|
|
|
|
import { StringCoder } from "./coders/string";
|
|
|
|
import { TupleCoder } from "./coders/tuple";
|
|
|
|
|
|
|
|
import { ParamType } from "./fragments";
|
|
|
|
|
|
|
|
|
|
|
|
const paramTypeBytes = new RegExp(/^bytes([0-9]*)$/);
|
|
|
|
const paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/);
|
|
|
|
|
|
|
|
|
|
|
|
export type CoerceFunc = (type: string, value: any) => any;
|
|
|
|
|
|
|
|
export class AbiCoder {
|
|
|
|
readonly coerceFunc: CoerceFunc;
|
|
|
|
|
|
|
|
constructor(coerceFunc?: CoerceFunc) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.checkNew(new.target, AbiCoder);
|
2019-05-15 01:25:46 +03:00
|
|
|
defineReadOnly(this, "coerceFunc", coerceFunc || null);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getCoder(param: ParamType): Coder {
|
|
|
|
|
|
|
|
switch (param.baseType) {
|
|
|
|
case "address":
|
|
|
|
return new AddressCoder(param.name);
|
|
|
|
case "bool":
|
|
|
|
return new BooleanCoder(param.name);
|
|
|
|
case "string":
|
|
|
|
return new StringCoder(param.name);
|
|
|
|
case "bytes":
|
|
|
|
return new BytesCoder(param.name);
|
|
|
|
case "array":
|
|
|
|
return new ArrayCoder(this._getCoder(param.arrayChildren), param.arrayLength, param.name);
|
|
|
|
case "tuple":
|
|
|
|
return new TupleCoder((param.components || []).map((component) => {
|
|
|
|
return this._getCoder(component);
|
|
|
|
}), param.name);
|
|
|
|
case "":
|
|
|
|
return new NullCoder(param.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// u?int[0-9]*
|
|
|
|
let match = param.type.match(paramTypeNumber);
|
|
|
|
if (match) {
|
|
|
|
let size = parseInt(match[2] || "256");
|
|
|
|
if (size === 0 || size > 256 || (size % 8) !== 0) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("invalid " + match[1] + " bit length", "param", param);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
return new NumberCoder(size / 8, (match[1] === "int"), param.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// bytes[0-9]+
|
|
|
|
match = param.type.match(paramTypeBytes);
|
|
|
|
if (match) {
|
|
|
|
let size = parseInt(match[1]);
|
|
|
|
if (size === 0 || size > 32) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("invalid bytes length", "param", param);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
return new FixedBytesCoder(size, param.name);
|
|
|
|
}
|
|
|
|
|
2019-08-02 01:04:06 +03:00
|
|
|
return logger.throwError("invalid type", "type", param.type);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_getWordSize(): number { return 32; }
|
|
|
|
|
|
|
|
_getReader(data: Uint8Array): Reader {
|
|
|
|
return new Reader(data, this._getWordSize(), this.coerceFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
_getWriter(): Writer {
|
|
|
|
return new Writer(this._getWordSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
encode(types: Array<string | ParamType>, values: Array<any>): string {
|
|
|
|
if (types.length !== values.length) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwError("types/values length mismatch", Logger.errors.INVALID_ARGUMENT, {
|
2019-05-15 01:25:46 +03:00
|
|
|
count: { types: types.length, values: values.length },
|
|
|
|
value: { types: types, values: values }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let coders = types.map((type) => this._getCoder(ParamType.from(type)));
|
|
|
|
let coder = (new TupleCoder(coders, "_"));
|
|
|
|
|
|
|
|
let writer = this._getWriter();
|
|
|
|
coder.encode(writer, values);
|
|
|
|
return writer.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
decode(types: Array<string | ParamType>, data: BytesLike): any {
|
|
|
|
let coders: Array<Coder> = types.map((type) => this._getCoder(ParamType.from(type)));
|
|
|
|
let coder = new TupleCoder(coders, "_");
|
|
|
|
return coder.decode(this._getReader(arrayify(data)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const defaultAbiCoder: AbiCoder = new AbiCoder();
|
|
|
|
|