Skip to content

Commit 92f77f6

Browse files
committed
Cross platform Wallet.IsDeployed for SW
+ better errors for native
1 parent f1bf7a5 commit 92f77f6

File tree

4 files changed

+57147
-57675
lines changed

4 files changed

+57147
-57675
lines changed

Assets/Thirdweb/Core/Plugin/thirdweb.jslib

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,29 @@ var plugin = {
436436
dynCall_viii(cb, idPtr, null, buffer);
437437
});
438438
},
439+
ThirdwebSmartWalletIsDeployed: async function (taskId, cb) {
440+
// convert taskId from pointer to str and allocate it to keep in memory
441+
var id = UTF8ToString(taskId);
442+
var idSize = lengthBytesUTF8(id) + 1;
443+
var idPtr = _malloc(idSize);
444+
stringToUTF8(id, idPtr, idSize);
445+
// execute bridge call
446+
window.bridge
447+
.smartWalletIsDeployed()
448+
.then((returnStr) => {
449+
var bufferSize = lengthBytesUTF8(returnStr) + 1;
450+
var buffer = _malloc(bufferSize);
451+
stringToUTF8(returnStr, buffer, bufferSize);
452+
dynCall_viii(cb, idPtr, buffer, null);
453+
})
454+
.catch((err) => {
455+
var msg = err.message;
456+
var bufferSize = lengthBytesUTF8(msg) + 1;
457+
var buffer = _malloc(bufferSize);
458+
stringToUTF8(msg, buffer, bufferSize);
459+
dynCall_viii(cb, idPtr, null, buffer);
460+
});
461+
},
439462
};
440463

441464
mergeInto(LibraryManager.library, plugin);

Assets/Thirdweb/Core/Scripts/Bridge.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,23 @@ public static async Task<string> GetSigner()
398398
return JsonConvert.DeserializeObject<Result<string>>(result).result;
399399
}
400400

401+
public static async Task<bool> SmartWalletIsDeployed()
402+
{
403+
if (!Utils.IsWebGLBuild())
404+
{
405+
ThirdwebDebug.LogWarning("Interacting with the thirdweb SDK is not fully supported in the editor.");
406+
return false;
407+
}
408+
string taskId = Guid.NewGuid().ToString();
409+
var task = new TaskCompletionSource<string>();
410+
taskMap[taskId] = task;
411+
#if UNITY_WEBGL
412+
ThirdwebSmartWalletIsDeployed(taskId, jsCallback);
413+
#endif
414+
string result = await task.Task;
415+
return JsonConvert.DeserializeObject<Result<bool>>(result).result;
416+
}
417+
401418
#if UNITY_WEBGL
402419
[DllImport("__Internal")]
403420
private static extern string ThirdwebInvoke(string taskId, string route, string payload, Action<string, string, string> cb);
@@ -437,6 +454,8 @@ public static async Task<string> GetSigner()
437454
private static extern string ThirdwebGetEmail(string taskId, Action<string, string, string> cb);
438455
[DllImport("__Internal")]
439456
private static extern string ThirdwebGetSignerAddress(string taskId, Action<string, string, string> cb);
457+
[DllImport("__Internal")]
458+
private static extern string ThirdwebSmartWalletIsDeployed(string taskId, Action<string, string, string> cb);
440459
#endif
441460
}
442461
}

Assets/Thirdweb/Core/Scripts/Wallet.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ public async Task<TransactionResult> AddAdmin(string admin)
643643
{
644644
if (ThirdwebManager.Instance.SDK.session.ActiveWallet.GetProvider() != WalletProvider.SmartWallet)
645645
throw new UnityException("This functionality is only available for SmartWallets.");
646+
646647
var smartWallet = ThirdwebManager.Instance.SDK.session.ActiveWallet as Wallets.ThirdwebSmartWallet;
647648
var request = new Contracts.Account.ContractDefinition.SignerPermissionRequest()
648649
{
@@ -677,6 +678,7 @@ public async Task<TransactionResult> RemoveAdmin(string admin)
677678
{
678679
if (ThirdwebManager.Instance.SDK.session.ActiveWallet.GetProvider() != WalletProvider.SmartWallet)
679680
throw new UnityException("This functionality is only available for SmartWallets.");
681+
680682
var smartWallet = ThirdwebManager.Instance.SDK.session.ActiveWallet as Wallets.ThirdwebSmartWallet;
681683
var request = new Contracts.Account.ContractDefinition.SignerPermissionRequest()
682684
{
@@ -735,6 +737,9 @@ string reqValidityEndTimestamp
735737
}
736738
else
737739
{
740+
if (ThirdwebManager.Instance.SDK.session.ActiveWallet.GetProvider() != WalletProvider.SmartWallet)
741+
throw new UnityException("This functionality is only available for SmartWallets.");
742+
738743
var smartWallet = ThirdwebManager.Instance.SDK.session.ActiveWallet as Wallets.ThirdwebSmartWallet;
739744
var request = new Contracts.Account.ContractDefinition.SignerPermissionRequest()
740745
{
@@ -766,6 +771,9 @@ public async Task<TransactionResult> RevokeSessionKey(string signerAddress)
766771
}
767772
else
768773
{
774+
if (ThirdwebManager.Instance.SDK.session.ActiveWallet.GetProvider() != WalletProvider.SmartWallet)
775+
throw new UnityException("This functionality is only available for SmartWallets.");
776+
769777
var smartWallet = ThirdwebManager.Instance.SDK.session.ActiveWallet as Wallets.ThirdwebSmartWallet;
770778
var request = new Contracts.Account.ContractDefinition.SignerPermissionRequest()
771779
{
@@ -804,6 +812,9 @@ public async Task<List<SignerWithPermissions>> GetAllActiveSigners()
804812
}
805813
else
806814
{
815+
if (ThirdwebManager.Instance.SDK.session.ActiveWallet.GetProvider() != WalletProvider.SmartWallet)
816+
throw new UnityException("This functionality is only available for SmartWallets.");
817+
807818
string address = await GetAddress();
808819
var raw = await TransactionManager.ThirdwebRead<Contracts.Account.ContractDefinition.GetAllActiveSignersFunction, Contracts.Account.ContractDefinition.GetAllActiveSignersOutputDTO>(
809820
address,
@@ -846,6 +857,26 @@ public async Task<List<SignerWithPermissions>> GetAllActiveSigners()
846857
}
847858
}
848859

860+
/// <summary>
861+
/// Smart Wallet only: check if the account is deployed.
862+
/// </summary>
863+
/// <returns>True if the account is deployed, false otherwise.</returns>
864+
public async Task<bool> IsDeployed()
865+
{
866+
if (Utils.IsWebGLBuild())
867+
{
868+
return await Bridge.SmartWalletIsDeployed();
869+
}
870+
else
871+
{
872+
if (ThirdwebManager.Instance.SDK.session.ActiveWallet.GetProvider() != WalletProvider.SmartWallet)
873+
throw new UnityException("This functionality is only available for SmartWallets.");
874+
875+
var smartWallet = ThirdwebManager.Instance.SDK.session.ActiveWallet as Wallets.ThirdwebSmartWallet;
876+
return smartWallet.SmartWallet.IsDeployed;
877+
}
878+
}
879+
849880
/// <summary>
850881
/// Sends a raw transaction from the connected wallet.
851882
/// </summary>

0 commit comments

Comments
 (0)