|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { |
| 4 | + Implementation, |
| 5 | + toMetaMaskSmartAccount, |
| 6 | +} from '@metamask/delegation-toolkit'; |
| 7 | +import { |
| 8 | + Domain, |
| 9 | + SigningCoordinatorAgent, |
| 10 | + UserOperation, |
| 11 | +} from '@nucypher/shared'; |
| 12 | +import { conditions, domains, initialize, signUserOp } from '@nucypher/taco'; |
| 13 | +import * as dotenv from 'dotenv'; |
| 14 | +import { ethers } from 'ethers'; |
| 15 | +import { Address, createPublicClient, http, parseEther } from 'viem'; |
| 16 | +import { |
| 17 | + createBundlerClient, |
| 18 | + createPaymasterClient, |
| 19 | +} from 'viem/account-abstraction'; |
| 20 | +import { privateKeyToAccount } from 'viem/accounts'; |
| 21 | +import { sepolia } from 'viem/chains'; |
| 22 | + |
| 23 | +dotenv.config(); |
| 24 | + |
| 25 | +const SEPOLIA_CHAIN_ID = 11155111; |
| 26 | +const TACO_DOMAIN: Domain = domains.DEVNET; |
| 27 | +const COHORT_ID = 1; |
| 28 | +const AA_VERSION = 'mdt'; |
| 29 | + |
| 30 | +async function createTacoSmartAccount( |
| 31 | + publicClient: any, |
| 32 | + localAccount: any, |
| 33 | + provider: ethers.providers.JsonRpcProvider, |
| 34 | +) { |
| 35 | + await initialize(); |
| 36 | + const participants = await SigningCoordinatorAgent.getParticipants( |
| 37 | + provider, |
| 38 | + TACO_DOMAIN, |
| 39 | + COHORT_ID, |
| 40 | + ); |
| 41 | + const threshold = await SigningCoordinatorAgent.getThreshold( |
| 42 | + provider, |
| 43 | + TACO_DOMAIN, |
| 44 | + COHORT_ID, |
| 45 | + ); |
| 46 | + const signers = participants.map((p: any) => p.operator as Address).sort(); |
| 47 | + |
| 48 | + const smartAccount = await toMetaMaskSmartAccount({ |
| 49 | + client: publicClient, |
| 50 | + implementation: Implementation.MultiSig, |
| 51 | + deployParams: [signers, BigInt(threshold)], |
| 52 | + deploySalt: '0x' as `0x${string}`, |
| 53 | + signatory: [{ account: localAccount }], |
| 54 | + }); |
| 55 | + |
| 56 | + return { smartAccount, threshold }; |
| 57 | +} |
| 58 | + |
| 59 | +async function signUserOpWithTaco( |
| 60 | + userOp: any, |
| 61 | + provider: ethers.providers.JsonRpcProvider, |
| 62 | +) { |
| 63 | + const signingContext = |
| 64 | + await conditions.context.ConditionContext.forSigningCohort( |
| 65 | + provider, |
| 66 | + TACO_DOMAIN, |
| 67 | + COHORT_ID, |
| 68 | + SEPOLIA_CHAIN_ID, |
| 69 | + ); |
| 70 | + |
| 71 | + const tacoUserOp: UserOperation = { |
| 72 | + sender: userOp.sender, |
| 73 | + nonce: Number(userOp.nonce), |
| 74 | + factory: userOp.factory || '0x', |
| 75 | + factoryData: userOp.factoryData || '0x', |
| 76 | + callData: userOp.callData, |
| 77 | + callGasLimit: Number(userOp.callGasLimit), |
| 78 | + verificationGasLimit: Number(userOp.verificationGasLimit), |
| 79 | + preVerificationGas: Number(userOp.preVerificationGas), |
| 80 | + maxFeePerGas: Number(userOp.maxFeePerGas), |
| 81 | + maxPriorityFeePerGas: Number(userOp.maxPriorityFeePerGas), |
| 82 | + paymaster: userOp.paymaster || '0x', |
| 83 | + paymasterVerificationGasLimit: Number( |
| 84 | + userOp.paymasterVerificationGasLimit || 0, |
| 85 | + ), |
| 86 | + paymasterPostOpGasLimit: Number(userOp.paymasterPostOpGasLimit || 0), |
| 87 | + paymasterData: userOp.paymasterData || '0x', |
| 88 | + signature: '0x', |
| 89 | + }; |
| 90 | + |
| 91 | + return await signUserOp( |
| 92 | + provider, |
| 93 | + TACO_DOMAIN, |
| 94 | + COHORT_ID, |
| 95 | + SEPOLIA_CHAIN_ID, |
| 96 | + tacoUserOp, |
| 97 | + AA_VERSION, |
| 98 | + signingContext, |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +async function logBalances( |
| 103 | + provider: ethers.providers.JsonRpcProvider, |
| 104 | + eoaAddress: string, |
| 105 | + smartAccountAddress: string, |
| 106 | +) { |
| 107 | + const eoaBalance = await provider.getBalance(eoaAddress); |
| 108 | + const smartAccountBalance = await provider.getBalance(smartAccountAddress); |
| 109 | + console.log(`\n💳 EOA Balance: ${ethers.utils.formatEther(eoaBalance)} ETH`); |
| 110 | + console.log(`🏦 Smart Account: ${ethers.utils.formatEther(smartAccountBalance)} ETH\n`); |
| 111 | +} |
| 112 | + |
| 113 | +async function main() { |
| 114 | + try { |
| 115 | + const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL!); |
| 116 | + const localAccount = privateKeyToAccount( |
| 117 | + process.env.PRIVATE_KEY as `0x${string}`, |
| 118 | + ); |
| 119 | + const publicClient = createPublicClient({ |
| 120 | + chain: sepolia, |
| 121 | + transport: http(process.env.RPC_URL), |
| 122 | + }); |
| 123 | + |
| 124 | + const paymasterClient = createPaymasterClient({ |
| 125 | + transport: http(process.env.BUNDLER_URL), |
| 126 | + }); |
| 127 | + const bundlerClient = createBundlerClient({ |
| 128 | + transport: http(process.env.BUNDLER_URL), |
| 129 | + paymaster: paymasterClient, |
| 130 | + chain: sepolia, |
| 131 | + }); |
| 132 | + |
| 133 | + const fee = { |
| 134 | + maxFeePerGas: parseEther('0.00001'), |
| 135 | + maxPriorityFeePerGas: parseEther('0.000001'), |
| 136 | + }; |
| 137 | + |
| 138 | + console.log('🔧 Creating TACo smart account...\n'); |
| 139 | + const { smartAccount, threshold } = await createTacoSmartAccount( |
| 140 | + publicClient, |
| 141 | + localAccount, |
| 142 | + provider, |
| 143 | + ); |
| 144 | + console.log(`✅ Smart account created: ${smartAccount.address}`); |
| 145 | + console.log(`🔐 Threshold: ${threshold} signatures required\n`); |
| 146 | + |
| 147 | + await logBalances(provider, localAccount.address, smartAccount.address); |
| 148 | + |
| 149 | + const smartAccountBalance = await provider.getBalance(smartAccount.address); |
| 150 | + if (smartAccountBalance.lt(ethers.utils.parseEther('0.01'))) { |
| 151 | + console.log('💰 Funding smart account...'); |
| 152 | + const eoaWallet = new ethers.Wallet( |
| 153 | + process.env.PRIVATE_KEY as string, |
| 154 | + provider, |
| 155 | + ); |
| 156 | + const fundTx = await eoaWallet.sendTransaction({ |
| 157 | + to: smartAccount.address, |
| 158 | + value: ethers.utils.parseEther('0.001'), |
| 159 | + }); |
| 160 | + await fundTx.wait(); |
| 161 | + console.log(`✅ Funded successfully!\n🔗 Tx: ${fundTx.hash}`); |
| 162 | + await logBalances(provider, localAccount.address, smartAccount.address); |
| 163 | + } |
| 164 | + |
| 165 | + const currentBalance = await provider.getBalance(smartAccount.address); |
| 166 | + const gasReserve = ethers.utils.parseEther('0.0005'); |
| 167 | + const transferAmount = currentBalance.gt(gasReserve) |
| 168 | + ? currentBalance.sub(gasReserve) |
| 169 | + : parseEther('0.0001'); |
| 170 | + |
| 171 | + console.log('📝 Preparing transaction...'); |
| 172 | + const userOp = await bundlerClient.prepareUserOperation({ |
| 173 | + account: smartAccount, |
| 174 | + calls: [ |
| 175 | + { |
| 176 | + target: localAccount.address, |
| 177 | + value: transferAmount, |
| 178 | + data: '0x', |
| 179 | + }, |
| 180 | + ], |
| 181 | + ...fee, |
| 182 | + verificationGasLimit: BigInt(500_000), |
| 183 | + }); |
| 184 | + console.log(`💸 Transfer amount: ${ethers.utils.formatEther(transferAmount)} ETH\n`); |
| 185 | + |
| 186 | + console.log('🔏 Signing with TACo...'); |
| 187 | + const signature = await signUserOpWithTaco(userOp, provider); |
| 188 | + console.log(`✅ Signature collected (${signature.aggregatedSignature.length / 2 - 1} bytes)\n`); |
| 189 | + |
| 190 | + console.log('🚀 Executing transaction...'); |
| 191 | + const userOpHash = await bundlerClient.sendUserOperation({ |
| 192 | + ...userOp, |
| 193 | + signature: signature.aggregatedSignature as `0x${string}`, |
| 194 | + }); |
| 195 | + console.log(`📝 UserOp Hash: ${userOpHash}`); |
| 196 | + |
| 197 | + const { receipt } = await bundlerClient.waitForUserOperationReceipt({ |
| 198 | + hash: userOpHash, |
| 199 | + }); |
| 200 | + console.log(`\n🎉 Transaction successful!`); |
| 201 | + console.log(`🔗 Tx: ${receipt.transactionHash}`); |
| 202 | + console.log(`🌐 View on Etherscan: https://sepolia.etherscan.io/tx/${receipt.transactionHash}\n`); |
| 203 | + |
| 204 | + await logBalances(provider, localAccount.address, smartAccount.address); |
| 205 | + console.log('✨ Demo completed successfully! ✨'); |
| 206 | + process.exit(0); |
| 207 | + } catch (error: any) { |
| 208 | + console.error(`❌ Demo failed: ${error.message}`); |
| 209 | + process.exit(1); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +if (require.main === module) { |
| 214 | + main(); |
| 215 | +} |
0 commit comments