Skip to content

Commit

Permalink
Stable SDK options + WalletConnection options + WalletConnect (#48)
Browse files Browse the repository at this point in the history
Co-authored-by: Joaquim Verges <[email protected]>
  • Loading branch information
0xFirekeeper and joaquim-verges authored Mar 21, 2023
1 parent bd83494 commit 902efbd
Show file tree
Hide file tree
Showing 14 changed files with 785 additions and 430 deletions.
4 changes: 2 additions & 2 deletions Assets/Plugin/thirdweb.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ var plugin = {
ThirdwebInitialize: function (chain, options) {
window.bridge.initialize(UTF8ToString(chain), UTF8ToString(options));
},
ThirdwebConnect: function (taskId, wallet, chainId, cb) {
ThirdwebConnect: function (taskId, wallet, chainId, password, cb) {
// convert taskId from pointer to str and allocate it to keep in memory
var id = UTF8ToString(taskId);
var idSize = lengthBytesUTF8(id) + 1;
var idPtr = _malloc(idSize);
stringToUTF8(id, idPtr, idSize);
// execute bridge call
window.bridge
.connect(UTF8ToString(wallet), chainId)
.connect(UTF8ToString(wallet), chainId, UTF8ToString(password))
.then((address) => {
if (address) {
var bufferSize = lengthBytesUTF8(address) + 1;
Expand Down
4 changes: 2 additions & 2 deletions 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, jsCallback);
ThirdwebConnect(taskId, walletConnection.provider.ToString(), walletConnection.chainId, walletConnection.password ?? Utils.GetDeviceIdentifier(), jsCallback);
#endif
string result = await task.Task;
return result;
Expand Down Expand Up @@ -190,7 +190,7 @@ public static async Task FundWallet(FundWalletOptions payload)
[DllImport("__Internal")]
private static extern string ThirdwebInitialize(string chainOrRPC, string options);
[DllImport("__Internal")]
private static extern string ThirdwebConnect(string taskId, string wallet, int chainId, Action<string, string, string> cb);
private static extern string ThirdwebConnect(string taskId, string wallet, int chainId, string password, Action<string, string, string> cb);
[DllImport("__Internal")]
private static extern string ThirdwebDisconnect(string taskId, Action<string, string, string> cb);
[DllImport("__Internal")]
Expand Down
33 changes: 24 additions & 9 deletions Assets/Thirdweb/Core/Scripts/ThirdwebSDK.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public struct Options
public struct WalletOptions
{
public string appName; // the app name that will show in different wallet providers
public string appDescription;
public string appUrl;
public string[] appIcons;
public Dictionary<string, object> extras; // extra data to pass to the wallet provider
}

Expand Down Expand Up @@ -92,8 +95,18 @@ public class NativeSession
public string lastRPC = null;
public Account account = null;
public Web3 web3 = null;
public Options options;
public SiweMessageService siweSession;
public Options options = new Options();
public SiweMessageService siweSession = new SiweMessageService();

public NativeSession(int lastChainId, string lastRPC, Account account, Web3 web3, Options options, SiweMessageService siweSession)
{
this.lastChainId = lastChainId;
this.lastRPC = lastRPC;
this.account = account;
this.web3 = web3;
this.options = options;
this.siweSession = siweSession;
}
}

public NativeSession nativeSession;
Expand All @@ -116,13 +129,15 @@ public class NativeSession
throw new UnityException("Chain ID override required for native platforms!");

string rpc = !chainOrRPC.StartsWith("https://") ? $"https://{chainOrRPC}.rpc.thirdweb.com/339d65590ba0fa79e4c8be0af33d64eda709e13652acb02c6be63f5a1fbef9c3" : chainOrRPC;

nativeSession = new NativeSession();
nativeSession.lastRPC = rpc;
nativeSession.lastChainId = chainId;
nativeSession.web3 = new Web3(nativeSession.lastRPC);
nativeSession.options = options;
nativeSession.siweSession = new Nethereum.Siwe.SiweMessageService();
nativeSession = new NativeSession(chainId, rpc, null, new Web3(rpc), options, new SiweMessageService());
// Set default WalletOptions
nativeSession.options.wallet = new WalletOptions()
{
appName = options.wallet?.appName ?? "Thirdweb Game",
appDescription = options.wallet?.appDescription ?? "Thirdweb Game Demo",
appIcons = options.wallet?.appIcons ?? new string[] { "https://thirdweb.com/favicon.ico" },
appUrl = options.wallet?.appUrl ?? "https://thirdweb.com"
};
}
else
{
Expand Down
108 changes: 78 additions & 30 deletions Assets/Thirdweb/Core/Scripts/Wallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
using Nethereum.Web3;
using UnityEngine;
using System;
using WalletConnectSharp.Core.Models;
using WalletConnectSharp.Unity;
using WalletConnectSharp.NEthereum;
using Nethereum.Siwe.Core;
using Nethereum.Siwe;
using System.Collections.Generic;
using Nethereum.Web3.Accounts;

//using WalletConnectSharp.NEthereum;

Expand All @@ -20,43 +19,82 @@ 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
/// </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, string password = null, WCSessionData wcSessionData = null)
public async Task<string> Connect(WalletConnection? walletConnection = null)
{
if (Utils.IsWebGLBuild())
{
var connection = walletConnection ?? new WalletConnection() { provider = WalletProvider.Injected, };
;
return await Bridge.Connect(connection);
}
else
{
ThirdwebSDK.NativeSession newNativeSession = new ThirdwebSDK.NativeSession();
if (wcSessionData != null)
ThirdwebSDK.NativeSession oldSession = ThirdwebManager.Instance.SDK.nativeSession;

if (walletConnection == null)
{
newNativeSession.lastRPC = ThirdwebManager.Instance.SDK.nativeSession.lastRPC;
newNativeSession.lastChainId = ThirdwebManager.Instance.SDK.nativeSession.lastChainId;
newNativeSession.account = null;
newNativeSession.web3 = WalletConnect.Instance.Session.BuildWeb3(new Uri(newNativeSession.lastRPC)).AsWalletAccount(true);
newNativeSession.siweSession = new SiweMessageService();
ThirdwebManager.Instance.SDK.nativeSession = newNativeSession;
return WalletConnect.Instance.Session.Accounts[0];
Account noPassAcc = Utils.UnlockOrGenerateAccount(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
{
newNativeSession.lastRPC = ThirdwebManager.Instance.SDK.nativeSession.lastRPC;
newNativeSession.lastChainId = ThirdwebManager.Instance.SDK.nativeSession.lastChainId;
newNativeSession.account = Utils.UnlockOrGenerateAccount(newNativeSession.lastChainId, password, null); // TODO: Allow custom private keys/passwords
newNativeSession.web3 = new Web3(newNativeSession.account, newNativeSession.lastRPC);
newNativeSession.siweSession = new SiweMessageService();
ThirdwebManager.Instance.SDK.nativeSession = newNativeSession;
return ThirdwebManager.Instance.SDK.nativeSession.account.Address;
if (walletConnection?.provider?.ToString() == "walletConnect")
{
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
);
return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(WalletConnect.Instance.Session.Accounts[0]);
}
else if (walletConnection?.password != null)
{
Account acc = Utils.UnlockOrGenerateAccount(oldSession.lastChainId, walletConnection?.password, null);
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
oldSession.lastChainId,
oldSession.lastRPC,
acc,
new Web3(acc, oldSession.lastRPC),
oldSession.options,
oldSession.siweSession
);
return acc.Address;
}
else if (walletConnection?.privateKey != null)
{
Account acc = Utils.UnlockOrGenerateAccount(oldSession.lastChainId, null, walletConnection?.privateKey);
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
oldSession.lastChainId,
oldSession.lastRPC,
acc,
new Web3(acc, oldSession.lastRPC),
oldSession.options,
oldSession.siweSession
);
return acc.Address;
}
else
{
throw new UnityException("This wallet connection method is not supported on this platform!");
}
}
}
}
Expand All @@ -72,17 +110,21 @@ public async Task Disconnect()
}
else
{
ThirdwebSDK.NativeSession oldSession = ThirdwebManager.Instance.SDK.nativeSession;

if (Utils.ActiveWalletConnectSession())
{
WalletConnect.Instance.DisableWalletConnect();
}
ThirdwebSDK.NativeSession newNativeSession = new ThirdwebSDK.NativeSession();
newNativeSession.lastRPC = ThirdwebManager.Instance.SDK.nativeSession.lastRPC;
newNativeSession.lastChainId = ThirdwebManager.Instance.SDK.nativeSession.lastChainId;
newNativeSession.account = null;
newNativeSession.web3 = new Web3(newNativeSession.lastRPC); // fallback
newNativeSession.siweSession = new SiweMessageService();
ThirdwebManager.Instance.SDK.nativeSession = newNativeSession;

ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
oldSession.lastChainId,
oldSession.lastRPC,
null,
new Web3(oldSession.lastRPC),
oldSession.options,
oldSession.siweSession
);
}
}

Expand Down Expand Up @@ -146,7 +188,7 @@ public async Task<string> Verify(LoginPayload payload)
{
if (Utils.IsWebGLBuild())
{
throw new UnityException("This functionality is not available on your current platform.");
return await Bridge.InvokeRoute<string>($"auth{subSeparator}verify", Utils.ToJsonStringArray(payload));
}
else
{
Expand Down Expand Up @@ -414,6 +456,8 @@ public struct WalletConnection
{
public WalletProvider provider;
public int chainId;
public string password;
public string privateKey;
}

public class WalletProvider
Expand Down Expand Up @@ -445,6 +489,10 @@ public static WalletProvider MagicAuth
{
get { return new WalletProvider("magicAuth"); }
}
public static WalletProvider DeviceWallet
{
get { return new WalletProvider("deviceWallet"); }
}

public override string ToString()
{
Expand Down
Loading

0 comments on commit 902efbd

Please sign in to comment.