Skip to content

Commit c732a15

Browse files
author
Andrey Dobrikov
committed
WebViewManager/Handler improvements
Generic SendRequest - less code duplication Proper disposal Cancellation token handling void Task -> Task as void is an antipattern that swallows exceptions. Format changes
1 parent 99bc653 commit c732a15

4 files changed

Lines changed: 172 additions & 453 deletions

File tree

SDK/Runtime/EmbeddedWallet/BrowserDomIframeHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private void InjectIframe(string url)
105105
Application.ExternalEval(jsCode);
106106

107107
// Trigger PingReadyUntilSuccessful after injecting the iframe
108-
_webViewManager.PingReadyUntilSuccessful();
108+
_ = _webViewManager.PingReadyUntilSuccessful();
109109
}
110110
}
111111
}

SDK/Runtime/EmbeddedWallet/EmbeddedWalletManager.cs

Lines changed: 27 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -91,72 +91,36 @@ private async Task AwaitConnected()
9191
}
9292

9393

94+
// Extracts TData from a successful IframeResponse, or throws PrivyWalletException.
95+
private static TData UnwrapResult<TData>(IframeResponse result, string errorMessage, EmbeddedWalletError errorCode)
96+
{
97+
if (result is IframeResponseSuccess<TData> success)
98+
return success.Data;
99+
100+
var detail = (result as IframeResponseError)?.Error?.Message;
101+
102+
throw new PrivyWalletException(
103+
detail != null ? $"{errorMessage}: {detail}" : errorMessage,
104+
errorCode);
105+
}
106+
94107
internal async Task<string> CreateEthereumWallet(string accessToken, string solanaAddress = null)
95108
{
96109
var result = await _webViewManager.CreateEthereumWallet(accessToken, solanaAddress);
97-
98-
if (result is IframeResponseError errorResponse)
99-
{
100-
throw new PrivyWalletException(
101-
$"Failed to create wallet: {errorResponse.Error.Message}",
102-
EmbeddedWalletError.CreateFailed); //Let this bubble up to Create
103-
}
104-
else if (result is IframeResponseSuccess<CreateEthereumWalletResponseData> walletResponse)
105-
{
106-
string connectedWalletAddress = walletResponse.Data.Address;
107-
return connectedWalletAddress;
108-
}
109-
else
110-
{
111-
throw new PrivyWalletException($"Failed to create wallet",
112-
EmbeddedWalletError.CreateFailed); //Let this bubble up to HandleAuthStateChanged and AwaitConnected
113-
}
110+
return UnwrapResult<CreateEthereumWalletResponseData>(result, "Failed to create wallet", EmbeddedWalletError.CreateFailed).Address;
114111
}
115112

116113
internal async Task<string> CreateSolanaWallet(string accessToken, string ethereumAddress = null)
117114
{
118115
var result = await _webViewManager.CreateSolanaWallet(accessToken);
119-
120-
if (result is IframeResponseError errorResponse)
121-
{
122-
throw new PrivyWalletException(
123-
$"Failed to create wallet: {errorResponse.Error.Message}",
124-
EmbeddedWalletError.CreateFailed); //Let this bubble up to Create
125-
}
126-
else if (result is IframeResponseSuccess<CreateSolanaWalletResponseData> walletResponse)
127-
{
128-
return walletResponse.Data.PublicKey;
129-
}
130-
else
131-
{
132-
throw new PrivyWalletException($"Failed to create wallet",
133-
EmbeddedWalletError.CreateFailed); //Let this bubble up to HandleAuthStateChanged and AwaitConnected
134-
}
116+
return UnwrapResult<CreateSolanaWalletResponseData>(result, "Failed to create wallet", EmbeddedWalletError.CreateFailed).PublicKey;
135117
}
136118

137119
internal async Task<string> CreateAdditionalWallet(string accessToken, WalletEntropy walletEntropy,
138120
ChainType chainType, int hdWalletIndex)
139121
{
140-
var result =
141-
await _webViewManager.CreateAdditionalWallet(accessToken, walletEntropy, chainType, hdWalletIndex);
142-
143-
if (result is IframeResponseError errorResponse)
144-
{
145-
throw new PrivyWalletException(
146-
$"Failed to create additional wallet: {errorResponse.Error.Message}",
147-
EmbeddedWalletError.CreateAdditionalFailed); //Let this bubble up to Create
148-
}
149-
else if (result is IframeResponseSuccess<CreateAdditionalWalletResponseData> walletResponse)
150-
{
151-
string connectedWalletAddress = walletResponse.Data.Address;
152-
return connectedWalletAddress;
153-
}
154-
else
155-
{
156-
throw new PrivyWalletException($"Failed to create additional wallet",
157-
EmbeddedWalletError
158-
.CreateAdditionalFailed); //Let this bubble up to HandleAuthStateChanged and AwaitConnected
159-
}
122+
var result = await _webViewManager.CreateAdditionalWallet(accessToken, walletEntropy, chainType, hdWalletIndex);
123+
return UnwrapResult<CreateAdditionalWalletResponseData>(result, "Failed to create additional wallet", EmbeddedWalletError.CreateAdditionalFailed).Address;
160124
}
161125

162126
internal async Task<string> ConnectWallet(string accessToken, WalletEntropy walletEntropy,
@@ -221,46 +185,19 @@ await RecoverWalletThenTryConnecting(accessToken,
221185
}
222186
}
223187

224-
else if (result is IframeResponseSuccess<ConnectWalletResponseData> walletResponse)
225-
{
226-
// EntropyId equals the wallet address in this case
227-
string connectedWalletAddress = walletResponse.Data.EntropyId;
228-
_embeddedWalletState = new EmbeddedWalletState.Connected(connectedWalletAddress);
229-
return connectedWalletAddress;
230-
}
231-
else
232-
{
233-
throw new PrivyWalletException($"Failed to connect wallet",
234-
EmbeddedWalletError
235-
.ConnectionFailed); //Let this bubble up to HandleAuthStateChanged and AwaitConnected
236-
}
188+
var data = UnwrapResult<ConnectWalletResponseData>(result, "Failed to connect wallet", EmbeddedWalletError.ConnectionFailed);
189+
// EntropyId equals the wallet address in this case
190+
_embeddedWalletState = new EmbeddedWalletState.Connected(data.EntropyId);
191+
return data.EntropyId;
237192
}
238193

239194

240195
internal async Task<string> RecoverWalletThenTryConnecting(string accessToken, WalletEntropy walletEntropy)
241196
{
242-
//This method will be used when there's an error in connecting wallet
243-
//Makes a request to recover the wallet
244-
//If recovery is successful, then we can connect without an error
245197
var result = await _webViewManager.RecoverWallet(accessToken, walletEntropy);
246-
247-
if (result is IframeResponseSuccess<RecoverWalletResponseData> walletResponse)
248-
{
249-
// After recovery, call ConnectWalletWithoutLock to avoid re-locking
250-
return await ConnectWalletWithoutLock(accessToken, walletEntropy,
251-
false); // No recovery on second attempt
252-
}
253-
else if (result is IframeResponseError errorResponse)
254-
{
255-
throw new PrivyWalletException(
256-
$"Failed to recover wallet: {errorResponse.Error.Message}",
257-
EmbeddedWalletError.RecoverFailed); //Let this bubble up to connect wallet without lock
258-
}
259-
else
260-
{
261-
throw new PrivyWalletException($"Failed to recover wallet",
262-
EmbeddedWalletError.RecoverFailed); //Let this bubble up to connect wallet without lock
263-
}
198+
UnwrapResult<RecoverWalletResponseData>(result, "Failed to recover wallet", EmbeddedWalletError.RecoverFailed);
199+
// After recovery, call ConnectWalletWithoutLock to avoid re-locking
200+
return await ConnectWalletWithoutLock(accessToken, walletEntropy, false);
264201
}
265202

266203
internal async Task<RpcResponseData.IRpcResponseDetails> Request(WalletEntropy walletEntropy,
@@ -275,43 +212,14 @@ internal async Task<string> RecoverWalletThenTryConnecting(string accessToken, W
275212

276213
string token = await _authDelegator.GetAccessToken();
277214
var result = await _webViewManager.Request(token, walletEntropy, chainType, hdWalletIndex, request);
278-
279-
if (result is IframeResponseSuccess<RpcResponseData> rpcResponseData)
280-
{
281-
return rpcResponseData.Data.Response;
282-
}
283-
else if (result is IframeResponseError errorResponse)
284-
{
285-
throw new PrivyWalletException(
286-
$"Failed to execute RPC Request: {errorResponse.Error.Message}",
287-
EmbeddedWalletError.RpcRequestFailed); //Let this bubble up to developer RPC Request
288-
}
289-
else
290-
{
291-
throw new PrivyWalletException($"Failed to execute RPC Request",
292-
EmbeddedWalletError.RpcRequestFailed); //Let this bubble up to developer RPC Request
293-
}
215+
return UnwrapResult<RpcResponseData>(result, "Failed to execute RPC Request", EmbeddedWalletError.RpcRequestFailed).Response;
294216
}
295217

296218
internal async Task<byte[]> SignWithUserSigner(string accessToken, byte[] message)
297219
{
298220
var result = await _webViewManager.SignWithUserSigner(accessToken, message);
299-
300-
if (result is IframeResponseError errorResponse)
301-
{
302-
throw new PrivyWalletException(
303-
$"Failed to sign with the user's authorization key: {errorResponse.Error.Message}",
304-
EmbeddedWalletError.UserSignerRequestFailed);
305-
}
306-
307-
if (result is IframeResponseSuccess<UserSignerSignResponseData> walletResponse)
308-
{
309-
string signatureAsBase64 = walletResponse.Data.Signature;
310-
return Convert.FromBase64String(signatureAsBase64);
311-
}
312-
313-
throw new PrivyWalletException($"Failed to sign with user signer",
314-
EmbeddedWalletError.CreateAdditionalFailed); //Let this bubble up to HandleAuthStateChanged and AwaitConnected
221+
var signature = UnwrapResult<UserSignerSignResponseData>(result, "Failed to sign with the user's authorization key", EmbeddedWalletError.UserSignerRequestFailed).Signature;
222+
return Convert.FromBase64String(signature);
315223
}
316224
}
317225
}

0 commit comments

Comments
 (0)