UNPKG

@biconomy/abstractjs

Version:

SDK for Biconomy integration with support for account abstraction, smart accounts, ERC-4337.

99 lines 3.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.signPermitQuote = void 0; const viem_1 = require("viem"); const constants_1 = require("../../../constants/index.js"); const TokenWithPermitAbi_1 = require("../../../constants/abi/TokenWithPermitAbi.js"); const PERMIT_PREFIX = "0x177eee02"; const signPermitQuote = async (client, parameters) => { const { companionAccount: account_ = client.account, fusionQuote: { quote, trigger } } = parameters; if (trigger.call) { throw new Error("Custom triggers are not supported for permit quotes"); } const signer = account_.signer; if (!trigger.amount) throw new Error("Amount is required to sign a permit quote"); if (trigger.approvalAmount && trigger.amount !== undefined && trigger.approvalAmount < trigger.amount) { throw new Error(`Approval amount must be bigger or equal with the amount from the trigger (triggerAmount: ${trigger.amount} amount: ${trigger.approvalAmount})`); } const amount = trigger.approvalAmount ?? trigger.amount; const { walletClient, address: spender } = account_.deploymentOn(trigger.chainId, true); const owner = signer.address; const token = (0, viem_1.getContract)({ abi: TokenWithPermitAbi_1.TokenWithPermitAbi, address: trigger.tokenAddress, client: walletClient }); const values = await Promise.allSettled([ token.read.nonces([owner]), token.read.name(), token.read.version(), token.read.DOMAIN_SEPARATOR() ]); const [nonce, name, version, domainSeparator] = values.map((value, i) => { const key = ["nonce", "name", "version", "domainSeparator"][i]; if (value.status === "fulfilled") { return value.value; } if (value.status === "rejected" && key === "version") { return "1"; } throw new Error(`Failed to get value: ${value.reason}`); }); const signature = await walletClient.signTypedData({ domain: { name, version, chainId: trigger.chainId, verifyingContract: trigger.tokenAddress }, types: { Permit: [ { name: "owner", type: "address" }, { name: "spender", type: "address" }, { name: "value", type: "uint256" }, { name: "nonce", type: "uint256" }, { name: "deadline", type: "uint256" } ] }, primaryType: "Permit", message: { owner, spender: spender, value: amount, nonce, deadline: BigInt(quote.hash) }, account: walletClient.account }); const sigComponents = (0, viem_1.parseSignature)(signature); const encodedSignature = (0, viem_1.encodeAbiParameters)([ { name: "token", type: "address" }, { name: "spender", type: "address" }, { name: "domainSeparator", type: "bytes32" }, { name: "permitTypehash", type: "bytes32" }, { name: "amount", type: "uint256" }, { name: "chainId", type: "uint256" }, { name: "nonce", type: "uint256" }, { name: "v", type: "uint256" }, { name: "r", type: "bytes32" }, { name: "s", type: "bytes32" } ], [ trigger.tokenAddress, spender, domainSeparator, constants_1.PERMIT_TYPEHASH, amount, BigInt(trigger.chainId), nonce, sigComponents.v, sigComponents.r, sigComponents.s ]); return { ...quote, signature: (0, viem_1.concatHex)([PERMIT_PREFIX, encodedSignature]) }; }; exports.signPermitQuote = signPermitQuote; exports.default = exports.signPermitQuote; //# sourceMappingURL=signPermitQuote.js.map