Skip to content

Commit

Permalink
Savepoint
Browse files Browse the repository at this point in the history
  • Loading branch information
skibitsky committed Oct 10, 2024
1 parent 3f9e172 commit 8c21535
Show file tree
Hide file tree
Showing 18 changed files with 194 additions and 49 deletions.
35 changes: 31 additions & 4 deletions .idea/.idea.Reown/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion sample/Reown.AppKit.Unity/Assets/Scripts/AppKitInit.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using mixpanel;
using Reown.AppKit.Unity;
using Reown.AppKit.Unity.Model;
using Reown.Core.Common.Logging;
using Skibitsky.Unity;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityLogger = Reown.Sign.Unity.UnityLogger;

namespace Sample
{
Expand All @@ -12,6 +14,8 @@ public class AppKitInit : MonoBehaviour

private async void Start()
{
ReownLogger.Instance = new UnityLogger();

Debug.Log($"[AppKit Init] Initializing AppKit...");
await AppKit.InitializeAsync(
new AppKitConfig(
Expand All @@ -27,6 +31,9 @@ await AppKit.InitializeAsync(
}
)
)
{
customWallets = GetCustomWallets()
}
);

#if !UNITY_WEBGL
Expand All @@ -37,5 +44,18 @@ await AppKit.InitializeAsync(
Debug.Log($"[AppKit Init] AppKit initialized. Loading menu scene...");
SceneManager.LoadScene(_menuScene);
}

private Wallet[] GetCustomWallets()
{
return new[]
{
new Wallet
{
Name = "Swift Wallet",
// ImageUrl = "https://raw.githubusercontent.com/reown-com/reown-dotnet/main/media/flutter-icon.png",
MobileLink = "walletapp://"
}
};
}
}
}
2 changes: 1 addition & 1 deletion sample/Reown.AppKit.Unity/Assets/Scripts/Dapp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private void BuildButtons()
{
new ButtonStruct
{
Text = "Connect",
Text = "Connect 1CA",
OnClick = OnConnectButton,
AccountRequired = false
},
Expand Down
11 changes: 7 additions & 4 deletions src/Reown.AppKit.Unity/Runtime/AppKitConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Reown.AppKit.Unity.Model;

namespace Reown.AppKit.Unity
{
Expand Down Expand Up @@ -26,6 +27,8 @@ public class AppKitConfig
ChainConstants.Chains.Ronin
};

public Wallet[] customWallets;

public readonly Metadata Metadata;
public readonly string ProjectId;

Expand All @@ -52,16 +55,16 @@ public Metadata(string name, string description, string url, string iconUrl, Red
IconUrl = iconUrl;
Redirect = redirect ?? new RedirectData();
}
public static implicit operator Reown.Core.Metadata(Metadata metadata)

public static implicit operator Core.Metadata(Metadata metadata)
{
return new Reown.Core.Metadata
return new Core.Metadata
{
Name = metadata.Name,
Description = metadata.Description,
Url = metadata.Url,
Icons = new[] { metadata.IconUrl },
Redirect = new Reown.Core.Models.RedirectData
Redirect = new Core.Models.RedirectData
{
Native = metadata.Redirect.Native,
Universal = metadata.Redirect.Universal
Expand Down
10 changes: 5 additions & 5 deletions src/Reown.AppKit.Unity/Runtime/Connectors/Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public async Task<bool> TryResumeSessionAsync()
if (isResumed)
{
IsAccountConnected = true;
OnAccountConnected(new AccountConnectedEventArgs(GetAccountAsync, GetAccounts));
OnAccountConnected(new AccountConnectedEventArgs(GetAccountAsync, GetAccountsAsync));
}

return isResumed;
Expand Down Expand Up @@ -95,12 +95,12 @@ public Task<Account> GetAccountAsync()
return GetAccountAsyncCore();
}

public Task<Account[]> GetAccounts()
public Task<Account[]> GetAccountsAsync()
{
if (!IsAccountConnected)
throw new Exception("No account connected"); // TODO: use custom ex type

return GetAccountsCore();
return GetAccountsAsyncCore();
}

protected virtual void ConnectionConnectedHandler(ConnectionProposal connectionProposal)
Expand All @@ -111,7 +111,7 @@ protected virtual void ConnectionConnectedHandler(ConnectionProposal connectionP

_connectionProposals.Clear();
Debug.Log("Connector calls OnAccountConnected");
OnAccountConnected(new AccountConnectedEventArgs(GetAccountAsync, GetAccounts));
OnAccountConnected(new AccountConnectedEventArgs(GetAccountAsync, GetAccountsAsync));
}

protected virtual void OnAccountConnected(AccountConnectedEventArgs e)
Expand Down Expand Up @@ -151,7 +151,7 @@ protected virtual void OnChainChanged(ChainChangedEventArgs e)

protected abstract Task<Account> GetAccountAsyncCore();

protected abstract Task<Account[]> GetAccountsCore();
protected abstract Task<Account[]> GetAccountsAsyncCore();

public class AccountConnectedEventArgs : EventArgs
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Reown.Core.Common.Model.Errors;
using Reown.Sign.Interfaces;
using Reown.Sign.Models;
using Reown.Sign.Models.Engine;
using Reown.Sign.Unity;
using UnityEngine;
Expand All @@ -18,18 +21,40 @@ public class WalletConnectConnectionProposal : ConnectionProposal

private bool _disposed;

// --- 1CA
private readonly AuthParams _authParams;

public WalletConnectConnectionProposal(Connector connector, ISignClient signClient, ConnectOptions connectOptions) : base(connector)
{
_client = signClient;
_connectOptions = connectOptions;

// --- 1CA
_authParams = new AuthParams(
connectOptions.OptionalNamespaces.Values.First().Chains,
"https://reown.com",
"1",
"https://reown.com",
null,
null,
null,
null,
null,
connectOptions.OptionalNamespaces.Values.First().Methods
);
_client.SessionAuthenticated += OnSessionAuthenticated;

_client.SessionConnectionErrored += OnSessionConnectionErrored;

RefreshConnection();

UnityEventsDispatcher.Instance.StartCoroutine(RefreshOnIntervalRoutine());
}

private void OnSessionAuthenticated(object sender, SessionAuthenticatedEventArgs e)
{
}

private void OnSessionConnectionErrored(object sender, Exception e)
{
AppKit.NotificationController.Notify(NotificationType.Error, e.Message);
Expand All @@ -38,7 +63,7 @@ private void OnSessionConnectionErrored(object sender, Exception e)
AppKit.EventsController.SendEvent(new Event
{
name = "CONNECT_ERROR",
properties = new System.Collections.Generic.Dictionary<string, object>
properties = new Dictionary<string, object>
{
{ "message", e.Message }
}
Expand All @@ -63,14 +88,15 @@ private async void RefreshConnection()
if (_disposed)
throw new ObjectDisposedException(nameof(WalletConnectConnectionProposal));

// --- 1CA
try
{
var connectedData = await _client.Connect(_connectOptions);
Uri = connectedData.Uri;

var authData = await _client.Authenticate(_authParams);
Uri = authData.Uri;
connectionUpdated?.Invoke(this);

await connectedData.Approval;
await authData.Approval;
IsConnected = true;
connected?.Invoke(this);
}
Expand All @@ -81,8 +107,29 @@ private async void RefreshConnection()
}
catch (Exception e)
{
Debug.LogException(e);
Debug.LogError($"[WCCP] Exception: {e.Message}");
}

// try
// {
// var connectedData = await _client.Connect(_connectOptions);
// Uri = connectedData.Uri;
//
// connectionUpdated?.Invoke(this);
//
// await connectedData.Approval;
// IsConnected = true;
// connected?.Invoke(this);
// }
// catch (ReownNetworkException e) when (e.CodeType == ErrorType.DISAPPROVED_CHAINS)
// {
// // Wallet declined connection, don't throw/log.
// // The `OnSessionConnectionErrored` will handle the error.
// }
// catch (Exception e)
// {
// Debug.LogException(e);
// }
}

protected override void Dispose(bool disposing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected override Task InitializeAsyncCore(AppKitConfig config, SignClientUnity

_signClient.SubscribeToSessionEvent("chainChanged", ActiveChainIdChangedHandler);

_signClient.SessionAuthenticated += SessionAuthenticatedHandler;
_signClient.SessionConnectedUnity += SessionConnectedHandler;
_signClient.SessionUpdatedUnity += ActiveSessionChangedHandler;
_signClient.SessionDisconnectedUnity += SessionDeletedHandler;
Expand Down Expand Up @@ -60,6 +61,12 @@ private async void ActiveChainIdChangedHandler(object sender, SessionEvent<JToke
OnAccountChanged(new AccountChangedEventArgs(GetCurrentAccount()));
}

private void SessionAuthenticatedHandler(object sender, SessionAuthenticatedEventArgs e)
{
AppKit.NotificationController.Notify(NotificationType.Success, "Session authenticated");
IsAccountConnected = true;
}

private void SessionConnectedHandler(object sender, SessionStruct e)
{
AppKit.NotificationController.Notify(NotificationType.Success, "Session connected");
Expand Down Expand Up @@ -177,7 +184,7 @@ protected override Task<Account> GetAccountAsyncCore()
return Task.FromResult(GetCurrentAccount());
}

protected override Task<Account[]> GetAccountsCore()
protected override Task<Account[]> GetAccountsAsyncCore()
{
var caipAddresses = _signClient.AddressProvider.AllAddresses();
return Task.FromResult(caipAddresses.Select(caip25Address => new Account(caip25Address.Address, caip25Address.ChainId)).ToArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected override async Task<Account> GetAccountAsyncCore()
return new Account(wagmiAccount.address, $"eip155:{wagmiAccount.chainId}");
}

protected override async Task<Account[]> GetAccountsCore()
protected override async Task<Account[]> GetAccountsAsyncCore()
{
var wagmiAccount = await WagmiInterop.GetAccountAsync();
var chainId = $"eip155:{wagmiAccount.chainId}";
Expand All @@ -137,7 +137,7 @@ private void WatchAccountTriggeredHandler(GetAccountReturnType arg)
if (_lastAccountStatus == "connected" && previousLastAccountStatus != "connected")
{
IsAccountConnected = true;
var accountConnectedEventArgs = new AccountConnectedEventArgs(GetAccountAsync, GetAccounts);
var accountConnectedEventArgs = new AccountConnectedEventArgs(GetAccountAsync, GetAccountsAsync);
OnAccountConnected(accountConnectedEventArgs);
}
else if (_lastAccountStatus == "disconnected" && previousLastAccountStatus != "disconnected")
Expand Down
Loading

0 comments on commit 8c21535

Please sign in to comment.