ethers.js/packages/abi/lib/abi-coder.js

106 lines
4.8 KiB
JavaScript
Raw Permalink Normal View History

2019-05-14 18:48:48 -04:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2021-03-07 18:24:04 -05:00
exports.defaultAbiCoder = exports.AbiCoder = void 0;
2019-05-14 18:48:48 -04:00
// See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
var bytes_1 = require("@ethersproject/bytes");
var properties_1 = require("@ethersproject/properties");
2019-08-02 02:10:58 -04:00
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
2019-05-14 18:48:48 -04:00
var abstract_coder_1 = require("./coders/abstract-coder");
var address_1 = require("./coders/address");
var array_1 = require("./coders/array");
var boolean_1 = require("./coders/boolean");
var bytes_2 = require("./coders/bytes");
var fixed_bytes_1 = require("./coders/fixed-bytes");
var null_1 = require("./coders/null");
var number_1 = require("./coders/number");
var string_1 = require("./coders/string");
var tuple_1 = require("./coders/tuple");
var fragments_1 = require("./fragments");
var paramTypeBytes = new RegExp(/^bytes([0-9]*)$/);
var paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/);
var AbiCoder = /** @class */ (function () {
function AbiCoder(coerceFunc) {
var _newTarget = this.constructor;
2019-08-02 02:10:58 -04:00
logger.checkNew(_newTarget, AbiCoder);
2019-05-14 18:48:48 -04:00
properties_1.defineReadOnly(this, "coerceFunc", coerceFunc || null);
}
AbiCoder.prototype._getCoder = function (param) {
var _this = this;
switch (param.baseType) {
case "address":
return new address_1.AddressCoder(param.name);
case "bool":
return new boolean_1.BooleanCoder(param.name);
case "string":
return new string_1.StringCoder(param.name);
case "bytes":
return new bytes_2.BytesCoder(param.name);
case "array":
return new array_1.ArrayCoder(this._getCoder(param.arrayChildren), param.arrayLength, param.name);
case "tuple":
return new tuple_1.TupleCoder((param.components || []).map(function (component) {
return _this._getCoder(component);
}), param.name);
case "":
return new null_1.NullCoder(param.name);
}
// u?int[0-9]*
var match = param.type.match(paramTypeNumber);
if (match) {
var size = parseInt(match[2] || "256");
if (size === 0 || size > 256 || (size % 8) !== 0) {
2019-08-02 02:10:58 -04:00
logger.throwArgumentError("invalid " + match[1] + " bit length", "param", param);
2019-05-14 18:48:48 -04:00
}
return new number_1.NumberCoder(size / 8, (match[1] === "int"), param.name);
}
// bytes[0-9]+
match = param.type.match(paramTypeBytes);
if (match) {
var size = parseInt(match[1]);
if (size === 0 || size > 32) {
2019-08-02 02:10:58 -04:00
logger.throwArgumentError("invalid bytes length", "param", param);
2019-05-14 18:48:48 -04:00
}
return new fixed_bytes_1.FixedBytesCoder(size, param.name);
}
2020-04-25 03:54:54 -04:00
return logger.throwArgumentError("invalid type", "type", param.type);
2019-05-14 18:48:48 -04:00
};
AbiCoder.prototype._getWordSize = function () { return 32; };
2020-09-07 19:55:52 -04:00
AbiCoder.prototype._getReader = function (data, allowLoose) {
return new abstract_coder_1.Reader(data, this._getWordSize(), this.coerceFunc, allowLoose);
2019-05-14 18:48:48 -04:00
};
AbiCoder.prototype._getWriter = function () {
return new abstract_coder_1.Writer(this._getWordSize());
};
2020-11-23 03:43:28 -05:00
AbiCoder.prototype.getDefaultValue = function (types) {
var _this = this;
var coders = types.map(function (type) { return _this._getCoder(fragments_1.ParamType.from(type)); });
var coder = new tuple_1.TupleCoder(coders, "_");
return coder.defaultValue();
};
2019-05-14 18:48:48 -04:00
AbiCoder.prototype.encode = function (types, values) {
var _this = this;
if (types.length !== values.length) {
2019-08-02 02:10:58 -04:00
logger.throwError("types/values length mismatch", logger_1.Logger.errors.INVALID_ARGUMENT, {
2019-05-14 18:48:48 -04:00
count: { types: types.length, values: values.length },
value: { types: types, values: values }
});
}
var coders = types.map(function (type) { return _this._getCoder(fragments_1.ParamType.from(type)); });
var coder = (new tuple_1.TupleCoder(coders, "_"));
var writer = this._getWriter();
coder.encode(writer, values);
return writer.data;
};
2020-09-07 19:55:52 -04:00
AbiCoder.prototype.decode = function (types, data, loose) {
2019-05-14 18:48:48 -04:00
var _this = this;
var coders = types.map(function (type) { return _this._getCoder(fragments_1.ParamType.from(type)); });
var coder = new tuple_1.TupleCoder(coders, "_");
2020-09-07 19:55:52 -04:00
return coder.decode(this._getReader(bytes_1.arrayify(data), loose));
2019-05-14 18:48:48 -04:00
};
return AbiCoder;
}());
exports.AbiCoder = AbiCoder;
exports.defaultAbiCoder = new AbiCoder();
2020-07-13 08:03:56 -04:00
//# sourceMappingURL=abi-coder.js.map