Skip to content

Commit 9abe28d

Browse files
committed
cov
1 parent f1d10db commit 9abe28d

File tree

7 files changed

+133
-7
lines changed

7 files changed

+133
-7
lines changed

Thirdweb.Tests/Thirdweb.Contracts/Thirdweb.Contracts.Tests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ public async Task ReadTest_Tuple()
7878
Assert.Equal(0, result.ReturnValue2);
7979
}
8080

81+
[Fact(Timeout = 120000)]
82+
public async Task ReadTest_4Bytes()
83+
{
84+
var contract = await this.GetContract();
85+
var result = await ThirdwebContract.Read<string>(contract, "0x06fdde03");
86+
Assert.Equal("Kitty DropERC20", result);
87+
}
88+
8189
[Fact(Timeout = 120000)]
8290
public async Task ReadTest_FullSig()
8391
{

Thirdweb.Tests/Thirdweb.Utils/Thirdweb.Utils.Tests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,4 +866,32 @@ public void PreprocessTypedDataJson_NestedLargeNumbers()
866866

867867
Assert.Equal(expectedJObject, processedJObject);
868868
}
869+
870+
[Fact]
871+
public void DecodeTransaction_1559WithAuthList()
872+
{
873+
var signedTxStr =
874+
"0x04f8ca830de9fb8082011882031083025bee94ff5d95e5aa1b5af3f106079518228a92818737728080c0f85ef85c830de9fb94654f42b74885ee6803f403f077bc0409f1066c588080a0a5caed9b0c46657a452250a3279f45937940c87c45854aead6a902d99bc638f39faa58026c6b018d36b8935a42f2bcf68097c712c9f09ca014c70887678e08a980a027ecc69e66eb9e28cbe6edab10fc827fcb6d2a34cdcb89d8b6aabc6e35608692a0750d306b04a50a35de57bd6aca11f207a8dd404f9d92502ce6e3817e52f79a1c";
875+
(var txInput, var signature) = Utils.DecodeTransaction(signedTxStr);
876+
Assert.Equal("0xfF5D95e5aA1B5Af3F106079518228A9281873772", txInput.To);
877+
Assert.Equal("0x", txInput.Data);
878+
Assert.Equal(0, txInput.Value.Value);
879+
Assert.NotNull(txInput.AuthorizationList);
880+
_ = Assert.Single(txInput.AuthorizationList);
881+
Assert.Equal("0x654F42b74885EE6803F403f077bc0409f1066c58", txInput.AuthorizationList[0].Address);
882+
Assert.Equal("0xde9fb", txInput.AuthorizationList[0].ChainId);
883+
Assert.Equal("0x0", txInput.AuthorizationList[0].Nonce);
884+
885+
(txInput, var signature2) = Utils.DecodeTransaction(signedTxStr.HexToBytes());
886+
Assert.Equal("0xfF5D95e5aA1B5Af3F106079518228A9281873772", txInput.To);
887+
Assert.Equal("0x", txInput.Data);
888+
Assert.Equal(0, txInput.Value.Value);
889+
Assert.NotNull(txInput.AuthorizationList);
890+
_ = Assert.Single(txInput.AuthorizationList);
891+
Assert.Equal("0x654F42b74885EE6803F403f077bc0409f1066c58", txInput.AuthorizationList[0].Address);
892+
Assert.Equal("0xde9fb", txInput.AuthorizationList[0].ChainId);
893+
Assert.Equal("0x0", txInput.AuthorizationList[0].Nonce);
894+
895+
Assert.Equal(signature, signature2);
896+
}
869897
}

Thirdweb.Tests/Thirdweb.Wallets/Thirdweb.PrivateKeyWallet.Tests.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,26 @@ public async Task SignTransaction_Success()
223223
Assert.NotNull(signature);
224224
}
225225

226+
[Fact(Timeout = 120000)]
227+
public async Task SignTransaction_WithAuthorizationList_Success()
228+
{
229+
var account = await this.GetAccount();
230+
var authorization = await account.SignAuthorization(421614, Constants.ADDRESS_ZERO, false);
231+
var transaction = new ThirdwebTransactionInput(
232+
chainId: 421614,
233+
from: await account.GetAddress(),
234+
to: Constants.ADDRESS_ZERO,
235+
value: 0,
236+
gas: 21000,
237+
data: "0x",
238+
nonce: 99999999999,
239+
gasPrice: 10000000000,
240+
authorization: authorization
241+
);
242+
var signature = await account.SignTransaction(transaction);
243+
Assert.NotNull(signature);
244+
}
245+
226246
[Fact(Timeout = 120000)]
227247
public async Task SignTransaction_NoFrom_Success()
228248
{
@@ -461,4 +481,26 @@ public async Task Export_ReturnsPrivateKey()
461481
Assert.NotNull(privateKey);
462482
Assert.Equal(privateKey, await wallet.Export());
463483
}
484+
485+
[Fact(Timeout = 120000)]
486+
public async Task SignAuthorization_SelfExecution()
487+
{
488+
var wallet = await PrivateKeyWallet.Generate(this.Client);
489+
var chainId = 911867;
490+
var targetAddress = "0x654F42b74885EE6803F403f077bc0409f1066c58";
491+
492+
var currentNonce = await wallet.GetTransactionCount(chainId);
493+
494+
var authorization = await wallet.SignAuthorization(chainId: chainId, contractAddress: targetAddress, willSelfExecute: false);
495+
496+
Assert.Equal(chainId.NumberToHex(), authorization.ChainId);
497+
Assert.Equal(targetAddress, authorization.Address);
498+
Assert.True(authorization.Nonce.HexToNumber() == currentNonce);
499+
500+
authorization = await wallet.SignAuthorization(chainId: chainId, contractAddress: targetAddress, willSelfExecute: true);
501+
502+
Assert.Equal(chainId.NumberToHex(), authorization.ChainId);
503+
Assert.Equal(targetAddress, authorization.Address);
504+
Assert.True(authorization.Nonce.HexToNumber() == currentNonce + 1);
505+
}
464506
}

Thirdweb.Tests/Thirdweb.Wallets/Thirdweb.SmartWallet.Tests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,18 @@ public async Task SwitchNetwork_NonZkToZk_Success()
364364
Assert.NotEqual(addy1, addy2);
365365
}
366366

367+
[Fact(Timeout = 120000)]
368+
public async Task SignAuthorization_WithPrivateKeyWallet_Success()
369+
{
370+
var smartWallet = await SmartWallet.Create(personalWallet: await PrivateKeyWallet.Generate(this.Client), chainId: 421614);
371+
var smartWalletSigner = await smartWallet.GetPersonalWallet();
372+
var signature1 = await smartWallet.SignAuthorization(chainId: 421614, contractAddress: Constants.ADDRESS_ZERO, willSelfExecute: true);
373+
var signature2 = await smartWalletSigner.SignAuthorization(chainId: 421614, contractAddress: Constants.ADDRESS_ZERO, willSelfExecute: true);
374+
Assert.Equal(signature1.ChainId, signature2.ChainId);
375+
Assert.Equal(signature1.Address, signature2.Address);
376+
Assert.Equal(signature1.Nonce, signature2.Nonce);
377+
}
378+
367379
// [Fact(Timeout = 120000)]
368380
// public async Task MultiChainTransaction_Success()
369381
// {

Thirdweb/Thirdweb.Transactions/ThirdwebTransaction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,12 @@ public static async Task<BigInteger> EstimateGasLimit(ThirdwebTransaction transa
271271
if (isZkSync)
272272
{
273273
var hex = (await rpc.SendRequestAsync<JToken>("zks_estimateFee", transaction.Input).ConfigureAwait(false))["gas_limit"].ToString();
274-
baseGas = hex.HexToBigInt();
274+
baseGas = hex.HexToNumber();
275275
}
276276
else
277277
{
278278
var hex = await rpc.SendRequestAsync<string>("eth_estimateGas", transaction.Input).ConfigureAwait(false);
279-
baseGas = hex.HexToBigInt();
279+
baseGas = hex.HexToNumber();
280280
}
281281
return baseGas * 10 / divider;
282282
}

Thirdweb/Thirdweb.Utils/Utils.cs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,46 @@ public static byte[] HexToBytes(this string hex)
138138
/// </summary>
139139
/// <param name="hex">The hex string to convert.</param>
140140
/// <returns>The big integer.</returns>
141+
[Obsolete("Use HexToNumber instead.")]
141142
public static BigInteger HexToBigInt(this string hex)
142143
{
143144
return new HexBigInteger(hex).Value;
144145
}
145146

147+
/// <summary>
148+
/// Converts the given hex string to a big integer.
149+
/// </summary>
150+
/// <param name="hex">The hex string to convert.</param>
151+
/// <returns>The big integer.</returns>
152+
public static BigInteger HexToNumber(this string hex)
153+
{
154+
return new HexBigInteger(hex).Value;
155+
}
156+
157+
/// <summary>
158+
/// Converts the given big integer to a hex string.
159+
/// </summary>
160+
public static string NumberToHex(this BigInteger number)
161+
{
162+
return new HexBigInteger(number).HexValue;
163+
}
164+
165+
/// <summary>
166+
/// Converts the given integer to a hex string.
167+
/// </summary>
168+
public static string NumberToHex(this int number)
169+
{
170+
return NumberToHex(number);
171+
}
172+
173+
/// <summary>
174+
/// Converts the given long to a hex string.
175+
/// </summary>
176+
public static string NumberToHex(this long number)
177+
{
178+
return NumberToHex(number);
179+
}
180+
146181
/// <summary>
147182
/// Converts the given string to a hex string.
148183
/// </summary>
@@ -885,7 +920,7 @@ public static async Task<BigInteger> FetchGasPrice(ThirdwebClient client, BigInt
885920
{
886921
var rpc = ThirdwebRPC.GetRpcInstance(client, chainId);
887922
var hex = await rpc.SendRequestAsync<string>("eth_gasPrice").ConfigureAwait(false);
888-
var gasPrice = hex.HexToBigInt();
923+
var gasPrice = hex.HexToNumber();
889924
return withBump ? gasPrice * 10 / 9 : gasPrice;
890925
}
891926

@@ -1099,7 +1134,7 @@ public static (ThirdwebTransactionInput transactionInput, string signature) Deco
10991134
return (
11001135
new ThirdwebTransactionInput(
11011136
chainId: chainId,
1102-
to: receiverAddress,
1137+
to: receiverAddress.ToChecksumAddress(),
11031138
nonce: nonce,
11041139
gas: gasLimit,
11051140
value: amount,
@@ -1133,7 +1168,7 @@ public static List<EIP7702Authorization> DecodeAutorizationList(byte[] authoriza
11331168
var authorizationListItem = new EIP7702Authorization
11341169
{
11351170
ChainId = new HexBigInteger(decodedItem[0].RLPData.ToBigIntegerFromRLPDecoded()).HexValue,
1136-
Address = decodedItem[1].RLPData.BytesToHex(),
1171+
Address = decodedItem[1].RLPData.BytesToHex().ToChecksumAddress(),
11371172
Nonce = new HexBigInteger(decodedItem[2].RLPData.ToBigIntegerFromRLPDecoded()).HexValue
11381173
};
11391174
var signature = RLPSignedDataDecoder.DecodeSignature(decodedItem, 3);

Thirdweb/Thirdweb.Wallets/PrivateKeyWallet/PrivateKeyWallet.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ public virtual Task<string> SignTransaction(ThirdwebTransactionInput transaction
346346
{
347347
var encodedItem = new List<byte[]>()
348348
{
349-
RLP.EncodeElement(authorizationList.ChainId.HexToBigInt().ToByteArrayForRLPEncoding()),
349+
RLP.EncodeElement(authorizationList.ChainId.HexToNumber().ToByteArrayForRLPEncoding()),
350350
RLP.EncodeElement(authorizationList.Address.HexToBytes()),
351-
RLP.EncodeElement(authorizationList.Nonce.HexToBigInt().ToByteArrayForRLPEncoding()),
351+
RLP.EncodeElement(authorizationList.Nonce.HexToNumber().ToByteArrayForRLPEncoding()),
352352
RLP.EncodeElement(authorizationList.YParity == "0x00" ? Array.Empty<byte>() : authorizationList.YParity.HexToBytes()),
353353
RLP.EncodeElement(authorizationList.R.HexToBytes().TrimZeroes()),
354354
RLP.EncodeElement(authorizationList.S.HexToBytes().TrimZeroes())
@@ -393,6 +393,7 @@ public virtual Task<string> SignTransaction(ThirdwebTransactionInput transaction
393393
// (var tx, var sig) = Utils.DecodeTransaction(returnBytes);
394394

395395
signedTransaction = returnBytes.ToHex();
396+
Console.WriteLine(signedTransaction);
396397

397398
// (var tx, var sig) = Utils.DecodeTransaction("0x" + signedTransaction);
398399
}

0 commit comments

Comments
 (0)