Skip to content

Commit

Permalink
remove client options, update tests and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Apr 3, 2024
1 parent f3075dc commit 9c29e82
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 55 deletions.
67 changes: 63 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>(contract, "name");
var readResult = await ThirdwebContract.ReadContract<string>(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<string>("eth_blockNumber");
Console.WriteLine($"Block number: {blockNumber}");

// Create accounts
var privateKeyAccount = new PrivateKeyAccount(client, privateKey);
var embeddedAccount = new EmbeddedAccount(client, "[email protected]");
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<string>() { 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<IThirdwebAccount> { 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}");
```
4 changes: 1 addition & 3 deletions Thirdweb.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>("eth_blockNumber");
Expand Down
20 changes: 10 additions & 10 deletions Thirdweb.Tests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ public ClientTests(ITestOutputHelper output)
[Fact]
public void NoSecretKeyNoClientId()
{
Assert.Throws<ArgumentNullException>(() => new ThirdwebClient(new ThirdwebClientOptions()));
Assert.Throws<InvalidOperationException>(() => 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);
Expand All @@ -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);
Expand All @@ -37,15 +37,15 @@ public void ClientIdInitialization()
public void BundleIdInitialization()
{
var bundleId = "test-bundle-id";
var exception = Assert.Throws<ArgumentNullException>(() => new ThirdwebClient(new ThirdwebClientOptions(bundleId: bundleId)));
Assert.Equal($"ClientId or SecretKey must be provided (Parameter 'options')", exception.Message);
var exception = Assert.Throws<InvalidOperationException>(() => 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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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));
Expand All @@ -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));
Expand Down
6 changes: 3 additions & 3 deletions Thirdweb.Tests/RpcTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>("eth_blockNumber");
Assert.NotNull(blockNumber);
Expand All @@ -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<HttpRequestException>(async () => await rpc.SendRequestAsync<string>("eth_blockNumber"));
_output.WriteLine($"TestAuth Exception Message: {exception.Message}");
Expand All @@ -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<TimeoutException>(async () => await rpc.SendRequestAsync<string>("eth_chainId"));
_output.WriteLine($"TestTimeout Exception Message: {exception.Message}");
Expand Down
28 changes: 13 additions & 15 deletions Thirdweb/Thirdweb.Client/ThirdwebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
20 changes: 0 additions & 20 deletions Thirdweb/Thirdweb.Client/ThirdwebClientOptions.cs

This file was deleted.

0 comments on commit 9c29e82

Please sign in to comment.