diff --git a/README.md b/README.md index 931be91..0607deb 100644 --- a/README.md +++ b/README.md @@ -25,17 +25,76 @@ dotnet add package Thirdweb using Thirdweb; // Create a client -var clientOptions = new ThirdwebClientOptions(secretKey: secretKey); -var client = new ThirdwebClient(clientOptions); +var client = new ThirdwebClient(secretKey: secretKey); // Interact with a contract -var contractOptions = new ThirdwebContractOptions(client: client, address: "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D", chain: 1, abi: "function name() view returns (string)"); var contract = new ThirdwebContract(contractOptions); -var readResult = await ThirdwebContract.ReadContract(contract, "name"); +var readResult = await ThirdwebContract.ReadContract(client: client, address: "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D", chain: 1, abi: "function name() view returns (string)"); Console.WriteLine($"Contract read result: {readResult}"); // Or directly interact with the RPC var rpc = ThirdwebRPC.GetRpcInstance(client, 1); var blockNumber = await rpc.SendRequestAsync("eth_blockNumber"); Console.WriteLine($"Block number: {blockNumber}"); + +// Create accounts +var privateKeyAccount = new PrivateKeyAccount(client, privateKey); +var embeddedAccount = new EmbeddedAccount(client, "myawesomeemail@gmail.com"); +var smartAccount = new SmartAccount(client, embeddedAccount, "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", true, 421614); + +// Attempt to connect pk accounts +await privateKeyAccount.Connect(); +await embeddedAccount.Connect(); + +// Relog if embedded account not logged in +if (!await embeddedAccount.IsConnected()) +{ + await embeddedAccount.SendOTP(); + Console.WriteLine("Please submit the OTP."); + var otp = Console.ReadLine(); + (var embeddedAccountAddress, var canRetry) = await embeddedAccount.SubmitOTP(otp); + if (embeddedAccountAddress == null && canRetry) + { + Console.WriteLine("Please submit the OTP again."); + otp = Console.ReadLine(); + (embeddedAccountAddress, _) = await embeddedAccount.SubmitOTP(otp); + } + if (embeddedAccountAddress == null) + { + Console.WriteLine("OTP login failed. Please try again."); + return; + } +} + +// Connect the smart account with embedded signer and grant a session key to pk account +await smartAccount.Connect(); +_ = await smartAccount.CreateSessionKey( + signerAddress: await privateKeyAccount.GetAddress(), + approvedTargets: new List() { Constants.ADDRESS_ZERO }, + nativeTokenLimitPerTransactionInWei: "0", + permissionStartTimestamp: "0", + permissionEndTimestamp: (Utils.GetUnixTimeStampNow() + 86400).ToString(), + reqValidityStartTimestamp: "0", + reqValidityEndTimestamp: Utils.GetUnixTimeStampIn10Years().ToString() +); + +// Reconnect to same smart account with pk account as signer +smartAccount = new SmartAccount(client, privateKeyAccount, "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", true, 421614, await smartAccount.GetAddress()); +await smartAccount.Connect(); + +// Log addresses +Console.WriteLine($"PrivateKey Account: {await privateKeyAccount.GetAddress()}"); +Console.WriteLine($"Embedded Account: {await embeddedAccount.GetAddress()}"); +Console.WriteLine($"Smart Account: {await smartAccount.GetAddress()}"); + +// Initialize wallet +var thirdwebWallet = new ThirdwebWallet(); +await thirdwebWallet.Initialize(new List { privateKeyAccount, embeddedAccount, smartAccount }); +thirdwebWallet.SetActive(await smartAccount.GetAddress()); +Console.WriteLine($"Active account: {await thirdwebWallet.GetAddress()}"); + +// Sign, triggering deploy as needed and 1271 verification +var message = "Hello, Thirdweb!"; +var signature = await thirdwebWallet.PersonalSign(message); +Console.WriteLine($"Signed message: {signature}"); ``` diff --git a/Thirdweb.Console/Program.cs b/Thirdweb.Console/Program.cs index d12b56e..6af7d5c 100644 --- a/Thirdweb.Console/Program.cs +++ b/Thirdweb.Console/Program.cs @@ -12,9 +12,7 @@ private static async Task Main(string[] args) var secretKey = Environment.GetEnvironmentVariable("THIRDWEB_SECRET_KEY"); var privateKey = Environment.GetEnvironmentVariable("PRIVATE_KEY"); - var clientOptions = new ThirdwebClientOptions(secretKey: secretKey, fetchTimeoutOptions: new TimeoutOptions(storage: 30000, rpc: 60000)); - var client = new ThirdwebClient(clientOptions); - Console.WriteLine($"Initialized ThirdwebClient: {JsonConvert.SerializeObject(clientOptions, Formatting.Indented)}"); + var client = new ThirdwebClient(secretKey: secretKey, fetchTimeoutOptions: new TimeoutOptions(storage: 30000, rpc: 60000)); // var rpc = ThirdwebRPC.GetRpcInstance(client, 421614); // var blockNumber = await rpc.SendRequestAsync("eth_blockNumber"); diff --git a/Thirdweb.Tests/ClientTests.cs b/Thirdweb.Tests/ClientTests.cs index e288976..83d3732 100644 --- a/Thirdweb.Tests/ClientTests.cs +++ b/Thirdweb.Tests/ClientTests.cs @@ -8,13 +8,13 @@ public ClientTests(ITestOutputHelper output) [Fact] public void NoSecretKeyNoClientId() { - Assert.Throws(() => new ThirdwebClient(new ThirdwebClientOptions())); + Assert.Throws(() => new ThirdwebClient()); } [Fact] public void SecretKeyInitialization() { - var client = new ThirdwebClient(new ThirdwebClientOptions(secretKey: _secretKey)); + var client = new ThirdwebClient(secretKey: _secretKey); Assert.NotNull(client.ClientId); Assert.NotNull(client.SecretKey); Assert.Null(client.BundleId); @@ -26,7 +26,7 @@ public void SecretKeyInitialization() public void ClientIdInitialization() { var clientId = "test-client-id"; - var client = new ThirdwebClient(new ThirdwebClientOptions(clientId: clientId)); + var client = new ThirdwebClient(clientId: clientId); Assert.NotNull(client.ClientId); Assert.Null(client.SecretKey); Assert.Null(client.BundleId); @@ -37,15 +37,15 @@ public void ClientIdInitialization() public void BundleIdInitialization() { var bundleId = "test-bundle-id"; - var exception = Assert.Throws(() => new ThirdwebClient(new ThirdwebClientOptions(bundleId: bundleId))); - Assert.Equal($"ClientId or SecretKey must be provided (Parameter 'options')", exception.Message); + var exception = Assert.Throws(() => new ThirdwebClient(bundleId: bundleId)); + Assert.Equal("ClientId or SecretKey must be provided", exception.Message); } [Fact] public void ClientIdAndSecretKeyInitialization() { var clientId = "test-client-id"; - var client = new ThirdwebClient(new ThirdwebClientOptions(clientId: clientId, secretKey: _secretKey)); + var client = new ThirdwebClient(clientId: clientId, secretKey: _secretKey); Assert.NotNull(client.ClientId); Assert.NotNull(client.SecretKey); Assert.Null(client.BundleId); @@ -59,7 +59,7 @@ public void ClientIdAndBundleIdInitialization() { var clientId = "test-client-id"; var bundleId = "test-bundle-id"; - var client = new ThirdwebClient(new ThirdwebClientOptions(clientId: clientId, bundleId: bundleId)); + var client = new ThirdwebClient(clientId: clientId, bundleId: bundleId); Assert.NotNull(client.ClientId); Assert.NotNull(client.BundleId); Assert.Null(client.SecretKey); @@ -71,7 +71,7 @@ public void ClientIdAndBundleIdInitialization() public void SecretKeyAndBundleIdInitialization() { var bundleId = "test-bundle-id"; - var client = new ThirdwebClient(new ThirdwebClientOptions(secretKey: _secretKey, bundleId: bundleId)); + var client = new ThirdwebClient(secretKey: _secretKey, bundleId: bundleId); Assert.NotNull(client.SecretKey); Assert.NotNull(client.BundleId); Assert.NotNull(client.ClientId); @@ -83,7 +83,7 @@ public void SecretKeyAndBundleIdInitialization() [Fact] public void TimeoutOptions() { - var client = new ThirdwebClient(new ThirdwebClientOptions(secretKey: _secretKey, fetchTimeoutOptions: new TimeoutOptions(storage: 30000, rpc: 30000))); + var client = new ThirdwebClient(secretKey: _secretKey, fetchTimeoutOptions: new TimeoutOptions(storage: 30000, rpc: 30000)); Assert.NotNull(client.FetchTimeoutOptions); Assert.Equal(30000, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Storage)); Assert.Equal(30000, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Rpc)); @@ -92,7 +92,7 @@ public void TimeoutOptions() [Fact] public void NoTimeoutOptions() { - var client = new ThirdwebClient(new ThirdwebClientOptions(secretKey: _secretKey)); + var client = new ThirdwebClient(secretKey: _secretKey); Assert.NotNull(client.FetchTimeoutOptions); Assert.Equal(Constants.DEFAULT_FETCH_TIMEOUT, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Storage)); Assert.Equal(Constants.DEFAULT_FETCH_TIMEOUT, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Rpc)); diff --git a/Thirdweb.Tests/RpcTests.cs b/Thirdweb.Tests/RpcTests.cs index 88a5a91..dde31f8 100644 --- a/Thirdweb.Tests/RpcTests.cs +++ b/Thirdweb.Tests/RpcTests.cs @@ -8,7 +8,7 @@ public RpcTests(ITestOutputHelper output) [Fact] public async Task GetBlockNumber() { - var client = new ThirdwebClient(new ThirdwebClientOptions(secretKey: _secretKey, fetchTimeoutOptions: new TimeoutOptions(rpc: 10000))); + var client = new ThirdwebClient(secretKey: _secretKey, fetchTimeoutOptions: new TimeoutOptions(rpc: 10000)); var rpc = ThirdwebRPC.GetRpcInstance(client, 1); var blockNumber = await rpc.SendRequestAsync("eth_blockNumber"); Assert.NotNull(blockNumber); @@ -18,7 +18,7 @@ public async Task GetBlockNumber() [Fact] public async Task TestAuth() { - var client = new ThirdwebClient(new ThirdwebClientOptions(clientId: "hi", fetchTimeoutOptions: new TimeoutOptions(rpc: 10000))); + var client = new ThirdwebClient(clientId: "hi", fetchTimeoutOptions: new TimeoutOptions(rpc: 60000)); var rpc = ThirdwebRPC.GetRpcInstance(client, 1); var exception = await Assert.ThrowsAsync(async () => await rpc.SendRequestAsync("eth_blockNumber")); _output.WriteLine($"TestAuth Exception Message: {exception.Message}"); @@ -27,7 +27,7 @@ public async Task TestAuth() [Fact] public async Task TestTimeout() { - var client = new ThirdwebClient(new ThirdwebClientOptions(secretKey: _secretKey, fetchTimeoutOptions: new TimeoutOptions(rpc: 0))); + var client = new ThirdwebClient(secretKey: _secretKey, fetchTimeoutOptions: new TimeoutOptions(rpc: 0)); var rpc = ThirdwebRPC.GetRpcInstance(client, 1); var exception = await Assert.ThrowsAsync(async () => await rpc.SendRequestAsync("eth_chainId")); _output.WriteLine($"TestTimeout Exception Message: {exception.Message}"); diff --git a/Thirdweb/Thirdweb.Client/ThirdwebClient.cs b/Thirdweb/Thirdweb.Client/ThirdwebClient.cs index 7102695..cc0cacb 100644 --- a/Thirdweb/Thirdweb.Client/ThirdwebClient.cs +++ b/Thirdweb/Thirdweb.Client/ThirdwebClient.cs @@ -4,33 +4,31 @@ namespace Thirdweb { public class ThirdwebClient { - private string _secretKey; + internal string SecretKey { get; } + internal string ClientId { get; } + internal string BundleId { get; } + internal ITimeoutOptions FetchTimeoutOptions { get; } - internal string SecretKey => _secretKey; - internal string ClientId { get; private set; } - internal string BundleId { get; private set; } - internal ITimeoutOptions FetchTimeoutOptions { get; private set; } - - public ThirdwebClient(ThirdwebClientOptions options) + public ThirdwebClient(string clientId = null, string secretKey = null, string bundleId = null, ITimeoutOptions fetchTimeoutOptions = null) { - if (string.IsNullOrEmpty(options.ClientId) && string.IsNullOrEmpty(options.SecretKey)) + if (string.IsNullOrEmpty(clientId) && string.IsNullOrEmpty(secretKey)) { - throw new ArgumentNullException(nameof(options), "ClientId or SecretKey must be provided"); + throw new InvalidOperationException("ClientId or SecretKey must be provided"); } - if (!string.IsNullOrEmpty(options.SecretKey)) + if (!string.IsNullOrEmpty(secretKey)) { - ClientId = Utils.ComputeClientIdFromSecretKey(options.SecretKey); - _secretKey = options.SecretKey; + ClientId = Utils.ComputeClientIdFromSecretKey(secretKey); + SecretKey = secretKey; } else { - ClientId = options.ClientId; + ClientId = clientId; } - BundleId = options.BundleId; + BundleId = bundleId; - FetchTimeoutOptions = options.FetchTimeoutOptions ?? new TimeoutOptions(); + FetchTimeoutOptions = fetchTimeoutOptions ?? new TimeoutOptions(); } } } diff --git a/Thirdweb/Thirdweb.Client/ThirdwebClientOptions.cs b/Thirdweb/Thirdweb.Client/ThirdwebClientOptions.cs deleted file mode 100644 index 02ce140..0000000 --- a/Thirdweb/Thirdweb.Client/ThirdwebClientOptions.cs +++ /dev/null @@ -1,20 +0,0 @@ -namespace Thirdweb -{ - public class ThirdwebClientOptions - { - private string _secretKey; - - internal string SecretKey => _secretKey; - internal string ClientId { get; private set; } - internal string BundleId { get; private set; } - internal ITimeoutOptions FetchTimeoutOptions { get; private set; } - - public ThirdwebClientOptions(string clientId = null, string secretKey = null, string bundleId = null, ITimeoutOptions fetchTimeoutOptions = null) - { - _secretKey = secretKey; - ClientId = clientId; - BundleId = bundleId; - FetchTimeoutOptions = fetchTimeoutOptions; - } - } -}