Skip to content

Commit

Permalink
fix(walletconnect): fix invalid chain id in wallet connect
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonboukheir committed Feb 27, 2023
1 parent 7bbeac8 commit abdedaf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public HandshakeUrl RequestWalletConnection()
default:
throw new InvalidOperationException($"Session connection status is {ConnectionStatus}");
}

return new HandshakeUrl(HandshakeTopic, Version, BridgeUrl, Key);
}

Expand All @@ -180,7 +181,8 @@ public async UniTask WaitForWalletApproval(CancellationToken cancellationToken =
throw new InvalidOperationException($"Session connection status is {ConnectionStatus}");

var handshakeResponse = await listeningForApproval;
var sessionData = AlgoApiSerializer.DeserializeJson<WalletConnectSessionData>(handshakeResponse.Result.Json);
var sessionData =
AlgoApiSerializer.DeserializeJson<WalletConnectSessionData>(handshakeResponse.Result.Json);
UpdateSession(sessionData);
HandshakeTopic = null;
ConnectionStatus = SessionStatus.WalletConnected;
Expand Down Expand Up @@ -228,13 +230,15 @@ public void DisconnectWallet(string reason = default)
WalletTransaction[] transactions,
SignTxnsOpts options = default,
CancellationToken cancellationToken = default
)
)
{
if (ConnectionStatus != SessionStatus.WalletConnected)
throw new InvalidOperationException($"Session connection status is {ConnectionStatus}");

if (ChainId != WalletConnectRpc.Algorand.ChainId)
throw new InvalidOperationException($"Wallet does not have Algorand as the active chain.");
var isNetworkAgnostic = ChainId == WalletConnectRpc.Algorand.ChainId;
if (!isNetworkAgnostic && WalletConnectRpc.Algorand.GetNetworkFromChainId(ChainId) == AlgorandNetwork.None)
throw new InvalidOperationException(
$"Wallet does not have Algorand as the active chain. Active chain id is: {ChainId}.");

var request = WalletConnectRpc.Algorand.SignTransactions(transactions, options);
var response = await rpc.Send(request, PeerId, cancellationToken);
Expand All @@ -254,8 +258,10 @@ private void HandleJsonRpcRequestReceived(JsonRpcRequest request)
{
case WalletConnectRpc.SessionUpdateMethod:
if (request.Params == null || request.Params.Length != 1)
throw new NotSupportedException($"The JsonRpcRequest method \"{WalletConnectRpc.SessionUpdateMethod}\" only supports params of length 1.");
var sessonUpdate = AlgoApiSerializer.DeserializeJson<WalletConnectSessionData>(request.Params[0].Json);
throw new NotSupportedException(
$"The JsonRpcRequest method \"{WalletConnectRpc.SessionUpdateMethod}\" only supports params of length 1.");
var sessonUpdate =
AlgoApiSerializer.DeserializeJson<WalletConnectSessionData>(request.Params[0].Json);
UpdateSession(sessonUpdate);
OnSessionUpdate?.Invoke(sessonUpdate);
break;
Expand All @@ -271,6 +277,7 @@ private void UpdateSession(WalletConnectSessionData sessionData)
DisconnectWallet("Session no longer approved.");
return;
}

switch (sessionData.ChainId)
{
case WalletConnectRpc.Algorand.TestNetChainId:
Expand All @@ -289,4 +296,4 @@ private void UpdateSession(WalletConnectSessionData sessionData)
this.sessionData.Accounts = sessionData.Accounts;
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Algorand.Unity.WalletConnect/Models/WalletConnectRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ public static class Algorand

public const int BetaNetChainId = 416003;

public static AlgorandNetwork GetNetworkFromChainId(int chainId)
{
switch (chainId)
{
case MainNetChainId: return AlgorandNetwork.MainNet;
case TestNetChainId: return AlgorandNetwork.TestNet;
case BetaNetChainId: return AlgorandNetwork.BetaNet;
default: return AlgorandNetwork.None;
}
}

/// <summary>
/// Builds a <see cref="JsonRpcRequest"/> used for signing transactions.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Samples~/WalletConnect/WalletConnectDemo.unity
Git LFS file not shown
9 changes: 8 additions & 1 deletion Samples~/WalletConnect/WalletConnectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,14 @@ private async UniTaskVoid PollForBalance()
if (status == SessionStatus.WalletConnected)
{
var (err, response) = await indexer.LookupAccountByID(account.Address);
if (err) Debug.LogError(err);
if (err)
{
currentBalance = 0;
if (!err.Message.Contains("no accounts found for address"))
{
Debug.LogError(err);
}
}
else
{
currentBalance = response.Account.Amount;
Expand Down

0 comments on commit abdedaf

Please sign in to comment.