UNPKG

@etherspot/prime-sdk

Version:

Etherspot Prime (Account Abstraction) SDK

75 lines (74 loc) 3.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SimpleAccountAPI = void 0; const ethers_1 = require("ethers"); const contracts_1 = require("../contracts"); const utils_1 = require("ethers/lib/utils"); const BaseAccountAPI_1 = require("./BaseAccountAPI"); const SimpleAccountAbi_1 = require("../contracts/SimpleAccount/SimpleAccountAbi"); const SimpleAccountFactoryAbi_1 = require("../contracts/SimpleAccount/SimpleAccountFactoryAbi"); class SimpleAccountAPI extends BaseAccountAPI_1.BaseAccountAPI { constructor(params) { var _a; super(params); this.factoryAddress = params.factoryAddress; this.index = (_a = params.index) !== null && _a !== void 0 ? _a : 0; } async _getAccountContract() { this.accountContract = new ethers_1.ethers.Contract(this.accountAddress, SimpleAccountAbi_1.SimpleAccountAbi, this.provider); return this.accountContract; } async getAccountInitCode() { this.factory = new ethers_1.ethers.Contract(this.factoryAddress, SimpleAccountFactoryAbi_1.SimpleAccountFactoryAbi, this.provider); return (0, utils_1.hexConcat)([ this.factoryAddress, this.factory.interface.encodeFunctionData('createAccount', [ this.services.walletService.EOAAddress, this.index, ]), ]); } async getCounterFactualAddress() { var _a; if (!this.accountAddress) { try { const initCode = await this.getAccountInitCode(); const entryPoint = contracts_1.EntryPoint__factory.connect(this.entryPointAddress, this.provider); await entryPoint.callStatic.getSenderAddress(initCode); throw new Error("getSenderAddress: unexpected result"); } catch (error) { const addr = (_a = error === null || error === void 0 ? void 0 : error.errorArgs) === null || _a === void 0 ? void 0 : _a.sender; if (!addr) throw error; if (addr === ethers_1.ethers.constants.AddressZero) throw new Error('Unsupported chain_id/walletFactoryAddress'); this.accountContract = new ethers_1.ethers.Contract(addr, SimpleAccountAbi_1.SimpleAccountAbi, this.provider); this.accountAddress = addr; } } return this.accountAddress; } async getNonce(key = 0) { if (await this.checkAccountPhantom()) { return ethers_1.BigNumber.from(0); } return await this.nonceManager.getNonce(await this.getAccountAddress(), key); } async encodeExecute(target, value, data) { const accountContract = await this._getAccountContract(); return accountContract.interface.encodeFunctionData('execute', [target, value, data]); } async signUserOpHash(userOpHash) { const signature = await this.services.walletService.signMessage((0, utils_1.arrayify)(userOpHash)); return signature; } get epView() { return this.entryPointView; } async encodeBatch(targets, values, datas) { const accountContract = await this._getAccountContract(); return accountContract.interface.encodeFunctionData('executeBatch', [targets, datas]); } } exports.SimpleAccountAPI = SimpleAccountAPI;