|
147 | 147 |
|
148 | 148 | #endregion |
149 | 149 |
|
| 150 | +#region Engine Wallet |
| 151 | + |
| 152 | +// // EngineWallet is compatible with IThirdwebWallet and can be used with any SDK method/extension |
| 153 | +// var engineWallet = await EngineWallet.Create( |
| 154 | +// client: client, |
| 155 | +// engineUrl: Environment.GetEnvironmentVariable("ENGINE_URL"), |
| 156 | +// authToken: Environment.GetEnvironmentVariable("ENGINE_ACCESS_TOKEN"), |
| 157 | +// walletAddress: Environment.GetEnvironmentVariable("ENGINE_BACKEND_WALLET_ADDRESS"), |
| 158 | +// timeoutSeconds: null, // no timeout |
| 159 | +// additionalHeaders: null // can set things like x-account-address if using basic session keys |
| 160 | +// ); |
| 161 | + |
| 162 | +// // Simple self transfer |
| 163 | +// var receipt = await engineWallet.Transfer(chainId: 11155111, toAddress: await engineWallet.GetAddress(), weiAmount: 0); |
| 164 | +// Console.WriteLine($"Receipt: {receipt}"); |
| 165 | + |
| 166 | +#endregion |
| 167 | + |
150 | 168 | #region EIP-7702 |
151 | 169 |
|
152 | | -// -------------------------------------------------------------------------- |
153 | | -// Configuration |
154 | | -// -------------------------------------------------------------------------- |
155 | | - |
156 | | -var chainWith7702 = 911867; |
157 | | -var delegationContractAddress = "0xb012446cba783d0f7723daf96cf4c49005022307"; // MinimalAccount |
158 | | - |
159 | | -// Required environment variables |
160 | | -var backendWalletAddress = Environment.GetEnvironmentVariable("ENGINE_BACKEND_WALLET_ADDRESS") ?? throw new Exception("ENGINE_BACKEND_WALLET_ADDRESS is required"); |
161 | | -var engineUrl = Environment.GetEnvironmentVariable("ENGINE_URL") ?? throw new Exception("ENGINE_URL is required"); |
162 | | -var engineAccessToken = Environment.GetEnvironmentVariable("ENGINE_ACCESS_TOKEN") ?? throw new Exception("ENGINE_ACCESS_TOKEN is required"); |
163 | | - |
164 | | -// -------------------------------------------------------------------------- |
165 | | -// Initialize Engine Wallet |
166 | | -// -------------------------------------------------------------------------- |
167 | | - |
168 | | -var engineWallet = await EngineWallet.Create(client, engineUrl, engineAccessToken, backendWalletAddress, 15); |
169 | | - |
170 | | -// -------------------------------------------------------------------------- |
171 | | -// Delegation Contract Implementation |
172 | | -// -------------------------------------------------------------------------- |
173 | | - |
174 | | -var delegationContract = await ThirdwebContract.Create(client, delegationContractAddress, chainWith7702); |
175 | | - |
176 | | -// Initialize a (to-be) 7702 EOA |
177 | | -var eoaWallet = await PrivateKeyWallet.Generate(client); |
178 | | -var eoaWalletAddress = await eoaWallet.GetAddress(); |
179 | | -Console.WriteLine($"EOA address: {eoaWalletAddress}"); |
180 | | - |
181 | | -// Sign the authorization to point to the delegation contract |
182 | | -var authorization = await eoaWallet.SignAuthorization(chainWith7702, delegationContractAddress, willSelfExecute: false); |
183 | | -Console.WriteLine($"Authorization: {JsonConvert.SerializeObject(authorization, Formatting.Indented)}"); |
184 | | - |
185 | | -// Sign message for session key |
186 | | -var sessionKeyParams = new SessionKeyParams_7702() |
187 | | -{ |
188 | | - Signer = backendWalletAddress, |
189 | | - NativeTokenLimitPerTransaction = 0, |
190 | | - StartTimestamp = 0, |
191 | | - EndTimestamp = Utils.GetUnixTimeStampNow() + (3600 * 24), |
192 | | - ApprovedTargets = new List<string> { Constants.ADDRESS_ZERO }, |
193 | | - Uid = Guid.NewGuid().ToByteArray() |
194 | | -}; |
195 | | -var sessionKeySig = await EIP712.GenerateSignature_SmartAccount_7702("MinimalAccount", "1", chainWith7702, eoaWalletAddress, sessionKeyParams, eoaWallet); |
196 | | - |
197 | | -// Create call data for the session key |
198 | | -var sessionKeyCallData = delegationContract.CreateCallData("createSessionKeyWithSig", sessionKeyParams, sessionKeySig.HexToBytes()); |
199 | | - |
200 | | -// Execute the delegation & session key creation!!!!!!!!!! |
201 | | -var delegationReceipt = await engineWallet.ExecuteTransaction(new ThirdwebTransactionInput(chainId: chainWith7702, to: eoaWalletAddress, data: sessionKeyCallData, authorization: authorization)); |
202 | | -Console.WriteLine($"Delegation Execution Receipt: {JsonConvert.SerializeObject(delegationReceipt, Formatting.Indented)}"); |
203 | | - |
204 | | -// Verify contract code deployed to the EOA |
205 | | -var rpc = ThirdwebRPC.GetRpcInstance(client, chainWith7702); |
206 | | -var code = await rpc.SendRequestAsync<string>("eth_getCode", eoaWalletAddress, "latest"); |
207 | | -Console.WriteLine($"EOA code: {code}"); |
208 | | - |
209 | | -// The EOA is now a contract |
210 | | -var eoaContract = await ThirdwebContract.Create(client, eoaWalletAddress, chainWith7702, delegationContract.Abi); |
211 | | - |
212 | | -// -------------------------------------------------------------------------- |
213 | | -// Mint Tokens (DropERC20) to the EOA Using the backend session key |
214 | | -// -------------------------------------------------------------------------- |
215 | | - |
216 | | -var erc20ContractAddress = "0xAA462a5BE0fc5214507FDB4fB2474a7d5c69065b"; // DropERC20 |
217 | | -var erc20Contract = await ThirdwebContract.Create(client, erc20ContractAddress, chainWith7702); |
218 | | - |
219 | | -// Log ERC20 balance before mint |
220 | | -var eoaBalanceBefore = await erc20Contract.ERC20_BalanceOf(eoaWalletAddress); |
221 | | -Console.WriteLine($"EOA balance before: {eoaBalanceBefore}"); |
222 | | - |
223 | | -// Create execution call data (calling 'claim' on the DropERC20) |
224 | | -var executeCallData = eoaContract.CreateCallData( |
225 | | - "execute", |
226 | | - new object[] |
227 | | - { |
228 | | - new List<Call> |
229 | | - { |
230 | | - new() |
231 | | - { |
232 | | - Data = erc20Contract |
233 | | - .CreateCallData( |
234 | | - "claim", |
235 | | - new object[] |
236 | | - { |
237 | | - eoaWalletAddress, // receiver |
238 | | - 100, // quantity |
239 | | - Constants.NATIVE_TOKEN_ADDRESS, // currency |
240 | | - 0, // pricePerToken |
241 | | - new object[] { Array.Empty<byte>(), BigInteger.Zero, BigInteger.Zero, Constants.ADDRESS_ZERO }, // allowlistProof |
242 | | - Array.Empty<byte>() // data |
243 | | - } |
244 | | - ) |
245 | | - .HexToBytes(), |
246 | | - To = erc20ContractAddress, |
247 | | - Value = BigInteger.Zero |
248 | | - } |
249 | | - } |
250 | | - } |
251 | | -); |
252 | | - |
253 | | -var executeReceipt = await engineWallet.ExecuteTransaction(new ThirdwebTransactionInput(chainId: chainWith7702, to: eoaWalletAddress, data: executeCallData)); |
254 | | -Console.WriteLine($"Execute receipt: {JsonConvert.SerializeObject(executeReceipt, Formatting.Indented)}"); |
255 | | - |
256 | | -// Log ERC20 balance after mint |
257 | | -var eoaBalanceAfter = await erc20Contract.ERC20_BalanceOf(eoaWalletAddress); |
258 | | -Console.WriteLine($"EOA balance after: {eoaBalanceAfter}"); |
259 | | - |
260 | | -// -------------------------------------------------------------------------- |
261 | | -// Mint Tokens (DropERC20) to the EOA Using an alternative executor |
262 | | -// -------------------------------------------------------------------------- |
263 | | - |
264 | | -// Executor wallet (managed) |
265 | | -var executorWallet = await PrivateKeyWallet.Create(client, privateKey); |
266 | | - |
267 | | -// Log ERC20 balance before mint |
268 | | -eoaBalanceBefore = await erc20Contract.ERC20_BalanceOf(eoaWalletAddress); |
269 | | -Console.WriteLine($"EOA balance before: {eoaBalanceBefore}"); |
270 | | - |
271 | | -// Sign wrapped calls 712 using an authorized session key (backend wallet in this case) |
272 | | -var wrappedCalls = new WrappedCalls() |
273 | | -{ |
274 | | - Calls = new List<Call> |
275 | | - { |
276 | | - new() |
277 | | - { |
278 | | - Data = erc20Contract |
279 | | - .CreateCallData( |
280 | | - "claim", |
281 | | - new object[] |
282 | | - { |
283 | | - eoaWalletAddress, // receiver |
284 | | - 100, // quantity |
285 | | - Constants.NATIVE_TOKEN_ADDRESS, // currency |
286 | | - 0, // pricePerToken |
287 | | - new object[] { Array.Empty<byte>(), BigInteger.Zero, BigInteger.Zero, Constants.ADDRESS_ZERO }, // allowlistProof |
288 | | - Array.Empty<byte>() // data |
289 | | - } |
290 | | - ) |
291 | | - .HexToBytes(), |
292 | | - To = erc20ContractAddress, |
293 | | - Value = BigInteger.Zero |
294 | | - } |
295 | | - }, |
296 | | - Uid = Guid.NewGuid().ToByteArray().BytesToHex().HexToBytes32() |
297 | | -}; |
298 | | -var wrappedCallsSig = await EIP712.GenerateSignature_SmartAccount_7702_WrappedCalls("MinimalAccount", "1", chainWith7702, eoaWalletAddress, wrappedCalls, engineWallet); |
299 | | - |
300 | | -// Create execution call data, this time in a way that can be broadcast by anyone |
301 | | -executeCallData = eoaContract.CreateCallData("executeWithSig", wrappedCalls, wrappedCallsSig.HexToBytes()); |
302 | | - |
303 | | -var executeTx = await ThirdwebTransaction.Create(wallet: executorWallet, txInput: new ThirdwebTransactionInput(chainId: chainWith7702, to: eoaWalletAddress, data: executeCallData)); |
304 | | -executeReceipt = await ThirdwebTransaction.SendAndWaitForTransactionReceipt(executeTx); |
305 | | -Console.WriteLine($"Execute receipt: {JsonConvert.SerializeObject(executeReceipt, Formatting.Indented)}"); |
306 | | - |
307 | | -// Log ERC20 balance after mint |
308 | | -eoaBalanceAfter = await erc20Contract.ERC20_BalanceOf(eoaWalletAddress); |
309 | | -Console.WriteLine($"EOA balance after: {eoaBalanceAfter}"); |
| 170 | +// // -------------------------------------------------------------------------- |
| 171 | +// // Configuration |
| 172 | +// // -------------------------------------------------------------------------- |
| 173 | + |
| 174 | +// var chainWith7702 = 911867; |
| 175 | +// var delegationContractAddress = "0xb012446cba783d0f7723daf96cf4c49005022307"; // MinimalAccount |
| 176 | + |
| 177 | +// // Required environment variables |
| 178 | +// var backendWalletAddress = Environment.GetEnvironmentVariable("ENGINE_BACKEND_WALLET_ADDRESS") ?? throw new Exception("ENGINE_BACKEND_WALLET_ADDRESS is required"); |
| 179 | +// var engineUrl = Environment.GetEnvironmentVariable("ENGINE_URL") ?? throw new Exception("ENGINE_URL is required"); |
| 180 | +// var engineAccessToken = Environment.GetEnvironmentVariable("ENGINE_ACCESS_TOKEN") ?? throw new Exception("ENGINE_ACCESS_TOKEN is required"); |
| 181 | + |
| 182 | +// // -------------------------------------------------------------------------- |
| 183 | +// // Initialize Engine Wallet |
| 184 | +// // -------------------------------------------------------------------------- |
| 185 | + |
| 186 | +// var engineWallet = await EngineWallet.Create(client, engineUrl, engineAccessToken, backendWalletAddress, 15); |
| 187 | + |
| 188 | +// // -------------------------------------------------------------------------- |
| 189 | +// // Delegation Contract Implementation |
| 190 | +// // -------------------------------------------------------------------------- |
| 191 | + |
| 192 | +// var delegationContract = await ThirdwebContract.Create(client, delegationContractAddress, chainWith7702); |
| 193 | + |
| 194 | +// // Initialize a (to-be) 7702 EOA |
| 195 | +// var eoaWallet = await PrivateKeyWallet.Generate(client); |
| 196 | +// var eoaWalletAddress = await eoaWallet.GetAddress(); |
| 197 | +// Console.WriteLine($"EOA address: {eoaWalletAddress}"); |
| 198 | + |
| 199 | +// // Sign the authorization to point to the delegation contract |
| 200 | +// var authorization = await eoaWallet.SignAuthorization(chainWith7702, delegationContractAddress, willSelfExecute: false); |
| 201 | +// Console.WriteLine($"Authorization: {JsonConvert.SerializeObject(authorization, Formatting.Indented)}"); |
| 202 | + |
| 203 | +// // Sign message for session key |
| 204 | +// var sessionKeyParams = new SessionKeyParams_7702() |
| 205 | +// { |
| 206 | +// Signer = backendWalletAddress, |
| 207 | +// NativeTokenLimitPerTransaction = 0, |
| 208 | +// StartTimestamp = 0, |
| 209 | +// EndTimestamp = Utils.GetUnixTimeStampNow() + (3600 * 24), |
| 210 | +// ApprovedTargets = new List<string> { Constants.ADDRESS_ZERO }, |
| 211 | +// Uid = Guid.NewGuid().ToByteArray() |
| 212 | +// }; |
| 213 | +// var sessionKeySig = await EIP712.GenerateSignature_SmartAccount_7702("MinimalAccount", "1", chainWith7702, eoaWalletAddress, sessionKeyParams, eoaWallet); |
| 214 | + |
| 215 | +// // Create call data for the session key |
| 216 | +// var sessionKeyCallData = delegationContract.CreateCallData("createSessionKeyWithSig", sessionKeyParams, sessionKeySig.HexToBytes()); |
| 217 | + |
| 218 | +// // Execute the delegation & session key creation in one go, from the backend! |
| 219 | +// var delegationReceipt = await engineWallet.ExecuteTransaction(new ThirdwebTransactionInput(chainId: chainWith7702, to: eoaWalletAddress, data: sessionKeyCallData, authorization: authorization)); |
| 220 | +// Console.WriteLine($"Delegation Execution Receipt: {JsonConvert.SerializeObject(delegationReceipt, Formatting.Indented)}"); |
| 221 | + |
| 222 | +// // Verify contract code deployed to the EOA |
| 223 | +// var rpc = ThirdwebRPC.GetRpcInstance(client, chainWith7702); |
| 224 | +// var code = await rpc.SendRequestAsync<string>("eth_getCode", eoaWalletAddress, "latest"); |
| 225 | +// Console.WriteLine($"EOA code: {code}"); |
| 226 | + |
| 227 | +// // The EOA is now a contract |
| 228 | +// var eoaContract = await ThirdwebContract.Create(client, eoaWalletAddress, chainWith7702, delegationContract.Abi); |
| 229 | + |
| 230 | +// // -------------------------------------------------------------------------- |
| 231 | +// // Mint Tokens (DropERC20) to the EOA Using the backend session key |
| 232 | +// // -------------------------------------------------------------------------- |
| 233 | + |
| 234 | +// var erc20ContractAddress = "0xAA462a5BE0fc5214507FDB4fB2474a7d5c69065b"; // DropERC20 |
| 235 | +// var erc20Contract = await ThirdwebContract.Create(client, erc20ContractAddress, chainWith7702); |
| 236 | + |
| 237 | +// // Log ERC20 balance before mint |
| 238 | +// var eoaBalanceBefore = await erc20Contract.ERC20_BalanceOf(eoaWalletAddress); |
| 239 | +// Console.WriteLine($"EOA balance before: {eoaBalanceBefore}"); |
| 240 | + |
| 241 | +// // Create execution call data (calling 'claim' on the DropERC20) |
| 242 | +// var executeCallData = eoaContract.CreateCallData( |
| 243 | +// "execute", |
| 244 | +// new object[] |
| 245 | +// { |
| 246 | +// new List<Call> |
| 247 | +// { |
| 248 | +// new() |
| 249 | +// { |
| 250 | +// Data = erc20Contract |
| 251 | +// .CreateCallData( |
| 252 | +// "claim", |
| 253 | +// new object[] |
| 254 | +// { |
| 255 | +// eoaWalletAddress, // receiver |
| 256 | +// 100, // quantity |
| 257 | +// Constants.NATIVE_TOKEN_ADDRESS, // currency |
| 258 | +// 0, // pricePerToken |
| 259 | +// new object[] { Array.Empty<byte>(), BigInteger.Zero, BigInteger.Zero, Constants.ADDRESS_ZERO }, // allowlistProof |
| 260 | +// Array.Empty<byte>() // data |
| 261 | +// } |
| 262 | +// ) |
| 263 | +// .HexToBytes(), |
| 264 | +// To = erc20ContractAddress, |
| 265 | +// Value = BigInteger.Zero |
| 266 | +// } |
| 267 | +// } |
| 268 | +// } |
| 269 | +// ); |
| 270 | + |
| 271 | +// var executeReceipt = await engineWallet.ExecuteTransaction(new ThirdwebTransactionInput(chainId: chainWith7702, to: eoaWalletAddress, data: executeCallData)); |
| 272 | +// Console.WriteLine($"Execute receipt: {JsonConvert.SerializeObject(executeReceipt, Formatting.Indented)}"); |
| 273 | + |
| 274 | +// // Log ERC20 balance after mint |
| 275 | +// var eoaBalanceAfter = await erc20Contract.ERC20_BalanceOf(eoaWalletAddress); |
| 276 | +// Console.WriteLine($"EOA balance after: {eoaBalanceAfter}"); |
310 | 277 |
|
311 | 278 | #endregion |
312 | 279 |
|
|
0 commit comments