Skip to content

Commit

Permalink
Cleanup connect api + remove supportedNetworks from wallet prefabs (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper authored May 15, 2023
1 parent 38371e4 commit da1976f
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 713 deletions.
2 changes: 1 addition & 1 deletion Assets/Thirdweb/Core/Scripts/Bridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static async Task<string> Connect(WalletConnection walletConnection)
string taskId = Guid.NewGuid().ToString();
taskMap[taskId] = task;
#if UNITY_WEBGL
ThirdwebConnect(taskId, walletConnection.provider.ToString(), walletConnection.chainId, walletConnection.password ?? Utils.GetDeviceIdentifier(), jsCallback);
ThirdwebConnect(taskId, walletConnection.provider.ToString().Substring(0,1).ToLower() + walletConnection.provider.ToString().Substring(1), walletConnection.chainId, walletConnection.password ?? Utils.GetDeviceIdentifier(), jsCallback);
#endif
string result = await task.Task;
return result;
Expand Down
16 changes: 15 additions & 1 deletion Assets/Thirdweb/Core/Scripts/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,21 @@ public static string GetAccountPath()
return Application.persistentDataPath + "/account.json";
}

public static Account UnlockOrGenerateAccount(int chainId, string password = null, string privateKey = null)
public static bool DeleteLocalAccount()
{
try
{
File.Delete(GetAccountPath());
return true;
}
catch (System.Exception e)
{
Debug.LogWarning("Error deleting account: " + e.Message);
return false;
}
}

public static Account UnlockOrGenerateLocalAccount(int chainId, string password = null, string privateKey = null)
{
password ??= GetDeviceIdentifier();

Expand Down
143 changes: 61 additions & 82 deletions Assets/Thirdweb/Core/Scripts/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,73 +26,61 @@ public Wallet()
/// <summary>
/// Connect a user's wallet via a given wallet provider
/// </summary>
/// <param name="walletConnection">The wallet provider and chainId to connect to. Defaults to the injected browser extension.</param>
public async Task<string> Connect(WalletConnection? walletConnection = null)
/// <param name="walletConnection">The wallet provider and optional parameters. Defaults to local wallet.</param>
public async Task<string> Connect(WalletConnection walletConnection = null)
{
walletConnection ??= new WalletConnection(WalletProvider.LocalWallet, 1);

if (Utils.IsWebGLBuild())
{
var connection = walletConnection ?? new WalletConnection() { provider = WalletProvider.Injected, };
return await Bridge.Connect(connection);
return await Bridge.Connect(walletConnection);
}
else
{
ThirdwebSDK.NativeSession oldSession = ThirdwebManager.Instance.SDK.nativeSession;

if (walletConnection == null)
if (walletConnection.provider == WalletProvider.WalletConnectV1)
{
Account noPassAcc = Utils.UnlockOrGenerateAccount(oldSession.lastChainId, null, null);
await WalletConnect.Instance.EnableWalletConnect();

ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
oldSession.lastChainId,
oldSession.lastRPC,
noPassAcc,
new Web3(noPassAcc, oldSession.lastRPC),
null,
WalletConnect.Instance.Session.BuildWeb3(new Uri(oldSession.lastRPC)).AsWalletAccount(true),
oldSession.options,
oldSession.siweSession
);
return noPassAcc.Address;
}
else
{
if (walletConnection?.provider?.ToString() == "walletConnectV1")
{
await WalletConnect.Instance.EnableWalletConnect();

ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
oldSession.lastChainId,
oldSession.lastRPC,
null,
WalletConnect.Instance.Session.BuildWeb3(new Uri(oldSession.lastRPC)).AsWalletAccount(true),
oldSession.options,
oldSession.siweSession
);
// Switch to chain
try
{
await WalletConnect.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId });
}
catch (System.Exception e)
{
Debug.LogWarning("Switching chain error, attempting to add chain: " + e.Message);

// Switch to chain
// Add chain
try
{
await WalletConnect.Instance.WalletAddEthChain(ThirdwebManager.Instance.SDK.currentChainData);
await WalletConnect.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId });
}
catch (System.Exception e)
catch (System.Exception f)
{
Debug.LogWarning("Switching chain error, attempting to add chain: " + e.Message);

// Add chain
try
{
await WalletConnect.Instance.WalletAddEthChain(ThirdwebManager.Instance.SDK.currentChainData);
await WalletConnect.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId });
}
catch (System.Exception f)
{
Debug.LogWarning("Adding chain error: " + f.Message);
return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(WalletConnect.Instance.Session.Accounts[0]);
}
Debug.LogWarning("Adding chain error: " + f.Message);
return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(WalletConnect.Instance.Session.Accounts[0]);
}

return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(WalletConnect.Instance.Session.Accounts[0]);
}
else if (walletConnection?.password != null)

return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(WalletConnect.Instance.Session.Accounts[0]);
}
else if (walletConnection.provider == WalletProvider.LocalWallet)
{
if (walletConnection.privateKey != null)
{
Account acc = Utils.UnlockOrGenerateAccount(oldSession.lastChainId, walletConnection?.password, null);
Account acc = Utils.UnlockOrGenerateLocalAccount(oldSession.lastChainId, null, walletConnection.privateKey);
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
oldSession.lastChainId,
oldSession.lastRPC,
Expand All @@ -103,9 +91,9 @@ public async Task<string> Connect(WalletConnection? walletConnection = null)
);
return acc.Address;
}
else if (walletConnection?.privateKey != null)
else if (walletConnection.password != null)
{
Account acc = Utils.UnlockOrGenerateAccount(oldSession.lastChainId, null, walletConnection?.privateKey);
Account acc = Utils.UnlockOrGenerateLocalAccount(oldSession.lastChainId, walletConnection.password, null);
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
oldSession.lastChainId,
oldSession.lastRPC,
Expand All @@ -118,9 +106,22 @@ public async Task<string> Connect(WalletConnection? walletConnection = null)
}
else
{
throw new UnityException("This wallet connection method is not supported on this platform!");
Account noPassAcc = Utils.UnlockOrGenerateLocalAccount(oldSession.lastChainId, null, null);
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
oldSession.lastChainId,
oldSession.lastRPC,
noPassAcc,
new Web3(noPassAcc, oldSession.lastRPC),
oldSession.options,
oldSession.siweSession
);
return noPassAcc.Address;
}
}
else
{
throw new UnityException("This wallet connection method is not supported on this platform!");
}
}
}

Expand Down Expand Up @@ -478,51 +479,29 @@ public async Task FundWallet(FundWalletOptions options)
}
}

public struct WalletConnection
public class WalletConnection
{
public WalletProvider provider;
public int chainId;
public string password;
public string privateKey;
}

public class WalletProvider
{
private WalletProvider(string value)
public WalletConnection(WalletProvider provider = WalletProvider.LocalWallet, int chainId = 1, string password = null, string privateKey = null)
{
Value = value;
}

public static string Value { get; private set; }

public static WalletProvider MetaMask
{
get { return new WalletProvider("metamask"); }
}
public static WalletProvider CoinbaseWallet
{
get { return new WalletProvider("coinbaseWallet"); }
}
public static WalletProvider WalletConnect
{
get { return new WalletProvider("walletConnectV1"); }
}
public static WalletProvider Injected
{
get { return new WalletProvider("injected"); }
}
public static WalletProvider MagicAuth
{
get { return new WalletProvider("magicAuth"); }
}
public static WalletProvider DeviceWallet
{
get { return new WalletProvider("localWallet"); }
this.provider = provider;
this.chainId = chainId;
this.password = password;
this.privateKey = privateKey;
}
}

public override string ToString()
{
return Value;
}
public enum WalletProvider
{
MetaMask,
CoinbaseWallet,
WalletConnectV1,
Injected,
MagicAuth,
LocalWallet,
}
}
Loading

0 comments on commit da1976f

Please sign in to comment.