Skip to content

Commit da1976f

Browse files
authored
Cleanup connect api + remove supportedNetworks from wallet prefabs (#64)
1 parent 38371e4 commit da1976f

File tree

10 files changed

+213
-713
lines changed

10 files changed

+213
-713
lines changed

Assets/Thirdweb/Core/Scripts/Bridge.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static async Task<string> Connect(WalletConnection walletConnection)
9191
string taskId = Guid.NewGuid().ToString();
9292
taskMap[taskId] = task;
9393
#if UNITY_WEBGL
94-
ThirdwebConnect(taskId, walletConnection.provider.ToString(), walletConnection.chainId, walletConnection.password ?? Utils.GetDeviceIdentifier(), jsCallback);
94+
ThirdwebConnect(taskId, walletConnection.provider.ToString().Substring(0,1).ToLower() + walletConnection.provider.ToString().Substring(1), walletConnection.chainId, walletConnection.password ?? Utils.GetDeviceIdentifier(), jsCallback);
9595
#endif
9696
string result = await task.Task;
9797
return result;

Assets/Thirdweb/Core/Scripts/Utils.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,21 @@ public static string GetAccountPath()
261261
return Application.persistentDataPath + "/account.json";
262262
}
263263

264-
public static Account UnlockOrGenerateAccount(int chainId, string password = null, string privateKey = null)
264+
public static bool DeleteLocalAccount()
265+
{
266+
try
267+
{
268+
File.Delete(GetAccountPath());
269+
return true;
270+
}
271+
catch (System.Exception e)
272+
{
273+
Debug.LogWarning("Error deleting account: " + e.Message);
274+
return false;
275+
}
276+
}
277+
278+
public static Account UnlockOrGenerateLocalAccount(int chainId, string password = null, string privateKey = null)
265279
{
266280
password ??= GetDeviceIdentifier();
267281

Assets/Thirdweb/Core/Scripts/Wallet.cs

Lines changed: 61 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -26,73 +26,61 @@ public Wallet()
2626
/// <summary>
2727
/// Connect a user's wallet via a given wallet provider
2828
/// </summary>
29-
/// <param name="walletConnection">The wallet provider and chainId to connect to. Defaults to the injected browser extension.</param>
30-
public async Task<string> Connect(WalletConnection? walletConnection = null)
29+
/// <param name="walletConnection">The wallet provider and optional parameters. Defaults to local wallet.</param>
30+
public async Task<string> Connect(WalletConnection walletConnection = null)
3131
{
32+
walletConnection ??= new WalletConnection(WalletProvider.LocalWallet, 1);
33+
3234
if (Utils.IsWebGLBuild())
3335
{
34-
var connection = walletConnection ?? new WalletConnection() { provider = WalletProvider.Injected, };
35-
return await Bridge.Connect(connection);
36+
return await Bridge.Connect(walletConnection);
3637
}
3738
else
3839
{
3940
ThirdwebSDK.NativeSession oldSession = ThirdwebManager.Instance.SDK.nativeSession;
4041

41-
if (walletConnection == null)
42+
if (walletConnection.provider == WalletProvider.WalletConnectV1)
4243
{
43-
Account noPassAcc = Utils.UnlockOrGenerateAccount(oldSession.lastChainId, null, null);
44+
await WalletConnect.Instance.EnableWalletConnect();
45+
4446
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
4547
oldSession.lastChainId,
4648
oldSession.lastRPC,
47-
noPassAcc,
48-
new Web3(noPassAcc, oldSession.lastRPC),
49+
null,
50+
WalletConnect.Instance.Session.BuildWeb3(new Uri(oldSession.lastRPC)).AsWalletAccount(true),
4951
oldSession.options,
5052
oldSession.siweSession
5153
);
52-
return noPassAcc.Address;
53-
}
54-
else
55-
{
56-
if (walletConnection?.provider?.ToString() == "walletConnectV1")
57-
{
58-
await WalletConnect.Instance.EnableWalletConnect();
5954

60-
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
61-
oldSession.lastChainId,
62-
oldSession.lastRPC,
63-
null,
64-
WalletConnect.Instance.Session.BuildWeb3(new Uri(oldSession.lastRPC)).AsWalletAccount(true),
65-
oldSession.options,
66-
oldSession.siweSession
67-
);
55+
// Switch to chain
56+
try
57+
{
58+
await WalletConnect.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId });
59+
}
60+
catch (System.Exception e)
61+
{
62+
Debug.LogWarning("Switching chain error, attempting to add chain: " + e.Message);
6863

69-
// Switch to chain
64+
// Add chain
7065
try
7166
{
67+
await WalletConnect.Instance.WalletAddEthChain(ThirdwebManager.Instance.SDK.currentChainData);
7268
await WalletConnect.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId });
7369
}
74-
catch (System.Exception e)
70+
catch (System.Exception f)
7571
{
76-
Debug.LogWarning("Switching chain error, attempting to add chain: " + e.Message);
77-
78-
// Add chain
79-
try
80-
{
81-
await WalletConnect.Instance.WalletAddEthChain(ThirdwebManager.Instance.SDK.currentChainData);
82-
await WalletConnect.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId });
83-
}
84-
catch (System.Exception f)
85-
{
86-
Debug.LogWarning("Adding chain error: " + f.Message);
87-
return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(WalletConnect.Instance.Session.Accounts[0]);
88-
}
72+
Debug.LogWarning("Adding chain error: " + f.Message);
73+
return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(WalletConnect.Instance.Session.Accounts[0]);
8974
}
90-
91-
return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(WalletConnect.Instance.Session.Accounts[0]);
9275
}
93-
else if (walletConnection?.password != null)
76+
77+
return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(WalletConnect.Instance.Session.Accounts[0]);
78+
}
79+
else if (walletConnection.provider == WalletProvider.LocalWallet)
80+
{
81+
if (walletConnection.privateKey != null)
9482
{
95-
Account acc = Utils.UnlockOrGenerateAccount(oldSession.lastChainId, walletConnection?.password, null);
83+
Account acc = Utils.UnlockOrGenerateLocalAccount(oldSession.lastChainId, null, walletConnection.privateKey);
9684
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
9785
oldSession.lastChainId,
9886
oldSession.lastRPC,
@@ -103,9 +91,9 @@ public async Task<string> Connect(WalletConnection? walletConnection = null)
10391
);
10492
return acc.Address;
10593
}
106-
else if (walletConnection?.privateKey != null)
94+
else if (walletConnection.password != null)
10795
{
108-
Account acc = Utils.UnlockOrGenerateAccount(oldSession.lastChainId, null, walletConnection?.privateKey);
96+
Account acc = Utils.UnlockOrGenerateLocalAccount(oldSession.lastChainId, walletConnection.password, null);
10997
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
11098
oldSession.lastChainId,
11199
oldSession.lastRPC,
@@ -118,9 +106,22 @@ public async Task<string> Connect(WalletConnection? walletConnection = null)
118106
}
119107
else
120108
{
121-
throw new UnityException("This wallet connection method is not supported on this platform!");
109+
Account noPassAcc = Utils.UnlockOrGenerateLocalAccount(oldSession.lastChainId, null, null);
110+
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
111+
oldSession.lastChainId,
112+
oldSession.lastRPC,
113+
noPassAcc,
114+
new Web3(noPassAcc, oldSession.lastRPC),
115+
oldSession.options,
116+
oldSession.siweSession
117+
);
118+
return noPassAcc.Address;
122119
}
123120
}
121+
else
122+
{
123+
throw new UnityException("This wallet connection method is not supported on this platform!");
124+
}
124125
}
125126
}
126127

@@ -478,51 +479,29 @@ public async Task FundWallet(FundWalletOptions options)
478479
}
479480
}
480481

481-
public struct WalletConnection
482+
public class WalletConnection
482483
{
483484
public WalletProvider provider;
484485
public int chainId;
485486
public string password;
486487
public string privateKey;
487-
}
488488

489-
public class WalletProvider
490-
{
491-
private WalletProvider(string value)
489+
public WalletConnection(WalletProvider provider = WalletProvider.LocalWallet, int chainId = 1, string password = null, string privateKey = null)
492490
{
493-
Value = value;
494-
}
495-
496-
public static string Value { get; private set; }
497-
498-
public static WalletProvider MetaMask
499-
{
500-
get { return new WalletProvider("metamask"); }
501-
}
502-
public static WalletProvider CoinbaseWallet
503-
{
504-
get { return new WalletProvider("coinbaseWallet"); }
505-
}
506-
public static WalletProvider WalletConnect
507-
{
508-
get { return new WalletProvider("walletConnectV1"); }
509-
}
510-
public static WalletProvider Injected
511-
{
512-
get { return new WalletProvider("injected"); }
513-
}
514-
public static WalletProvider MagicAuth
515-
{
516-
get { return new WalletProvider("magicAuth"); }
517-
}
518-
public static WalletProvider DeviceWallet
519-
{
520-
get { return new WalletProvider("localWallet"); }
491+
this.provider = provider;
492+
this.chainId = chainId;
493+
this.password = password;
494+
this.privateKey = privateKey;
521495
}
496+
}
522497

523-
public override string ToString()
524-
{
525-
return Value;
526-
}
498+
public enum WalletProvider
499+
{
500+
MetaMask,
501+
CoinbaseWallet,
502+
WalletConnectV1,
503+
Injected,
504+
MagicAuth,
505+
LocalWallet,
527506
}
528507
}

0 commit comments

Comments
 (0)