Skip to content

Commit

Permalink
BigInteger Chain ID on native excl WCV1/ML // remove TWManager helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper committed Jun 21, 2023
1 parent f76aa10 commit 80d77df
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 63 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 @@ -94,7 +94,7 @@ public static async Task<string> Connect(WalletConnection walletConnection)
ThirdwebConnect(
taskId,
walletConnection.provider.ToString().Substring(0,1).ToLower() + walletConnection.provider.ToString().Substring(1),
walletConnection.chainId, string.IsNullOrEmpty(walletConnection.password) ? Utils.GetDeviceIdentifier() : walletConnection.password,
(int)walletConnection.chainId, string.IsNullOrEmpty(walletConnection.password) ? Utils.GetDeviceIdentifier() : walletConnection.password,
walletConnection.email,
walletConnection.personalWallet.ToString().Substring(0,1).ToLower() + walletConnection.personalWallet.ToString().Substring(1),
jsCallback
Expand Down
27 changes: 4 additions & 23 deletions Assets/Thirdweb/Core/Scripts/ThirdwebManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;
using Thirdweb;
using System.Collections.Generic;
using System.Numerics;

[System.Serializable]
public class ChainData
Expand Down Expand Up @@ -117,18 +118,18 @@ private void Awake()

// Inspector chain data dictionary.

ChainData currentChain = GetChainData(chain);
ChainData currentChain = supportedChains.Find(x => x.identifier == chain);

// Chain ID must be provided on native platforms.

int chainId = -1;
BigInteger chainId = -1;

if (!Utils.IsWebGLBuild())
{
if (string.IsNullOrEmpty(currentChain.chainId))
throw new UnityException("You must provide a Chain ID on native platforms!");

if (!int.TryParse(currentChain.chainId, out chainId))
if (!BigInteger.TryParse(currentChain.chainId, out chainId))
throw new UnityException("The Chain ID must be a non-negative integer!");
}

Expand Down Expand Up @@ -198,24 +199,4 @@ private void Awake()

SDK = new ThirdwebSDK(chainOrRPC, chainId, options);
}

public ChainData GetChainData(string chainIdentifier)
{
return supportedChains.Find(x => x.identifier == chainIdentifier);
}

public ChainData GetCurrentChainData()
{
return supportedChains.Find(x => x.identifier == chain);
}

public int GetCurrentChainID()
{
return int.Parse(GetCurrentChainData().chainId);
}

public string GetCurrentChainIdentifier()
{
return chain;
}
}
7 changes: 4 additions & 3 deletions Assets/Thirdweb/Core/Scripts/ThirdwebSDK.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;

namespace Thirdweb
Expand Down Expand Up @@ -110,7 +111,7 @@ public struct BiconomyOptions
/// </summary>
/// <param name="chainOrRPC">The chain name or RPC url to connect to</param>
/// <param name="options">Configuration options</param>
public ThirdwebSDK(string chainOrRPC, int chainId = -1, Options options = new Options())
public ThirdwebSDK(string chainOrRPC, BigInteger? chainId = null, Options options = new Options())
{
this.chainOrRPC = chainOrRPC;
this.wallet = new Wallet();
Expand All @@ -123,10 +124,10 @@ public struct BiconomyOptions
}
else
{
if (chainId == -1)
if (chainId == null)
throw new UnityException("Chain ID override required for native platforms!");
string rpc = !chainOrRPC.StartsWith("https://") ? $"https://{chainOrRPC}.rpc.thirdweb.com/339d65590ba0fa79e4c8be0af33d64eda709e13652acb02c6be63f5a1fbef9c3" : chainOrRPC;
this.session = new ThirdwebSession(options, chainId, rpc);
this.session = new ThirdwebSession(options, chainId.Value, rpc);
}
}

Expand Down
6 changes: 3 additions & 3 deletions Assets/Thirdweb/Core/Scripts/ThirdwebSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ThirdwebSession


public ThirdwebSDK.Options Options { get; private set; }
public int ChainId { get; private set; }
public BigInteger ChainId { get; private set; }
public string RPC { get; private set; }
public SiweMessageService SiweSession { get; private set; }
public Web3 Web3 { get; private set; }
Expand All @@ -30,7 +30,7 @@ public class ThirdwebSession

#region Constructors

public ThirdwebSession(ThirdwebSDK.Options options, int chainId, string rpcUrl)
public ThirdwebSession(ThirdwebSDK.Options options, BigInteger chainId, string rpcUrl)
{
Options = options;
ChainId = chainId;
Expand Down Expand Up @@ -159,7 +159,7 @@ private ThirdwebChainData FetchChainData()
{
chainId = BigInteger.Parse(currentNetwork.chainId).ToHex(false, true) ?? BigInteger.Parse(ChainId.ToString()).ToHex(false, true),
blockExplorerUrls = explorerUrls.ToArray(),
chainName = currentNetwork.name ?? ThirdwebManager.Instance.GetCurrentChainIdentifier(),
chainName = currentNetwork.name ?? ThirdwebManager.Instance.chain,
iconUrls = new string[] { "ipfs://QmdwQDr6vmBtXmK2TmknkEuZNoaDqTasFdZdu3DRw8b2wt" },
nativeCurrency = new ThirdwebNativeCurrency()
{
Expand Down
41 changes: 21 additions & 20 deletions Assets/Thirdweb/Core/Scripts/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public static bool DeleteLocalAccount()
}
}

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

Expand Down Expand Up @@ -380,43 +380,44 @@ public static string ToChecksumAddress(this string address)
return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(address);
}

public static string GetNativeTokenWrapper(int chainId)
public static string GetNativeTokenWrapper(BigInteger chainId)
{
switch (chainId)
string id = chainId.ToString();
switch (id)
{
case 1:
case "1":
return "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
case 4:
case "4":
return "0xc778417E063141139Fce010982780140Aa0cD5Ab"; // rinkeby
case 5:
case "5":
return "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6"; // goerli
case 137:
case "137":
return "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270";
case 80001:
case "80001":
return "0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889";
case 43114:
case "43114":
return "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7";
case 43113:
case "43113":
return "0xd00ae08403B9bbb9124bB305C09058E32C39A48c";
case 250:
case "250":
return "0x21be370D5312f44cB42ce377BC9b8a0cEF1A4C83";
case 4002:
case "4002":
return "0xf1277d1Ed8AD466beddF92ef448A132661956621";
case 10:
case "10":
return "0x4200000000000000000000000000000000000006"; // optimism
case 69:
case "69":
return "0xbC6F6b680bc61e30dB47721c6D1c5cde19C1300d"; // optimism kovan
case 420:
case "420":
return "0x4200000000000000000000000000000000000006"; // optimism goerli
case 42161:
case "42161":
return "0x82af49447d8a07e3bd95bd0d56f35241523fbab1"; // arbitrum
case 421611:
case "421611":
return "0xEBbc3452Cc911591e4F18f3b36727Df45d6bd1f9"; // arbitrum rinkeby
case 421613:
case "421613":
return "0xe39Ab88f8A4777030A534146A9Ca3B52bd5D43A3"; // arbitrum goerli
case 56:
case "56":
return "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"; // binance mainnet
case 97:
case "97":
return "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd"; // binance testnet
default:
throw new UnityException("Native Token Wrapper Unavailable For This Chain!");
Expand Down
4 changes: 2 additions & 2 deletions Assets/Thirdweb/Core/Scripts/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public class WalletConnection
{
public WalletProvider provider;

public int chainId;
public BigInteger chainId;

public string password;

Expand All @@ -508,7 +508,7 @@ public class WalletConnection
/// <param name="password">The wallet password if using local wallets.</param>
/// <param name="email">The email to login with if using email based providers.</param>
/// <param name="personalWallet">The personal wallet provider if using smart wallets.</param>
public WalletConnection(WalletProvider provider, int chainId = 1, string password = null, string email = null, WalletProvider personalWallet = WalletProvider.LocalWallet)
public WalletConnection(WalletProvider provider, BigInteger chainId, string password = null, string email = null, WalletProvider personalWallet = WalletProvider.LocalWallet)
{
this.provider = provider;
this.chainId = chainId;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Thirdweb/Core/Scripts/Wallets/ThirdwebMagicLink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ThirdwebMagicLink(string magicLinkApiKey)

public async Task<string> Connect(WalletConnection walletConnection, string rpc)
{
_magic = new Magic(_magicLinkApiKey, new CustomNodeConfiguration(rpc, walletConnection.chainId));
_magic = new Magic(_magicLinkApiKey, new CustomNodeConfiguration(rpc, (int)walletConnection.chainId));

await _magic.Auth.LoginWithEmailOtp(walletConnection.email);
_web3 = new Web3(_magic.Provider);
Expand Down
15 changes: 8 additions & 7 deletions Assets/Thirdweb/Examples/Scripts/Prefabs/Prefab_ConnectWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using UnityEngine.UI;
using UnityEngine.Events;
using WalletConnectSharp.Core.Models;
using System.Numerics;

[Serializable]
public struct WalletButton
Expand Down Expand Up @@ -75,10 +76,7 @@ public class Prefab_ConnectWallet : MonoBehaviour

public string Address
{
get
{
return address;
}
get { return address; }
}

// UI Initialization
Expand Down Expand Up @@ -168,7 +166,9 @@ public async void OnConnect(WalletProvider wallet, string password = null, strin
{
try
{
address = await ThirdwebManager.Instance.SDK.wallet.Connect(new WalletConnection(wallet, ThirdwebManager.Instance.GetCurrentChainID(), password, email, personalWallet));
ChainData currentChain = ThirdwebManager.Instance.supportedChains.Find(x => x.identifier == ThirdwebManager.Instance.chain);

address = await ThirdwebManager.Instance.SDK.wallet.Connect(new WalletConnection(wallet, BigInteger.Parse(currentChain.chainId), password, email, personalWallet));

if (wallet == WalletProvider.LocalWallet || (wallet == WalletProvider.SmartWallet && personalWallet == WalletProvider.LocalWallet))
{
Expand Down Expand Up @@ -206,7 +206,7 @@ async void OnConnected()
balanceText2.text = balanceText.text;
walletAddressText.text = await Utils.GetENSName(address) ?? address.ShortenAddress();
walletAddressText2.text = walletAddressText.text;
currentNetworkText.text = ThirdwebManager.Instance.GetCurrentChainIdentifier();
currentNetworkText.text = ThirdwebManager.Instance.chain;
currentNetworkImage.sprite = networkSprites.Find(x => x.chain == _chain).sprite;
connectButton.SetActive(false);
connectedButton.SetActive(true);
Expand All @@ -230,7 +230,8 @@ public async void OnSwitchNetwork(string _chain)
try
{
ThirdwebManager.Instance.chain = _chain;
await ThirdwebManager.Instance.SDK.wallet.SwitchNetwork(int.Parse(ThirdwebManager.Instance.GetCurrentChainData().chainId));
ChainData currentChain = ThirdwebManager.Instance.supportedChains.Find(x => x.identifier == ThirdwebManager.Instance.chain);
await ThirdwebManager.Instance.SDK.wallet.SwitchNetwork(int.Parse(currentChain.chainId));
OnConnected();
OnSwitchNetworkCallback?.Invoke();
Debug.Log($"Switched Network Successfully: {_chain}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void Initialize()
public async Task<WCSessionData> EnableWalletConnect()
{
SetMode(true);
var sessionData = await Connect(ThirdwebManager.Instance.SDK.session.ChainId);
var sessionData = await Connect((int)ThirdwebManager.Instance.SDK.session.ChainId);
SetMode(false);
return sessionData;
}
Expand Down Expand Up @@ -336,7 +336,7 @@ private async void OnApplicationPause(bool pauseStatus)
if (pauseStatus)
SaveSession();
else if (PlayerPrefs.HasKey(SessionKey))
await Connect(ThirdwebManager.Instance.SDK.session.ChainId);
await Connect((int)ThirdwebManager.Instance.SDK.session.ChainId);
}

private void SaveSession()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override async Task Connect()

public override async Task<WCSessionData> ConnectSession()
{
return await unityObjectSource.Connect(ThirdwebManager.Instance.SDK.session.ChainId);
return await unityObjectSource.Connect((int)ThirdwebManager.Instance.SDK.session.ChainId);
}
}
}

0 comments on commit 80d77df

Please sign in to comment.