Skip to content

Commit 0328632

Browse files
committed
Add smart wallet upgrade option to PlaygroundManager
Introduces the AlwaysUpgradeToSmartWallet flag to PlaygroundManager, allowing automatic upgrade of connected wallets to smart wallets with sponsored gas. Updates the scene and connection methods to support this option. Also improves PersonalSign handling in ReownWallet to support hex-encoded messages.
1 parent 2506862 commit 0328632

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

Assets/Thirdweb/Examples/Scenes/Scene_Playground.unity

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,7 @@ MonoBehaviour:
19611961
m_Script: {fileID: 11500000, guid: f139d8153587c1d46aaf3b6bf05d2c25, type: 3}
19621962
m_Name:
19631963
m_EditorClassIdentifier: Thirdweb.Examples::ThirdwebPlayground
1964+
AlwaysUpgradeToSmartWallet: 1
19641965
ChainId: 421614
19651966
Email:
19661967
Phone:

Assets/Thirdweb/Examples/Scripts/PlaygroundManager.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class PlaygroundManager : MonoBehaviour
1616
{
1717
#region Inspector
1818

19+
public bool AlwaysUpgradeToSmartWallet;
1920
public ulong ChainId;
2021
public string Email;
2122
public string Phone;
@@ -111,7 +112,7 @@ private async void Wallet_Guest()
111112
);
112113

113114
var smartWalletAddress = await smartWallet.GetAddress();
114-
this.LogPlayground($"[Guest] Connected to smart wallet (sponsored gas):\n{smartWalletAddress}");
115+
this.LogPlayground($"[Guest] Connected to wallet (sponsored gas):\n{smartWalletAddress}");
115116

116117
// // --Smart wallets have special functionality other than just gas sponsorship
117118
// var sessionKeyReceipt = await smartWallet.CreateSessionKey(
@@ -131,6 +132,10 @@ private async void Wallet_Social()
131132
{
132133
var walletOptions = new WalletOptions(provider: WalletProvider.InAppWallet, chainId: this.ChainId, new InAppWalletOptions(authprovider: AuthProvider.Github));
133134
var wallet = await ThirdwebManager.Instance.ConnectWallet(walletOptions);
135+
if (this.AlwaysUpgradeToSmartWallet)
136+
{
137+
wallet = await ThirdwebManager.Instance.UpgradeToSmartWallet(wallet, chainId: this.ChainId, smartWalletOptions: new SmartWalletOptions(sponsorGas: true));
138+
}
134139
var address = await wallet.GetAddress();
135140
this.LogPlayground($"[Social] Connected to wallet:\n{address}");
136141
}
@@ -145,6 +150,10 @@ private async void Wallet_Email()
145150

146151
var walletOptions = new WalletOptions(provider: WalletProvider.InAppWallet, chainId: this.ChainId, new InAppWalletOptions(email: this.Email));
147152
var wallet = await ThirdwebManager.Instance.ConnectWallet(walletOptions);
153+
if (this.AlwaysUpgradeToSmartWallet)
154+
{
155+
wallet = await ThirdwebManager.Instance.UpgradeToSmartWallet(wallet, chainId: this.ChainId, smartWalletOptions: new SmartWalletOptions(sponsorGas: true));
156+
}
148157
var address = await wallet.GetAddress();
149158
this.LogPlayground($"[Email] Connected to wallet:\n{address}");
150159
}
@@ -159,6 +168,10 @@ private async void Wallet_Phone()
159168

160169
var walletOptions = new WalletOptions(provider: WalletProvider.InAppWallet, chainId: this.ChainId, new InAppWalletOptions(phoneNumber: this.Phone));
161170
var wallet = await ThirdwebManager.Instance.ConnectWallet(walletOptions);
171+
if (this.AlwaysUpgradeToSmartWallet)
172+
{
173+
wallet = await ThirdwebManager.Instance.UpgradeToSmartWallet(wallet, chainId: this.ChainId, smartWalletOptions: new SmartWalletOptions(sponsorGas: true));
174+
}
162175
var address = await wallet.GetAddress();
163176
this.LogPlayground($"[Phone] Connected to wallet:\n{address}");
164177
}
@@ -171,6 +184,10 @@ private async void Wallet_External()
171184
reownOptions: new ReownOptions(projectId: null, name: null, description: null, url: null, iconUrl: null, includedWalletIds: null, excludedWalletIds: null)
172185
);
173186
var wallet = await ThirdwebManager.Instance.ConnectWallet(walletOptions);
187+
if (this.AlwaysUpgradeToSmartWallet)
188+
{
189+
wallet = await ThirdwebManager.Instance.UpgradeToSmartWallet(wallet, chainId: this.ChainId, smartWalletOptions: new SmartWalletOptions(sponsorGas: true));
190+
}
174191
var address = await wallet.GetAddress();
175192
this.LogPlayground($"[SIWE] Connected to wallet:\n{address}");
176193
}

Assets/Thirdweb/Runtime/Unity/Wallets/Reown/ReownWallet.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,13 @@ public async Task<string> PersonalSign(byte[] rawMessage)
149149
return await AppKit.Evm.SignMessageAsync(rawMessage);
150150
}
151151

152-
public Task<string> PersonalSign(string message)
152+
public async Task<string> PersonalSign(string message)
153153
{
154-
var rawMessage = System.Text.Encoding.UTF8.GetBytes(message);
155-
return this.PersonalSign(rawMessage);
154+
if (message.StartsWith("0x"))
155+
{
156+
return await this.PersonalSign(message.HexToBytes());
157+
}
158+
return await AppKit.Evm.SignMessageAsync(message);
156159
}
157160

158161
public Task<string> RecoverAddressFromPersonalSign(string message, string signature)

0 commit comments

Comments
 (0)