Skip to content

Commit 54578b1

Browse files
committed
[WebGL] Implement Wallet.GetSignerAddress for Smart Wallets
1 parent 16913c0 commit 54578b1

File tree

4 files changed

+57949
-57378
lines changed

4 files changed

+57949
-57378
lines changed

Assets/Thirdweb/Core/Plugin/thirdweb.jslib

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,29 @@ var plugin = {
413413
dynCall_viii(cb, idPtr, null, buffer);
414414
});
415415
},
416+
ThirdwebGetSignerAddress: async function (taskId, cb) {
417+
// convert taskId from pointer to str and allocate it to keep in memory
418+
var id = UTF8ToString(taskId);
419+
var idSize = lengthBytesUTF8(id) + 1;
420+
var idPtr = _malloc(idSize);
421+
stringToUTF8(id, idPtr, idSize);
422+
// execute bridge call
423+
window.bridge
424+
.getSignerAddress()
425+
.then((returnStr) => {
426+
var bufferSize = lengthBytesUTF8(returnStr) + 1;
427+
var buffer = _malloc(bufferSize);
428+
stringToUTF8(returnStr, buffer, bufferSize);
429+
dynCall_viii(cb, idPtr, buffer, null);
430+
})
431+
.catch((err) => {
432+
var msg = err.message;
433+
var bufferSize = lengthBytesUTF8(msg) + 1;
434+
var buffer = _malloc(bufferSize);
435+
stringToUTF8(msg, buffer, bufferSize);
436+
dynCall_viii(cb, idPtr, null, buffer);
437+
});
438+
},
416439
};
417440

418441
mergeInto(LibraryManager.library, plugin);

Assets/Thirdweb/Core/Scripts/Bridge.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,23 @@ public static async Task<string> GetEmail()
381381
return JsonConvert.DeserializeObject<Result<string>>(result).result;
382382
}
383383

384+
public static async Task<string> GetSigner()
385+
{
386+
if (!Utils.IsWebGLBuild())
387+
{
388+
ThirdwebDebug.LogWarning("Interacting with the thirdweb SDK is not fully supported in the editor.");
389+
return "";
390+
}
391+
string taskId = Guid.NewGuid().ToString();
392+
var task = new TaskCompletionSource<string>();
393+
taskMap[taskId] = task;
394+
#if UNITY_WEBGL
395+
ThirdwebGetSignerAddress(taskId, jsCallback);
396+
#endif
397+
string result = await task.Task;
398+
return JsonConvert.DeserializeObject<Result<string>>(result).result;
399+
}
400+
384401
#if UNITY_WEBGL
385402
[DllImport("__Internal")]
386403
private static extern string ThirdwebInvoke(string taskId, string route, string payload, Action<string, string, string> cb);
@@ -418,6 +435,8 @@ public static async Task<string> GetEmail()
418435
private static extern string ThirdwebGetBlockWithTransactions(string taskId, string blockNumber, Action<string, string, string> cb);
419436
[DllImport("__Internal")]
420437
private static extern string ThirdwebGetEmail(string taskId, Action<string, string, string> cb);
438+
[DllImport("__Internal")]
439+
private static extern string ThirdwebGetSignerAddress(string taskId, Action<string, string, string> cb);
421440
#endif
422441
}
423442
}

Assets/Thirdweb/Core/Scripts/Wallet.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,18 @@ public async Task<string> GetSignerAddress()
309309
{
310310
if (Utils.IsWebGLBuild())
311311
{
312-
return await GetAddress();
312+
try
313+
{
314+
var signer = await Bridge.GetSigner();
315+
if (string.IsNullOrEmpty(signer))
316+
return await GetAddress();
317+
else
318+
return signer;
319+
}
320+
catch
321+
{
322+
return await GetAddress();
323+
}
313324
}
314325
else
315326
{

0 commit comments

Comments
 (0)