import { SimpleAccountFactoryABI } from './chunk-ZSSNU3UF.js';
import { http, createWalletClient, encodeAbiParameters, keccak256, encodeFunctionData, concatHex } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';

var createEOAWalletClient = (privateKey, chain, transport = http()) => {
  const account = privateKeyToAccount(privateKey);
  const client = createWalletClient({
    account,
    chain,
    transport
  });
  return Object.assign(client, {
    async sendTransaction(args) {
      return client.sendTransaction({
        ...args,
        account,
        chain
      });
    },
    getAddress: () => account.address
  });
};
function packUserOpLimits(high, low) {
  return `0x${(high << 128n | low).toString(16).padStart(64, "0")}`;
}
function getUserOpHash(op, ep, chainId) {
  const packed = encodeAbiParameters([
    { type: "address" },
    { type: "uint256" },
    { type: "bytes32" },
    { type: "bytes32" },
    { type: "bytes32" },
    { type: "uint256" },
    { type: "bytes32" },
    { type: "bytes32" }
  ], [
    op.sender,
    BigInt(op.nonce),
    keccak256(op.initCode && op.initCode !== "0x" ? op.initCode : "0x"),
    keccak256(op.callData),
    op.accountGasLimits,
    BigInt(op.preVerificationGas),
    op.gasFees,
    keccak256(op.paymasterAndData)
  ]);
  const enc = encodeAbiParameters([{ type: "bytes32" }, { type: "address" }, { type: "uint256" }], [keccak256(packed), ep, BigInt(chainId)]);
  return keccak256(enc);
}
var UserOpClient = class {
  static async estimateGas(bundler, op, entryPoint) {
    return bundler.request({
      method: "eth_estimateUserOperationGas",
      params: [op, entryPoint]
    });
  }
  static async sendUserOp(bundler, op, entryPoint) {
    return bundler.request({
      method: "eth_sendUserOperation",
      params: [op, entryPoint]
    });
  }
  static async getReceipt(bundler, hash) {
    return bundler.request({
      method: "eth_getUserOperationReceipt",
      params: [hash]
    });
  }
};

// ../account/dist/accounts/simple.js
async function toSimpleSmartAccount(parameters) {
  const { client, owner, factoryAddress, entryPoint, index = 0n, salt = 0n } = parameters;
  const factoryData = encodeFunctionData({
    abi: SimpleAccountFactoryABI,
    functionName: "createAccount",
    args: [owner.address, salt]
  });
  const initCode = concatHex([factoryAddress, factoryData]);
  const address = await client.readContract({
    address: factoryAddress,
    abi: SimpleAccountFactoryABI,
    functionName: "getAddress",
    args: [owner.address, salt]
  });
  return {
    address,
    publicKey: owner.address,
    source: "custom",
    type: "local",
    entryPoint: entryPoint.address,
    async signMessage({ message }) {
      return owner.signMessage({ message });
    },
    async signTypedData(typedData) {
      return owner.signTypedData(typedData);
    },
    async signTransaction(transaction) {
      throw new Error("Smart Accounts cannot sign transactions directly. Use UserOperations.");
    },
    async signUserOperation(userOp) {
      const chainId = client.chain?.id || 31337;
      const hash = getUserOpHash(userOp, entryPoint.address, chainId);
      return owner.signMessage({ message: { raw: hash } });
    },
    async getInitCode() {
      return initCode;
    },
    async getDummySignature() {
      return "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
    }
  };
}

export { UserOpClient, createEOAWalletClient, getUserOpHash, packUserOpLimits, toSimpleSmartAccount };
//# sourceMappingURL=chunk-HVAB4TTT.js.map
//# sourceMappingURL=chunk-HVAB4TTT.js.map