Skip to content

Commit 902efbd

Browse files
Stable SDK options + WalletConnection options + WalletConnect (#48)
Co-authored-by: Joaquim Verges <[email protected]>
1 parent bd83494 commit 902efbd

File tree

14 files changed

+785
-430
lines changed

14 files changed

+785
-430
lines changed

Assets/Plugin/thirdweb.jslib

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ var plugin = {
5151
ThirdwebInitialize: function (chain, options) {
5252
window.bridge.initialize(UTF8ToString(chain), UTF8ToString(options));
5353
},
54-
ThirdwebConnect: function (taskId, wallet, chainId, cb) {
54+
ThirdwebConnect: function (taskId, wallet, chainId, password, cb) {
5555
// convert taskId from pointer to str and allocate it to keep in memory
5656
var id = UTF8ToString(taskId);
5757
var idSize = lengthBytesUTF8(id) + 1;
5858
var idPtr = _malloc(idSize);
5959
stringToUTF8(id, idPtr, idSize);
6060
// execute bridge call
6161
window.bridge
62-
.connect(UTF8ToString(wallet), chainId)
62+
.connect(UTF8ToString(wallet), chainId, UTF8ToString(password))
6363
.then((address) => {
6464
if (address) {
6565
var bufferSize = lengthBytesUTF8(address) + 1;

Assets/Thirdweb/Core/Scripts/Bridge.cs

+2-2
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, jsCallback);
94+
ThirdwebConnect(taskId, walletConnection.provider.ToString(), walletConnection.chainId, walletConnection.password ?? Utils.GetDeviceIdentifier(), jsCallback);
9595
#endif
9696
string result = await task.Task;
9797
return result;
@@ -190,7 +190,7 @@ public static async Task FundWallet(FundWalletOptions payload)
190190
[DllImport("__Internal")]
191191
private static extern string ThirdwebInitialize(string chainOrRPC, string options);
192192
[DllImport("__Internal")]
193-
private static extern string ThirdwebConnect(string taskId, string wallet, int chainId, Action<string, string, string> cb);
193+
private static extern string ThirdwebConnect(string taskId, string wallet, int chainId, string password, Action<string, string, string> cb);
194194
[DllImport("__Internal")]
195195
private static extern string ThirdwebDisconnect(string taskId, Action<string, string, string> cb);
196196
[DllImport("__Internal")]

Assets/Thirdweb/Core/Scripts/ThirdwebSDK.cs

+24-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ public struct Options
2929
public struct WalletOptions
3030
{
3131
public string appName; // the app name that will show in different wallet providers
32+
public string appDescription;
33+
public string appUrl;
34+
public string[] appIcons;
3235
public Dictionary<string, object> extras; // extra data to pass to the wallet provider
3336
}
3437

@@ -92,8 +95,18 @@ public class NativeSession
9295
public string lastRPC = null;
9396
public Account account = null;
9497
public Web3 web3 = null;
95-
public Options options;
96-
public SiweMessageService siweSession;
98+
public Options options = new Options();
99+
public SiweMessageService siweSession = new SiweMessageService();
100+
101+
public NativeSession(int lastChainId, string lastRPC, Account account, Web3 web3, Options options, SiweMessageService siweSession)
102+
{
103+
this.lastChainId = lastChainId;
104+
this.lastRPC = lastRPC;
105+
this.account = account;
106+
this.web3 = web3;
107+
this.options = options;
108+
this.siweSession = siweSession;
109+
}
97110
}
98111

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

118131
string rpc = !chainOrRPC.StartsWith("https://") ? $"https://{chainOrRPC}.rpc.thirdweb.com/339d65590ba0fa79e4c8be0af33d64eda709e13652acb02c6be63f5a1fbef9c3" : chainOrRPC;
119-
120-
nativeSession = new NativeSession();
121-
nativeSession.lastRPC = rpc;
122-
nativeSession.lastChainId = chainId;
123-
nativeSession.web3 = new Web3(nativeSession.lastRPC);
124-
nativeSession.options = options;
125-
nativeSession.siweSession = new Nethereum.Siwe.SiweMessageService();
132+
nativeSession = new NativeSession(chainId, rpc, null, new Web3(rpc), options, new SiweMessageService());
133+
// Set default WalletOptions
134+
nativeSession.options.wallet = new WalletOptions()
135+
{
136+
appName = options.wallet?.appName ?? "Thirdweb Game",
137+
appDescription = options.wallet?.appDescription ?? "Thirdweb Game Demo",
138+
appIcons = options.wallet?.appIcons ?? new string[] { "https://thirdweb.com/favicon.ico" },
139+
appUrl = options.wallet?.appUrl ?? "https://thirdweb.com"
140+
};
126141
}
127142
else
128143
{

Assets/Thirdweb/Core/Scripts/Wallet.cs

+78-30
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
using Nethereum.Web3;
55
using UnityEngine;
66
using System;
7-
using WalletConnectSharp.Core.Models;
87
using WalletConnectSharp.Unity;
98
using WalletConnectSharp.NEthereum;
109
using Nethereum.Siwe.Core;
11-
using Nethereum.Siwe;
1210
using System.Collections.Generic;
11+
using Nethereum.Web3.Accounts;
1312

1413
//using WalletConnectSharp.NEthereum;
1514

@@ -20,43 +19,82 @@ namespace Thirdweb
2019
/// </summary>
2120
public class Wallet : Routable
2221
{
23-
public Wallet()
24-
: base($"sdk{subSeparator}wallet") { }
22+
public Wallet() : base($"sdk{subSeparator}wallet") { }
2523

2624
/// <summary>
2725
/// Connect a user's wallet via a given wallet provider
2826
/// </summary>
2927
/// <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, string password = null, WCSessionData wcSessionData = null)
28+
public async Task<string> Connect(WalletConnection? walletConnection = null)
3129
{
3230
if (Utils.IsWebGLBuild())
3331
{
3432
var connection = walletConnection ?? new WalletConnection() { provider = WalletProvider.Injected, };
35-
;
3633
return await Bridge.Connect(connection);
3734
}
3835
else
3936
{
40-
ThirdwebSDK.NativeSession newNativeSession = new ThirdwebSDK.NativeSession();
41-
if (wcSessionData != null)
37+
ThirdwebSDK.NativeSession oldSession = ThirdwebManager.Instance.SDK.nativeSession;
38+
39+
if (walletConnection == null)
4240
{
43-
newNativeSession.lastRPC = ThirdwebManager.Instance.SDK.nativeSession.lastRPC;
44-
newNativeSession.lastChainId = ThirdwebManager.Instance.SDK.nativeSession.lastChainId;
45-
newNativeSession.account = null;
46-
newNativeSession.web3 = WalletConnect.Instance.Session.BuildWeb3(new Uri(newNativeSession.lastRPC)).AsWalletAccount(true);
47-
newNativeSession.siweSession = new SiweMessageService();
48-
ThirdwebManager.Instance.SDK.nativeSession = newNativeSession;
49-
return WalletConnect.Instance.Session.Accounts[0];
41+
Account noPassAcc = Utils.UnlockOrGenerateAccount(oldSession.lastChainId, null, null);
42+
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
43+
oldSession.lastChainId,
44+
oldSession.lastRPC,
45+
noPassAcc,
46+
new Web3(noPassAcc, oldSession.lastRPC),
47+
oldSession.options,
48+
oldSession.siweSession
49+
);
50+
return noPassAcc.Address;
5051
}
5152
else
5253
{
53-
newNativeSession.lastRPC = ThirdwebManager.Instance.SDK.nativeSession.lastRPC;
54-
newNativeSession.lastChainId = ThirdwebManager.Instance.SDK.nativeSession.lastChainId;
55-
newNativeSession.account = Utils.UnlockOrGenerateAccount(newNativeSession.lastChainId, password, null); // TODO: Allow custom private keys/passwords
56-
newNativeSession.web3 = new Web3(newNativeSession.account, newNativeSession.lastRPC);
57-
newNativeSession.siweSession = new SiweMessageService();
58-
ThirdwebManager.Instance.SDK.nativeSession = newNativeSession;
59-
return ThirdwebManager.Instance.SDK.nativeSession.account.Address;
54+
if (walletConnection?.provider?.ToString() == "walletConnect")
55+
{
56+
await WalletConnect.Instance.EnableWalletConnect();
57+
58+
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
59+
oldSession.lastChainId,
60+
oldSession.lastRPC,
61+
null,
62+
WalletConnect.Instance.Session.BuildWeb3(new Uri(oldSession.lastRPC)).AsWalletAccount(true),
63+
oldSession.options,
64+
oldSession.siweSession
65+
);
66+
return Nethereum.Util.AddressUtil.Current.ConvertToChecksumAddress(WalletConnect.Instance.Session.Accounts[0]);
67+
}
68+
else if (walletConnection?.password != null)
69+
{
70+
Account acc = Utils.UnlockOrGenerateAccount(oldSession.lastChainId, walletConnection?.password, null);
71+
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
72+
oldSession.lastChainId,
73+
oldSession.lastRPC,
74+
acc,
75+
new Web3(acc, oldSession.lastRPC),
76+
oldSession.options,
77+
oldSession.siweSession
78+
);
79+
return acc.Address;
80+
}
81+
else if (walletConnection?.privateKey != null)
82+
{
83+
Account acc = Utils.UnlockOrGenerateAccount(oldSession.lastChainId, null, walletConnection?.privateKey);
84+
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
85+
oldSession.lastChainId,
86+
oldSession.lastRPC,
87+
acc,
88+
new Web3(acc, oldSession.lastRPC),
89+
oldSession.options,
90+
oldSession.siweSession
91+
);
92+
return acc.Address;
93+
}
94+
else
95+
{
96+
throw new UnityException("This wallet connection method is not supported on this platform!");
97+
}
6098
}
6199
}
62100
}
@@ -72,17 +110,21 @@ public async Task Disconnect()
72110
}
73111
else
74112
{
113+
ThirdwebSDK.NativeSession oldSession = ThirdwebManager.Instance.SDK.nativeSession;
114+
75115
if (Utils.ActiveWalletConnectSession())
76116
{
77117
WalletConnect.Instance.DisableWalletConnect();
78118
}
79-
ThirdwebSDK.NativeSession newNativeSession = new ThirdwebSDK.NativeSession();
80-
newNativeSession.lastRPC = ThirdwebManager.Instance.SDK.nativeSession.lastRPC;
81-
newNativeSession.lastChainId = ThirdwebManager.Instance.SDK.nativeSession.lastChainId;
82-
newNativeSession.account = null;
83-
newNativeSession.web3 = new Web3(newNativeSession.lastRPC); // fallback
84-
newNativeSession.siweSession = new SiweMessageService();
85-
ThirdwebManager.Instance.SDK.nativeSession = newNativeSession;
119+
120+
ThirdwebManager.Instance.SDK.nativeSession = new ThirdwebSDK.NativeSession(
121+
oldSession.lastChainId,
122+
oldSession.lastRPC,
123+
null,
124+
new Web3(oldSession.lastRPC),
125+
oldSession.options,
126+
oldSession.siweSession
127+
);
86128
}
87129
}
88130

@@ -146,7 +188,7 @@ public async Task<string> Verify(LoginPayload payload)
146188
{
147189
if (Utils.IsWebGLBuild())
148190
{
149-
throw new UnityException("This functionality is not available on your current platform.");
191+
return await Bridge.InvokeRoute<string>($"auth{subSeparator}verify", Utils.ToJsonStringArray(payload));
150192
}
151193
else
152194
{
@@ -414,6 +456,8 @@ public struct WalletConnection
414456
{
415457
public WalletProvider provider;
416458
public int chainId;
459+
public string password;
460+
public string privateKey;
417461
}
418462

419463
public class WalletProvider
@@ -445,6 +489,10 @@ public static WalletProvider MagicAuth
445489
{
446490
get { return new WalletProvider("magicAuth"); }
447491
}
492+
public static WalletProvider DeviceWallet
493+
{
494+
get { return new WalletProvider("deviceWallet"); }
495+
}
448496

449497
public override string ToString()
450498
{

0 commit comments

Comments
 (0)