@etherspot/prime-sdk
Version:
Etherspot Prime (Account Abstraction) SDK
113 lines (112 loc) • 5.26 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ZeroDevWalletAPI = 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 KernalAccountAbi_1 = require("../contracts/zeroDevKernal/KernalAccountAbi");
const MultiSendAbi_1 = require("../contracts/zeroDevKernal/MultiSendAbi");
const constants_1 = require("../network/constants");
const KernalFactoryAbi_1 = require("../contracts/zeroDevKernal/KernalFactoryAbi");
var Operation;
(function (Operation) {
Operation[Operation["Call"] = 0] = "Call";
Operation[Operation["DelegateCall"] = 1] = "DelegateCall";
})(Operation || (Operation = {}));
class ZeroDevWalletAPI 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, KernalAccountAbi_1.KernelAccountAbi, this.provider);
return this.accountContract;
}
async getKernelFactoryInitCode() {
try {
const KernalFactoryInterface = new ethers_1.ethers.utils.Interface(KernalFactoryAbi_1.KernelFactoryAbi);
return KernalFactoryInterface.encodeFunctionData('createAccount', [
"0xf048AD83CB2dfd6037A43902a2A5Be04e53cd2Eb",
new ethers_1.ethers.utils.Interface(KernalAccountAbi_1.KernelAccountAbi).encodeFunctionData("initialize", ["0xd9AB5096a832b9ce79914329DAEE236f8Eea0390", this.services.walletService.EOAAddress]),
this.index,
]);
}
catch (err) {
throw new Error("Factory Code generation failed");
}
}
async getAccountInitCode() {
this.factory = new ethers_1.ethers.Contract(this.factoryAddress, KernalFactoryAbi_1.KernelFactoryAbi, this.provider);
return (0, utils_1.hexConcat)([
this.factoryAddress, await this.getKernelFactoryInitCode()
]);
}
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');
const chain = await this.provider.getNetwork().then((n) => n.chainId);
const ms = constants_1.Safe.MultiSend[chain.toString()];
if (!ms)
throw new Error(`Multisend contract not deployed on network: ${chain.toString()}`);
this.multisend = new ethers_1.ethers.Contract(ms, MultiSendAbi_1.MultiSendAbi, this.provider);
this.accountContract = new ethers_1.ethers.Contract(addr, KernalAccountAbi_1.KernelAccountAbi, 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 signUserOpHash(userOpHash) {
const signature = await this.services.walletService.signMessage((0, utils_1.arrayify)(userOpHash));
return ethers_1.ethers.utils.hexConcat([
"0x00000000",
signature,
]);
}
get epView() {
return this.entryPointView;
}
async encodeExecute(target, value, data) {
const accountContract = await this._getAccountContract();
return accountContract.interface.encodeFunctionData('execute', [target, value, data]);
}
async encodeBatch(targets, values, datas) {
const accountContract = await this._getAccountContract();
const data = this.multisend.interface.encodeFunctionData("multiSend", [
ethers_1.ethers.utils.hexConcat(targets.map((c, index) => ethers_1.ethers.utils.solidityPack(["uint8", "address", "uint256", "uint256", "bytes"], [
Operation.Call,
c,
values[index],
ethers_1.ethers.utils.hexDataLength(datas[index]),
datas[index],
]))),
]);
return accountContract.interface.encodeFunctionData('execute', [
this.multisend.address,
ethers_1.ethers.constants.Zero,
data,
Operation.DelegateCall,
]);
}
}
exports.ZeroDevWalletAPI = ZeroDevWalletAPI;