Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Assets/Resources.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Assets/Resources/all_chains.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Assets/Resources/all_chains.json.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions Assets/Thirdweb/Core/Scripts/ThirdwebSDK.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Collections.Generic;
using System.Numerics;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Siwe;
using Nethereum.Web3;
using Nethereum.Web3.Accounts;
using UnityEngine;
using WalletConnectSharp.Core.Models.Ethereum;

namespace Thirdweb
{
Expand Down Expand Up @@ -89,6 +92,8 @@ public struct BiconomyOptions

public Storage storage;

public EthChainData currentChainData;

public class NativeSession
{
public int lastChainId = -1;
Expand Down Expand Up @@ -138,6 +143,8 @@ public NativeSession(int lastChainId, string lastRPC, Account account, Web3 web3
appIcons = options.wallet?.appIcons ?? new string[] { "https://thirdweb.com/favicon.ico" },
appUrl = options.wallet?.appUrl ?? "https://thirdweb.com"
};

FetchChainData();
}
else
{
Expand All @@ -155,5 +162,71 @@ public Contract GetContract(string address, string abi = null)
{
return new Contract(this.chainOrRPC, address, abi);
}

void FetchChainData()
{
var allChainsJson = (TextAsset)Resources.Load("all_chains", typeof(TextAsset));
// Get networks from chainidnetwork
List<ChainIDNetworkData> allNetworkData = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ChainIDNetworkData>>(allChainsJson.text);
// Get current network
ChainIDNetworkData currentNetwork = allNetworkData.Find(x => x.chainId == nativeSession.lastChainId.ToString());
// Get block explorer urls
List<string> explorerUrls = new List<string>();
foreach (var explorer in currentNetwork.explorers)
explorerUrls.Add(explorer.url);
if (explorerUrls.Count == 0)
explorerUrls.Add("https://etherscan.io");
// Add chain
currentChainData = new EthChainData()
{
chainId = BigInteger.Parse(currentNetwork.chainId).ToHex(false, true) ?? BigInteger.Parse(nativeSession.lastChainId.ToString()).ToHex(false, true),
blockExplorerUrls = explorerUrls.ToArray(),
chainName = currentNetwork.name ?? ThirdwebManager.Instance.supportedChainData[ThirdwebManager.Instance.chain].identifier,
iconUrls = new string[] { "ipfs://QmdwQDr6vmBtXmK2TmknkEuZNoaDqTasFdZdu3DRw8b2wt" },
nativeCurrency = new NativeCurrency()
{
name = currentNetwork.nativeCurrency?.name ?? "Ether",
symbol = currentNetwork.nativeCurrency?.symbol ?? "ETH",
decimals = int.Parse(currentNetwork.nativeCurrency?.decimals ?? "18")
},
rpcUrls = new string[] { nativeSession.lastRPC }
};
}

[System.Serializable]
public class ChainIDNetworkData
{
public string name;
public string chain;
public string icon;
public List<string> rpc;
public ChainIDNetworkNativeCurrency nativeCurrency;
public string chainId;
public List<ChainIDNetworkExplorer> explorers;
}

[System.Serializable]
public class ChainIDNetworkNativeCurrency
{
public string name;
public string symbol;
public string decimals;
}

[System.Serializable]
public class ChainIDNetworkExplorer
{
public string name;
public string url;
public string standard;
}

public struct ChaiNIDNetworkIcon
{
public string url;
public int width;
public int height;
public string format;
}
}
}
23 changes: 21 additions & 2 deletions Assets/Thirdweb/Core/Scripts/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
using Nethereum.Siwe.Core;
using System.Collections.Generic;
using Nethereum.Web3.Accounts;
using UnityEngine.Networking;
using Nethereum.Hex.HexConvertors.Extensions;
using WalletConnectSharp.Core.Models.Ethereum;

//using WalletConnectSharp.NEthereum;

Expand All @@ -19,7 +22,8 @@ namespace Thirdweb
/// </summary>
public class Wallet : Routable
{
public Wallet() : base($"sdk{subSeparator}wallet") { }
public Wallet()
: base($"sdk{subSeparator}wallet") { }

/// <summary>
/// Connect a user's wallet via a given wallet provider
Expand Down Expand Up @@ -63,6 +67,20 @@ public async Task<string> Connect(WalletConnection? walletConnection = null)
oldSession.options,
oldSession.siweSession
);

// Add chain
try
{
await WalletConnect.Instance.WalletAddEthChain(ThirdwebManager.Instance.SDK.currentChainData);
}
catch (System.Exception e)
{
Debug.Log("Adding chain error: " + e.Message);
}

// Switch to chain
await WalletConnect.Instance.WalletSwitchEthChain(new EthChain() { chainId = ThirdwebManager.Instance.SDK.currentChainData.chainId });

return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(WalletConnect.Instance.Session.Accounts[0]);
}
else if (walletConnection?.password != null)
Expand Down Expand Up @@ -262,7 +280,8 @@ public async Task<CurrencyValue> GetBalance(string currencyAddress = Utils.Nativ
else
{
var balance = await ThirdwebManager.Instance.SDK.nativeSession.web3.Eth.GetBalance.SendRequestAsync(await ThirdwebManager.Instance.SDK.wallet.GetAddress());
return new CurrencyValue("Ether", "ETH", "18", balance.Value.ToString(), balance.Value.ToString().ToEth()); // TODO: Get actual name/symbol
var nativeCurrency = ThirdwebManager.Instance.SDK.currentChainData.nativeCurrency;
return new CurrencyValue(nativeCurrency.name, nativeCurrency.symbol, nativeCurrency.decimals.ToString(), balance.Value.ToString(), balance.Value.ToString().ToEth());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ protected async void TransportOnMessageReceived(object sender, MessageReceivedEv

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

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

if (payload != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,11 @@ public virtual async Task<string> WalletAddEthChain(EthChainData chainData)
return response.Result;
}

public virtual async Task<string> WalletSwitchEthChain(EthChainData chainData)
public virtual async Task<string> WalletSwitchEthChain(EthChain ethChain)
{
EnsureNotDisconnected();

var request = new WalletSwitchEthChain(chainData);
var request = new WalletSwitchEthChain(ethChain);

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,9 @@ public async Task<string> WalletAddEthChain(EthChainData chainData)
return results;
}

public async Task<string> WalletSwitchEthChain(EthChainData chainData)
public async Task<string> WalletSwitchEthChain(EthChain ethChain)
{
var results = await Session.WalletSwitchEthChain(chainData);
var results = await Session.WalletSwitchEthChain(ethChain);
return results;
}

Expand Down
2 changes: 1 addition & 1 deletion ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ PlayerSettings:
16:10: 1
16:9: 1
Others: 1
bundleVersion: 2.0.1-alpha
bundleVersion: 2.0.2
preloadedAssets: []
metroInputSource: 0
wsaTransparentSwapchain: 0
Expand Down