@etherspot/remote-signer
Version:
Etherspot Permissioned Signer SDK - signs the UserOp with SessionKey and sends it to the Bundler
753 lines (744 loc) • 22.1 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __esm = (fn, res) => function __init() {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
};
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// node_modules/viem/_esm/utils/data/isHex.js
function isHex(value, { strict = true } = {}) {
if (!value)
return false;
if (typeof value !== "string")
return false;
return strict ? /^0x[0-9a-fA-F]*$/.test(value) : value.startsWith("0x");
}
var init_isHex = __esm({
"node_modules/viem/_esm/utils/data/isHex.js"() {
}
});
// src/sdk/common/utils/bignumber-utils.ts
var bignumber_utils_exports = {};
__export(bignumber_utils_exports, {
bigNumberishToBigInt: () => bigNumberishToBigInt,
fromBigInt: () => fromBigInt,
isBigNumber: () => isBigNumber
});
module.exports = __toCommonJS(bignumber_utils_exports);
// src/sdk/types/bignumber.ts
var import_bn = __toESM(require("bn.js"));
// node_modules/viem/_esm/utils/data/isBytes.js
function isBytes(value) {
if (!value)
return false;
if (typeof value !== "object")
return false;
if (!("BYTES_PER_ELEMENT" in value))
return false;
return value.BYTES_PER_ELEMENT === 1 && value.constructor.name === "Uint8Array";
}
// node_modules/viem/_esm/index.js
init_isHex();
// src/sdk/types/bignumber-logger.ts
var _permanentCensorErrors = false;
var _censorErrors = false;
var LogLevels = { debug: 1, "default": 2, info: 2, warning: 3, error: 4, off: 5 };
var _logLevel = LogLevels["default"];
var version = "logger/5.7.0";
var _globalLogger = null;
function _checkNormalize() {
try {
const missing = [];
["NFD", "NFC", "NFKD", "NFKC"].forEach((form) => {
try {
if ("test".normalize(form) !== "test") {
throw new Error("bad normalize");
}
;
} catch (error) {
missing.push(form);
}
});
if (missing.length) {
throw new Error("missing " + missing.join(", "));
}
if (String.fromCharCode(233).normalize("NFD") !== String.fromCharCode(101, 769)) {
throw new Error("broken implementation");
}
} catch (error) {
return error.message;
}
return null;
}
var _normalizeError = _checkNormalize();
var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
LogLevel2["DEBUG"] = "DEBUG";
LogLevel2["INFO"] = "INFO";
LogLevel2["WARNING"] = "WARNING";
LogLevel2["ERROR"] = "ERROR";
LogLevel2["OFF"] = "OFF";
return LogLevel2;
})(LogLevel || {});
var ErrorCode = /* @__PURE__ */ ((ErrorCode2) => {
ErrorCode2["UNKNOWN_ERROR"] = "UNKNOWN_ERROR";
ErrorCode2["NOT_IMPLEMENTED"] = "NOT_IMPLEMENTED";
ErrorCode2["UNSUPPORTED_OPERATION"] = "UNSUPPORTED_OPERATION";
ErrorCode2["NETWORK_ERROR"] = "NETWORK_ERROR";
ErrorCode2["SERVER_ERROR"] = "SERVER_ERROR";
ErrorCode2["TIMEOUT"] = "TIMEOUT";
ErrorCode2["BUFFER_OVERRUN"] = "BUFFER_OVERRUN";
ErrorCode2["NUMERIC_FAULT"] = "NUMERIC_FAULT";
ErrorCode2["MISSING_NEW"] = "MISSING_NEW";
ErrorCode2["INVALID_ARGUMENT"] = "INVALID_ARGUMENT";
ErrorCode2["MISSING_ARGUMENT"] = "MISSING_ARGUMENT";
ErrorCode2["UNEXPECTED_ARGUMENT"] = "UNEXPECTED_ARGUMENT";
ErrorCode2["CALL_EXCEPTION"] = "CALL_EXCEPTION";
ErrorCode2["INSUFFICIENT_FUNDS"] = "INSUFFICIENT_FUNDS";
ErrorCode2["NONCE_EXPIRED"] = "NONCE_EXPIRED";
ErrorCode2["REPLACEMENT_UNDERPRICED"] = "REPLACEMENT_UNDERPRICED";
ErrorCode2["UNPREDICTABLE_GAS_LIMIT"] = "UNPREDICTABLE_GAS_LIMIT";
ErrorCode2["TRANSACTION_REPLACED"] = "TRANSACTION_REPLACED";
ErrorCode2["ACTION_REJECTED"] = "ACTION_REJECTED";
return ErrorCode2;
})(ErrorCode || {});
var HEX = "0123456789abcdef";
var _Logger = class _Logger {
constructor(version3) {
Object.defineProperty(this, "version", {
enumerable: true,
value: version3,
writable: false
});
}
_log(logLevel, args) {
const level = logLevel.toLowerCase();
if (LogLevels[level] == null) {
this.throwArgumentError("invalid log level name", "logLevel", logLevel);
}
if (_logLevel > LogLevels[level]) {
return;
}
console.log.apply(console, args);
}
debug(...args) {
this._log(_Logger.levels.DEBUG, args);
}
info(...args) {
this._log(_Logger.levels.INFO, args);
}
warn(...args) {
this._log(_Logger.levels.WARNING, args);
}
makeError(message, code, params) {
if (_censorErrors) {
return this.makeError("censored error", code, {});
}
if (!code) {
code = _Logger.errors.UNKNOWN_ERROR;
}
if (!params) {
params = {};
}
const messageDetails = [];
Object.keys(params).forEach((key) => {
const value = params[key];
try {
if (value instanceof Uint8Array) {
let hex = "";
for (let i = 0; i < value.length; i++) {
hex += HEX[value[i] >> 4];
hex += HEX[value[i] & 15];
}
messageDetails.push(key + "=Uint8Array(0x" + hex + ")");
} else {
messageDetails.push(key + "=" + JSON.stringify(value));
}
} catch (error2) {
messageDetails.push(key + "=" + JSON.stringify(params[key].toString()));
}
});
messageDetails.push(`code=${code}`);
messageDetails.push(`version=${this.version}`);
const reason = message;
let url = "";
switch (code) {
case "NUMERIC_FAULT" /* NUMERIC_FAULT */: {
url = "NUMERIC_FAULT";
const fault = message;
switch (fault) {
case "overflow":
case "underflow":
case "division-by-zero":
url += "-" + fault;
break;
case "negative-power":
case "negative-width":
url += "-unsupported";
break;
case "unbound-bitwise-result":
url += "-unbound-result";
break;
}
break;
}
case "CALL_EXCEPTION" /* CALL_EXCEPTION */:
case "INSUFFICIENT_FUNDS" /* INSUFFICIENT_FUNDS */:
case "MISSING_NEW" /* MISSING_NEW */:
case "NONCE_EXPIRED" /* NONCE_EXPIRED */:
case "REPLACEMENT_UNDERPRICED" /* REPLACEMENT_UNDERPRICED */:
case "TRANSACTION_REPLACED" /* TRANSACTION_REPLACED */:
case "UNPREDICTABLE_GAS_LIMIT" /* UNPREDICTABLE_GAS_LIMIT */:
url = code;
break;
}
if (url) {
message += " [ See: https://links.ethers.org/v5-errors-" + url + " ]";
}
if (messageDetails.length) {
message += " (" + messageDetails.join(", ") + ")";
}
const error = new Error(message);
error.reason = reason;
error.code = code;
Object.keys(params).forEach(function(key) {
error[key] = params[key];
});
return error;
}
throwError(message, code, params) {
throw this.makeError(message, code, params);
}
throwArgumentError(message, name, value) {
return this.throwError(message, _Logger.errors.INVALID_ARGUMENT, {
argument: name,
value
});
}
assert(condition, message, code, params) {
if (!!condition) {
return;
}
this.throwError(message, code, params);
}
assertArgument(condition, message, name, value) {
if (!!condition) {
return;
}
this.throwArgumentError(message, name, value);
}
checkNormalize(message) {
if (message == null) {
message = "platform missing String.prototype.normalize";
}
if (_normalizeError) {
this.throwError("platform missing String.prototype.normalize", _Logger.errors.UNSUPPORTED_OPERATION, {
operation: "String.prototype.normalize",
form: _normalizeError
});
}
}
checkSafeUint53(value, message) {
if (typeof value !== "number") {
return;
}
if (message == null) {
message = "value not safe";
}
if (value < 0 || value >= 9007199254740991) {
this.throwError(message, _Logger.errors.NUMERIC_FAULT, {
operation: "checkSafeInteger",
fault: "out-of-safe-range",
value
});
}
if (value % 1) {
this.throwError(message, _Logger.errors.NUMERIC_FAULT, {
operation: "checkSafeInteger",
fault: "non-integer",
value
});
}
}
checkArgumentCount(count, expectedCount, message) {
if (message) {
message = ": " + message;
} else {
message = "";
}
if (count < expectedCount) {
this.throwError("missing argument" + message, _Logger.errors.MISSING_ARGUMENT, {
count,
expectedCount
});
}
if (count > expectedCount) {
this.throwError("too many arguments" + message, _Logger.errors.UNEXPECTED_ARGUMENT, {
count,
expectedCount
});
}
}
checkNew(target, kind) {
if (target === Object || target == null) {
this.throwError("missing new", _Logger.errors.MISSING_NEW, { name: kind.name });
}
}
checkAbstract(target, kind) {
if (target === kind) {
this.throwError(
"cannot instantiate abstract class " + JSON.stringify(kind.name) + " directly; use a sub-class",
_Logger.errors.UNSUPPORTED_OPERATION,
{ name: target.name, operation: "new" }
);
} else if (target === Object || target == null) {
this.throwError("missing new", _Logger.errors.MISSING_NEW, { name: kind.name });
}
}
static globalLogger() {
if (!_globalLogger) {
_globalLogger = new _Logger(version);
}
return _globalLogger;
}
static setCensorship(censorship, permanent) {
if (!censorship && permanent) {
this.globalLogger().throwError("cannot permanently disable censorship", _Logger.errors.UNSUPPORTED_OPERATION, {
operation: "setCensorship"
});
}
if (_permanentCensorErrors) {
if (!censorship) {
return;
}
this.globalLogger().throwError("error censorship permanent", _Logger.errors.UNSUPPORTED_OPERATION, {
operation: "setCensorship"
});
}
_censorErrors = !!censorship;
_permanentCensorErrors = !!permanent;
}
static setLogLevel(logLevel) {
const level = LogLevels[logLevel.toLowerCase()];
if (level == null) {
_Logger.globalLogger().warn("invalid log level - " + logLevel);
return;
}
_logLevel = level;
}
static from(version3) {
return new _Logger(version3);
}
};
_Logger.errors = ErrorCode;
_Logger.levels = LogLevel;
var Logger = _Logger;
// src/sdk/common/utils/hexlify.ts
function isHexableValue(value) {
return !!value.toHexString;
}
function isInteger(value) {
return typeof value === "number" && value == value && value % 1 === 0;
}
function isBytes2(value) {
if (value == null) {
return false;
}
if (value.constructor === Uint8Array) {
return true;
}
if (typeof value === "string") {
return false;
}
if (!isInteger(value.length) || value.length < 0) {
return false;
}
for (let i = 0; i < value.length; i++) {
const v = value[i];
if (!isInteger(v) || v < 0 || v >= 256) {
return false;
}
}
return true;
}
function isHexString(value, length) {
if (typeof value !== "string" || !value.match(/^0x[0-9A-Fa-f]*$/)) {
return false;
}
if (length && value.length !== 2 + 2 * length) {
return false;
}
return true;
}
function checkSafeUint53(value, message) {
if (typeof value !== "number") {
return;
}
if (message == null) {
message = "value not safe";
}
if (value < 0 || value >= 9007199254740991) {
throw new Error(`Invalid Hexlify value - CheckSafeInteger Failed due to out-of-safe-range value: ${value}`);
}
if (value % 1) {
throw new Error(`Invalid Hexlify value - CCheckSafeInteger Failed due to non-integer value: ${value}`);
}
}
var HexCharacters = "0123456789abcdef";
function hexlifyValue(value, options) {
if (!options) {
options = {};
}
if (typeof value === "number") {
checkSafeUint53(value);
let hex = "";
while (value) {
hex = HexCharacters[value & 15] + hex;
value = Math.floor(value / 16);
}
if (hex.length) {
if (hex.length % 2) {
hex = "0" + hex;
}
return "0x" + hex;
}
return "0x00";
}
if (typeof value === "bigint") {
value = value.toString(16);
if (value.length % 2) {
return "0x0" + value;
}
return "0x" + value;
}
if (options.allowMissingPrefix && typeof value === "string" && value.substring(0, 2) !== "0x") {
value = "0x" + value;
}
if (isHexableValue(value)) {
return value.toHexString();
}
if (isHexString(value)) {
if (value.length % 2) {
if (options.hexPad === "left") {
value = "0x0" + value.substring(2);
} else if (options.hexPad === "right") {
value += "0";
} else {
throw new Error(`invalid hexlify value - hex data is odd-length for value: ${value}`);
}
}
return value.toLowerCase();
}
if (isBytes2(value)) {
let result = "0x";
for (let i = 0; i < value.length; i++) {
let v = value[i];
result += HexCharacters[(v & 240) >> 4] + HexCharacters[v & 15];
}
return result;
}
throw new Error(`invalid hexlify value - ${value}`);
}
// src/sdk/types/bignumber.ts
var version2 = "logger/5.7.0";
var logger = new Logger(version2);
var _constructorGuard = {};
var MAX_SAFE = 9007199254740991;
var _warnedToStringRadix = false;
var BigNumber = class _BigNumber {
constructor(constructorGuard, hex) {
if (constructorGuard !== _constructorGuard) {
logger.throwError("cannot call constructor directly; use BigNumber.from", Logger.errors.UNSUPPORTED_OPERATION, {
operation: "new (BigNumber)"
});
}
this._hex = hex;
this._isBigNumber = true;
Object.freeze(this);
}
fromTwos(value) {
return toBigNumber(toBN(this).fromTwos(value));
}
toTwos(value) {
return toBigNumber(toBN(this).toTwos(value));
}
abs() {
if (this._hex[0] === "-") {
return _BigNumber.from(this._hex.substring(1));
}
return this;
}
add(other) {
return toBigNumber(toBN(this).add(toBN(other)));
}
sub(other) {
return toBigNumber(toBN(this).sub(toBN(other)));
}
div(other) {
const o = _BigNumber.from(other);
if (o.isZero()) {
throwFault("division-by-zero", "div");
}
return toBigNumber(toBN(this).div(toBN(other)));
}
mul(other) {
return toBigNumber(toBN(this).mul(toBN(other)));
}
mod(other) {
const value = toBN(other);
if (value.isNeg()) {
throwFault("division-by-zero", "mod");
}
return toBigNumber(toBN(this).umod(value));
}
pow(other) {
const value = toBN(other);
if (value.isNeg()) {
throwFault("negative-power", "pow");
}
return toBigNumber(toBN(this).pow(value));
}
and(other) {
const value = toBN(other);
if (this.isNegative() || value.isNeg()) {
throwFault("unbound-bitwise-result", "and");
}
return toBigNumber(toBN(this).and(value));
}
or(other) {
const value = toBN(other);
if (this.isNegative() || value.isNeg()) {
throwFault("unbound-bitwise-result", "or");
}
return toBigNumber(toBN(this).or(value));
}
xor(other) {
const value = toBN(other);
if (this.isNegative() || value.isNeg()) {
throwFault("unbound-bitwise-result", "xor");
}
return toBigNumber(toBN(this).xor(value));
}
mask(value) {
if (this.isNegative() || value < 0) {
throwFault("negative-width", "mask");
}
return toBigNumber(toBN(this).maskn(value));
}
shl(value) {
if (this.isNegative() || value < 0) {
throwFault("negative-width", "shl");
}
return toBigNumber(toBN(this).shln(value));
}
shr(value) {
if (this.isNegative() || value < 0) {
throwFault("negative-width", "shr");
}
return toBigNumber(toBN(this).shrn(value));
}
eq(other) {
return toBN(this).eq(toBN(other));
}
lt(other) {
return toBN(this).lt(toBN(other));
}
lte(other) {
return toBN(this).lte(toBN(other));
}
gt(other) {
return toBN(this).gt(toBN(other));
}
gte(other) {
return toBN(this).gte(toBN(other));
}
isNegative() {
return this._hex[0] === "-";
}
isZero() {
return toBN(this).isZero();
}
toNumber() {
try {
return toBN(this).toNumber();
} catch (error) {
throwFault("overflow", "toNumber", this.toString());
}
return null;
}
toBigInt() {
try {
return BigInt(this.toString());
} catch (e) {
}
return logger.throwError("this platform does not support BigInt", Logger.errors.UNSUPPORTED_OPERATION, {
value: this.toString()
});
}
toString() {
if (arguments.length > 0) {
if (arguments[0] === 10) {
if (!_warnedToStringRadix) {
_warnedToStringRadix = true;
logger.warn("BigNumber.toString does not accept any parameters; base-10 is assumed");
}
} else if (arguments[0] === 16) {
logger.throwError("BigNumber.toString does not accept any parameters; use bigNumber.toHexString()", Logger.errors.UNEXPECTED_ARGUMENT, {});
} else {
logger.throwError("BigNumber.toString does not accept parameters", Logger.errors.UNEXPECTED_ARGUMENT, {});
}
}
return toBN(this).toString(10);
}
toHexString() {
return this._hex;
}
toJSON(key) {
return { type: "BigNumber", hex: this.toHexString() };
}
static from(value) {
if (value instanceof _BigNumber) {
return value;
}
if (typeof value === "string") {
if (value.match(/^-?0x[0-9a-f]+$/i)) {
return new _BigNumber(_constructorGuard, toHex(value));
}
if (value.match(/^-?[0-9]+$/)) {
return new _BigNumber(_constructorGuard, toHex(new import_bn.default(value)));
}
return logger.throwArgumentError("invalid BigNumber string", "value", value);
}
if (typeof value === "number") {
if (value % 1) {
throwFault("underflow", "BigNumber.from", value);
}
if (value >= MAX_SAFE || value <= -MAX_SAFE) {
throwFault("overflow", "BigNumber.from", value);
}
return _BigNumber.from(String(value));
}
const anyValue = value;
if (typeof anyValue === "bigint") {
return _BigNumber.from(anyValue.toString());
}
if (isBytes(anyValue)) {
return _BigNumber.from(hexlifyValue(anyValue));
}
if (anyValue) {
if (anyValue.toHexString) {
const hex = anyValue.toHexString();
if (typeof hex === "string") {
return _BigNumber.from(hex);
}
} else {
let hex = anyValue._hex;
if (hex == null && anyValue.type === "BigNumber") {
hex = anyValue.hex;
}
if (typeof hex === "string") {
if (isHex(hex) || hex[0] === "-" && isHex(hex.substring(1))) {
return _BigNumber.from(hex);
}
}
}
}
return logger.throwArgumentError("invalid BigNumber value", "value", value);
}
static isBigNumber(value) {
return !!(value && value._isBigNumber);
}
};
function toHex(value) {
if (typeof value !== "string") {
return toHex(value.toString(16));
}
if (value[0] === "-") {
value = value.substring(1);
if (value[0] === "-") {
throw new Error(`invalid hex value: ${value}`);
}
value = toHex(value);
if (value === "0x00") {
return value;
}
return "-" + value;
}
if (value.substring(0, 2) !== "0x") {
value = "0x" + value;
}
if (value === "0x") {
return "0x00";
}
if (value.length % 2) {
value = "0x0" + value.substring(2);
}
while (value.length > 4 && value.substring(0, 4) === "0x00") {
value = "0x" + value.substring(4);
}
return value;
}
function toBigNumber(value) {
return BigNumber.from(toHex(value));
}
function toBN(value) {
const hex = BigNumber.from(value).toHexString();
if (hex[0] === "-") {
return new import_bn.default("-" + hex.substring(3), 16);
}
return new import_bn.default(hex.substring(2), 16);
}
function throwFault(fault, operation, value) {
const params = { fault, operation };
if (value != null) {
params.value = value;
}
return logger.throwError(fault, Logger.errors.NUMERIC_FAULT, params);
}
// src/sdk/common/utils/bignumber-utils.ts
function bigNumberishToBigInt(value) {
if (typeof value === "bigint") {
return value;
} else if (typeof value === "string" || typeof value === "number") {
return BigInt(value);
} else if (BigNumber.isBigNumber(value)) {
return BigInt(value.toString());
} else {
throw new Error("Unsupported BigNumberish type");
}
}
function isBigNumber(value) {
return value instanceof BigNumber;
}
function fromBigInt(value) {
return BigNumber.from(value.toString());
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
bigNumberishToBigInt,
fromBigInt,
isBigNumber
});
//# sourceMappingURL=bignumber-utils.js.map