Skip to content

Commit d89187c

Browse files
author
Andrey Dobrikov
committed
PR cleanup
Added null‑checks/try‑catches in token helpers and unsubscribed auth event Corrected .editorconfig key Hardened README samples Added InternalError enum & refined OTP error mapping Used SafeFireAndForget with logging for initialization Fixed misleading exception message in wallet manager
1 parent a8d7fa9 commit d89187c

9 files changed

Lines changed: 76 additions & 16 deletions

File tree

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ csharp_space_after_colon_in_inheritance_clause = true
3939
dotnet_style_prefer_auto_properties = true:suggestion
4040

4141
# prefer expression-bodied members when simple
42-
dotnet_style_prefer_expression_bodied_methods = true:suggestion
42+
csharp_style_expression_bodied_methods = true:suggestion
4343

4444
dotnet_style_object_initializer = true:error
4545
dotnet_style_collection_initializer = true:error

SDK/README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,23 @@ try {
113113

114114
```csharp
115115
try {
116-
IEmbeddedEthereumWallet embeddedWallet = PrivyManager.Instance.User.EmbeddedWallets[0];
116+
// obtain the current user and ensure they're authenticated
117+
IPrivyUser privyUser = await PrivyManager.Instance.GetUser();
118+
if (privyUser == null)
119+
{
120+
Debug.LogWarning("No authenticated user – cannot perform RPC request.");
121+
return;
122+
}
123+
124+
// make sure there is at least one embedded wallet available
125+
var wallets = privyUser.EmbeddedWallets;
126+
if (wallets == null || wallets.Count == 0)
127+
{
128+
Debug.LogWarning("No embedded wallets found for user.");
129+
return;
130+
}
131+
132+
IEmbeddedEthereumWallet embeddedWallet = wallets[0];
117133

118134
var rpcRequest = new RpcRequest
119135
{

SDK/Runtime/Auth/AuthDelegator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public async Task<bool> SendEmailCode(string email)
4444
{
4545
if (string.IsNullOrEmpty(email))
4646
{
47-
//This check saves us from making a request we know will faill
47+
//This check saves us from making a request we know will fail
4848
throw new PrivyAuthenticationException("Email cannot be null or empty",
4949
AuthenticationError.EmailEmpty);
5050
}

SDK/Runtime/Auth/AuthRepository.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,17 @@ public async Task<InternalAuthSession> LoginWithEmailCode(string email, string c
7575

7676
return _internalAuthSession;
7777
}
78+
catch (Exception ex) when (ex.Message.Contains("422"))
79+
{
80+
// server returned a 422 Unprocessable Entity, which means the OTP was wrong
81+
throw new PrivyAuthenticationException("Incorrect OTP code.",
82+
AuthenticationError.IncorrectOtpCode);
83+
}
7884
catch (Exception ex)
7985
{
80-
//This catches request failures
86+
// this catch now handles any other underlying failure (network, deserialization, etc.)
8187
throw new PrivyAuthenticationException($"Failed to login with email code: {ex.Message}",
82-
AuthenticationError.WrongOtpCode, ex);
88+
AuthenticationError.InternalError, ex);
8389
}
8490
}
8591

SDK/Runtime/Auth/Models/IPrivyUser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public interface IPrivyUser
4949
/// <summary>
5050
/// Creates a new embedded wallet for the user.
5151
/// </summary>
52-
/// <param name="allowAditional">Whether to allow the creation of additional wallets derived from the primary HD wallet</param>
52+
/// <param name="allowAdditional">Whether to allow the creation of additional wallets derived from the primary HD wallet</param>
5353
/// <returns>A task that represents the asynchronous operation. The task result contains the newly created embedded wallet.</returns>
5454
/// <exception cref="PrivyAuthenticationException">
5555
/// Thrown if there is an issue with authentication, such as a failure to refresh the access token.

SDK/Runtime/Core/PrivyManager.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
33
using Privy.Config;
4+
using Privy.Utils;
45

56
namespace Privy.Core
67
{
@@ -30,8 +31,10 @@ public static IPrivy Initialize(PrivyConfig config)
3031
{
3132
_privyInstance = new PrivyImpl(config);
3233
// fire-and-forget initialization; any calls to GetAuthState/GetUser will
33-
// await internally until initialization completes.
34-
_ = _privyInstance.InitializeAsync();
34+
// await internally until initialization completes. catch errors so
35+
// they don't get swallowed silently.
36+
_privyInstance.InitializeAsync()
37+
.SafeFireAndForget(ex => PrivyLogger.Error("Privy initialization failed", ex));
3538
}
3639

3740
return _privyInstance; // return immediately

SDK/Runtime/EmbeddedWallet/EmbeddedWalletManager.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,8 @@ internal async Task<byte[]> SignWithUserSigner(string accessToken, byte[] messag
310310
return Convert.FromBase64String(signatureAsBase64);
311311
}
312312

313-
throw new PrivyWalletException($"Failed to create additional wallet",
314-
EmbeddedWalletError
315-
.CreateAdditionalFailed); //Let this bubble up to HandleAuthStateChanged and AwaitConnected
313+
throw new PrivyWalletException($"Failed to sign with user signer",
314+
EmbeddedWalletError.CreateAdditionalFailed); //Let this bubble up to HandleAuthStateChanged and AwaitConnected
316315
}
317316
}
318317
}

SDK/Runtime/Utils/PrivyException.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public enum AuthenticationError
5555
InvalidPhoneNumber,
5656
LinkFailed,
5757
UnlinkFailed,
58-
IncorrectOtpCode
58+
IncorrectOtpCode,
59+
InternalError
5960
}
6061

6162
public enum EmbeddedWalletError

SampleApp/Assets/Scripts/AuthScreenController.cs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ private void Awake()
7878
PrivyManager.Instance.AuthStateChanged += OnAuthStateChange;
7979
}
8080

81+
private void OnDestroy()
82+
{
83+
// remove event handler to prevent memory leaks when this object is destroyed
84+
if (PrivyManager.Instance != null)
85+
{
86+
PrivyManager.Instance.AuthStateChanged -= OnAuthStateChange;
87+
}
88+
}
89+
8190
// ── Login method switching ───────────────────────────────────────────────
8291

8392
/// <summary>Called by UIManager before showing the send-code screen.</summary>
@@ -338,15 +347,41 @@ private void OnWalletButtonClick()
338347
private async void OnGetAccessTokenButtonClick()
339348
{
340349
IPrivyUser user = await PrivyManager.Instance.GetUser();
341-
string accessToken = await user!.GetAccessToken();
342-
Debug.Log("Access token: " + accessToken);
350+
if (user == null)
351+
{
352+
Debug.LogWarning("No authenticated user available to fetch access token.");
353+
return;
354+
}
355+
356+
try
357+
{
358+
string accessToken = await user.GetAccessToken();
359+
Debug.Log("Access token: " + accessToken);
360+
}
361+
catch (Exception ex)
362+
{
363+
Debug.LogError("Failed to get access token: " + ex.Message);
364+
}
343365
}
344366

345367
private async void OnGetIdentityTokenButtonClick()
346368
{
347369
IPrivyUser user = await PrivyManager.Instance.GetUser();
348-
string identityToken = await user!.GetIdentityToken();
349-
Debug.Log("Identity token: " + identityToken);
370+
if (user == null)
371+
{
372+
Debug.LogWarning("No authenticated user available to fetch identity token.");
373+
return;
374+
}
375+
376+
try
377+
{
378+
string identityToken = await user.GetIdentityToken();
379+
Debug.Log("Identity token: " + identityToken);
380+
}
381+
catch (Exception ex)
382+
{
383+
Debug.LogError("Failed to get identity token: " + ex.Message);
384+
}
350385
}
351386

352387
private void OnLogOutButtonClick()

0 commit comments

Comments
 (0)