Skip to content

Commit f55232b

Browse files
authoredMar 23, 2023
WalletConnect Switch (#50)
1 parent 8b516f1 commit f55232b

File tree

9 files changed

+116
-8
lines changed

9 files changed

+116
-8
lines changed
 

‎Assets/Resources.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/Resources/all_chains.json

+1
Large diffs are not rendered by default.

‎Assets/Resources/all_chains.json.meta

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Assets/Thirdweb/Core/Scripts/ThirdwebSDK.cs

+73
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System.Collections.Generic;
2+
using System.Numerics;
3+
using Nethereum.Hex.HexConvertors.Extensions;
24
using Nethereum.Siwe;
35
using Nethereum.Web3;
46
using Nethereum.Web3.Accounts;
57
using UnityEngine;
8+
using WalletConnectSharp.Core.Models.Ethereum;
69

710
namespace Thirdweb
811
{
@@ -89,6 +92,8 @@ public struct BiconomyOptions
8992

9093
public Storage storage;
9194

95+
public EthChainData currentChainData;
96+
9297
public class NativeSession
9398
{
9499
public int lastChainId = -1;
@@ -138,6 +143,8 @@ public NativeSession(int lastChainId, string lastRPC, Account account, Web3 web3
138143
appIcons = options.wallet?.appIcons ?? new string[] { "https://thirdweb.com/favicon.ico" },
139144
appUrl = options.wallet?.appUrl ?? "https://thirdweb.com"
140145
};
146+
147+
FetchChainData();
141148
}
142149
else
143150
{
@@ -155,5 +162,71 @@ public Contract GetContract(string address, string abi = null)
155162
{
156163
return new Contract(this.chainOrRPC, address, abi);
157164
}
165+
166+
void FetchChainData()
167+
{
168+
var allChainsJson = (TextAsset)Resources.Load("all_chains", typeof(TextAsset));
169+
// Get networks from chainidnetwork
170+
List<ChainIDNetworkData> allNetworkData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ChainIDNetworkData>>(allChainsJson.text);
171+
// Get current network
172+
ChainIDNetworkData currentNetwork = allNetworkData.Find(x => x.chainId == nativeSession.lastChainId.ToString());
173+
// Get block explorer urls
174+
List<string> explorerUrls = new List<string>();
175+
foreach (var explorer in currentNetwork.explorers)
176+
explorerUrls.Add(explorer.url);
177+
if (explorerUrls.Count == 0)
178+
explorerUrls.Add("https://etherscan.io");
179+
// Add chain
180+
currentChainData = new EthChainData()
181+
{
182+
chainId = BigInteger.Parse(currentNetwork.chainId).ToHex(false, true) ?? BigInteger.Parse(nativeSession.lastChainId.ToString()).ToHex(false, true),
183+
blockExplorerUrls = explorerUrls.ToArray(),
184+
chainName = currentNetwork.name ?? ThirdwebManager.Instance.supportedChainData[ThirdwebManager.Instance.chain].identifier,
185+
iconUrls = new string[] { "ipfs://QmdwQDr6vmBtXmK2TmknkEuZNoaDqTasFdZdu3DRw8b2wt" },
186+
nativeCurrency = new NativeCurrency()
187+
{
188+
name = currentNetwork.nativeCurrency?.name ?? "Ether",
189+
symbol = currentNetwork.nativeCurrency?.symbol ?? "ETH",
190+
decimals = int.Parse(currentNetwork.nativeCurrency?.decimals ?? "18")
191+
},
192+
rpcUrls = new string[] { nativeSession.lastRPC }
193+
};
194+
}
195+
196+
[System.Serializable]
197+
public class ChainIDNetworkData
198+
{
199+
public string name;
200+
public string chain;
201+
public string icon;
202+
public List<string> rpc;
203+
public ChainIDNetworkNativeCurrency nativeCurrency;
204+
public string chainId;
205+
public List<ChainIDNetworkExplorer> explorers;
206+
}
207+
208+
[System.Serializable]
209+
public class ChainIDNetworkNativeCurrency
210+
{
211+
public string name;
212+
public string symbol;
213+
public string decimals;
214+
}
215+
216+
[System.Serializable]
217+
public class ChainIDNetworkExplorer
218+
{
219+
public string name;
220+
public string url;
221+
public string standard;
222+
}
223+
224+
public struct ChaiNIDNetworkIcon
225+
{
226+
public string url;
227+
public int width;
228+
public int height;
229+
public string format;
230+
}
158231
}
159232
}

‎Assets/Thirdweb/Core/Scripts/Wallet.cs

+21-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
using Nethereum.Siwe.Core;
1010
using System.Collections.Generic;
1111
using Nethereum.Web3.Accounts;
12+
using UnityEngine.Networking;
13+
using Nethereum.Hex.HexConvertors.Extensions;
14+
using WalletConnectSharp.Core.Models.Ethereum;
1215

1316
//using WalletConnectSharp.NEthereum;
1417

@@ -19,7 +22,8 @@ namespace Thirdweb
1922
/// </summary>
2023
public class Wallet : Routable
2124
{
22-
public Wallet() : base($"sdk{subSeparator}wallet") { }
25+
public Wallet()
26+
: base($"sdk{subSeparator}wallet") { }
2327

2428
/// <summary>
2529
/// Connect a user's wallet via a given wallet provider
@@ -63,6 +67,20 @@ public async Task<string> Connect(WalletConnection? walletConnection = null)
6367
oldSession.options,
6468
oldSession.siweSession
6569
);
70+
71+
// Add chain
72+
try
73+
{
74+
await WalletConnect.Instance.WalletAddEthChain(ThirdwebManager.Instance.SDK.currentChainData);
75+
}
76+
catch (System.Exception e)
77+
{
78+
Debug.Log("Adding chain error: " + e.Message);
79+
}
80+
81+
// Switch to chain
82+
await WalletConnect.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId });
83+
6684
return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(WalletConnect.Instance.Session.Accounts[0]);
6785
}
6886
else if (walletConnection?.password != null)
@@ -262,7 +280,8 @@ public async Task<CurrencyValue> GetBalance(string currencyAddress = Utils.Nativ
262280
else
263281
{
264282
var balance = await ThirdwebManager.Instance.SDK.nativeSession.web3.Eth.GetBalance.SendRequestAsync(await ThirdwebManager.Instance.SDK.wallet.GetAddress());
265-
return new CurrencyValue("Ether", "ETH", "18", balance.Value.ToString(), balance.Value.ToString().ToEth()); // TODO: Get actual name/symbol
283+
var nativeCurrency = ThirdwebManager.Instance.SDK.currentChainData.nativeCurrency;
284+
return new CurrencyValue(nativeCurrency.name, nativeCurrency.symbol, nativeCurrency.decimals.ToString(), balance.Value.ToString(), balance.Value.ToString().ToEth());
266285
}
267286
}
268287
}

‎Assets/Thirdweb/Plugins/WalletConnectSharp.Core/WalletConnectProtocol.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ protected async void TransportOnMessageReceived(object sender, MessageReceivedEv
188188

189189
var json = await Cipher.DecryptWithKey(_keyRaw, encryptedPayload);
190190

191-
var payload = JsonConvert.DeserializeObject<JsonRpcPayload>(json);
191+
var payload = JsonConvert.DeserializeObject<JsonRpcResponse>(json); // Edited from JsonRpcPayload
192192

193193
if (payload != null)
194194
{

‎Assets/Thirdweb/Plugins/WalletConnectSharp.Core/WalletConnectSession.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,11 @@ public virtual async Task<string> WalletAddEthChain(EthChainData chainData)
315315
return response.Result;
316316
}
317317

318-
public virtual async Task<string> WalletSwitchEthChain(EthChainData chainData)
318+
public virtual async Task<string> WalletSwitchEthChain(EthChain ethChain)
319319
{
320320
EnsureNotDisconnected();
321321

322-
var request = new WalletSwitchEthChain(chainData);
322+
var request = new WalletSwitchEthChain(ethChain);
323323

324324
var response = await Send<WalletSwitchEthChain, EthResponse>(request);
325325

‎Assets/Thirdweb/Plugins/WalletConnectSharp.Unity/WalletConnect.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -521,9 +521,9 @@ public async Task<string> WalletAddEthChain(EthChainData chainData)
521521
return results;
522522
}
523523

524-
public async Task<string> WalletSwitchEthChain(EthChainData chainData)
524+
public async Task<string> WalletSwitchEthChain(EthChain ethChain)
525525
{
526-
var results = await Session.WalletSwitchEthChain(chainData);
526+
var results = await Session.WalletSwitchEthChain(ethChain);
527527
return results;
528528
}
529529

‎ProjectSettings/ProjectSettings.asset

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ PlayerSettings:
136136
16:10: 1
137137
16:9: 1
138138
Others: 1
139-
bundleVersion: 2.0.1-alpha
139+
bundleVersion: 2.0.2
140140
preloadedAssets: []
141141
metroInputSource: 0
142142
wsaTransparentSwapchain: 0

0 commit comments

Comments
 (0)
Please sign in to comment.