Current Folder
This commit is contained in:
1 parent
8b3382289b
commit
cfcada880d
4379 files changed
No files matched your search
+156
@@ -0,0 +1,156 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var bytes_1 = require("@ethersproject/bytes");
|
||||
var bignumber_1 = require("@ethersproject/bignumber");
|
||||
var properties_1 = require("@ethersproject/properties");
|
||||
var logger_1 = require("@ethersproject/logger");
|
||||
var _version_1 = require("../_version");
|
||||
var logger = new logger_1.Logger(_version_1.version);
|
||||
function checkResultErrors(result) {
|
||||
// Find the first error (if any)
|
||||
var errors = [];
|
||||
var checkErrors = function (path, object) {
|
||||
if (!Array.isArray(object)) {
|
||||
return;
|
||||
}
|
||||
for (var key in object) {
|
||||
var childPath = path.slice();
|
||||
childPath.push(key);
|
||||
try {
|
||||
checkErrors(childPath, object[key]);
|
||||
}
|
||||
catch (error) {
|
||||
errors.push({ path: childPath, error: error });
|
||||
}
|
||||
}
|
||||
};
|
||||
checkErrors([], result);
|
||||
return errors;
|
||||
}
|
||||
exports.checkResultErrors = checkResultErrors;
|
||||
var Coder = /** @class */ (function () {
|
||||
function Coder(name, type, localName, dynamic) {
|
||||
// @TODO: defineReadOnly these
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.localName = localName;
|
||||
this.dynamic = dynamic;
|
||||
}
|
||||
Coder.prototype._throwError = function (message, value) {
|
||||
logger.throwArgumentError(message, this.localName, value);
|
||||
};
|
||||
return Coder;
|
||||
}());
|
||||
exports.Coder = Coder;
|
||||
var Writer = /** @class */ (function () {
|
||||
function Writer(wordSize) {
|
||||
properties_1.defineReadOnly(this, "wordSize", wordSize || 32);
|
||||
this._data = bytes_1.arrayify([]);
|
||||
this._padding = new Uint8Array(wordSize);
|
||||
}
|
||||
Object.defineProperty(Writer.prototype, "data", {
|
||||
get: function () { return bytes_1.hexlify(this._data); },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
Object.defineProperty(Writer.prototype, "length", {
|
||||
get: function () { return this._data.length; },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
Writer.prototype._writeData = function (data) {
|
||||
this._data = bytes_1.concat([this._data, data]);
|
||||
return data.length;
|
||||
};
|
||||
// Arrayish items; padded on the right to wordSize
|
||||
Writer.prototype.writeBytes = function (value) {
|
||||
var bytes = bytes_1.arrayify(value);
|
||||
if (bytes.length % this.wordSize) {
|
||||
bytes = bytes_1.concat([bytes, this._padding.slice(bytes.length % this.wordSize)]);
|
||||
}
|
||||
return this._writeData(bytes);
|
||||
};
|
||||
Writer.prototype._getValue = function (value) {
|
||||
var bytes = bytes_1.arrayify(bignumber_1.BigNumber.from(value));
|
||||
if (bytes.length > this.wordSize) {
|
||||
logger.throwError("value out-of-bounds", logger_1.Logger.errors.BUFFER_OVERRUN, {
|
||||
length: this.wordSize,
|
||||
offset: bytes.length
|
||||
});
|
||||
}
|
||||
if (bytes.length % this.wordSize) {
|
||||
bytes = bytes_1.concat([this._padding.slice(bytes.length % this.wordSize), bytes]);
|
||||
}
|
||||
return bytes;
|
||||
};
|
||||
// BigNumberish items; padded on the left to wordSize
|
||||
Writer.prototype.writeValue = function (value) {
|
||||
return this._writeData(this._getValue(value));
|
||||
};
|
||||
Writer.prototype.writeUpdatableValue = function () {
|
||||
var _this = this;
|
||||
var offset = this.length;
|
||||
this.writeValue(0);
|
||||
return function (value) {
|
||||
_this._data.set(_this._getValue(value), offset);
|
||||
};
|
||||
};
|
||||
return Writer;
|
||||
}());
|
||||
exports.Writer = Writer;
|
||||
var Reader = /** @class */ (function () {
|
||||
function Reader(data, wordSize, coerceFunc) {
|
||||
properties_1.defineReadOnly(this, "_data", bytes_1.arrayify(data));
|
||||
properties_1.defineReadOnly(this, "wordSize", wordSize || 32);
|
||||
properties_1.defineReadOnly(this, "_coerceFunc", coerceFunc);
|
||||
this._offset = 0;
|
||||
}
|
||||
Object.defineProperty(Reader.prototype, "data", {
|
||||
get: function () { return bytes_1.hexlify(this._data); },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
Object.defineProperty(Reader.prototype, "consumed", {
|
||||
get: function () { return this._offset; },
|
||||
enumerable: true,
|
||||
configurable: true
|
||||
});
|
||||
// The default Coerce function
|
||||
Reader.coerce = function (name, value) {
|
||||
var match = name.match("^u?int([0-9]+)$");
|
||||
if (match && parseInt(match[1]) <= 48) {
|
||||
value = value.toNumber();
|
||||
}
|
||||
return value;
|
||||
};
|
||||
Reader.prototype.coerce = function (name, value) {
|
||||
if (this._coerceFunc) {
|
||||
return this._coerceFunc(name, value);
|
||||
}
|
||||
return Reader.coerce(name, value);
|
||||
};
|
||||
Reader.prototype._peekBytes = function (offset, length) {
|
||||
var alignedLength = Math.ceil(length / this.wordSize) * this.wordSize;
|
||||
if (this._offset + alignedLength > this._data.length) {
|
||||
logger.throwError("data out-of-bounds", logger_1.Logger.errors.BUFFER_OVERRUN, {
|
||||
length: this._data.length,
|
||||
offset: this._offset + alignedLength
|
||||
});
|
||||
}
|
||||
return this._data.slice(this._offset, this._offset + alignedLength);
|
||||
};
|
||||
Reader.prototype.subReader = function (offset) {
|
||||
return new Reader(this._data.slice(this._offset + offset), this.wordSize, this._coerceFunc);
|
||||
};
|
||||
Reader.prototype.readBytes = function (length) {
|
||||
var bytes = this._peekBytes(0, length);
|
||||
this._offset += bytes.length;
|
||||
// @TODO: Make sure the length..end bytes are all 0?
|
||||
return bytes.slice(0, length);
|
||||
};
|
||||
Reader.prototype.readValue = function () {
|
||||
return bignumber_1.BigNumber.from(this.readBytes(this.wordSize));
|
||||
};
|
||||
return Reader;
|
||||
}());
|
||||
exports.Reader = Reader;
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var address_1 = require("@ethersproject/address");
|
||||
var bytes_1 = require("@ethersproject/bytes");
|
||||
var abstract_coder_1 = require("./abstract-coder");
|
||||
var AddressCoder = /** @class */ (function (_super) {
|
||||
__extends(AddressCoder, _super);
|
||||
function AddressCoder(localName) {
|
||||
return _super.call(this, "address", "address", localName, false) || this;
|
||||
}
|
||||
AddressCoder.prototype.encode = function (writer, value) {
|
||||
try {
|
||||
address_1.getAddress(value);
|
||||
}
|
||||
catch (error) {
|
||||
this._throwError(error.message, value);
|
||||
}
|
||||
return writer.writeValue(value);
|
||||
};
|
||||
AddressCoder.prototype.decode = function (reader) {
|
||||
return address_1.getAddress(bytes_1.hexZeroPad(reader.readValue().toHexString(), 20));
|
||||
};
|
||||
return AddressCoder;
|
||||
}(abstract_coder_1.Coder));
|
||||
exports.AddressCoder = AddressCoder;
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var abstract_coder_1 = require("./abstract-coder");
|
||||
// Clones the functionality of an existing Coder, but without a localName
|
||||
var AnonymousCoder = /** @class */ (function (_super) {
|
||||
__extends(AnonymousCoder, _super);
|
||||
function AnonymousCoder(coder) {
|
||||
var _this = _super.call(this, coder.name, coder.type, undefined, coder.dynamic) || this;
|
||||
_this.coder = coder;
|
||||
return _this;
|
||||
}
|
||||
AnonymousCoder.prototype.encode = function (writer, value) {
|
||||
return this.coder.encode(writer, value);
|
||||
};
|
||||
AnonymousCoder.prototype.decode = function (reader) {
|
||||
return this.coder.decode(reader);
|
||||
};
|
||||
return AnonymousCoder;
|
||||
}(abstract_coder_1.Coder));
|
||||
exports.AnonymousCoder = AnonymousCoder;
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var logger_1 = require("@ethersproject/logger");
|
||||
var _version_1 = require("../_version");
|
||||
var logger = new logger_1.Logger(_version_1.version);
|
||||
var abstract_coder_1 = require("./abstract-coder");
|
||||
var anonymous_1 = require("./anonymous");
|
||||
function pack(writer, coders, values) {
|
||||
if (Array.isArray(values)) {
|
||||
// do nothing
|
||||
}
|
||||
else if (values && typeof (values) === "object") {
|
||||
var arrayValues_1 = [];
|
||||
coders.forEach(function (coder) {
|
||||
arrayValues_1.push(values[coder.localName]);
|
||||
});
|
||||
values = arrayValues_1;
|
||||
}
|
||||
else {
|
||||
logger.throwArgumentError("invalid tuple value", "tuple", values);
|
||||
}
|
||||
if (coders.length !== values.length) {
|
||||
logger.throwArgumentError("types/value length mismatch", "tuple", values);
|
||||
}
|
||||
var staticWriter = new abstract_coder_1.Writer(writer.wordSize);
|
||||
var dynamicWriter = new abstract_coder_1.Writer(writer.wordSize);
|
||||
var updateFuncs = [];
|
||||
coders.forEach(function (coder, index) {
|
||||
var value = values[index];
|
||||
if (coder.dynamic) {
|
||||
// Get current dynamic offset (for the future pointer)
|
||||
var dynamicOffset_1 = dynamicWriter.length;
|
||||
// Encode the dynamic value into the dynamicWriter
|
||||
coder.encode(dynamicWriter, value);
|
||||
// Prepare to populate the correct offset once we are done
|
||||
var updateFunc_1 = staticWriter.writeUpdatableValue();
|
||||
updateFuncs.push(function (baseOffset) {
|
||||
updateFunc_1(baseOffset + dynamicOffset_1);
|
||||
});
|
||||
}
|
||||
else {
|
||||
coder.encode(staticWriter, value);
|
||||
}
|
||||
});
|
||||
// Backfill all the dynamic offsets, now that we know the static length
|
||||
updateFuncs.forEach(function (func) { func(staticWriter.length); });
|
||||
var length = writer.writeBytes(staticWriter.data);
|
||||
length += writer.writeBytes(dynamicWriter.data);
|
||||
return length;
|
||||
}
|
||||
exports.pack = pack;
|
||||
function unpack(reader, coders) {
|
||||
var values = [];
|
||||
// A reader anchored to this base
|
||||
var baseReader = reader.subReader(0);
|
||||
// The amount of dynamic data read; to consume later to synchronize
|
||||
var dynamicLength = 0;
|
||||
coders.forEach(function (coder) {
|
||||
var value = null;
|
||||
if (coder.dynamic) {
|
||||
var offset = reader.readValue();
|
||||
var offsetReader = baseReader.subReader(offset.toNumber());
|
||||
try {
|
||||
value = coder.decode(offsetReader);
|
||||
}
|
||||
catch (error) {
|
||||
// Cannot recover from this
|
||||
if (error.code === logger_1.Logger.errors.BUFFER_OVERRUN) {
|
||||
throw error;
|
||||
}
|
||||
value = error;
|
||||
value.baseType = coder.name;
|
||||
value.name = coder.localName;
|
||||
value.type = coder.type;
|
||||
}
|
||||
dynamicLength += offsetReader.consumed;
|
||||
}
|
||||
else {
|
||||
try {
|
||||
value = coder.decode(reader);
|
||||
}
|
||||
catch (error) {
|
||||
// Cannot recover from this
|
||||
if (error.code === logger_1.Logger.errors.BUFFER_OVERRUN) {
|
||||
throw error;
|
||||
}
|
||||
value = error;
|
||||
value.baseType = coder.name;
|
||||
value.name = coder.localName;
|
||||
value.type = coder.type;
|
||||
}
|
||||
}
|
||||
if (value != undefined) {
|
||||
values.push(value);
|
||||
}
|
||||
});
|
||||
// @TODO: get rid of this an see if it still works?
|
||||
// Consume the dynamic components in the main reader
|
||||
reader.readBytes(dynamicLength);
|
||||
// Add any named parameters (i.e. tuples)
|
||||
coders.forEach(function (coder, index) {
|
||||
var name = coder.localName;
|
||||
if (!name) {
|
||||
return;
|
||||
}
|
||||
if (name === "length") {
|
||||
name = "_length";
|
||||
}
|
||||
if (values[name] != null) {
|
||||
return;
|
||||
}
|
||||
var value = values[index];
|
||||
if (value instanceof Error) {
|
||||
Object.defineProperty(values, name, {
|
||||
get: function () { throw value; }
|
||||
});
|
||||
}
|
||||
else {
|
||||
values[name] = value;
|
||||
}
|
||||
});
|
||||
var _loop_1 = function (i) {
|
||||
var value = values[i];
|
||||
if (value instanceof Error) {
|
||||
Object.defineProperty(values, i, {
|
||||
get: function () { throw value; }
|
||||
});
|
||||
}
|
||||
};
|
||||
for (var i = 0; i < values.length; i++) {
|
||||
_loop_1(i);
|
||||
}
|
||||
return Object.freeze(values);
|
||||
}
|
||||
exports.unpack = unpack;
|
||||
var ArrayCoder = /** @class */ (function (_super) {
|
||||
__extends(ArrayCoder, _super);
|
||||
function ArrayCoder(coder, length, localName) {
|
||||
var _this = this;
|
||||
var type = (coder.type + "[" + (length >= 0 ? length : "") + "]");
|
||||
var dynamic = (length === -1 || coder.dynamic);
|
||||
_this = _super.call(this, "array", type, localName, dynamic) || this;
|
||||
_this.coder = coder;
|
||||
_this.length = length;
|
||||
return _this;
|
||||
}
|
||||
ArrayCoder.prototype.encode = function (writer, value) {
|
||||
if (!Array.isArray(value)) {
|
||||
this._throwError("expected array value", value);
|
||||
}
|
||||
var count = this.length;
|
||||
if (count === -1) {
|
||||
count = value.length;
|
||||
writer.writeValue(value.length);
|
||||
}
|
||||
logger.checkArgumentCount(count, value.length, "coder array" + (this.localName ? (" " + this.localName) : ""));
|
||||
var coders = [];
|
||||
for (var i = 0; i < value.length; i++) {
|
||||
coders.push(this.coder);
|
||||
}
|
||||
return pack(writer, coders, value);
|
||||
};
|
||||
ArrayCoder.prototype.decode = function (reader) {
|
||||
var count = this.length;
|
||||
if (count === -1) {
|
||||
count = reader.readValue().toNumber();
|
||||
}
|
||||
var coders = [];
|
||||
for (var i = 0; i < count; i++) {
|
||||
coders.push(new anonymous_1.AnonymousCoder(this.coder));
|
||||
}
|
||||
return reader.coerce(this.name, unpack(reader, coders));
|
||||
};
|
||||
return ArrayCoder;
|
||||
}(abstract_coder_1.Coder));
|
||||
exports.ArrayCoder = ArrayCoder;
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var abstract_coder_1 = require("./abstract-coder");
|
||||
var BooleanCoder = /** @class */ (function (_super) {
|
||||
__extends(BooleanCoder, _super);
|
||||
function BooleanCoder(localName) {
|
||||
return _super.call(this, "bool", "bool", localName, false) || this;
|
||||
}
|
||||
BooleanCoder.prototype.encode = function (writer, value) {
|
||||
return writer.writeValue(value ? 1 : 0);
|
||||
};
|
||||
BooleanCoder.prototype.decode = function (reader) {
|
||||
return reader.coerce(this.type, !reader.readValue().isZero());
|
||||
};
|
||||
return BooleanCoder;
|
||||
}(abstract_coder_1.Coder));
|
||||
exports.BooleanCoder = BooleanCoder;
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var bytes_1 = require("@ethersproject/bytes");
|
||||
var abstract_coder_1 = require("./abstract-coder");
|
||||
var DynamicBytesCoder = /** @class */ (function (_super) {
|
||||
__extends(DynamicBytesCoder, _super);
|
||||
function DynamicBytesCoder(type, localName) {
|
||||
return _super.call(this, type, type, localName, true) || this;
|
||||
}
|
||||
DynamicBytesCoder.prototype.encode = function (writer, value) {
|
||||
value = bytes_1.arrayify(value);
|
||||
var length = writer.writeValue(value.length);
|
||||
length += writer.writeBytes(value);
|
||||
return length;
|
||||
};
|
||||
DynamicBytesCoder.prototype.decode = function (reader) {
|
||||
return reader.readBytes(reader.readValue().toNumber());
|
||||
};
|
||||
return DynamicBytesCoder;
|
||||
}(abstract_coder_1.Coder));
|
||||
exports.DynamicBytesCoder = DynamicBytesCoder;
|
||||
var BytesCoder = /** @class */ (function (_super) {
|
||||
__extends(BytesCoder, _super);
|
||||
function BytesCoder(localName) {
|
||||
return _super.call(this, "bytes", localName) || this;
|
||||
}
|
||||
BytesCoder.prototype.decode = function (reader) {
|
||||
return reader.coerce(this.name, bytes_1.hexlify(_super.prototype.decode.call(this, reader)));
|
||||
};
|
||||
return BytesCoder;
|
||||
}(DynamicBytesCoder));
|
||||
exports.BytesCoder = BytesCoder;
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var bytes_1 = require("@ethersproject/bytes");
|
||||
var abstract_coder_1 = require("./abstract-coder");
|
||||
// @TODO: Merge this with bytes
|
||||
var FixedBytesCoder = /** @class */ (function (_super) {
|
||||
__extends(FixedBytesCoder, _super);
|
||||
function FixedBytesCoder(size, localName) {
|
||||
var _this = this;
|
||||
var name = "bytes" + String(size);
|
||||
_this = _super.call(this, name, name, localName, false) || this;
|
||||
_this.size = size;
|
||||
return _this;
|
||||
}
|
||||
FixedBytesCoder.prototype.encode = function (writer, value) {
|
||||
var data = bytes_1.arrayify(value);
|
||||
if (data.length !== this.size) {
|
||||
this._throwError("incorrect data length", value);
|
||||
}
|
||||
return writer.writeBytes(data);
|
||||
};
|
||||
FixedBytesCoder.prototype.decode = function (reader) {
|
||||
return reader.coerce(this.name, bytes_1.hexlify(reader.readBytes(this.size)));
|
||||
};
|
||||
return FixedBytesCoder;
|
||||
}(abstract_coder_1.Coder));
|
||||
exports.FixedBytesCoder = FixedBytesCoder;
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var abstract_coder_1 = require("./abstract-coder");
|
||||
var NullCoder = /** @class */ (function (_super) {
|
||||
__extends(NullCoder, _super);
|
||||
function NullCoder(localName) {
|
||||
return _super.call(this, "null", "", localName, false) || this;
|
||||
}
|
||||
NullCoder.prototype.encode = function (writer, value) {
|
||||
if (value != null) {
|
||||
this._throwError("not null", value);
|
||||
}
|
||||
return writer.writeBytes([]);
|
||||
};
|
||||
NullCoder.prototype.decode = function (reader) {
|
||||
reader.readBytes(0);
|
||||
return reader.coerce(this.name, null);
|
||||
};
|
||||
return NullCoder;
|
||||
}(abstract_coder_1.Coder));
|
||||
exports.NullCoder = NullCoder;
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var bignumber_1 = require("@ethersproject/bignumber");
|
||||
var constants_1 = require("@ethersproject/constants");
|
||||
var abstract_coder_1 = require("./abstract-coder");
|
||||
var NumberCoder = /** @class */ (function (_super) {
|
||||
__extends(NumberCoder, _super);
|
||||
function NumberCoder(size, signed, localName) {
|
||||
var _this = this;
|
||||
var name = ((signed ? "int" : "uint") + (size * 8));
|
||||
_this = _super.call(this, name, name, localName, false) || this;
|
||||
_this.size = size;
|
||||
_this.signed = signed;
|
||||
return _this;
|
||||
}
|
||||
NumberCoder.prototype.encode = function (writer, value) {
|
||||
var v = bignumber_1.BigNumber.from(value);
|
||||
// Check bounds are safe for encoding
|
||||
var maxUintValue = constants_1.MaxUint256.mask(writer.wordSize * 8);
|
||||
if (this.signed) {
|
||||
var bounds = maxUintValue.mask(this.size * 8 - 1);
|
||||
if (v.gt(bounds) || v.lt(bounds.add(constants_1.One).mul(constants_1.NegativeOne))) {
|
||||
this._throwError("value out-of-bounds", value);
|
||||
}
|
||||
}
|
||||
else if (v.lt(constants_1.Zero) || v.gt(maxUintValue.mask(this.size * 8))) {
|
||||
this._throwError("value out-of-bounds", value);
|
||||
}
|
||||
v = v.toTwos(this.size * 8).mask(this.size * 8);
|
||||
if (this.signed) {
|
||||
v = v.fromTwos(this.size * 8).toTwos(8 * writer.wordSize);
|
||||
}
|
||||
return writer.writeValue(v);
|
||||
};
|
||||
NumberCoder.prototype.decode = function (reader) {
|
||||
var value = reader.readValue().mask(this.size * 8);
|
||||
if (this.signed) {
|
||||
value = value.fromTwos(this.size * 8);
|
||||
}
|
||||
return reader.coerce(this.name, value);
|
||||
};
|
||||
return NumberCoder;
|
||||
}(abstract_coder_1.Coder));
|
||||
exports.NumberCoder = NumberCoder;
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var strings_1 = require("@ethersproject/strings");
|
||||
var bytes_1 = require("./bytes");
|
||||
var StringCoder = /** @class */ (function (_super) {
|
||||
__extends(StringCoder, _super);
|
||||
function StringCoder(localName) {
|
||||
return _super.call(this, "string", localName) || this;
|
||||
}
|
||||
StringCoder.prototype.encode = function (writer, value) {
|
||||
return _super.prototype.encode.call(this, writer, strings_1.toUtf8Bytes(value));
|
||||
};
|
||||
StringCoder.prototype.decode = function (reader) {
|
||||
return strings_1.toUtf8String(_super.prototype.decode.call(this, reader));
|
||||
};
|
||||
return StringCoder;
|
||||
}(bytes_1.DynamicBytesCoder));
|
||||
exports.StringCoder = StringCoder;
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || (function () {
|
||||
var extendStatics = function (d, b) {
|
||||
extendStatics = Object.setPrototypeOf ||
|
||||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
||||
return extendStatics(d, b);
|
||||
};
|
||||
return function (d, b) {
|
||||
extendStatics(d, b);
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
})();
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
var abstract_coder_1 = require("./abstract-coder");
|
||||
var array_1 = require("./array");
|
||||
var TupleCoder = /** @class */ (function (_super) {
|
||||
__extends(TupleCoder, _super);
|
||||
function TupleCoder(coders, localName) {
|
||||
var _this = this;
|
||||
var dynamic = false;
|
||||
var types = [];
|
||||
coders.forEach(function (coder) {
|
||||
if (coder.dynamic) {
|
||||
dynamic = true;
|
||||
}
|
||||
types.push(coder.type);
|
||||
});
|
||||
var type = ("tuple(" + types.join(",") + ")");
|
||||
_this = _super.call(this, "tuple", type, localName, dynamic) || this;
|
||||
_this.coders = coders;
|
||||
return _this;
|
||||
}
|
||||
TupleCoder.prototype.encode = function (writer, value) {
|
||||
return array_1.pack(writer, this.coders, value);
|
||||
};
|
||||
TupleCoder.prototype.decode = function (reader) {
|
||||
return reader.coerce(this.name, array_1.unpack(reader, this.coders));
|
||||
};
|
||||
return TupleCoder;
|
||||
}(abstract_coder_1.Coder));
|
||||
exports.TupleCoder = TupleCoder;
|
||||
Reference in new issue
Block a user