Skip to content

Commit 57efccd

Browse files
committed
[General] Add GetEmail to Wallet interface
1 parent cfdcfa7 commit 57efccd

File tree

11 files changed

+20944
-20818
lines changed

11 files changed

+20944
-20818
lines changed

Assets/Thirdweb/Core/Plugin/thirdweb.jslib

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,29 @@ var plugin = {
344344
dynCall_viii(cb, idPtr, null, buffer);
345345
});
346346
},
347+
ThirdwebGetEmail: async function (taskId, cb) {
348+
// convert taskId from pointer to str and allocate it to keep in memory
349+
var id = UTF8ToString(taskId);
350+
var idSize = lengthBytesUTF8(id) + 1;
351+
var idPtr = _malloc(idSize);
352+
stringToUTF8(id, idPtr, idSize);
353+
// execute bridge call
354+
window.bridge
355+
.getEmail()
356+
.then((returnStr) => {
357+
var bufferSize = lengthBytesUTF8(returnStr) + 1;
358+
var buffer = _malloc(bufferSize);
359+
stringToUTF8(returnStr, buffer, bufferSize);
360+
dynCall_viii(cb, idPtr, buffer, null);
361+
})
362+
.catch((err) => {
363+
var msg = err.message;
364+
var bufferSize = lengthBytesUTF8(msg) + 1;
365+
var buffer = _malloc(bufferSize);
366+
stringToUTF8(msg, buffer, bufferSize);
367+
dynCall_viii(cb, idPtr, null, buffer);
368+
});
369+
},
347370
};
348371

349372
mergeInto(LibraryManager.library, plugin);

Assets/Thirdweb/Core/Scripts/Bridge.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,23 @@ public static async Task<BlockWithTransactions> GetBlockWithTransactions(BigInte
330330
return JsonConvert.DeserializeObject<Result<BlockWithTransactions>>(result).result;
331331
}
332332

333+
public static async Task<string> GetEmail()
334+
{
335+
if (!Utils.IsWebGLBuild())
336+
{
337+
ThirdwebDebug.LogWarning("Interacting with the thirdweb SDK is not fully supported in the editor.");
338+
return "";
339+
}
340+
string taskId = Guid.NewGuid().ToString();
341+
var task = new TaskCompletionSource<string>();
342+
taskMap[taskId] = task;
343+
#if UNITY_WEBGL
344+
ThirdwebGetEmail(taskId, jsCallback);
345+
#endif
346+
string result = await task.Task;
347+
return JsonConvert.DeserializeObject<Result<string>>(result).result;
348+
}
349+
333350
#if UNITY_WEBGL
334351
[DllImport("__Internal")]
335352
private static extern string ThirdwebInvoke(string taskId, string route, string payload, Action<string, string, string> cb);
@@ -361,7 +378,8 @@ public static async Task<BlockWithTransactions> GetBlockWithTransactions(BigInte
361378
private static extern string ThirdwebGetBlock(string taskId, string blockNumber, Action<string, string, string> cb);
362379
[DllImport("__Internal")]
363380
private static extern string ThirdwebGetBlockWithTransactions(string taskId, string blockNumber, Action<string, string, string> cb);
364-
381+
[DllImport("__Internal")]
382+
private static extern string ThirdwebGetEmail(string taskId, Action<string, string, string> cb);
365383
#endif
366384
}
367385
}

Assets/Thirdweb/Core/Scripts/Wallet.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,24 @@ public async Task<string> GetAddress()
248248
}
249249
}
250250

251+
/// <summary>
252+
/// Gets the connected embedded wallet email if any.
253+
/// </summary>
254+
public async Task<string> GetEmail()
255+
{
256+
if (Utils.IsWebGLBuild())
257+
{
258+
return await Bridge.GetEmail();
259+
}
260+
else
261+
{
262+
if (!await IsConnected())
263+
throw new Exception("No account connected!");
264+
265+
return await ThirdwebManager.Instance.SDK.session.ActiveWallet.GetEmail();
266+
}
267+
}
268+
251269
/// <summary>
252270
/// Gets the address of the signer associated with the connected wallet.
253271
/// </summary>

Assets/Thirdweb/Core/Scripts/Wallets/IThirdwebWallet.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public interface IThirdwebWallet
3535
/// <returns>The address of the main account.</returns>
3636
Task<string> GetAddress();
3737

38+
/// <summary>
39+
/// Return the email of the main account (if any, otherwise return an empty string).
40+
/// </summary>
41+
/// <returns>The email of the main account or an empty string.</returns>
42+
Task<string> GetEmail();
43+
3844
/// <summary>
3945
/// Return the address of the signer account (if any, otherwise return GetAddress).
4046
/// </summary>

Assets/Thirdweb/Core/Scripts/Wallets/ThirdwebEmbeddedWallet.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class ThirdwebEmbeddedWallet : IThirdwebWallet
1414
private readonly WalletProvider _signerProvider;
1515
private readonly EmbeddedWallet _embeddedWallet;
1616
private Account _account;
17+
private string _email;
1718

1819
public ThirdwebEmbeddedWallet(string clientId, string bundleId)
1920
{
@@ -32,7 +33,9 @@ public async Task<string> Connect(WalletConnection walletConnection, string rpc)
3233
GameObject.Instantiate(ThirdwebManager.Instance.EmbeddedWalletPrefab);
3334
}
3435

35-
_account = (await EmbeddedWalletUI.Instance.Connect(_embeddedWallet, walletConnection.email, walletConnection.authOptions)).Account;
36+
var user = await EmbeddedWalletUI.Instance.Connect(_embeddedWallet, walletConnection.email, walletConnection.authOptions);
37+
_account = user.Account;
38+
_email = user.EmailAddress;
3639
_web3 = new Web3(_account, rpc);
3740

3841
return await GetAddress();
@@ -58,6 +61,11 @@ public Task<string> GetAddress()
5861
return Task.FromResult(addy);
5962
}
6063

64+
public Task<string> GetEmail()
65+
{
66+
return Task.FromResult(_email);
67+
}
68+
6169
public async Task<string> GetSignerAddress()
6270
{
6371
return await GetAddress();

Assets/Thirdweb/Core/Scripts/Wallets/ThirdwebHyperplay.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public Task<string> GetAddress()
4747
return Task.FromResult(addy);
4848
}
4949

50+
public Task<string> GetEmail()
51+
{
52+
return Task.FromResult("");
53+
}
54+
5055
public async Task<string> GetSignerAddress()
5156
{
5257
return await GetAddress();

Assets/Thirdweb/Core/Scripts/Wallets/ThirdwebLocalWallet.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public Task<string> GetAddress()
4747
return Task.FromResult(addy);
4848
}
4949

50+
public Task<string> GetEmail()
51+
{
52+
return Task.FromResult("");
53+
}
54+
5055
public async Task<string> GetSignerAddress()
5156
{
5257
return await GetAddress();

Assets/Thirdweb/Core/Scripts/Wallets/ThirdwebMetamask.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public Task<string> GetAddress()
5454
return Task.FromResult(addy);
5555
}
5656

57+
public Task<string> GetEmail()
58+
{
59+
return Task.FromResult("");
60+
}
61+
5762
public async Task<string> GetSignerAddress()
5863
{
5964
return await GetAddress();

Assets/Thirdweb/Core/Scripts/Wallets/ThirdwebSmartWallet.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public Task<string> GetAddress()
5757
return Task.FromResult(addy);
5858
}
5959

60+
public async Task<string> GetEmail()
61+
{
62+
return await _personalWallet.GetEmail();
63+
}
64+
6065
public async Task<string> GetSignerAddress()
6166
{
6267
var addy = await _smartWallet.GetPersonalAddress();

Assets/Thirdweb/Core/Scripts/Wallets/ThirdwebWalletConnect.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public Task<string> GetAddress()
7777
return Task.FromResult(addy);
7878
}
7979

80+
public Task<string> GetEmail()
81+
{
82+
return Task.FromResult("");
83+
}
84+
8085
public async Task<string> GetSignerAddress()
8186
{
8287
return await GetAddress();

0 commit comments

Comments
 (0)