Skip to content

Commit

Permalink
Merge pull request #2 from thirdweb-dev/firekeeper/wallets-pk
Browse files Browse the repository at this point in the history
Contract Write, Thirdweb Wallet (Accounts: PrivateKey, Embedded, Smart)
  • Loading branch information
0xFirekeeper authored Apr 3, 2024
2 parents dcb3d5d + 5642841 commit ef20bf3
Show file tree
Hide file tree
Showing 37 changed files with 3,491 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<DefaultVersion>0.0.1</DefaultVersion>
<DefaultTargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net7.0;</DefaultTargetFrameworks>
<DefaultTargetFrameworks>netstandard2.1;net6.0;net7.0;</DefaultTargetFrameworks>
</PropertyGroup>
<PropertyGroup Label="C#">
<LangVersion>latest</LangVersion>
Expand Down
6 changes: 6 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@
<PackageVersion Include="Nethereum.ABI" Version="4.19.0" />
<PackageVersion Include="Nethereum.Hex" Version="4.19.0" />
<PackageVersion Include="Nethereum.Contracts" Version="4.19.0" />
<PackageVersion Include="Nethereum.Signer" Version="4.19.0" />
<PackageVersion Include="Nethereum.Signer.EIP712" Version="4.19.0" />
<PackageVersion Include="Portable.BouncyCastle" Version="1.9.0" />
<PackageVersion Include="Amazon.Extensions.CognitoAuthentication" Version="2.5.0" />
<PackageVersion Include="AWSSDK.Lambda" Version="3.7.113.2" />
<PackageVersion Include="Nethereum.HdWallet" Version="4.14.0" />
</ItemGroup>
</Project>
84 changes: 70 additions & 14 deletions Thirdweb.Console/Program.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Thirdweb.Tests/ClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void NoTimeoutOptions()
{
var client = new ThirdwebClient(new ThirdwebClientOptions(secretKey: _secretKey));
Assert.NotNull(client.FetchTimeoutOptions);
Assert.Equal(Constants.DefaultFetchTimeout, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Storage));
Assert.Equal(Constants.DefaultFetchTimeout, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Rpc));
Assert.Equal(Constants.DEFAULT_FETCH_TIMEOUT, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Storage));
Assert.Equal(Constants.DEFAULT_FETCH_TIMEOUT, client.FetchTimeoutOptions.GetTimeout(TimeoutType.Rpc));
}
}
2 changes: 1 addition & 1 deletion Thirdweb/Thirdweb.Client/ITimeoutOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public interface ITimeoutOptions
{
int GetTimeout(TimeoutType type, int fallback = Constants.DefaultFetchTimeout);
int GetTimeout(TimeoutType type, int fallback = Constants.DEFAULT_FETCH_TIMEOUT);
}
}
2 changes: 1 addition & 1 deletion Thirdweb/Thirdweb.Client/TimeoutOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public TimeoutOptions(int? storage = null, int? rpc = null, int? other = null)
Other = other;
}

public int GetTimeout(TimeoutType type, int fallback = Constants.DefaultFetchTimeout)
public int GetTimeout(TimeoutType type, int fallback = Constants.DEFAULT_FETCH_TIMEOUT)
{
return type switch
{
Expand Down
68 changes: 59 additions & 9 deletions Thirdweb/Thirdweb.Contracts/ThirdwebContract.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Numerics;
using Nethereum.Hex.HexTypes;
using Nethereum.RPC.Eth.DTOs;

namespace Thirdweb
{
Expand All @@ -9,24 +11,32 @@ public class ThirdwebContract
internal BigInteger Chain { get; private set; }
internal string Abi { get; private set; }

public ThirdwebContract(ThirdwebContractOptions options)
public ThirdwebContract(ThirdwebClient client, string address, BigInteger chain, string abi)
{
if (options.Client == null)
if (client == null)
{
throw new ArgumentException("Client must be provided");
}

if (string.IsNullOrEmpty(options.Address))
if (string.IsNullOrEmpty(address))
{
throw new ArgumentException("Address must be provided");
}

if (options.Chain == 0)
if (chain == 0)
{
throw new ArgumentException("Chain must be provided");
}

if (string.IsNullOrEmpty(options.Abi))
if (string.IsNullOrEmpty(abi))
{
throw new ArgumentException("Abi must be provided");
}

Client = options.Client;
Address = options.Address;
Chain = options.Chain;
Abi = options.Abi;
Client = client;
Address = address;
Chain = chain;
Abi = abi;
}

public static async Task<T> ReadContract<T>(ThirdwebContract contract, string method, params object[] parameters)
Expand All @@ -40,5 +50,45 @@ public static async Task<T> ReadContract<T>(ThirdwebContract contract, string me
var resultData = await rpc.SendRequestAsync<string>("eth_call", new { to = contract.Address, data = data, }, "latest");
return function.DecodeTypeOutput<T>(resultData);
}

public static async Task<string> WriteContract(ThirdwebWallet wallet, ThirdwebContract contract, string method, BigInteger weiValue, params object[] parameters)
{
var rpc = ThirdwebRPC.GetRpcInstance(contract.Client, contract.Chain);

var service = new Nethereum.Contracts.Contract(null, contract.Abi, contract.Address);
var function = service.GetFunction(method);
var data = function.GetData(parameters);

var transaction = new TransactionInput
{
From = await wallet.GetAddress(),
To = contract.Address,
Data = data,
};

// TODO: Implement 1559
transaction.Gas = new HexBigInteger(await rpc.SendRequestAsync<string>("eth_estimateGas", transaction));
transaction.GasPrice = new HexBigInteger(await rpc.SendRequestAsync<string>("eth_gasPrice"));
transaction.Value = new HexBigInteger(weiValue);

string hash;
if (wallet.ActiveAccount.AccountType is ThirdwebAccountType.PrivateKeyAccount)
{
transaction.Nonce = new HexBigInteger(await rpc.SendRequestAsync<string>("eth_getTransactionCount", wallet.GetAddress(), "latest"));
var signedTx = wallet.SignTransaction(transaction, contract.Chain);
Console.WriteLine($"Signed transaction: {signedTx}");
hash = await rpc.SendRequestAsync<string>("eth_sendRawTransaction", signedTx);
}
else if (wallet.ActiveAccount.AccountType is ThirdwebAccountType.SmartAccount)
{
var smartAccount = wallet.ActiveAccount as SmartAccount;
hash = await smartAccount.SendTransaction(transaction);
}
else
{
throw new NotImplementedException("Account type not supported");
}
return hash;
}
}
}
20 changes: 0 additions & 20 deletions Thirdweb/Thirdweb.Contracts/ThirdwebContractOptions.cs

This file was deleted.

11 changes: 8 additions & 3 deletions Thirdweb/Thirdweb.RPC/ThirdwebRPC.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using System.Numerics;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Numerics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Thirdweb
Expand Down Expand Up @@ -75,7 +80,7 @@ static ThirdwebRPC()
_httpClient.DefaultRequestHeaders.Add("x-sdk-name", "Thirdweb.NET");
_httpClient.DefaultRequestHeaders.Add("x-sdk-os", System.Runtime.InteropServices.RuntimeInformation.OSDescription);
_httpClient.DefaultRequestHeaders.Add("x-sdk-platform", "dotnet");
_httpClient.DefaultRequestHeaders.Add("x-sdk-version", Constants.Version);
_httpClient.DefaultRequestHeaders.Add("x-sdk-version", Constants.VERSION);
}

private ThirdwebRPC(ThirdwebClient client, BigInteger chainId)
Expand Down Expand Up @@ -112,7 +117,7 @@ private void SendBatchNow()
private async Task SendBatchAsync(List<RpcRequest> batch)
{
var batchJson = JsonConvert.SerializeObject(batch);
Console.WriteLine($"Sending batch: {batchJson}");

var requestMessage = new HttpRequestMessage(HttpMethod.Post, _rpcUrl) { Content = new StringContent(batchJson, Encoding.UTF8, "application/json") };
if (!string.IsNullOrEmpty(_clientId))
requestMessage.Headers.Add("x-client-id", _clientId);
Expand Down
8 changes: 6 additions & 2 deletions Thirdweb/Thirdweb.Utils/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
{
public static class Constants
{
internal const string Version = "0.0.1";
internal const int DefaultFetchTimeout = 60000;
internal const string VERSION = "0.0.1";
internal const int DEFAULT_FETCH_TIMEOUT = 60000;
internal const string DEFAULT_ENTRYPOINT_ADDRESS = "0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789"; // v0.6
internal const string DUMMY_SIG = "0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c";
internal const string DUMMY_PAYMASTER_AND_DATA_HEX =
"0x0101010101010101010101010101010101010101000000000000000000000000000000000000000000000000000001010101010100000000000000000000000000000000000000000000000000000000000000000101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101";
}
}
12 changes: 12 additions & 0 deletions Thirdweb/Thirdweb.Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,17 @@ public static string ComputeClientIdFromSecretKey(string secretKey)
var hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(secretKey));
return BitConverter.ToString(hash).Replace("-", "").ToLower().Substring(0, 32);
}

public static string HexConcat(params string[] hexStrings)
{
var hex = new StringBuilder("0x");

foreach (var hexStr in hexStrings)
{
_ = hex.Append(hexStr[2..]);
}

return hex.ToString();
}
}
}
Loading

0 comments on commit ef20bf3

Please sign in to comment.