Skip to content

Commit 7f42f7f

Browse files
authored
[WebGL] Support for admin and session key management (#128)
1 parent 151b3a5 commit 7f42f7f

File tree

4 files changed

+347465
-367
lines changed

4 files changed

+347465
-367
lines changed

Assets/Thirdweb/Core/Plugin/thirdweb.jslib

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,75 @@ var plugin = {
181181
dynCall_viii(cb, idPtr, null, buffer);
182182
});
183183
},
184+
ThirdwebSmartWalletAddAdmin: async function (taskId, admin, cb) {
185+
// convert taskId from pointer to str and allocate it to keep in memory
186+
var id = UTF8ToString(taskId);
187+
var idSize = lengthBytesUTF8(id) + 1;
188+
var idPtr = _malloc(idSize);
189+
stringToUTF8(id, idPtr, idSize);
190+
// execute bridge call
191+
window.bridge
192+
.smartWalletAddAdmin(UTF8ToString(admin))
193+
.then((returnStr) => {
194+
var bufferSize = lengthBytesUTF8(returnStr) + 1;
195+
var buffer = _malloc(bufferSize);
196+
stringToUTF8(returnStr, buffer, bufferSize);
197+
dynCall_viii(cb, idPtr, buffer, null);
198+
})
199+
.catch((err) => {
200+
var msg = err.message;
201+
var bufferSize = lengthBytesUTF8(msg) + 1;
202+
var buffer = _malloc(bufferSize);
203+
stringToUTF8(msg, buffer, bufferSize);
204+
dynCall_viii(cb, idPtr, null, buffer);
205+
});
206+
},
207+
ThirdwebSmartWalletRemoveAdmin: async function (taskId, admin, cb) {
208+
// convert taskId from pointer to str and allocate it to keep in memory
209+
var id = UTF8ToString(taskId);
210+
var idSize = lengthBytesUTF8(id) + 1;
211+
var idPtr = _malloc(idSize);
212+
stringToUTF8(id, idPtr, idSize);
213+
// execute bridge call
214+
window.bridge
215+
.smartWalletRemoveAdmin(UTF8ToString(admin))
216+
.then((returnStr) => {
217+
var bufferSize = lengthBytesUTF8(returnStr) + 1;
218+
var buffer = _malloc(bufferSize);
219+
stringToUTF8(returnStr, buffer, bufferSize);
220+
dynCall_viii(cb, idPtr, buffer, null);
221+
})
222+
.catch((err) => {
223+
var msg = err.message;
224+
var bufferSize = lengthBytesUTF8(msg) + 1;
225+
var buffer = _malloc(bufferSize);
226+
stringToUTF8(msg, buffer, bufferSize);
227+
dynCall_viii(cb, idPtr, null, buffer);
228+
});
229+
},
230+
ThirdwebSmartWalletCreateSessionKey: async function (taskId, options, cb) {
231+
// convert taskId from pointer to str and allocate it to keep in memory
232+
var id = UTF8ToString(taskId);
233+
var idSize = lengthBytesUTF8(id) + 1;
234+
var idPtr = _malloc(idSize);
235+
stringToUTF8(id, idPtr, idSize);
236+
// execute bridge call
237+
window.bridge
238+
.smartWalletCreateSessionKey(UTF8ToString(options))
239+
.then((returnStr) => {
240+
var bufferSize = lengthBytesUTF8(returnStr) + 1;
241+
var buffer = _malloc(bufferSize);
242+
stringToUTF8(returnStr, buffer, bufferSize);
243+
dynCall_viii(cb, idPtr, buffer, null);
244+
})
245+
.catch((err) => {
246+
var msg = err.message;
247+
var bufferSize = lengthBytesUTF8(msg) + 1;
248+
var buffer = _malloc(bufferSize);
249+
stringToUTF8(msg, buffer, bufferSize);
250+
dynCall_viii(cb, idPtr, null, buffer);
251+
});
252+
},
184253
};
185254

186255
mergeInto(LibraryManager.library, plugin);

Assets/Thirdweb/Core/Scripts/Bridge.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,57 @@ public static async Task<string> ExportWallet(string password)
208208
return result;
209209
}
210210

211+
public static async Task<T> SmartWalletAddAdmin<T>(string admin)
212+
{
213+
if (!Utils.IsWebGLBuild())
214+
{
215+
ThirdwebDebug.LogWarning("Interacting with the thirdweb SDK is not fully supported in the editor.");
216+
return default;
217+
}
218+
string taskId = Guid.NewGuid().ToString();
219+
var task = new TaskCompletionSource<string>();
220+
taskMap[taskId] = task;
221+
#if UNITY_WEBGL
222+
ThirdwebSmartWalletAddAdmin(taskId, admin, jsCallback);
223+
#endif
224+
string result = await task.Task;
225+
return JsonConvert.DeserializeObject<Result<T>>(result).result;
226+
}
227+
228+
public static async Task<T> SmartWalletRemoveAdmin<T>(string admin)
229+
{
230+
if (!Utils.IsWebGLBuild())
231+
{
232+
ThirdwebDebug.LogWarning("Interacting with the thirdweb SDK is not fully supported in the editor.");
233+
return default;
234+
}
235+
string taskId = Guid.NewGuid().ToString();
236+
var task = new TaskCompletionSource<string>();
237+
taskMap[taskId] = task;
238+
#if UNITY_WEBGL
239+
ThirdwebSmartWalletRemoveAdmin(taskId, admin, jsCallback);
240+
#endif
241+
string result = await task.Task;
242+
return JsonConvert.DeserializeObject<Result<T>>(result).result;
243+
}
244+
245+
public static async Task<T> SmartWalletCreateSessionKey<T>(string options)
246+
{
247+
if (!Utils.IsWebGLBuild())
248+
{
249+
ThirdwebDebug.LogWarning("Interacting with the thirdweb SDK is not fully supported in the editor.");
250+
return default;
251+
}
252+
string taskId = Guid.NewGuid().ToString();
253+
var task = new TaskCompletionSource<string>();
254+
taskMap[taskId] = task;
255+
#if UNITY_WEBGL
256+
ThirdwebSmartWalletCreateSessionKey(taskId, options, jsCallback);
257+
#endif
258+
string result = await task.Task;
259+
return JsonConvert.DeserializeObject<Result<T>>(result).result;
260+
}
261+
211262
#if UNITY_WEBGL
212263
[DllImport("__Internal")]
213264
private static extern string ThirdwebInvoke(string taskId, string route, string payload, Action<string, string, string> cb);
@@ -225,6 +276,12 @@ public static async Task<string> ExportWallet(string password)
225276
private static extern string ThirdwebFundWallet(string taskId, string payload, Action<string, string, string> cb);
226277
[DllImport("__Internal")]
227278
private static extern string ThirdwebExportWallet(string taskId, string password, Action<string, string, string> cb);
279+
[DllImport("__Internal")]
280+
private static extern string ThirdwebSmartWalletAddAdmin(string taskId, string admin, Action<string, string, string> cb);
281+
[DllImport("__Internal")]
282+
private static extern string ThirdwebSmartWalletRemoveAdmin(string taskId, string admin, Action<string, string, string> cb);
283+
[DllImport("__Internal")]
284+
private static extern string ThirdwebSmartWalletCreateSessionKey(string taskId, string options, Action<string, string, string> cb);
228285
#endif
229286
}
230287
}

Assets/Thirdweb/Core/Scripts/Wallet.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Nethereum.Signer.EIP712;
1111
using Newtonsoft.Json.Linq;
1212
using Nethereum.Hex.HexTypes;
13+
using Newtonsoft.Json;
1314

1415
namespace Thirdweb
1516
{
@@ -476,7 +477,7 @@ public async Task<TransactionResult> AddAdmin(string admin)
476477
{
477478
if (Utils.IsWebGLBuild())
478479
{
479-
throw new UnityException("This functionality is not yet available on your current platform.");
480+
return await Bridge.SmartWalletAddAdmin<TransactionResult>(admin);
480481
}
481482
else
482483
{
@@ -510,7 +511,7 @@ public async Task<TransactionResult> RemoveAdmin(string admin)
510511
{
511512
if (Utils.IsWebGLBuild())
512513
{
513-
throw new UnityException("This functionality is not yet available on your current platform.");
514+
return await Bridge.SmartWalletRemoveAdmin<TransactionResult>(admin);
514515
}
515516
else
516517
{
@@ -558,7 +559,20 @@ string reqValidityEndTimestamp
558559
{
559560
if (Utils.IsWebGLBuild())
560561
{
561-
throw new UnityException("This functionality is not yet available on your current platform.");
562+
return await Bridge.SmartWalletCreateSessionKey<TransactionResult>(
563+
Utils.ToJson(
564+
new
565+
{
566+
signerAddress,
567+
approvedCallTargets = approvedTargets,
568+
nativeTokenLimitPerTransactionInWei,
569+
startDate = permissionStartTimestamp,
570+
expirationDate = permissionEndTimestamp,
571+
reqValidityStartTimestamp,
572+
reqValidityEndTimestamp
573+
}
574+
)
575+
);
562576
}
563577
else
564578
{

Assets/WebGLTemplates/Thirdweb/lib/thirdweb-unity-bridge.js

Lines changed: 347322 additions & 364 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)