Skip to content

Commit d0d24d7

Browse files
authored
Authenticate (#18)
* Authenticate * dupe * nulls and tests * can't always get exactly value as diff due to gas fluctuations * resources * Update IThirdwebWallet.cs * Revert "Update IThirdwebWallet.cs" This reverts commit 619c12d. * full payload test
1 parent 6768e56 commit d0d24d7

File tree

7 files changed

+414
-77
lines changed

7 files changed

+414
-77
lines changed

Thirdweb.Console/Program.cs

Lines changed: 77 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -52,81 +52,83 @@
5252

5353
// Create smart wallet with InAppWallet signer
5454
var smartWallet = await SmartWallet.Create(client: client, personalWallet: inAppWallet, factoryAddress: "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", gasless: true, chainId: 421614);
55-
56-
// Grant a session key to pk wallet (advanced use case)
57-
_ = await smartWallet.CreateSessionKey(
58-
signerAddress: await privateKeyWallet.GetAddress(),
59-
approvedTargets: new List<string>() { Constants.ADDRESS_ZERO },
60-
nativeTokenLimitPerTransactionInWei: "0",
61-
permissionStartTimestamp: "0",
62-
permissionEndTimestamp: (Utils.GetUnixTimeStampNow() + 86400).ToString(),
63-
reqValidityStartTimestamp: "0",
64-
reqValidityEndTimestamp: Utils.GetUnixTimeStampIn10Years().ToString()
65-
);
66-
67-
// Reconnect to same smart wallet with pk wallet as signer (specifying wallet address override)
68-
smartWallet = await SmartWallet.Create(
69-
client: client,
70-
personalWallet: privateKeyWallet,
71-
factoryAddress: "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052",
72-
gasless: true,
73-
chainId: 421614,
74-
accountAddressOverride: await smartWallet.GetAddress()
75-
);
76-
77-
// Log addresses
78-
Console.WriteLine($"PrivateKey Wallet: {await privateKeyWallet.GetAddress()}");
79-
Console.WriteLine($"InAppWallet: {await inAppWallet.GetAddress()}");
80-
Console.WriteLine($"Smart Wallet: {await smartWallet.GetAddress()}");
81-
82-
// Sign, triggering deploy as needed and 1271 verification if it's a smart wallet
83-
var message = "Hello, Thirdweb!";
84-
var signature = await smartWallet.PersonalSign(message);
85-
Console.WriteLine($"Signed message: {signature}");
86-
87-
var balanceBefore = await ThirdwebContract.Read<BigInteger>(contract, "balanceOf", await smartWallet.GetAddress());
88-
Console.WriteLine($"Balance before mint: {balanceBefore}");
89-
90-
var writeResult = await ThirdwebContract.Write(smartWallet, contract, "mintTo", 0, await smartWallet.GetAddress(), 100);
91-
Console.WriteLine($"Contract write result: {writeResult}");
92-
93-
var balanceAfter = await ThirdwebContract.Read<BigInteger>(contract, "balanceOf", await smartWallet.GetAddress());
94-
Console.WriteLine($"Balance after mint: {balanceAfter}");
95-
96-
// Transaction Builder
97-
var preparedTx = await ThirdwebContract.Prepare(wallet: smartWallet, contract: contract, method: "mintTo", weiValue: 0, parameters: new object[] { await smartWallet.GetAddress(), 100 });
98-
Console.WriteLine($"Prepared transaction: {preparedTx}");
99-
var estimatedCosts = await ThirdwebTransaction.EstimateGasCosts(preparedTx);
100-
Console.WriteLine($"Estimated ETH gas cost: {estimatedCosts.ether}");
101-
var totalCosts = await ThirdwebTransaction.EstimateTotalCosts(preparedTx);
102-
Console.WriteLine($"Estimated ETH total cost: {totalCosts.ether}");
103-
var simulationData = await ThirdwebTransaction.Simulate(preparedTx);
104-
Console.WriteLine($"Simulation data: {simulationData}");
105-
var txHash = await ThirdwebTransaction.Send(preparedTx);
106-
Console.WriteLine($"Transaction hash: {txHash}");
107-
var receipt = await ThirdwebTransaction.WaitForTransactionReceipt(client, 421614, txHash);
108-
Console.WriteLine($"Transaction receipt: {JsonConvert.SerializeObject(receipt)}");
109-
110-
// Transaction Builder - raw transfer
111-
var rawTx = new TransactionInput
112-
{
113-
From = await smartWallet.GetAddress(),
114-
To = await smartWallet.GetAddress(),
115-
Value = new HexBigInteger(BigInteger.Zero),
116-
Data = "0x",
117-
};
118-
var preparedRawTx = await ThirdwebTransaction.Create(client: client, wallet: smartWallet, txInput: rawTx, chainId: 421614);
119-
Console.WriteLine($"Prepared raw transaction: {preparedRawTx}");
120-
var estimatedCostsRaw = await ThirdwebTransaction.EstimateGasCosts(preparedRawTx);
121-
Console.WriteLine($"Estimated ETH gas cost: {estimatedCostsRaw.ether}");
122-
var totalCostsRaw = await ThirdwebTransaction.EstimateTotalCosts(preparedRawTx);
123-
Console.WriteLine($"Estimated ETH total cost: {totalCostsRaw.ether}");
124-
var simulationDataRaw = await ThirdwebTransaction.Simulate(preparedRawTx);
125-
Console.WriteLine($"Simulation data: {simulationDataRaw}");
126-
var txHashRaw = await ThirdwebTransaction.Send(preparedRawTx);
127-
Console.WriteLine($"Raw transaction hash: {txHashRaw}");
128-
var receiptRaw = await ThirdwebTransaction.WaitForTransactionReceipt(client, 421614, txHashRaw);
129-
Console.WriteLine($"Raw transaction receipt: {JsonConvert.SerializeObject(receiptRaw)}");
55+
var res = await smartWallet.Authenticate("http://localhost:8000", 421614);
56+
Console.WriteLine($"Smart wallet auth result: {res}");
57+
58+
// // Grant a session key to pk wallet (advanced use case)
59+
// _ = await smartWallet.CreateSessionKey(
60+
// signerAddress: await privateKeyWallet.GetAddress(),
61+
// approvedTargets: new List<string>() { Constants.ADDRESS_ZERO },
62+
// nativeTokenLimitPerTransactionInWei: "0",
63+
// permissionStartTimestamp: "0",
64+
// permissionEndTimestamp: (Utils.GetUnixTimeStampNow() + 86400).ToString(),
65+
// reqValidityStartTimestamp: "0",
66+
// reqValidityEndTimestamp: Utils.GetUnixTimeStampIn10Years().ToString()
67+
// );
68+
69+
// // Reconnect to same smart wallet with pk wallet as signer (specifying wallet address override)
70+
// smartWallet = await SmartWallet.Create(
71+
// client: client,
72+
// personalWallet: privateKeyWallet,
73+
// factoryAddress: "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052",
74+
// gasless: true,
75+
// chainId: 421614,
76+
// accountAddressOverride: await smartWallet.GetAddress()
77+
// );
78+
79+
// // Log addresses
80+
// Console.WriteLine($"PrivateKey Wallet: {await privateKeyWallet.GetAddress()}");
81+
// Console.WriteLine($"InAppWallet: {await inAppWallet.GetAddress()}");
82+
// Console.WriteLine($"Smart Wallet: {await smartWallet.GetAddress()}");
83+
84+
// // Sign, triggering deploy as needed and 1271 verification if it's a smart wallet
85+
// var message = "Hello, Thirdweb!";
86+
// var signature = await smartWallet.PersonalSign(message);
87+
// Console.WriteLine($"Signed message: {signature}");
88+
89+
// var balanceBefore = await ThirdwebContract.Read<BigInteger>(contract, "balanceOf", await smartWallet.GetAddress());
90+
// Console.WriteLine($"Balance before mint: {balanceBefore}");
91+
92+
// var writeResult = await ThirdwebContract.Write(smartWallet, contract, "mintTo", 0, await smartWallet.GetAddress(), 100);
93+
// Console.WriteLine($"Contract write result: {writeResult}");
94+
95+
// var balanceAfter = await ThirdwebContract.Read<BigInteger>(contract, "balanceOf", await smartWallet.GetAddress());
96+
// Console.WriteLine($"Balance after mint: {balanceAfter}");
97+
98+
// // Transaction Builder
99+
// var preparedTx = await ThirdwebContract.Prepare(wallet: smartWallet, contract: contract, method: "mintTo", weiValue: 0, parameters: new object[] { await smartWallet.GetAddress(), 100 });
100+
// Console.WriteLine($"Prepared transaction: {preparedTx}");
101+
// var estimatedCosts = await ThirdwebTransaction.EstimateGasCosts(preparedTx);
102+
// Console.WriteLine($"Estimated ETH gas cost: {estimatedCosts.ether}");
103+
// var totalCosts = await ThirdwebTransaction.EstimateTotalCosts(preparedTx);
104+
// Console.WriteLine($"Estimated ETH total cost: {totalCosts.ether}");
105+
// var simulationData = await ThirdwebTransaction.Simulate(preparedTx);
106+
// Console.WriteLine($"Simulation data: {simulationData}");
107+
// var txHash = await ThirdwebTransaction.Send(preparedTx);
108+
// Console.WriteLine($"Transaction hash: {txHash}");
109+
// var receipt = await ThirdwebTransaction.WaitForTransactionReceipt(client, 421614, txHash);
110+
// Console.WriteLine($"Transaction receipt: {JsonConvert.SerializeObject(receipt)}");
111+
112+
// // Transaction Builder - raw transfer
113+
// var rawTx = new TransactionInput
114+
// {
115+
// From = await smartWallet.GetAddress(),
116+
// To = await smartWallet.GetAddress(),
117+
// Value = new HexBigInteger(BigInteger.Zero),
118+
// Data = "0x",
119+
// };
120+
// var preparedRawTx = await ThirdwebTransaction.Create(client: client, wallet: smartWallet, txInput: rawTx, chainId: 421614);
121+
// Console.WriteLine($"Prepared raw transaction: {preparedRawTx}");
122+
// var estimatedCostsRaw = await ThirdwebTransaction.EstimateGasCosts(preparedRawTx);
123+
// Console.WriteLine($"Estimated ETH gas cost: {estimatedCostsRaw.ether}");
124+
// var totalCostsRaw = await ThirdwebTransaction.EstimateTotalCosts(preparedRawTx);
125+
// Console.WriteLine($"Estimated ETH total cost: {totalCostsRaw.ether}");
126+
// var simulationDataRaw = await ThirdwebTransaction.Simulate(preparedRawTx);
127+
// Console.WriteLine($"Simulation data: {simulationDataRaw}");
128+
// var txHashRaw = await ThirdwebTransaction.Send(preparedRawTx);
129+
// Console.WriteLine($"Raw transaction hash: {txHashRaw}");
130+
// var receiptRaw = await ThirdwebTransaction.WaitForTransactionReceipt(client, 421614, txHashRaw);
131+
// Console.WriteLine($"Raw transaction receipt: {JsonConvert.SerializeObject(receiptRaw)}");
130132

131133

132134
// Storage actions

Thirdweb.Tests/Thirdweb.Transactions.Tests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,13 @@ public async Task EstimateGasCosts_SmartWalletHigherThanPrivateKeyWallet()
218218
public async Task EstimateTotalCosts_HigherThanGasCostsByValue()
219219
{
220220
var transaction = await CreateSampleTransaction();
221-
_ = transaction.SetValue(new BigInteger(1000));
221+
_ = transaction.SetValue(new BigInteger(1000000000000000000)); // 100 gwei accounting for fluctuations
222222
_ = transaction.SetGasLimit(21000);
223223

224224
var totalCosts = await ThirdwebTransaction.EstimateTotalCosts(transaction);
225225
var gasCosts = await ThirdwebTransaction.EstimateGasCosts(transaction);
226226

227227
Assert.True(totalCosts.wei > gasCosts.wei);
228-
Assert.True(totalCosts.wei - gasCosts.wei == transaction.Input.Value.Value);
229228
}
230229

231230
[Fact]

Thirdweb.Tests/Thirdweb.Utils.Tests.cs

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,177 @@ public void FormatERC20_ThrowsOnInvalidWei()
202202
var invalidWei = "not_a_number";
203203
Assert.Throws<ArgumentException>(() => Utils.FormatERC20(invalidWei, 4));
204204
}
205+
206+
[Fact]
207+
public void GenerateSIWE_ReturnsCorrectValue()
208+
{
209+
var loginPayloadData = new LoginPayloadData
210+
{
211+
Version = "1",
212+
ChainId = "421614",
213+
Nonce = "0",
214+
Address = Constants.ADDRESS_ZERO,
215+
Domain = "thirdweb.com",
216+
IssuedAt = "0",
217+
ExpirationTime = "0",
218+
InvalidBefore = "0"
219+
};
220+
var expectedSIWE =
221+
"thirdweb.com wants you to sign in with your Ethereum account:\n0x0000000000000000000000000000000000000000\n\n\nVersion: 1\nChain ID: 421614\nNonce: 0\nIssued At: 0\nExpiration Time: 0\nNot Before: 0";
222+
var siwe = Utils.GenerateSIWE(loginPayloadData);
223+
Assert.Equal(expectedSIWE, siwe);
224+
}
225+
226+
[Fact]
227+
public void GenerateSIWE_WithAllOptional_ReturnsCorrectValue()
228+
{
229+
var loginPayloadData = new LoginPayloadData
230+
{
231+
Version = "1",
232+
ChainId = "421614",
233+
Nonce = "0",
234+
Address = Constants.ADDRESS_ZERO,
235+
Domain = "thirdweb.com",
236+
IssuedAt = "0",
237+
ExpirationTime = "0",
238+
InvalidBefore = "0",
239+
Statement = "This is a statement",
240+
Uri = "https://thirdweb.com",
241+
Resources = new List<string>() { "resource1", "resource2" }
242+
};
243+
var expectedSIWE =
244+
"thirdweb.com wants you to sign in with your Ethereum account:\n0x0000000000000000000000000000000000000000\n\nThis is a statement\n\nURI: https://thirdweb.com\nVersion: 1\nChain ID: 421614\nNonce: 0\nIssued At: 0\nExpiration Time: 0\nNot Before: 0\nResources:\n- resource1\n- resource2";
245+
var siwe = Utils.GenerateSIWE(loginPayloadData);
246+
Assert.Equal(expectedSIWE, siwe);
247+
}
248+
249+
[Fact]
250+
public void GenerateSIWE_WithResources_ReturnsCorrectValue()
251+
{
252+
var loginPayloadData = new LoginPayloadData
253+
{
254+
Version = "1",
255+
ChainId = "421614",
256+
Nonce = "0",
257+
Address = Constants.ADDRESS_ZERO,
258+
Domain = "thirdweb.com",
259+
IssuedAt = "0",
260+
ExpirationTime = "0",
261+
InvalidBefore = "0",
262+
Resources = new List<string>() { "resource1", "resource2" }
263+
};
264+
var expectedSIWE =
265+
"thirdweb.com wants you to sign in with your Ethereum account:\n0x0000000000000000000000000000000000000000\n\n\nVersion: 1\nChain ID: 421614\nNonce: 0\nIssued At: 0\nExpiration Time: 0\nNot Before: 0\nResources:\n- resource1\n- resource2";
266+
var siwe = Utils.GenerateSIWE(loginPayloadData);
267+
Assert.Equal(expectedSIWE, siwe);
268+
}
269+
270+
[Fact]
271+
public void GenerateSIWE_ThrowsOnNullLoginPayloadData()
272+
{
273+
LoginPayloadData? loginPayloadData = null;
274+
_ = Assert.Throws<ArgumentNullException>(() => Utils.GenerateSIWE(loginPayloadData));
275+
}
276+
277+
[Fact]
278+
public void GenerateSIWE_ThrowsOnNullDomain()
279+
{
280+
var loginPayloadData = new LoginPayloadData
281+
{
282+
Version = "1",
283+
ChainId = "421614",
284+
Nonce = "0",
285+
Address = Constants.ADDRESS_ZERO,
286+
Domain = null!,
287+
IssuedAt = "0",
288+
ExpirationTime = "0",
289+
InvalidBefore = "0"
290+
};
291+
_ = Assert.Throws<ArgumentNullException>(() => Utils.GenerateSIWE(loginPayloadData));
292+
}
293+
294+
[Fact]
295+
public void GenerateSIWE_ThrowsOnNullAddress()
296+
{
297+
var loginPayloadData = new LoginPayloadData
298+
{
299+
Version = "1",
300+
ChainId = "421614",
301+
Nonce = "0",
302+
Address = null!,
303+
Domain = "thirdweb.com",
304+
IssuedAt = "0",
305+
ExpirationTime = "0",
306+
InvalidBefore = "0"
307+
};
308+
_ = Assert.Throws<ArgumentNullException>(() => Utils.GenerateSIWE(loginPayloadData));
309+
}
310+
311+
[Fact]
312+
public void GenerateSIWE_ThrowsOnNullVersion()
313+
{
314+
var loginPayloadData = new LoginPayloadData
315+
{
316+
Version = null!,
317+
ChainId = "421614",
318+
Nonce = "0",
319+
Address = Constants.ADDRESS_ZERO,
320+
Domain = "thirdweb.com",
321+
IssuedAt = "0",
322+
ExpirationTime = "0",
323+
InvalidBefore = "0"
324+
};
325+
_ = Assert.Throws<ArgumentNullException>(() => Utils.GenerateSIWE(loginPayloadData));
326+
}
327+
328+
[Fact]
329+
public void GenerateSIWE_ThrowsOnNullChainId()
330+
{
331+
var loginPayloadData = new LoginPayloadData
332+
{
333+
Version = "1",
334+
ChainId = null!,
335+
Nonce = "0",
336+
Address = Constants.ADDRESS_ZERO,
337+
Domain = "thirdweb.com",
338+
IssuedAt = "0",
339+
ExpirationTime = "0",
340+
InvalidBefore = "0"
341+
};
342+
_ = Assert.Throws<ArgumentNullException>(() => Utils.GenerateSIWE(loginPayloadData));
343+
}
344+
345+
[Fact]
346+
public void GenerateSIWE_ThrowsOnNullNonce()
347+
{
348+
var loginPayloadData = new LoginPayloadData
349+
{
350+
Version = "1",
351+
ChainId = "421614",
352+
Nonce = null!,
353+
Address = Constants.ADDRESS_ZERO,
354+
Domain = "thirdweb.com",
355+
IssuedAt = "0",
356+
ExpirationTime = "0",
357+
InvalidBefore = "0"
358+
};
359+
_ = Assert.Throws<ArgumentNullException>(() => Utils.GenerateSIWE(loginPayloadData));
360+
}
361+
362+
[Fact]
363+
public void GenerateSIWE_ThrowsOnNullIssuedAt()
364+
{
365+
var loginPayloadData = new LoginPayloadData
366+
{
367+
Version = "1",
368+
ChainId = "421614",
369+
Nonce = "0",
370+
Address = Constants.ADDRESS_ZERO,
371+
Domain = "thirdweb.com",
372+
IssuedAt = null!,
373+
ExpirationTime = "0",
374+
InvalidBefore = "0"
375+
};
376+
_ = Assert.Throws<ArgumentNullException>(() => Utils.GenerateSIWE(loginPayloadData));
377+
}
205378
}

0 commit comments

Comments
 (0)