Skip to content

Commit c428cbe

Browse files
committed
WalletConnect auto switch on connect fix
1 parent 9385c45 commit c428cbe

File tree

4 files changed

+38
-122
lines changed

4 files changed

+38
-122
lines changed

Assets/Thirdweb/Examples/Scenes/Scene_Playground.unity

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4212,22 +4212,6 @@ PrefabInstance:
42124212
serializedVersion: 3
42134213
m_TransformParent: {fileID: 0}
42144214
m_Modifications:
4215-
- target: {fileID: 376894756574179703, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
4216-
propertyPath: m_AnchorMax.y
4217-
value: 0
4218-
objectReference: {fileID: 0}
4219-
- target: {fileID: 376894756574179703, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
4220-
propertyPath: m_AnchorMin.y
4221-
value: 0
4222-
objectReference: {fileID: 0}
4223-
- target: {fileID: 376894756574179703, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
4224-
propertyPath: m_SizeDelta.x
4225-
value: 0
4226-
objectReference: {fileID: 0}
4227-
- target: {fileID: 376894756574179703, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
4228-
propertyPath: m_AnchoredPosition.x
4229-
value: 0
4230-
objectReference: {fileID: 0}
42314215
- target: {fileID: 1288923049808236541, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
42324216
propertyPath: <BundleId>k__BackingField
42334217
value: com.thirdweb.unitysdk
@@ -4324,26 +4308,6 @@ PrefabInstance:
43244308
propertyPath: m_AnchoredPosition.y
43254309
value: 0
43264310
objectReference: {fileID: 0}
4327-
- target: {fileID: 7170212914370332091, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
4328-
propertyPath: m_AnchorMax.y
4329-
value: 0
4330-
objectReference: {fileID: 0}
4331-
- target: {fileID: 7170212914370332091, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
4332-
propertyPath: m_AnchorMin.y
4333-
value: 0
4334-
objectReference: {fileID: 0}
4335-
- target: {fileID: 7170212914370332091, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
4336-
propertyPath: m_SizeDelta.x
4337-
value: 0
4338-
objectReference: {fileID: 0}
4339-
- target: {fileID: 7170212914370332091, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
4340-
propertyPath: m_AnchoredPosition.x
4341-
value: 0
4342-
objectReference: {fileID: 0}
4343-
- target: {fileID: 7170212914370332091, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
4344-
propertyPath: m_AnchoredPosition.y
4345-
value: 0
4346-
objectReference: {fileID: 0}
43474311
- target: {fileID: 7977017408921700919, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
43484312
propertyPath: m_LocalPosition.x
43494313
value: 0
@@ -4387,10 +4351,7 @@ PrefabInstance:
43874351
m_RemovedComponents: []
43884352
m_RemovedGameObjects: []
43894353
m_AddedGameObjects: []
4390-
m_AddedComponents:
4391-
- targetCorrespondingSourceObject: {fileID: 7581174088450466508, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
4392-
insertIndex: -1
4393-
addedObject: {fileID: 686223896}
4354+
m_AddedComponents: []
43944355
m_SourcePrefab: {fileID: 100100000, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
43954356
--- !u!1 &581207178
43964357
GameObject:
@@ -5445,24 +5406,6 @@ MonoBehaviour:
54455406
m_Spacing: {x: 50, y: 50}
54465407
m_Constraint: 0
54475408
m_ConstraintCount: 2
5448-
--- !u!1 &686223891 stripped
5449-
GameObject:
5450-
m_CorrespondingSourceObject: {fileID: 7581174088450466508, guid: f1f7a02a8c5efe54c95062bd90592227, type: 3}
5451-
m_PrefabInstance: {fileID: 565346545}
5452-
m_PrefabAsset: {fileID: 0}
5453-
--- !u!114 &686223896
5454-
MonoBehaviour:
5455-
m_ObjectHideFlags: 0
5456-
m_CorrespondingSourceObject: {fileID: 0}
5457-
m_PrefabInstance: {fileID: 0}
5458-
m_PrefabAsset: {fileID: 0}
5459-
m_GameObject: {fileID: 686223891}
5460-
m_Enabled: 1
5461-
m_EditorHideFlags: 0
5462-
m_Script: {fileID: 11500000, guid: cc33a39070010f94fb1c2dd721c1286d, type: 3}
5463-
m_Name:
5464-
m_EditorClassIdentifier:
5465-
showHtmlElement: 0
54665409
--- !u!1 &700700974
54675410
GameObject:
54685411
m_ObjectHideFlags: 0

Assets/Thirdweb/Runtime/Unity/Prefabs/ThirdwebManager.prefab

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ MonoBehaviour:
5151
<InitializeOnAwake>k__BackingField: 1
5252
<ShowDebugLogs>k__BackingField: 1
5353
<OptOutUsageAnalytics>k__BackingField: 0
54-
<SupportedChains>k__BackingField: 0100000000000000a736aa000000000089000000000000008238010000000000fa00000000000000a20f0000000000006aa800000000000069a8000000000000b1a4000000000000ee6e060000000000380000000000000061000000000000000a00000000000000dc37aa0000000000
54+
<SupportedChains>k__BackingField: ee6e060000000000
5555
--- !u!1001 &1183062832708090628
5656
PrefabInstance:
5757
m_ObjectHideFlags: 0

Assets/Thirdweb/Runtime/Unity/Wallets/Core/WalletConnectWallet.cs

Lines changed: 35 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Nethereum.RPC.Eth.DTOs;
1616
using WalletConnectUnity.Core.Evm;
1717
using Nethereum.Hex.HexTypes;
18+
using UnityEngine;
1819

1920
namespace Thirdweb.Unity
2021
{
@@ -48,22 +49,26 @@ public async static Task<WalletConnectWallet> Create(ThirdwebClient client, BigI
4849
_isConnected = false;
4950
_supportedChains = eip155ChainsSupported;
5051

51-
WalletConnectModal.Ready += OnReady;
52-
WalletConnect.Instance.ActiveSessionChanged += OnActiveSessionChanged;
53-
WalletConnect.Instance.SessionDisconnected += OnSessionDisconnected;
52+
if (WalletConnect.Instance != null && WalletConnect.Instance.IsConnected)
53+
{
54+
try
55+
{
56+
await WalletConnect.Instance.DisconnectAsync();
57+
}
58+
catch
59+
{
60+
// no-op
61+
}
62+
await Task.Delay(100);
63+
}
5464

55-
if (WalletConnect.Instance.IsInitialized)
56-
CreateNewSession();
65+
CreateNewSession(eip155ChainsSupported);
5766

58-
while (!_isConnected && _exception == null)
67+
while (!WalletConnect.Instance.IsConnected && _exception == null)
5968
{
6069
await Task.Delay(100);
6170
}
6271

63-
WalletConnectModal.Ready -= OnReady;
64-
WalletConnect.Instance.ActiveSessionChanged -= OnActiveSessionChanged;
65-
WalletConnect.Instance.SessionDisconnected -= OnSessionDisconnected;
66-
6772
if (_exception != null)
6873
{
6974
throw _exception;
@@ -72,17 +77,9 @@ public async static Task<WalletConnectWallet> Create(ThirdwebClient client, BigI
7277
{
7378
try
7479
{
75-
var chainInfo = await Utils.FetchThirdwebChainDataAsync(client, initialChainId);
76-
var wcChainInfo = new EthereumChain()
77-
{
78-
chainIdHex = new HexBigInteger(chainInfo.ChainId).HexValue,
79-
name = chainInfo.Name,
80-
nativeCurrency = new Currency(chainInfo.NativeCurrency.Name, chainInfo.NativeCurrency.Symbol, chainInfo.NativeCurrency.Decimals),
81-
rpcUrls = new string[] { $"https://{chainInfo.ChainId}.rpc.thirdweb.com" },
82-
blockExplorerUrls = chainInfo.Explorers == null || chainInfo.Explorers.Count == 0 ? null : new string[] { chainInfo.Explorers[0].Url },
83-
chainIdDecimal = chainInfo.ChainId.ToString(),
84-
};
85-
await WalletConnect.Instance.SwitchEthereumChainAsync(wcChainInfo);
80+
var data = new WalletSwitchEthereumChain(new HexBigInteger(initialChainId).HexValue);
81+
await WalletConnect.Instance.RequestAsync<WalletSwitchEthereumChain, string>(data);
82+
await Task.Delay(5000); // wait for chain switch to take effect
8683
await WalletConnect.Instance.SignClient.AddressProvider.SetDefaultChainIdAsync($"eip155:{initialChainId}");
8784
}
8885
catch (Exception e)
@@ -107,15 +104,26 @@ public async Task EnsureCorrectNetwork(BigInteger chainId)
107104
blockExplorerUrls = chainInfo.Explorers == null || chainInfo.Explorers.Count == 0 ? null : new string[] { chainInfo.Explorers[0].Url },
108105
chainIdDecimal = chainInfo.ChainId.ToString(),
109106
};
110-
await WalletConnect.Instance.SwitchEthereumChainAsync(wcChainInfo);
107+
var request = new WalletAddEthereumChain(wcChainInfo);
108+
try
109+
{
110+
await WalletConnect.Instance.RequestAsync<WalletAddEthereumChain, string>(request);
111+
}
112+
catch
113+
{
114+
// no-op
115+
}
116+
var data = new WalletSwitchEthereumChain(new HexBigInteger(chainId).HexValue);
117+
await WalletConnect.Instance.RequestAsync<WalletSwitchEthereumChain, string>(data);
118+
await Task.Delay(5000); // wait for chain switch to take effect
111119
await WalletConnect.Instance.SignClient.AddressProvider.SetDefaultChainIdAsync($"eip155:{chainId}");
112120
}
113121

114122
#region IThirdwebWallet
115123

116124
public Task<string> GetAddress()
117125
{
118-
return Task.FromResult(WalletConnect.Instance.SignClient.AddressProvider.CurrentAddress().Address.ToChecksumAddress());
126+
return Task.FromResult(WalletConnect.Instance.SignClient.AddressProvider.CurrentAddress(WalletConnect.Instance.ActiveChainId).Address.ToChecksumAddress());
119127
}
120128

121129
public Task<string> EthSign(byte[] rawMessage)
@@ -272,54 +280,19 @@ public Task<string> RecoverAddressFromTypedDataV4<T, TDomain>(T data, TypedData<
272280

273281
#region UI
274282

275-
protected static void OnSessionDisconnected(object sender, EventArgs e)
276-
{
277-
_isConnected = false;
278-
}
279-
280-
protected static void OnActiveSessionChanged(object sender, SessionStruct sessionStruct)
281-
{
282-
if (!string.IsNullOrEmpty(sessionStruct.Topic))
283-
{
284-
_isConnected = true;
285-
}
286-
else
287-
{
288-
_isConnected = false;
289-
}
290-
}
291-
292-
protected static async void OnReady(object sender, ModalReadyEventArgs args)
293-
{
294-
try
295-
{
296-
if (args.SessionResumed)
297-
{
298-
// Session exists
299-
await WalletConnect.Instance.DisconnectAsync();
300-
}
301-
302-
CreateNewSession();
303-
}
304-
catch (Exception e)
305-
{
306-
_exception = e;
307-
}
308-
}
309-
310-
protected static void CreateNewSession()
283+
protected static void CreateNewSession(string[] supportedChains)
311284
{
312285
try
313286
{
314-
var optionalNamespaces = new Dictionary<string, ProposedNamespace>
287+
var optionalNamespaces = new Dictionary<string, ProposedNamespace>()
315288
{
316289
{
317290
"eip155",
318291
new ProposedNamespace
319292
{
320293
Methods = new[] { "eth_sendTransaction", "personal_sign", "eth_signTypedData_v4", "wallet_switchEthereumChain", "wallet_addEthereumChain" },
321-
Chains = _supportedChains,
322-
Events = new[] { "chainChanged", "accountsChanged" },
294+
Chains = supportedChains,
295+
Events = new string[] { "chainChanged", "accountsChanged" },
323296
}
324297
}
325298
};

ProjectSettings/ProjectSettings.asset

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ PlayerSettings:
253253
clonedFromGUID: c0afd0d1d80e3634a9dac47e8a0426ea
254254
templatePackageId: [email protected]
255255
templateDefaultScene: Assets/Scenes/SampleScene.unity
256-
useCustomMainManifest: 0
256+
useCustomMainManifest: 1
257257
useCustomLauncherManifest: 0
258258
useCustomMainGradleTemplate: 0
259259
useCustomLauncherGradleManifest: 0

0 commit comments

Comments
 (0)