@biconomy/abstractjs
Version:
SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.
98 lines • 4.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.signMMDtkQuote = void 0;
const delegation_toolkit_1 = require("@metamask/delegation-toolkit");
const viem_1 = require("viem");
const MM_DTK_PREFIX = "0x177eee03";
const signMMDtkQuote = async (client, parameters) => {
const { companionAccount: account_ = client.account, delegatorSmartAccount, fusionQuote: { quote, trigger } } = parameters;
if (trigger.call) {
throw new Error("Custom triggers are not supported for fusion transactions");
}
if (!trigger.amount)
throw new Error("Amount is required to sign a MM DTK quote");
const environment = delegatorSmartAccount.environment;
const caveatBuilder = (0, delegation_toolkit_1.createCaveatBuilder)(environment);
const approvedCallData = (0, viem_1.concatHex)([
(0, viem_1.encodeFunctionData)({
abi: viem_1.erc20Abi,
functionName: "approve",
args: [
account_.addressOn(trigger.chainId, true),
trigger.amount
]
}),
quote.hash
]);
const caveats = caveatBuilder.addCaveat("exactExecution", {
target: trigger.tokenAddress,
value: 0n,
callData: approvedCallData
});
const openRootDelegation = (0, delegation_toolkit_1.createOpenDelegation)({
from: delegatorSmartAccount.address,
caveats
});
const signature = await delegatorSmartAccount.signDelegation({
delegation: openRootDelegation
});
const delegationManager = getDelegationManager(trigger.chainId);
const redeemDelegationErc7579ExecutionCalldata = (0, viem_1.encodePacked)(["address", "uint256", "bytes"], [trigger.tokenAddress, 0n, approvedCallData]);
const encodedSignature = (0, viem_1.encodeAbiParameters)([
{ name: "delegationManager", type: "address" },
{
name: "delegation",
type: "tuple",
components: [
{ name: "delegate", type: "address" },
{ name: "delegator", type: "address" },
{ name: "authority", type: "bytes32" },
{
name: "caveats",
type: "tuple[]",
components: [
{ name: "enforcer", type: "address" },
{ name: "terms", type: "bytes" },
{ name: "args", type: "bytes" }
]
},
{ name: "salt", type: "uint256" },
{ name: "signature", type: "bytes" }
]
},
{ name: "redeemDelegationErc7579ExecutionCalldata", type: "bytes" }
], [
delegationManager,
{
delegate: openRootDelegation.delegate,
delegator: openRootDelegation.delegator,
authority: openRootDelegation.authority,
caveats: openRootDelegation.caveats.map((caveat) => ({
enforcer: caveat.enforcer,
terms: caveat.terms,
args: caveat.args
})),
salt: openRootDelegation.salt === "0x"
? BigInt(0)
: BigInt(openRootDelegation.salt),
signature: signature
},
redeemDelegationErc7579ExecutionCalldata
]);
return { ...quote, signature: (0, viem_1.concatHex)([MM_DTK_PREFIX, encodedSignature]) };
};
exports.signMMDtkQuote = signMMDtkQuote;
exports.default = exports.signMMDtkQuote;
const getDelegationManager = (chainId) => {
if (delegation_toolkit_1.DELEGATOR_CONTRACTS["1.3.0"][chainId]?.DelegationManager) {
return delegation_toolkit_1.DELEGATOR_CONTRACTS["1.3.0"][chainId].DelegationManager;
}
if (delegation_toolkit_1.DELEGATOR_CONTRACTS["1.1.0"][chainId]?.DelegationManager) {
return delegation_toolkit_1.DELEGATOR_CONTRACTS["1.1.0"][chainId].DelegationManager;
}
if (delegation_toolkit_1.DELEGATOR_CONTRACTS["1.0.0"][chainId]?.DelegationManager) {
return delegation_toolkit_1.DELEGATOR_CONTRACTS["1.0.0"][chainId].DelegationManager;
}
throw new Error(`No delegation manager found for chainId ${chainId}. Supported chains are: ${Object.values(delegation_toolkit_1.CHAIN_ID).join(", ")}`);
};
//# sourceMappingURL=signMmDtkQuote.js.map