@kiroboio/fct-core
Version:
Kirobo.io FCT Core library
156 lines • 6.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Version_020201 = exports.Call = void 0;
const eth_sig_util_1 = require("@metamask/eth-sig-util");
const utils_1 = require("ethers/lib/utils");
const constants_1 = require("../../../constants");
const flows_1 = require("../../../constants/flows");
const classes_1 = require("../../classes");
const oldVersion_1 = require("../oldVersion");
const CallId_1 = require("./CallId");
const EIP712_1 = require("./EIP712");
const variableArgs_1 = require("./helpers/variableArgs");
const SessionId_1 = require("./SessionId");
const Utils_1 = require("./Utils");
const Limits = [
{ name: "valid_from", type: "uint40" },
{ name: "expires_at", type: "uint40" },
{ name: "tx_data_limit", type: "uint32" },
{ name: "payable_gas_limit", type: "uint32" },
{ name: "max_payable_gas_price", type: "uint40" },
{ name: "purgeable", type: "bool" },
{ name: "blockable", type: "bool" },
];
exports.Call = [
{ name: "call_index", type: "uint16" },
{ name: "payer_index", type: "uint16" },
{ name: "call_type", type: "string" },
{ name: "from", type: "address" },
{ name: "to", type: "address" },
{ name: "to_ens", type: "string" },
{ name: "value", type: "uint256" },
{ name: "gas_limit", type: "uint32" },
{ name: "permissions", type: "uint16" },
{ name: "validation", type: "uint16" },
{ name: "flow_control", type: "string" },
{ name: "returned_false_means_fail", type: "bool" },
{ name: "jump_on_success", type: "uint16" },
{ name: "jump_on_fail", type: "uint16" },
{ name: "variable_arguments_start", type: "uint32" },
{ name: "variable_arguments_end", type: "uint32" },
{ name: "method_interface", type: "string" },
];
// This version introduced payable_gas_limit, max_payable_gas_price, tx_data_limit.
// Removed gas_price_limit
class Version_020201 extends oldVersion_1.Version_old {
// public SessionId: SessionId_020201;
// public CallId: CallId_020201;
Limits = Limits;
Call = exports.Call;
batchMultiSigSelector = "0xead413ea";
constructor(FCT) {
super(FCT);
this.SessionId = new SessionId_1.SessionId_020201(FCT);
this.CallId = new CallId_1.CallId_020201(FCT);
this.EIP712 = new EIP712_1.EIP712_020201();
this.Utils = new Utils_1.Utils_020201(FCT);
}
getLimitsMessage(FCT) {
const FCTOptions = FCT.options;
return {
valid_from: FCTOptions.validFrom,
expires_at: FCTOptions.expiresAt,
tx_data_limit: "0",
payable_gas_limit: FCTOptions.payableGasLimit === undefined ? FCT.utils.getMaxGasIgnoreCalldata() : FCTOptions.payableGasLimit,
max_payable_gas_price: FCTOptions.maxGasPrice,
purgeable: FCTOptions.purgeable,
blockable: FCTOptions.blockable,
};
}
exportFCT(exportOptions) {
const FCT = this.FCT;
const strictGasLimits = typeof exportOptions?.strictGasLimits === "boolean" ? exportOptions.strictGasLimits : undefined;
const forceDryRun = Boolean(exportOptions?.forceDryRun);
if (!FCT) {
throw new Error("FCT is not defined, this should not happen");
}
if (FCT.calls.length === 0) {
throw new Error("No calls added to FCT");
}
const options = FCT.options;
const initialGasLimits = {};
if ((FCT.isImported && strictGasLimits === false) || (!FCT.isImported && !strictGasLimits)) {
FCT.calls.forEach((call, i) => {
initialGasLimits[i] = call.options.gasLimit;
call.setOptions({ gasLimit: "0" });
});
}
if (forceDryRun) {
FCT.setOptions({ forceDryRun: true });
}
const typedData = new classes_1.EIP712(FCT).getTypedData();
const payableGasLimit = typedData.message.limits.payable_gas_limit;
const FCTData = {
typedData,
typeHash: (0, utils_1.hexlify)(eth_sig_util_1.TypedDataUtils.hashType(typedData.primaryType, typedData.types)),
sessionId: this.SessionId.asString(),
nameHash: (0, utils_1.id)(options.name),
appHash: (0, utils_1.id)(options.app.name),
appVersionHash: (0, utils_1.id)(options.app.version),
builderHash: (0, utils_1.id)(options.builder.name),
builderAddress: options.builder.address,
domainHash: (0, utils_1.id)(options.domain),
verifierHash: (0, utils_1.id)(options.verifier),
mcall: FCT.calls.map((call, index) => call.getAsMCall(typedData, index)),
externalSigners: options.multisig.externalSigners,
signatures: [FCT.utils.getAuthenticatorSignature()],
computed: FCT.computedAsData,
validations: FCT.validation.getForData(),
variables: [],
txDataLimit: "0",
// payableGasLimit: "0",
payableGasLimit,
};
if (!strictGasLimits) {
FCT.calls.forEach((call, i) => {
call.setOptions({ gasLimit: initialGasLimits[i] });
});
}
if (forceDryRun) {
FCT.setOptions({ forceDryRun: false });
}
return FCTData;
}
generateCallForEIP712Message(call, index) {
const FCT = this.FCT;
if (!FCT) {
throw new Error("FCT is not defined, this should not happen");
}
const callData = call.get();
const options = call.options;
const flow = flows_1.flows[options.flow].text;
const { jumpOnSuccess, jumpOnFail } = call.getJumps(index);
const variableArgs = (0, variableArgs_1.getVariableArgsForEIP712)(call);
return {
call_index: index + 1,
payer_index: typeof call.options.payerIndex === "number" ? call.options.payerIndex : index + 1,
call_type: constants_1.CALL_TYPE_MSG[call.options.callType],
from: FCT.variables.getValue(callData.from, "address"),
to: FCT.variables.getValue(callData.to, "address"),
to_ens: callData.toENS || "",
value: FCT.variables.getValue(callData.value, "uint256", "0"),
gas_limit: callData.options.gasLimit,
permissions: 0,
validation: call.options.validation ? FCT.validation.getIndex(call.options.validation) : 0,
flow_control: flow,
returned_false_means_fail: options.falseMeansFail,
jump_on_success: jumpOnSuccess,
jump_on_fail: jumpOnFail,
variable_arguments_start: variableArgs.variable_arguments_start,
variable_arguments_end: variableArgs.variable_arguments_end,
method_interface: call.getFunction(),
};
}
}
exports.Version_020201 = Version_020201;
//# sourceMappingURL=index.js.map