-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switching between eoa and smart account
- Loading branch information
Showing
19 changed files
with
255 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
107 changes: 107 additions & 0 deletions
107
src/Reown.AppKit.Unity/Runtime/Connectors/Profile/ProfileConnector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
using Reown.Sign.Models; | ||
using UnityEngine; | ||
|
||
namespace Reown.AppKit.Unity.Profile | ||
{ | ||
public class ProfileConnector : WalletConnectConnector | ||
{ | ||
public string Email { get; private set; } | ||
|
||
public Account[] SmartAccounts { get; private set; } | ||
|
||
public Account[] EoaAccounts { get; private set; } | ||
|
||
public AccountType PreferredAccountType { get; private set; } | ||
|
||
public Account PreferredAccount { get; private set; } | ||
|
||
private const string PreferredAccountTypeKey = "PreferredAccount"; | ||
|
||
public ProfileConnector() | ||
{ | ||
Type = ConnectorType.Profile; | ||
} | ||
|
||
protected override async void OnAccountConnected(AccountConnectedEventArgs e) | ||
{ | ||
try | ||
{ | ||
var addressProvider = SignClient.AddressProvider; | ||
var sessionProperties = addressProvider.DefaultSession.SessionProperties; | ||
|
||
SmartAccounts = JsonConvert | ||
.DeserializeObject<string[]>(sessionProperties["smartAccounts"]) | ||
.Select(x => new Account(x)) | ||
.ToArray(); | ||
|
||
var allAccounts = await GetAccountsAsyncCore(); | ||
EoaAccounts = allAccounts | ||
.Except(SmartAccounts) | ||
.ToArray(); | ||
|
||
Email = sessionProperties["email"]; | ||
|
||
base.OnAccountConnected(e); | ||
|
||
var preferredAccountTypeStr = PlayerPrefs.GetString(PreferredAccountTypeKey); | ||
var preferredAccountType = Enum.TryParse<AccountType>(preferredAccountTypeStr, out var accountType) | ||
? accountType | ||
: AccountType.SmartAccount; | ||
SetPreferredAccount(preferredAccountType); | ||
} | ||
catch (Exception exception) | ||
{ | ||
Debug.LogException(exception); | ||
} | ||
} | ||
|
||
public void SetPreferredAccount(AccountType accountType) | ||
{ | ||
Debug.Log($"SetPreferredAccount: {accountType}. Current: {PreferredAccountType}"); | ||
if (PreferredAccountType == accountType) | ||
return; | ||
|
||
SetPreferredAccountCore(accountType); | ||
OnAccountChanged(new AccountChangedEventArgs(GetCurrentAccount())); | ||
} | ||
|
||
private void SetPreferredAccountCore(AccountType accountType) | ||
{ | ||
if (PreferredAccountType == accountType) | ||
return; | ||
|
||
if (AppKit.NetworkController.ActiveChain == null) | ||
return; | ||
|
||
var chainId = AppKit.NetworkController.ActiveChain.ChainId; | ||
|
||
// Find preferred account for the current chain | ||
PreferredAccount = accountType == AccountType.SmartAccount | ||
? SmartAccounts.First(a => a.ChainId == chainId) | ||
: EoaAccounts.First(a => a.ChainId == chainId); | ||
|
||
PreferredAccountType = accountType; | ||
|
||
// The preferred account type is saved so it can be recovered after the session is resumed | ||
PlayerPrefs.SetString(PreferredAccountTypeKey, accountType.ToString()); | ||
} | ||
|
||
protected override Account GetCurrentAccount() | ||
{ | ||
return PreferredAccount != default | ||
? PreferredAccount | ||
: base.GetCurrentAccount(); | ||
} | ||
} | ||
|
||
public enum AccountType | ||
{ | ||
None, | ||
SmartAccount, | ||
Eoa | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/Reown.AppKit.Unity/Runtime/Connectors/Profile/ProfileConnector.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.