From 4880e4199a9c64e804f251d920de2e7b00284048 Mon Sep 17 00:00:00 2001 From: 0xFirekeeper <0xFirekeeper@gmail.com> Date: Tue, 21 Nov 2023 10:54:20 +0300 Subject: [PATCH 1/3] Session keys skeleton --- .../unity-js-bridge/src/thirdweb-bridge.ts | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/unity-js-bridge/src/thirdweb-bridge.ts b/packages/unity-js-bridge/src/thirdweb-bridge.ts index e388e3c29d4..bdef3b53411 100644 --- a/packages/unity-js-bridge/src/thirdweb-bridge.ts +++ b/packages/unity-js-bridge/src/thirdweb-bridge.ts @@ -18,7 +18,7 @@ import { MetaMaskWallet } from "@thirdweb-dev/wallets/evm/wallets/metamask"; import { SmartWallet } from "@thirdweb-dev/wallets/evm/wallets/smart-wallet"; import { WalletConnect } from "@thirdweb-dev/wallets/evm/wallets/wallet-connect"; import { EmbeddedWallet } from "@thirdweb-dev/wallets/evm/wallets/embedded-wallet"; -import { BigNumber } from "ethers"; +import { BigNumber, ethers } from "ethers"; import { Ethereum, defaultChains, @@ -644,6 +644,42 @@ class ThirdwebBridge implements TWBridge { return localWallet; } + public async smartWalletAddAdmin(admin: string) { + if (!this.activeWallet) { + throw new Error("No wallet connected"); + } + const smartWallet = this.activeWallet as SmartWallet; + return await smartWallet.addAdmin(admin); + } + + public async smartWalletRemoveAdmin(admin: string) { + if (!this.activeWallet) { + throw new Error("No wallet connected"); + } + const smartWallet = this.activeWallet as SmartWallet; + return await smartWallet.removeAdmin(admin); + } + + public async smartWalletCreateSessionKey(options: string) { + if (!this.activeWallet) { + throw new Error("No wallet connected"); + } + const smartWallet = this.activeWallet as SmartWallet; + const optionsParsed = JSON.parse(options); + const approvedCallTargets = optionsParsed.approvedCallTargets; + const nativeTokenLimitPerTransaction = ethers.utils.formatEther( + optionsParsed.nativeTokenLimitPerTransactionInWei, + ); + const startDate = optionsParsed.startDate; + const expirationDate = optionsParsed.expirationDate; + return await smartWallet.createSessionKey(optionsParsed.signerAddress, { + approvedCallTargets: approvedCallTargets, + nativeTokenLimitPerTransaction: nativeTokenLimitPerTransaction, + startDate: startDate, + expirationDate: expirationDate, + }); + } + public openPopupWindow() { const win = window.open("", undefined, "width=350, height=500"); if (win) { From da45a635d464b4821fd3962fcd6af4d07ffddb10 Mon Sep 17 00:00:00 2001 From: 0xFirekeeper <0xFirekeeper@gmail.com> Date: Tue, 21 Nov 2023 13:32:35 +0300 Subject: [PATCH 2/3] Connect to jslib --- packages/unity-js-bridge/src/thirdweb-bridge.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/unity-js-bridge/src/thirdweb-bridge.ts b/packages/unity-js-bridge/src/thirdweb-bridge.ts index bdef3b53411..b497d553a98 100644 --- a/packages/unity-js-bridge/src/thirdweb-bridge.ts +++ b/packages/unity-js-bridge/src/thirdweb-bridge.ts @@ -88,6 +88,9 @@ interface TWBridge { ) => void; fundWallet: (options: string) => Promise; exportWallet: (password: string) => Promise; + smartWalletAddAdmin: (admin: string) => Promise; + smartWalletRemoveAdmin: (admin: string) => Promise; + smartWalletCreateSessionKey: (options: string) => Promise; } const w = window; @@ -649,7 +652,8 @@ class ThirdwebBridge implements TWBridge { throw new Error("No wallet connected"); } const smartWallet = this.activeWallet as SmartWallet; - return await smartWallet.addAdmin(admin); + const result = await smartWallet.addAdmin(admin); + return JSON.stringify({ result: result }, bigNumberReplacer); } public async smartWalletRemoveAdmin(admin: string) { @@ -657,7 +661,8 @@ class ThirdwebBridge implements TWBridge { throw new Error("No wallet connected"); } const smartWallet = this.activeWallet as SmartWallet; - return await smartWallet.removeAdmin(admin); + const result = await smartWallet.removeAdmin(admin); + return JSON.stringify({ result: result }, bigNumberReplacer); } public async smartWalletCreateSessionKey(options: string) { @@ -670,14 +675,15 @@ class ThirdwebBridge implements TWBridge { const nativeTokenLimitPerTransaction = ethers.utils.formatEther( optionsParsed.nativeTokenLimitPerTransactionInWei, ); - const startDate = optionsParsed.startDate; - const expirationDate = optionsParsed.expirationDate; - return await smartWallet.createSessionKey(optionsParsed.signerAddress, { + const startDate = BigNumber.from(optionsParsed.startDate).toNumber(); + const expirationDate = BigNumber.from(optionsParsed.expirationDate).toNumber(); + const result = await smartWallet.createSessionKey(optionsParsed.signerAddress, { approvedCallTargets: approvedCallTargets, nativeTokenLimitPerTransaction: nativeTokenLimitPerTransaction, startDate: startDate, expirationDate: expirationDate, }); + return JSON.stringify({ result: result }, bigNumberReplacer); } public openPopupWindow() { From 74c72571c0a665f381c65b4c189308b6f114ada2 Mon Sep 17 00:00:00 2001 From: 0xFirekeeper <0xFirekeeper@gmail.com> Date: Tue, 21 Nov 2023 13:33:30 +0300 Subject: [PATCH 3/3] [Unity] Support Add/Remove Admin and Create Session Key for SW --- .changeset/gentle-kings-shout.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/gentle-kings-shout.md diff --git a/.changeset/gentle-kings-shout.md b/.changeset/gentle-kings-shout.md new file mode 100644 index 00000000000..6d4117fe0ca --- /dev/null +++ b/.changeset/gentle-kings-shout.md @@ -0,0 +1,5 @@ +--- +"@thirdweb-dev/unity-js-bridge": patch +--- + +[Unity] Support Add/Remove Admin and Create Session Key for SW