Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native transfer extension #30

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion Thirdweb.Tests/Thirdweb.WalletExtension.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,35 @@ private async Task<IThirdwebWallet> GetWallet()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var privateKeyWallet = await PrivateKeyWallet.Create(client, _testPrivateKey);
return privateKeyWallet;
var smartAccount = await SmartWallet.Create(client, personalWallet: privateKeyWallet, factoryAddress: "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", gasless: true, chainId: 421614);
return smartAccount;
}

[Fact]
public async Task NullChecks()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var wallet = await GetWallet();

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await wallet.GetBalance(null, _chainId));
_ = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await wallet.GetBalance(client, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await wallet.GetBalance(client, -1));

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await wallet.Transfer(null, BigInteger.Zero, null, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await wallet.Transfer(client, BigInteger.Zero, null, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await wallet.Transfer(client, -1, string.Empty, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await wallet.Transfer(client, _chainId, null, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await wallet.Transfer(client, _chainId, string.Empty, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await wallet.Transfer(client, _chainId, Constants.ADDRESS_ZERO, -1));

client = null;

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await wallet.GetBalance(client, _chainId));
_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await wallet.Transfer(client, BigInteger.Zero, null, BigInteger.Zero));

wallet = null;
_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await wallet.GetBalance(client, _chainId));
_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await wallet.Transfer(client, BigInteger.Zero, null, BigInteger.Zero));
}

[Fact]
Expand All @@ -40,5 +52,16 @@ public async Task GetBalance()
var balance = await wallet.GetBalance(client, _chainId);
Assert.True(balance >= 0);
}

[Fact]
public async Task Transfer()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var wallet = await GetWallet();
var toAddress = await wallet.GetAddress();
var receipt = await wallet.Transfer(client, _chainId, toAddress, BigInteger.Zero);
Assert.NotNull(receipt);
Assert.True(receipt.TransactionHash.Length == 66);
}
}
}
25 changes: 24 additions & 1 deletion Thirdweb/Thirdweb.Wallets/SmartWallet/SmartWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,30 @@ public async Task<string> Authenticate(
IThirdwebHttpClient httpClientOverride = null
)
{
return await _personalAccount.Authenticate(domain, chainId, authPayloadPath, authLoginPath, httpClientOverride);
var payloadURL = domain + authPayloadPath;
var loginURL = domain + authLoginPath;

var payloadBodyRaw = new { address = await GetAddress(), chainId = chainId.ToString() };
var payloadBody = JsonConvert.SerializeObject(payloadBodyRaw);

using var httpClient = httpClientOverride ?? _client.HttpClient;

var payloadContent = new StringContent(payloadBody, Encoding.UTF8, "application/json");
var payloadResponse = await httpClient.PostAsync(payloadURL, payloadContent);
_ = payloadResponse.EnsureSuccessStatusCode();
var payloadString = await payloadResponse.Content.ReadAsStringAsync();

var loginBodyRaw = JsonConvert.DeserializeObject<LoginPayload>(payloadString);
var payloadToSign = Utils.GenerateSIWE(loginBodyRaw.payload);

loginBodyRaw.signature = await PersonalSign(payloadToSign);
var loginBody = JsonConvert.SerializeObject(new { payload = loginBodyRaw });

var loginContent = new StringContent(loginBody, Encoding.UTF8, "application/json");
var loginResponse = await httpClient.PostAsync(loginURL, loginContent);
_ = loginResponse.EnsureSuccessStatusCode();
var responseString = await loginResponse.Content.ReadAsStringAsync();
return responseString;
}
}
}
38 changes: 38 additions & 0 deletions Thirdweb/Thirdweb.Wallets/ThirdwebWalletExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Numerics;
using Nethereum.Hex.HexTypes;
using Nethereum.RPC.Eth.DTOs;

namespace Thirdweb
{
Expand Down Expand Up @@ -27,6 +28,43 @@ public static async Task<BigInteger> GetBalance(this IThirdwebWallet wallet, Thi
return new HexBigInteger(balanceHex).Value;
}

public static async Task<TransactionReceipt> Transfer(this IThirdwebWallet wallet, ThirdwebClient client, BigInteger chainId, string toAddress, BigInteger weiAmount)
{
if (wallet == null)
{
throw new ArgumentNullException(nameof(wallet));
}

if (client == null)
{
throw new ArgumentNullException(nameof(client));
}

if (chainId <= 0)
{
throw new ArgumentOutOfRangeException(nameof(chainId), "Chain ID must be greater than 0.");
}

if (string.IsNullOrEmpty(toAddress))
{
throw new ArgumentException(nameof(toAddress), "Recipient address cannot be null or empty.");
}

if (weiAmount < 0)
{
throw new ArgumentOutOfRangeException(nameof(weiAmount), "Amount must be 0 or greater.");
}

var txInput = new ThirdwebTransactionInput()
{
From = await wallet.GetAddress(),
To = toAddress,
Value = new HexBigInteger(weiAmount)
};
var tx = await ThirdwebTransaction.Create(client, wallet, txInput, chainId);
return await ThirdwebTransaction.SendAndWaitForTransactionReceipt(tx);
}

// TODO: Tx Listener?
}
}
Loading