Skip to content

Commit 6edd6cf

Browse files
authored
Magic integration mobile/web + merged connect wallet button prefabs
1 parent 7e0b3d8 commit 6edd6cf

File tree

109 files changed

+13884
-7590
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+13884
-7590
lines changed

Assets/Thirdweb/Core/Scripts/Bridge.cs

Lines changed: 2 additions & 2 deletions
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().Substring(0,1).ToLower() + walletConnection.provider.ToString().Substring(1), walletConnection.chainId, walletConnection.password ?? Utils.GetDeviceIdentifier(), jsCallback);
94+
ThirdwebConnect(taskId, walletConnection.provider.ToString().Substring(0,1).ToLower() + walletConnection.provider.ToString().Substring(1), walletConnection.chainId, walletConnection.password ?? Utils.GetDeviceIdentifier(), walletConnection.email, 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, string password, Action<string, string, string> cb);
193+
private static extern string ThirdwebConnect(string taskId, string wallet, int chainId, string password, string email, 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/EIP712.cs

Lines changed: 47 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,16 @@ public async static Task<string> GenerateSignature_TokenERC20(
2727
string privateKeyOverride = null
2828
)
2929
{
30-
if (privateKeyOverride != null || ThirdwebManager.Instance.SDK.nativeSession.account != null)
31-
{
32-
var signer = new Eip712TypedDataSigner();
33-
var key = new EthECKey(privateKeyOverride ?? ThirdwebManager.Instance.SDK.nativeSession.account.PrivateKey);
34-
var typedData = GetTypedDefinition_TokenERC20(domainName, version, chainId, verifyingContract);
35-
var signature = signer.SignTypedDataV4(mintRequest, typedData, key);
36-
return signature;
37-
}
38-
else
30+
var typedData = GetTypedDefinition_TokenERC20(domainName, version, chainId, verifyingContract);
31+
32+
switch (ThirdwebManager.Instance.SDK.nativeSession.provider)
3933
{
40-
if (Utils.ActiveWalletConnectSession())
41-
{
34+
case WalletProvider.LocalWallet:
35+
var signer = new Eip712TypedDataSigner();
36+
var key = new EthECKey(privateKeyOverride ?? ThirdwebManager.Instance.SDK.nativeSession.account.PrivateKey);
37+
var signature = signer.SignTypedDataV4(mintRequest, typedData, key);
38+
return signature;
39+
case WalletProvider.WalletConnectV1:
4240
var walletConnectMintRequest = new ERC20MintRequestWalletConnect()
4341
{
4442
To = mintRequest.To,
@@ -50,21 +48,12 @@ public async static Task<string> GenerateSignature_TokenERC20(
5048
ValidityEndTimestamp = mintRequest.ValidityEndTimestamp,
5149
Uid = mintRequest.Uid.ByteArrayToHexString()
5250
};
53-
var typedData = GetTypedDefinition_TokenERC20(domainName, version, chainId, verifyingContract);
5451
return await WalletConnect.Instance.Session.EthSignTypedData(await ThirdwebManager.Instance.SDK.wallet.GetAddress(), walletConnectMintRequest, typedData);
55-
}
56-
else
57-
{
52+
case WalletProvider.MagicLink:
53+
throw new UnityException("Magic Auth does not support EIP712 signing");
54+
default:
5855
throw new UnityException("No account connected!");
59-
}
6056
}
61-
62-
// Debug.Log("Typed Data JSON: " + typedData.ToJson(mintRequest));
63-
// Debug.Log("Signing address: " + key.GetPublicAddress());
64-
// Debug.Log("Signature: " + signature);
65-
66-
// var addressRecovered = signer.RecoverFromSignatureV4(mintRequest, typedData, signature);
67-
// Debug.Log("Recovered address from signature:" + addressRecovered);
6857
}
6958

7059
public partial class ERC20MintRequestWalletConnect : ERC20MintRequestBaseWalletConnect { }
@@ -105,18 +94,16 @@ public async static Task<string> GenerateSignature_TokenERC721(
10594
string privateKeyOverride = null
10695
)
10796
{
108-
if (privateKeyOverride != null || ThirdwebManager.Instance.SDK.nativeSession.account != null)
109-
{
110-
var signer = new Eip712TypedDataSigner();
111-
var key = new EthECKey(privateKeyOverride ?? ThirdwebManager.Instance.SDK.nativeSession.account.PrivateKey);
112-
var typedData = GetTypedDefinition_TokenERC721(domainName, version, chainId, verifyingContract);
113-
var signature = signer.SignTypedDataV4(mintRequest, typedData, key);
114-
return signature;
115-
}
116-
else
97+
var typedData = GetTypedDefinition_TokenERC721(domainName, version, chainId, verifyingContract);
98+
99+
switch (ThirdwebManager.Instance.SDK.nativeSession.provider)
117100
{
118-
if (Utils.ActiveWalletConnectSession())
119-
{
101+
case WalletProvider.LocalWallet:
102+
var signer = new Eip712TypedDataSigner();
103+
var key = new EthECKey(privateKeyOverride ?? ThirdwebManager.Instance.SDK.nativeSession.account.PrivateKey);
104+
var signature = signer.SignTypedDataV4(mintRequest, typedData, key);
105+
return signature;
106+
case WalletProvider.WalletConnectV1:
120107
var walletConnectMintRequest = new ERC721MintRequestWalletConnect()
121108
{
122109
To = mintRequest.To,
@@ -130,13 +117,11 @@ public async static Task<string> GenerateSignature_TokenERC721(
130117
ValidityEndTimestamp = mintRequest.ValidityEndTimestamp,
131118
Uid = mintRequest.Uid.ByteArrayToHexString()
132119
};
133-
var typedData = GetTypedDefinition_TokenERC721(domainName, version, chainId, verifyingContract);
134120
return await WalletConnect.Instance.Session.EthSignTypedData(await ThirdwebManager.Instance.SDK.wallet.GetAddress(), walletConnectMintRequest, typedData);
135-
}
136-
else
137-
{
121+
case WalletProvider.MagicLink:
122+
throw new UnityException("Magic Auth does not support EIP712 signing");
123+
default:
138124
throw new UnityException("No account connected!");
139-
}
140125
}
141126
}
142127

@@ -184,18 +169,16 @@ public async static Task<string> GenerateSignature_TokenERC1155(
184169
string privateKeyOverride = null
185170
)
186171
{
187-
if (privateKeyOverride != null || ThirdwebManager.Instance.SDK.nativeSession.account != null)
188-
{
189-
var signer = new Eip712TypedDataSigner();
190-
var key = new EthECKey(privateKeyOverride ?? ThirdwebManager.Instance.SDK.nativeSession.account.PrivateKey);
191-
var typedData = GetTypedDefinition_TokenERC1155(domainName, version, chainId, verifyingContract);
192-
var signature = signer.SignTypedDataV4(mintRequest, typedData, key);
193-
return signature;
194-
}
195-
else
172+
var typedData = GetTypedDefinition_TokenERC1155(domainName, version, chainId, verifyingContract);
173+
174+
switch (ThirdwebManager.Instance.SDK.nativeSession.provider)
196175
{
197-
if (Utils.ActiveWalletConnectSession())
198-
{
176+
case WalletProvider.LocalWallet:
177+
var signer = new Eip712TypedDataSigner();
178+
var key = new EthECKey(privateKeyOverride ?? ThirdwebManager.Instance.SDK.nativeSession.account.PrivateKey);
179+
var signature = signer.SignTypedDataV4(mintRequest, typedData, key);
180+
return signature;
181+
case WalletProvider.WalletConnectV1:
199182
var walletConnectMintRequest = new ERC1155MintRequestWalletConnect()
200183
{
201184
To = mintRequest.To,
@@ -211,13 +194,11 @@ public async static Task<string> GenerateSignature_TokenERC1155(
211194
ValidityEndTimestamp = mintRequest.ValidityEndTimestamp,
212195
Uid = mintRequest.Uid.ByteArrayToHexString()
213196
};
214-
var typedData = GetTypedDefinition_TokenERC1155(domainName, version, chainId, verifyingContract);
215197
return await WalletConnect.Instance.Session.EthSignTypedData(await ThirdwebManager.Instance.SDK.wallet.GetAddress(), walletConnectMintRequest, typedData);
216-
}
217-
else
218-
{
198+
case WalletProvider.MagicLink:
199+
throw new UnityException("Magic Auth does not support EIP712 signing");
200+
default:
219201
throw new UnityException("No account connected!");
220-
}
221202
}
222203
}
223204

@@ -323,25 +304,20 @@ public async static Task<string> GenerateSignature_MinimalForwarder(
323304
string privateKeyOverride = null
324305
)
325306
{
326-
if (privateKeyOverride != null || ThirdwebManager.Instance.SDK.nativeSession.account != null)
327-
{
328-
var signer = new Eip712TypedDataSigner();
329-
var key = new EthECKey(privateKeyOverride ?? ThirdwebManager.Instance.SDK.nativeSession.account.PrivateKey);
330-
var typedData = GetTypedDefinition_MinimalForwarder(domainName, version, chainId, verifyingContract);
331-
var signature = signer.SignTypedDataV4(forwardRequest, typedData, key);
332-
return signature;
333-
}
334-
else
307+
var typedData = GetTypedDefinition_MinimalForwarder(domainName, version, chainId, verifyingContract);
308+
309+
switch (ThirdwebManager.Instance.SDK.nativeSession.provider)
335310
{
336-
if (Utils.ActiveWalletConnectSession())
337-
{
338-
var typedData = GetTypedDefinition_MinimalForwarder(domainName, version, chainId, verifyingContract);
311+
case WalletProvider.LocalWallet:
312+
var signer = new Eip712TypedDataSigner();
313+
var key = new EthECKey(privateKeyOverride ?? ThirdwebManager.Instance.SDK.nativeSession.account.PrivateKey);
314+
return signer.SignTypedDataV4(forwardRequest, typedData, key);
315+
case WalletProvider.WalletConnectV1:
339316
return await WalletConnect.Instance.Session.EthSignTypedData(await ThirdwebManager.Instance.SDK.wallet.GetAddress(), forwardRequest, typedData);
340-
}
341-
else
342-
{
317+
case WalletProvider.MagicLink:
318+
throw new UnityException("Magic Auth does not support EIP712 signing");
319+
default:
343320
throw new UnityException("No account connected!");
344-
}
345321
}
346322
}
347323

Assets/Thirdweb/Core/Scripts/ThirdwebSDK.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public struct WalletOptions
3535
public string appDescription;
3636
public string appUrl;
3737
public string[] appIcons;
38+
public string magicLinkApiKey; // the magic link api key to use for magic link auth
3839
public Dictionary<string, object> extras; // extra data to pass to the wallet provider
3940
}
4041

@@ -98,15 +99,17 @@ public struct BiconomyOptions
9899

99100
public class NativeSession
100101
{
102+
public WalletProvider provider = WalletProvider.LocalWallet;
101103
public int lastChainId = -1;
102104
public string lastRPC = null;
103105
public Account account = null;
104106
public Web3 web3 = null;
105107
public Options options = new Options();
106108
public SiweMessageService siweSession = new SiweMessageService();
107109

108-
public NativeSession(int lastChainId, string lastRPC, Account account, Web3 web3, Options options, SiweMessageService siweSession)
110+
public NativeSession(WalletProvider provider, int lastChainId, string lastRPC, Account account, Web3 web3, Options options, SiweMessageService siweSession)
109111
{
112+
this.provider = provider;
110113
this.lastChainId = lastChainId;
111114
this.lastRPC = lastRPC;
112115
this.account = account;
@@ -136,7 +139,7 @@ public NativeSession(int lastChainId, string lastRPC, Account account, Web3 web3
136139
throw new UnityException("Chain ID override required for native platforms!");
137140

138141
string rpc = !chainOrRPC.StartsWith("https://") ? $"https://{chainOrRPC}.rpc.thirdweb.com/339d65590ba0fa79e4c8be0af33d64eda709e13652acb02c6be63f5a1fbef9c3" : chainOrRPC;
139-
nativeSession = new NativeSession(chainId, rpc, null, new Web3(rpc), options, new SiweMessageService());
142+
nativeSession = new NativeSession(WalletProvider.LocalWallet, chainId, rpc, null, new Web3(rpc), options, new SiweMessageService());
140143
// Set default WalletOptions
141144
nativeSession.options.wallet = new WalletOptions()
142145
{

Assets/Thirdweb/Core/Scripts/Utils.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,6 @@ public static Account UnlockOrGenerateLocalAccount(int chainId, string password
319319
}
320320
}
321321

322-
public static bool ActiveWalletConnectSession()
323-
{
324-
return WalletConnect.Instance != null && WalletConnect.Instance.Session != null && WalletConnect.Instance.Session.Connected;
325-
}
326-
327322
public static string cidToIpfsUrl(this string cid, bool useGateway = false)
328323
{
329324
string ipfsRaw = $"ipfs://{cid}";

0 commit comments

Comments
 (0)