From b60c94f1dac654d95399716df9aba15bb34bdf82 Mon Sep 17 00:00:00 2001 From: Evgenii Alekseev <9112595+Vukiz@users.noreply.github.com> Date: Fri, 9 Jun 2023 19:03:57 +0400 Subject: [PATCH] Updated master (#137) * optional timeout introduced * clover to mrg renaming * Mirage NFT usage example (#131) * mirage nft example finished * minor renaming * remove unused method * post review changes * remove unused using statement * Webgl build errors fixes (#132) * webgl updates wip * webgl wip * minor renamings + webgl template change * build errors fixes wip * webgl websocket fixes * meta file small update * change dlls meta settings * remove unused dlls * integrate webgl websocket * log assets with extension * minor typo fix * webgl template updates * Nethereum source note added * meta files * minor safety improvement + reformat * template updated (#133) * turn off il2cpp for standalone builds because QRCoder does not compile well in this case * WebGL WalletConnect WebSocket bugfixes + updates to recent version of Unity WebGL (#135) * websocket bug fixes for webgl * triple s typo fix * single quotes * reformat and cleanup jslib * turn off il2cpp for standalone builds because QRCoder does not compile well in this case * WebGLConnect refactoring and interface generalization with WalletConnect (#136) * webgl connect interface generalization * webgl generalization * webgl generalization finished * platform related compile errors fixed * settings update * default wallet to none * Pointer_stringify changed to UTFToString * turn off debug mode * webgl hardcoded value moved to variable * Revert "Dev to Master (#134)" This reverts commit 293e57e94704c2b208fb5179ea7c74066c6b48c4. # Conflicts: # ProjectSettings/ProjectSettings.asset --------- Co-authored-by: alekseeveg Co-authored-by: Anton VIkharev --- .../Scripts/UI/ConnectionController.cs | 14 +- .../Scripts/UI/DisconnectButtonController.cs | 2 +- .../MirageSDK.Examples.WebGlLogin.asmdef | 3 +- .../WebGlLogin/WebGLConnectionController.cs | 30 +- .../Nethereum/BouncyCastle.Crypto.dll.meta | 2 +- ...t.Extensions.Logging.Abstractions.dll.meta | 2 +- .../Plugins/Nethereum/Nethereum.ABI.dll.meta | 2 +- .../Nethereum.BlockchainProcessing.dll.meta | 2 +- .../Nethereum/Nethereum.Contracts.dll.meta | 2 +- .../Plugins/Nethereum/Nethereum.Hex.dll.meta | 2 +- .../Nethereum.JsonRpc.Client.dll.meta | 2 +- .../Nethereum.JsonRpc.RpcClient.dll.meta | 2 +- .../Nethereum.Merkle.Patricia.dll.meta | 2 +- .../Nethereum/Nethereum.Model.dll.meta | 2 +- .../Plugins/Nethereum/Nethereum.RLP.dll.meta | 2 +- .../Plugins/Nethereum/Nethereum.RPC.dll.meta | 2 +- .../Nethereum/Nethereum.Signer.dll.meta | 2 +- .../Plugins/Nethereum/Nethereum.Util.dll.meta | 2 +- .../Plugins/Nethereum/Nethereum.Web3.dll.meta | 2 +- .../Nethereum/NethereumMetamask.jslib.meta | 2 +- .../IWalletConnectable.cs | 6 + .../IWalletConnectCommunicator.cs | 1 - .../IWalletConnectStatusHolder.cs | 11 + .../IWalletConnectStatusHolder.cs.meta | 3 + .../Network/WebGLAESCipher.cs | 14 +- .../Network/WebGLAESCipher.cs.meta | 0 .../StatusEvents.meta} | 0 .../StatusEvents}/SessionCreatedTransition.cs | 5 +- .../SessionCreatedTransition.cs.meta | 0 .../SessionRequestSentTransition.cs | 7 +- .../SessionRequestSentTransition.cs.meta | 0 .../StatusEvents}/TransitionDataFactory.cs | 10 +- .../TransitionDataFactory.cs.meta | 0 .../TransportConnectedTransition.cs | 5 +- .../TransportConnectedTransition.cs.meta | 0 .../WalletConnectTransitionBase.cs | 3 +- .../WalletConnectTransitionBase.cs.meta | 0 .../WalletConnectedTransition.cs | 5 +- .../WalletConnectedTransition.cs.meta | 0 .../WalletDisconnectedTransition.cs | 5 +- .../WalletDisconnectedTransition.cs.meta | 0 .../WalletConnectProtocol.cs | 23 +- .../WalletConnectSession.cs | 25 +- .../Client/Implementation/WebGLWebSocket.cs | 29 +- .../Network/Client/WebSocket.jslib | 527 +++++++++--------- .../Network/NativeWebSocketTransport.cs | 3 + .../WalletConnectSharp.Unity/WalletConnect.cs | 32 +- .../Runtime/Core/Infrastructure/IMirageSDK.cs | 1 - .../Runtime/Core/MirageSDKWrapper.cs | 5 +- Assets/MirageSDK/Runtime/Mobile/EthHandler.cs | 2 +- Assets/MirageSDK/Runtime/Provider/AoTSetup.cs | 5 +- .../Runtime/Provider/MirageSDKFactory.cs | 10 +- .../Implementation/SilentSigningProtocol.cs | 2 +- .../Implementation/ContractFunctionsWebGL.cs | 12 +- .../WebGL/Implementation/EthHandlerWebGL.cs | 38 +- .../Resources/WebGLConnectSettings.asset | 4 +- .../MirageSDK/Runtime/WebGL/WebGLConnect.cs | 306 +++++++++- .../MirageSDK/Runtime/WebGL/WebGLWrapper.cs | 258 +-------- Assets/MirageSDK/Runtime/WebGL/web3gl.jslib | 50 +- ProjectSettings/ProjectSettings.asset | 8 +- 60 files changed, 807 insertions(+), 689 deletions(-) create mode 100644 Assets/MirageSDK/Plugins/WalletConnectSharp.Core/IWalletConnectStatusHolder.cs create mode 100644 Assets/MirageSDK/Plugins/WalletConnectSharp.Core/IWalletConnectStatusHolder.cs.meta rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity => WalletConnectSharp.Core}/Network/WebGLAESCipher.cs (95%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity => WalletConnectSharp.Core}/Network/WebGLAESCipher.cs.meta (100%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events.meta => WalletConnectSharp.Core/StatusEvents.meta} (100%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/SessionCreatedTransition.cs (79%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/SessionCreatedTransition.cs.meta (100%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/SessionRequestSentTransition.cs (71%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/SessionRequestSentTransition.cs.meta (100%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/TransitionDataFactory.cs (85%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/TransitionDataFactory.cs.meta (100%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/TransportConnectedTransition.cs (79%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/TransportConnectedTransition.cs.meta (100%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/WalletConnectTransitionBase.cs (84%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/WalletConnectTransitionBase.cs.meta (100%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/WalletConnectedTransition.cs (79%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/WalletConnectedTransition.cs.meta (100%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/WalletDisconnectedTransition.cs (79%) rename Assets/MirageSDK/Plugins/{WalletConnectSharp.Unity/Events => WalletConnectSharp.Core/StatusEvents}/WalletDisconnectedTransition.cs.meta (100%) diff --git a/Assets/MirageSDK/Examples/Scripts/UI/ConnectionController.cs b/Assets/MirageSDK/Examples/Scripts/UI/ConnectionController.cs index 4a0e09ab..3a379b59 100644 --- a/Assets/MirageSDK/Examples/Scripts/UI/ConnectionController.cs +++ b/Assets/MirageSDK/Examples/Scripts/UI/ConnectionController.cs @@ -1,7 +1,7 @@ using System; using MirageSDK.Utils; using MirageSDK.WalletConnectSharp.Core; -using MirageSDK.WalletConnectSharp.Unity.Events; +using MirageSDK.WalletConnectSharp.Core.StatusEvents; using TMPro; using UnityEngine.Events; using UnityEngine.UI; @@ -30,7 +30,7 @@ private void OnEnable() SubscribeToWalletEvents(); UpdateLoginButtonState(); } - + private UnityAction GetLoginAction() { if (!Application.isEditor) @@ -43,7 +43,7 @@ private UnityAction GetLoginAction() return WalletConnect.OpenDeepLink; } } - + return () => { _qrCodeImage.UpdateQRCode(WalletConnect.ConnectURL); @@ -74,21 +74,21 @@ private void SessionStatusUpdated(WalletConnectTransitionBase walletConnectTrans private void UpdateLoginButtonState() { var status = WalletConnect.Status; - + if (status == WalletConnectStatus.Uninitialized) { return; } - + var walletConnected = status == WalletConnectStatus.WalletConnected; _sceneChooser.SetActive(walletConnected); _chooseWalletScreen.SetActive(!walletConnected); bool waitingForLoginInput = status == WalletConnectStatus.SessionRequestSent; - + _loginButton.gameObject.SetActive(waitingForLoginInput); _stateText.gameObject.SetActive(!waitingForLoginInput && !walletConnected); - + _qrCodeImage.SetImageActive(false); if (!waitingForLoginInput) diff --git a/Assets/MirageSDK/Examples/Scripts/UI/DisconnectButtonController.cs b/Assets/MirageSDK/Examples/Scripts/UI/DisconnectButtonController.cs index 31ae4aa0..a69ec843 100644 --- a/Assets/MirageSDK/Examples/Scripts/UI/DisconnectButtonController.cs +++ b/Assets/MirageSDK/Examples/Scripts/UI/DisconnectButtonController.cs @@ -1,7 +1,7 @@ using MirageSDK.Utils; using MirageSDK.WalletConnectSharp.Core; -using MirageSDK.WalletConnectSharp.Unity.Events; using Cysharp.Threading.Tasks; +using MirageSDK.WalletConnectSharp.Core.StatusEvents; using UnityEngine; using UnityEngine.UI; diff --git a/Assets/MirageSDK/Examples/Scripts/UseCases/WebGlLogin/MirageSDK.Examples.WebGlLogin.asmdef b/Assets/MirageSDK/Examples/Scripts/UseCases/WebGlLogin/MirageSDK.Examples.WebGlLogin.asmdef index 805a9f44..c10e4a9a 100644 --- a/Assets/MirageSDK/Examples/Scripts/UseCases/WebGlLogin/MirageSDK.Examples.WebGlLogin.asmdef +++ b/Assets/MirageSDK/Examples/Scripts/UseCases/WebGlLogin/MirageSDK.Examples.WebGlLogin.asmdef @@ -12,7 +12,8 @@ "WalletConnectSharp", "WalletConnectSharp.Core", "WalletConnectSharp.VersionShared", - "Unity.TextMeshPro" + "Unity.TextMeshPro", + "WalletConnectSharp.Unity" ], "includePlatforms": [ "Editor", diff --git a/Assets/MirageSDK/Examples/Scripts/UseCases/WebGlLogin/WebGLConnectionController.cs b/Assets/MirageSDK/Examples/Scripts/UseCases/WebGlLogin/WebGLConnectionController.cs index 074d8084..d3c53a40 100644 --- a/Assets/MirageSDK/Examples/Scripts/UseCases/WebGlLogin/WebGLConnectionController.cs +++ b/Assets/MirageSDK/Examples/Scripts/UseCases/WebGlLogin/WebGLConnectionController.cs @@ -1,5 +1,6 @@ using System; using MirageSDK.Data; +using MirageSDK.WalletConnectSharp.Core.StatusEvents; using MirageSDK.WebGL; using UnityEngine; @@ -33,8 +34,7 @@ private void Awake() { if (WebGLConnect != null) { - WebGLConnect.OnLoginPanelRequested += ActivatePanel; - WebGLConnect.OnConnect += HandleConnect; + WebGLConnect.SessionStatusUpdated += OnSessionStatusUpdated; _webGlLoginManager.NetworkChosen += OnNetworkChosen; _webGlLoginManager.WalletChosen += OnWalletChosen; } @@ -58,8 +58,7 @@ private void OnDisable() var webGlConnect = WebGLConnect; if (webGlConnect != null) { - webGlConnect.OnLoginPanelRequested -= ActivatePanel; - webGlConnect.OnConnect -= HandleConnect; + WebGLConnect.SessionStatusUpdated -= OnSessionStatusUpdated; } if (_webGlLoginManager != null) @@ -69,15 +68,22 @@ private void OnDisable() } } - private void ActivatePanel() + private void OnSessionStatusUpdated(WalletConnectTransitionBase obj) { - _webGlLoginManager.ShowPanel(); - } - - private void HandleConnect(WebGLWrapper provider) - { - _webGlLoginManager.HidePanel(); - _sceneChooser.SetActive(true); + switch (obj) + { + case TransportConnectedTransition transition: + { + _webGlLoginManager.ShowPanel(); + break; + } + case WalletConnectedTransition transition: + { + _webGlLoginManager.HidePanel(); + _sceneChooser.SetActive(true); + break; + } + } } private void OnNetworkChosen(NetworkName network) diff --git a/Assets/MirageSDK/Plugins/Nethereum/BouncyCastle.Crypto.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/BouncyCastle.Crypto.dll.meta index d5f10223..32e8832f 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/BouncyCastle.Crypto.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/BouncyCastle.Crypto.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Microsoft.Extensions.Logging.Abstractions.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Microsoft.Extensions.Logging.Abstractions.dll.meta index f0dbf92e..841c3499 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Microsoft.Extensions.Logging.Abstractions.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Microsoft.Extensions.Logging.Abstractions.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.ABI.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.ABI.dll.meta index cd418939..f630f612 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.ABI.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.ABI.dll.meta @@ -33,7 +33,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.BlockchainProcessing.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.BlockchainProcessing.dll.meta index fd71b79b..912bf862 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.BlockchainProcessing.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.BlockchainProcessing.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Contracts.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Contracts.dll.meta index e096d524..593a5c31 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Contracts.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Contracts.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Hex.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Hex.dll.meta index 5c410a28..34aa8e95 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Hex.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Hex.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.JsonRpc.Client.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.JsonRpc.Client.dll.meta index b3ff1ee1..5dd3a8b9 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.JsonRpc.Client.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.JsonRpc.Client.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.JsonRpc.RpcClient.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.JsonRpc.RpcClient.dll.meta index a71e2cca..ca03e015 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.JsonRpc.RpcClient.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.JsonRpc.RpcClient.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Merkle.Patricia.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Merkle.Patricia.dll.meta index 926221a4..0617df3f 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Merkle.Patricia.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Merkle.Patricia.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Model.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Model.dll.meta index 38b7365d..3282ae05 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Model.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Model.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.RLP.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.RLP.dll.meta index 40080dad..84bf0b7b 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.RLP.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.RLP.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.RPC.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.RPC.dll.meta index 7febca5b..1f54c188 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.RPC.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.RPC.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Signer.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Signer.dll.meta index 5d9efe71..1dc6b078 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Signer.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Signer.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Util.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Util.dll.meta index f73c2f2d..2eea17b9 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Util.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Util.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Web3.dll.meta b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Web3.dll.meta index af532308..9ef24162 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Web3.dll.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/Nethereum.Web3.dll.meta @@ -32,7 +32,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/Nethereum/NethereumMetamask.jslib.meta b/Assets/MirageSDK/Plugins/Nethereum/NethereumMetamask.jslib.meta index ae3d795f..b4064270 100644 --- a/Assets/MirageSDK/Plugins/Nethereum/NethereumMetamask.jslib.meta +++ b/Assets/MirageSDK/Plugins/Nethereum/NethereumMetamask.jslib.meta @@ -33,7 +33,7 @@ PluginImporter: - first: Any: second: - enabled: 1 + enabled: 0 settings: {} - first: Editor: Editor diff --git a/Assets/MirageSDK/Plugins/WalletConnect.VersionShared/IWalletConnectable.cs b/Assets/MirageSDK/Plugins/WalletConnect.VersionShared/IWalletConnectable.cs index c9dcde51..26907194 100644 --- a/Assets/MirageSDK/Plugins/WalletConnect.VersionShared/IWalletConnectable.cs +++ b/Assets/MirageSDK/Plugins/WalletConnect.VersionShared/IWalletConnectable.cs @@ -1,3 +1,4 @@ +using System; using Cysharp.Threading.Tasks; using UnityEngine; @@ -5,8 +6,13 @@ namespace MirageSDK.WalletConnect.VersionShared { public interface IWalletConnectable { + string WalletName { get; } + UniTask GetDefaultAccount(string network = null); + event Action OnAccountChanged; string SettingsFilename { get; } void Initialize(ScriptableObject settings); UniTask Connect(); + UniTask CloseSession(bool connectNewSession = true); + UniTask ReconnectSession(); } } \ No newline at end of file diff --git a/Assets/MirageSDK/Plugins/WalletConnect.VersionShared/Infrastructure/IWalletConnectCommunicator.cs b/Assets/MirageSDK/Plugins/WalletConnect.VersionShared/Infrastructure/IWalletConnectCommunicator.cs index 92ec9563..6f367456 100644 --- a/Assets/MirageSDK/Plugins/WalletConnect.VersionShared/Infrastructure/IWalletConnectCommunicator.cs +++ b/Assets/MirageSDK/Plugins/WalletConnect.VersionShared/Infrastructure/IWalletConnectCommunicator.cs @@ -18,7 +18,6 @@ public interface IWalletConnectCommunicator UniTask WalletSwitchEthChain(EthChain chainData); UniTask WalletUpdateEthChain(EthUpdateChainData chainData); UniTask EthChainId(); - string GetDefaultAccount(string network); UniTask Send(TRequest request) where TRequest : IIdentifiable diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/IWalletConnectStatusHolder.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/IWalletConnectStatusHolder.cs new file mode 100644 index 00000000..ebb39abe --- /dev/null +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/IWalletConnectStatusHolder.cs @@ -0,0 +1,11 @@ +using System; +using MirageSDK.WalletConnectSharp.Core.StatusEvents; + +namespace MirageSDK.WalletConnectSharp.Core +{ + public interface IWalletConnectStatusHolder + { + event Action SessionStatusUpdated; + WalletConnectStatus Status { get; } + } +} \ No newline at end of file diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/IWalletConnectStatusHolder.cs.meta b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/IWalletConnectStatusHolder.cs.meta new file mode 100644 index 00000000..8eef741f --- /dev/null +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/IWalletConnectStatusHolder.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 191cbe1826534714b3059b27de2eb37a +timeCreated: 1685020913 \ No newline at end of file diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/WebGLAESCipher.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/Network/WebGLAESCipher.cs similarity index 95% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/WebGLAESCipher.cs rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/Network/WebGLAESCipher.cs index 98fc37b0..6776a175 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/WebGLAESCipher.cs +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/Network/WebGLAESCipher.cs @@ -8,7 +8,7 @@ using MirageSDK.WalletConnectSharp.Core.Network; using Cysharp.Threading.Tasks; -namespace MirageSDK.WalletConnectSharp.Unity.Network +namespace MirageSDK.WalletConnectSharp.Core.Network { public class WebGlAESCipher : ICipher { @@ -29,7 +29,7 @@ public UniTask EncryptWithKey(byte[] key, string message, Enco cipher.Mode = CipherMode.CBC; cipher.Padding = PaddingMode.PKCS7; cipher.KeySize = 256; - + var iv = cipher.IV; using (var cs = new CryptoStream(ms, cipher.CreateEncryptor(key, iv), @@ -43,7 +43,7 @@ public UniTask EncryptWithKey(byte[] key, string message, Enco using (var hmac = new HMACSHA256(key)) { hmac.Initialize(); - + var toSign = new byte[iv.Length + encryptedContent.Length]; //copy our 2 array into one @@ -51,7 +51,7 @@ public UniTask EncryptWithKey(byte[] key, string message, Enco Buffer.BlockCopy(iv, 0, toSign, encryptedContent.Length, iv.Length); var signature = hmac.ComputeHash(toSign); - + var ivHex = iv.ToHex(); var dataHex = encryptedContent.ToHex(); var hmacHex = signature.ToHex(); @@ -83,11 +83,11 @@ public UniTask DecryptWithKey(byte[] key, EncryptedPayload encryptedData hmac.Initialize(); var toSign = new byte[iv.Length + rawData.Length]; - + //copy our 2 array into one Buffer.BlockCopy(rawData, 0, toSign, 0,rawData.Length); Buffer.BlockCopy(iv, 0, toSign, rawData.Length, iv.Length); - + var signature = hmac.ComputeHash(toSign); if (!signature.SequenceEqual(hmacReceived)) @@ -118,7 +118,7 @@ public UniTask DecryptWithKey(byte[] key, EncryptedPayload encryptedData do { read = cs.Read(buffer, 0, buffer.Length); - + if (read > 0) { sink.Write(buffer, 0, read); diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/WebGLAESCipher.cs.meta b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/Network/WebGLAESCipher.cs.meta similarity index 100% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/WebGLAESCipher.cs.meta rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/Network/WebGLAESCipher.cs.meta diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events.meta b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents.meta similarity index 100% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events.meta rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents.meta diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/SessionCreatedTransition.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/SessionCreatedTransition.cs similarity index 79% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/SessionCreatedTransition.cs rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/SessionCreatedTransition.cs index 922b0d19..01feade3 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/SessionCreatedTransition.cs +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/SessionCreatedTransition.cs @@ -1,14 +1,13 @@ using MirageSDK.WalletConnect.VersionShared.Infrastructure; -using MirageSDK.WalletConnectSharp.Core; -namespace MirageSDK.WalletConnectSharp.Unity.Events +namespace MirageSDK.WalletConnectSharp.Core.StatusEvents { public class SessionCreatedTransition : WalletConnectTransitionBase { public SessionCreatedTransition(IWalletConnectTransitionDataProvider transitionDataProvider, WalletConnectStatus previousStatus, WalletConnectStatus newStatus) : base(transitionDataProvider, previousStatus, newStatus) { - + } } } \ No newline at end of file diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/SessionCreatedTransition.cs.meta b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/SessionCreatedTransition.cs.meta similarity index 100% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/SessionCreatedTransition.cs.meta rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/SessionCreatedTransition.cs.meta diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/SessionRequestSentTransition.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/SessionRequestSentTransition.cs similarity index 71% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/SessionRequestSentTransition.cs rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/SessionRequestSentTransition.cs index a1ee5856..00d9e156 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/SessionRequestSentTransition.cs +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/SessionRequestSentTransition.cs @@ -1,14 +1,13 @@ using MirageSDK.WalletConnect.VersionShared.Infrastructure; -using MirageSDK.WalletConnectSharp.Core; -namespace MirageSDK.WalletConnectSharp.Unity.Events +namespace MirageSDK.WalletConnectSharp.Core.StatusEvents { public class SessionRequestSentTransition : WalletConnectTransitionBase { - public SessionRequestSentTransition(IWalletConnectTransitionDataProvider transitionDataProvider, WalletConnectStatus previousStatus, WalletConnectStatus newStatus) + public SessionRequestSentTransition(IWalletConnectTransitionDataProvider transitionDataProvider, WalletConnectStatus previousStatus, WalletConnectStatus newStatus) : base(transitionDataProvider, previousStatus, newStatus) { - + } } } \ No newline at end of file diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/SessionRequestSentTransition.cs.meta b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/SessionRequestSentTransition.cs.meta similarity index 100% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/SessionRequestSentTransition.cs.meta rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/SessionRequestSentTransition.cs.meta diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/TransitionDataFactory.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/TransitionDataFactory.cs similarity index 85% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/TransitionDataFactory.cs rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/TransitionDataFactory.cs index 842387c4..d0fd2031 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/TransitionDataFactory.cs +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/TransitionDataFactory.cs @@ -1,23 +1,23 @@ using System; -using MirageSDK.WalletConnectSharp.Core; +using MirageSDK.WalletConnect.VersionShared.Infrastructure; -namespace MirageSDK.WalletConnectSharp.Unity.Events +namespace MirageSDK.WalletConnectSharp.Core.StatusEvents { public static class TransitionDataFactory { public static WalletConnectTransitionBase CreateTransitionObj(WalletConnectStatus previousStatus, - WalletConnectStatus newStatus, WalletConnectSession session) + WalletConnectStatus newStatus, IWalletConnectTransitionDataProvider session) { if (previousStatus == WalletConnectStatus.Uninitialized) { return new SessionCreatedTransition(session, previousStatus, newStatus); } - + switch (newStatus) { case WalletConnectStatus.DisconnectedSessionCached: case WalletConnectStatus.DisconnectedNoSession: - { + { return new WalletDisconnectedTransition(session, previousStatus, newStatus); } case WalletConnectStatus.TransportConnected: diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/TransitionDataFactory.cs.meta b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/TransitionDataFactory.cs.meta similarity index 100% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/TransitionDataFactory.cs.meta rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/TransitionDataFactory.cs.meta diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/TransportConnectedTransition.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/TransportConnectedTransition.cs similarity index 79% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/TransportConnectedTransition.cs rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/TransportConnectedTransition.cs index 40ca9f3d..d6ac6094 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/TransportConnectedTransition.cs +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/TransportConnectedTransition.cs @@ -1,14 +1,13 @@ using MirageSDK.WalletConnect.VersionShared.Infrastructure; -using MirageSDK.WalletConnectSharp.Core; -namespace MirageSDK.WalletConnectSharp.Unity.Events +namespace MirageSDK.WalletConnectSharp.Core.StatusEvents { public class TransportConnectedTransition : WalletConnectTransitionBase { public TransportConnectedTransition(IWalletConnectTransitionDataProvider transitionDataProvider, WalletConnectStatus previousStatus, WalletConnectStatus newStatus) : base(transitionDataProvider, previousStatus, newStatus) { - + } } } \ No newline at end of file diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/TransportConnectedTransition.cs.meta b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/TransportConnectedTransition.cs.meta similarity index 100% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/TransportConnectedTransition.cs.meta rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/TransportConnectedTransition.cs.meta diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletConnectTransitionBase.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletConnectTransitionBase.cs similarity index 84% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletConnectTransitionBase.cs rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletConnectTransitionBase.cs index 4c794c52..36433c46 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletConnectTransitionBase.cs +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletConnectTransitionBase.cs @@ -1,7 +1,6 @@ using MirageSDK.WalletConnect.VersionShared.Infrastructure; -using MirageSDK.WalletConnectSharp.Core; -namespace MirageSDK.WalletConnectSharp.Unity.Events +namespace MirageSDK.WalletConnectSharp.Core.StatusEvents { public class WalletConnectTransitionBase { diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletConnectTransitionBase.cs.meta b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletConnectTransitionBase.cs.meta similarity index 100% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletConnectTransitionBase.cs.meta rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletConnectTransitionBase.cs.meta diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletConnectedTransition.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletConnectedTransition.cs similarity index 79% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletConnectedTransition.cs rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletConnectedTransition.cs index b975a237..291858f2 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletConnectedTransition.cs +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletConnectedTransition.cs @@ -1,14 +1,13 @@ using MirageSDK.WalletConnect.VersionShared.Infrastructure; -using MirageSDK.WalletConnectSharp.Core; -namespace MirageSDK.WalletConnectSharp.Unity.Events +namespace MirageSDK.WalletConnectSharp.Core.StatusEvents { public class WalletConnectedTransition : WalletConnectTransitionBase { public WalletConnectedTransition(IWalletConnectTransitionDataProvider transitionDataProvider, WalletConnectStatus previousStatus, WalletConnectStatus newStatus) : base(transitionDataProvider, previousStatus, newStatus) { - + } } } \ No newline at end of file diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletConnectedTransition.cs.meta b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletConnectedTransition.cs.meta similarity index 100% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletConnectedTransition.cs.meta rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletConnectedTransition.cs.meta diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletDisconnectedTransition.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletDisconnectedTransition.cs similarity index 79% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletDisconnectedTransition.cs rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletDisconnectedTransition.cs index 5d0a5ee1..a99f86f9 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletDisconnectedTransition.cs +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletDisconnectedTransition.cs @@ -1,14 +1,13 @@ using MirageSDK.WalletConnect.VersionShared.Infrastructure; -using MirageSDK.WalletConnectSharp.Core; -namespace MirageSDK.WalletConnectSharp.Unity.Events +namespace MirageSDK.WalletConnectSharp.Core.StatusEvents { public class WalletDisconnectedTransition : WalletConnectTransitionBase { public WalletDisconnectedTransition(IWalletConnectTransitionDataProvider transitionDataProvider, WalletConnectStatus previousStatus, WalletConnectStatus newStatus) : base(transitionDataProvider, previousStatus, newStatus) { - + } } } \ No newline at end of file diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletDisconnectedTransition.cs.meta b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletDisconnectedTransition.cs.meta similarity index 100% rename from Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Events/WalletDisconnectedTransition.cs.meta rename to Assets/MirageSDK/Plugins/WalletConnectSharp.Core/StatusEvents/WalletDisconnectedTransition.cs.meta diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/WalletConnectProtocol.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/WalletConnectProtocol.cs index 7f0bf4f0..4930dc79 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/WalletConnectProtocol.cs +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/WalletConnectProtocol.cs @@ -112,13 +112,16 @@ protected WalletConnectProtocol(SavedSession savedSession, ITransport transport BridgeUrl = savedSession.BridgeURL; SetTransport(transport); - cipher = cipher ?? new AESCipher(); + if (cipher == null) + { + cipher = CreateCipher(); + } _cipher = cipher; KeyRaw = savedSession.KeyRaw; - //Convert hex + //Convert hex Key = savedSession.Key; PeerId = savedSession.PeerID; @@ -160,11 +163,23 @@ protected WalletConnectProtocol(ITransport transport = null, SetTransport(transport); - cipher = cipher ?? new AESCipher(); + if (cipher == null) + { + cipher = CreateCipher(); + } _cipher = cipher; } + public static ICipher CreateCipher() + { + #if UNITY_WEBGL && !UNITY_EDITOR + return new WebGlAESCipher(); + #else + return new AESCipher(); + #endif + } + protected async UniTask OpenTransport() { await Transport.Open(BridgeUrl); @@ -274,7 +289,7 @@ protected async UniTask SendRequest(object requestObject, string topic, bool sil protected static bool IsSilent(object requestObj) { - return requestObj is JsonRpcRequest request + return requestObj is JsonRpcRequest request && (request.Method.StartsWith("wc_") || !SigningMethods.Contains(request.Method)); } diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/WalletConnectSession.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/WalletConnectSession.cs index ceb9f07e..8695e2e3 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/WalletConnectSession.cs +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Core/WalletConnectSession.cs @@ -116,7 +116,7 @@ public WalletConnectSession(ClientMeta clientMeta, string bridgeUrl = null, ITra BridgeUrl = bridgeUrl; BridgeUrl = DefaultBridge.GetBridgeUrl(BridgeUrl); - + _sessionId = Guid.NewGuid().ToString(); GenerateKey(); @@ -134,7 +134,7 @@ private void GenerateKey() KeyRaw = secret; - //Convert hex + //Convert hex Key = KeyRaw.ToHex().ToLower(); } @@ -143,10 +143,12 @@ public async UniTask ConnectSession() var prevStatus = Status; Connecting = true; + Debug.Log("Connecting session"); try { if (!TransportConnected) { + Debug.Log("Opening transport"); await OpenTransport(); } else @@ -154,16 +156,19 @@ public async UniTask ConnectSession() Debug.Log("Transport already connected. No need to setup"); } + Debug.Log("SubscribeAndListenToTopic started"); await SubscribeAndListenToTopic(_sessionId); - + Debug.Log("SubscribeAndListenToTopic finished"); + WCSessionData result; _handshakeTopic = Guid.NewGuid().ToString(); ListenToTopic(_handshakeTopic); SubscribeForSessionResponse(); - + if (prevStatus == WalletConnectStatus.DisconnectedNoSession) { + Debug.Log("CreateSession started"); result = await CreateSession(_handshakeTopic); Connecting = false; OnSessionCreated?.Invoke(); @@ -355,7 +360,7 @@ void HandleSendResponse(object sender, JsonRpcResponseEvent @event) } EventDelegator.ListenForResponse(request.ID, HandleSendResponse); - + await SendRequest(request, PeerId, IsSilent(request)); OnSend?.Invoke(); @@ -546,11 +551,11 @@ public SavedSession GetSavedSession() return new SavedSession(_sessionId, BridgeUrl, Key, KeyRaw, PeerId, NetworkId, Accounts, ChainId, DappMetadata, WalletMetadata); } - - //network argument is not used because WC1 - //only supports Ethereum network but still kept here to + + //network argument is not used because WC1 + //only supports Ethereum network but still kept here to //support unified interface with WC2 - public string GetDefaultAccount(string network = null) + public UniTask GetDefaultAccount(string network = null) { var activeSessionAccount = Accounts[0]; @@ -559,7 +564,7 @@ public string GetDefaultAccount(string network = null) Debug.LogError("Account is null"); } - return activeSessionAccount; + return UniTask.FromResult(activeSessionAccount); } } } \ No newline at end of file diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/Client/Implementation/WebGLWebSocket.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/Client/Implementation/WebGLWebSocket.cs index 1f627450..05a569a7 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/Client/Implementation/WebGLWebSocket.cs +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/Client/Implementation/WebGLWebSocket.cs @@ -1,4 +1,5 @@ #if UNITY_WEBGL && !UNITY_EDITOR +using System; using System.Runtime.InteropServices; using System.Collections.Generic; using MirageSDK.WalletConnectSharp.Unity.Network.Client.Data; @@ -29,6 +30,8 @@ public class WebGLWebSocket : IWebSocket [DllImport("__Internal")] public static extern int WebSocketGetState(int instanceId); + private readonly TimeSpan SocketOpenCheckTimeDelay = TimeSpan.FromMilliseconds(50); + private readonly int _instanceId; public event WebSocketOpenEventHandler OnOpen; @@ -74,10 +77,10 @@ public WebGLWebSocket(string url, List subprotocols) int allocatedSocketId = WebGLWebSocketNativeBridge.WebSocketAllocate(url); WebGLWebSocketNativeBridge.Instances.Add(_instanceId, this); - foreach (var subprotocol in subprotocols) - { - WebGLWebSocketNativeBridge.WebSocketAddSubProtocol(_instanceId, subprotocol); - } + foreach (var subprotocol in subprotocols) + { + WebGLWebSocketNativeBridge.WebSocketAddSubProtocol(_instanceId, subprotocol); + } _instanceId = allocatedSocketId; } @@ -92,23 +95,31 @@ public int GetInstanceId() return _instanceId; } - public UniTask Connect() + public async UniTask Connect() { + UnityEngine.Debug.Log($"Calling WebSocket connect for {_instanceId}"); var ret = WebSocketConnect(_instanceId); + UnityEngine.Debug.Log($"WebSocketConnect returned {ret}"); if (ret < 0) { throw WebSocketHelpers.GetErrorMessageFromCode(ret, null); } - return UniTask.CompletedTask; + while(State != WebSocketState.Open) + { + await UniTask.Delay(SocketOpenCheckTimeDelay); + } + + UnityEngine.Debug.Log($"WebSocketConnect await finished"); + OnOpen?.Invoke(); } public UniTask Close() { if (State == WebSocketState.Open) { - return Close(WebSocketCloseCode.Abnormal); + return CloseSocket(); } return UniTask.CompletedTask; @@ -116,7 +127,7 @@ public UniTask Close() public void CancelConnection() {} - public UniTask Close(WebSocketCloseCode code = WebSocketCloseCode.Normal, string reason = null) + private UniTask CloseSocket(WebSocketCloseCode code = WebSocketCloseCode.Normal, string reason = null) { var ret = WebSocketClose(_instanceId, (int)code, reason); @@ -189,7 +200,7 @@ public void DispatchMessageQueue() public void DelegateOnOpenEvent() { - OnOpen?.Invoke(); + } public void DelegateOnMessageEvent(byte[] data) diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/Client/WebSocket.jslib b/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/Client/WebSocket.jslib index e3e0d3a1..36aa5a7a 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/Client/WebSocket.jslib +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/Client/WebSocket.jslib @@ -1,330 +1,351 @@ var LibraryWebSocket = { - $webSocketState: { - /* - * Map of instances - * - * Instance structure: - * { - * url: string, - * ws: WebSocket - * } - */ - instances: {}, - - /* Last instance ID */ - lastId: 0, - - /* Event listeners */ - onOpen: null, - onMessage: null, - onError: null, - onClose: null, - - /* Debug mode */ - debug: false - }, - - /** - * Set onOpen callback - * - * @param callback Reference to C# static function - */ - WebSocketSetOnOpen: function(callback) { - - this.$webSocketState.onOpen = callback; - - }, - - /** - * Set onMessage callback - * - * @param callback Reference to C# static function - */ - WebSocketSetOnMessage: function(callback) { + $webSocketState: { + /* + * Map of instances + * + * Instance structure: + * { + * url: string, + * ws: WebSocket + * } + */ + instances: {}, + + /* Last instance ID */ + lastId: 0, + + /* Event listeners */ + onOpen: null, + onMessage: null, + onError: null, + onClose: null, + + /* Debug mode */ + debug: false + }, + + /** + * Set onOpen callback + * + * @param callback Reference to C# static function + */ + WebSocketSetOnOpen: function (callback) { + + webSocketState.onOpen = callback; + + }, + + /** + * Set onMessage callback + * + * @param callback Reference to C# static function + */ + WebSocketSetOnMessage: function (callback) { + + webSocketState.onMessage = callback; + + }, + + /** + * Set onError callback + * + * @param callback Reference to C# static function + */ + WebSocketSetOnError: function (callback) { + + webSocketState.onError = callback; + + }, + + /** + * Set onClose callback + * + * @param callback Reference to C# static function + */ + WebSocketSetOnClose: function (callback) { - this.$webSocketState.onMessage = callback; + webSocketState.onClose = callback; - }, + }, - /** - * Set onError callback - * - * @param callback Reference to C# static function - */ - WebSocketSetOnError: function(callback) { + /** + * Allocate new WebSocket instance struct + * + * @param url Server URL + */ + WebSocketAllocate: function (url) { - this.$webSocketState.onError = callback; + var urlStr = UTF8ToString(url); + var id = webSocketState.lastId++; - }, + webSocketState.instances[id] = { + subprotocols: [], + url: urlStr, + ws: null + }; - /** - * Set onClose callback - * - * @param callback Reference to C# static function - */ - WebSocketSetOnClose: function(callback) { + return id; - this.$webSocketState.onClose = callback; + }, - }, + /** + * Add subprotocol to instance + * + * @param instanceId Instance ID + * @param subprotocol Subprotocol name to add to instance + */ + WebSocketAddSubProtocol: function (instanceId, subprotocol) { + + var subprotocolStr = UTF8ToString(subprotocol); + webSocketState.instances[instanceId].subprotocols.push(subprotocolStr); + + }, - /** - * Allocate new WebSocket instance struct - * - * @param url Server URL - */ - WebSocketAllocate: function(url) { + /** + * Remove reference to WebSocket instance + * + * If socket is not closed function will close it but onClose event will not be emitted because + * this function should be invoked by C# WebSocket destructor. + * + * @param instanceId Instance ID + */ + WebSocketFree: function (instanceId) { - var urlStr = UTF8ToString(url); - var id = webSocketState.lastId++; + var instance = webSocketState.instances[instanceId]; - webSocketState.instances[id] = { - subprotocols: [], - url: urlStr, - ws: null - }; + if (!instance) return 0; - return id; + // Close if not closed + if (instance.ws !== null && instance.ws.readyState < 2) + instance.ws.close(); - }, + // Remove reference + delete webSocketState.instances[instanceId]; - /** - * Add subprotocol to instance - * - * @param instanceId Instance ID - * @param subprotocol Subprotocol name to add to instance - */ - WebSocketAddSubProtocol: function(instanceId, subprotocol) { + return 0; - var subprotocolStr = UTF8ToString(subprotocol); - webSocketState.instances[instanceId].subprotocols.push(subprotocolStr); + }, - }, + /** + * Connect WebSocket to the server + * + * @param instanceId Instance ID + */ + WebSocketConnect: function (instanceId) { - /** - * Remove reference to WebSocket instance - * - * If socket is not closed function will close it but onClose event will not be emitted because - * this function should be invoked by C# WebSocket destructor. - * - * @param instanceId Instance ID - */ - WebSocketFree: function(instanceId) { + var instance = webSocketState.instances[instanceId]; + if (!instance) + { + console.error("WebSocketConnect: instance not found"); + return -1; + } - var instance = webSocketState.instances[instanceId]; + if (instance.ws !== null) + { + console.error("WebSocketConnect: ws already created"); + return -2; + } - if (!instance) return 0; + instance.ws = new WebSocket(instance.url); - // Close if not closed - if (instance.ws && instance.ws.readyState < 2) - instance.ws.close(); + instance.ws.binaryType = 'arraybuffer'; - // Remove reference - delete webSocketState.instances[instanceId]; + instance.ws.onopen = function () { - return 0; + if (webSocketState.debug) + console.log("[JSLIB WebSocket] Connected."); - }, + if (webSocketState.onOpen) + dynCall('vi', webSocketState.onOpen, [instanceId]); - /** - * Connect WebSocket to the server - * - * @param instanceId Instance ID - */ - WebSocketConnect: function(instanceId) { + }; - var instance = webSocketState.instances[instanceId]; - if (!instance) return -1; + instance.ws.onmessage = function (ev) { - if (instance.ws !== null) - return -2; + if (webSocketState.debug) + console.log("[JSLIB WebSocket] Received message:", ev.data); - instance.ws = new WebSocket(instance.url, instance.subprotocols); + if (webSocketState.onMessage === null) + return; - instance.ws.binaryType = 'arraybuffer'; + if (ev.data instanceof ArrayBuffer) { - instance.ws.onopen = function() { + var dataBuffer = new Uint8Array(ev.data); - if (webSocketState.debug) - console.log("[JSLIB WebSocket] Connected."); + var buffer = _malloc(dataBuffer.length); + HEAPU8.set(dataBuffer, buffer); - if (webSocketState.onOpen) - Runtime.dynCall('vi', webSocketState.onOpen, [ instanceId ]); + try { + dynCall('viii', webSocketState.onMessage, [instanceId, buffer, dataBuffer.length]); + } finally { + _free(buffer); + } - }; + } else if (typeof ev.data == 'string') { + var arrBuffer = new ArrayBuffer(ev.data.length) + var dataBuffer = new Uint8Array(arrBuffer) + for (var i = 0, len = ev.data.length; i < len; i++) { + dataBuffer[i] = ev.data.charCodeAt(i) + } + var buffer = _malloc(dataBuffer.length); + HEAPU8.set(dataBuffer, buffer); + try { + dynCall('viii', webSocketState.onMessage, [instanceId, buffer, dataBuffer.length]); + } finally { + _free(buffer); + } + } - instance.ws.onmessage = function(ev) { + }; - if (webSocketState.debug) - console.log("[JSLIB WebSocket] Received message:", ev.data); + instance.ws.onerror = function (ev) { - if (webSocketState.onMessage === null) - return; + if (webSocketState.debug) + console.log("[JSLIB WebSocket] Error occured."); - if (ev.data instanceof ArrayBuffer) { + if (webSocketState.onError) { - var dataBuffer = new Uint8Array(ev.data); + var msg = "WebSocket error."; + var msgBytes = lengthBytesUTF8(msg); + var msgBuffer = _malloc(msgBytes + 1); + stringToUTF8(msg, msgBuffer, msgBytes); - var buffer = _malloc(dataBuffer.length); - HEAPU8.set(dataBuffer, buffer); + try { + dynCall('vii', webSocketState.onError, [instanceId, msgBuffer]); + } finally { + _free(msgBuffer); + } - try { - Runtime.dynCall('viii', webSocketState.onMessage, [ instanceId, buffer, dataBuffer.length ]); - } finally { - _free(buffer); - } + } - } else { - var dataBuffer = textEncoder.encode(ev.data); + }; - var buffer = _malloc(dataBuffer.length); - HEAPU8.set(dataBuffer, buffer); + instance.ws.onclose = function (ev) { - try { - Runtime.dynCall('viii', webSocketState.onMessage, [ instanceId, buffer, dataBuffer.length ]); - } finally { - _free(buffer); - } + if (webSocketState.debug) + console.log("[JSLIB WebSocket] Closed."); - } + if (webSocketState.onClose) + dynCall('vii', webSocketState.onClose, [instanceId, ev.code]); - }; + delete instance.ws; - instance.ws.onerror = function(ev) { + }; - if (webSocketState.debug) - console.log("[JSLIB WebSocket] Error occured."); + return 0; - if (webSocketState.onError) { + }, - var msg = "WebSocket error."; - var msgBytes = lengthBytesUTF8(msg); - var msgBuffer = _malloc(msgBytes + 1); - stringToUTF8(msg, msgBuffer, msgBytes); + /** + * Close WebSocket connection + * + * @param instanceId Instance ID + * @param code Close status code + * @param reasonPtr Pointer to reason string + */ + WebSocketClose: function (instanceId, code, reasonPtr) { - try { - Runtime.dynCall('vii', webSocketState.onError, [ instanceId, msgBuffer ]); - } finally { - _free(msgBuffer); - } + var instance = webSocketState.instances[instanceId]; + if (!instance) return -1; - } + if (instance.ws === null) + return -3; - }; + if (instance.ws.readyState === 2) + return -4; - instance.ws.onclose = function(ev) { + if (instance.ws.readyState === 3) + return -5; - if (webSocketState.debug) - console.log("[JSLIB WebSocket] Closed."); + var reason = (reasonPtr ? UTF8ToString(reasonPtr) : undefined); - if (webSocketState.onClose) - Runtime.dynCall('vii', webSocketState.onClose, [ instanceId, ev.code ]); + try { + instance.ws.close(code, reason); + } catch (err) { + return -7; + } - delete instance.ws; + return 0; - }; + }, - return 0; + /** + * Send message over WebSocket + * + * @param instanceId Instance ID + * @param bufferPtr Pointer to the message buffer + * @param length Length of the message in the buffer + */ + WebSocketSend: function (instanceId, bufferPtr, length) { - }, + var instance = webSocketState.instances[instanceId]; + if (!instance) return -1; - /** - * Close WebSocket connection - * - * @param instanceId Instance ID - * @param code Close status code - * @param reasonPtr Pointer to reason string - */ - WebSocketClose: function(instanceId, code, reasonPtr) { + if (instance.ws === null) + return -3; - var instance = webSocketState.instances[instanceId]; - if (!instance) return -1; + if (instance.ws.readyState !== 1) + return -6; - if (!instance.ws) - return -3; + instance.ws.send(HEAPU8.buffer.slice(bufferPtr, bufferPtr + length)); - if (instance.ws.readyState === 2) - return -4; + return 0; - if (instance.ws.readyState === 3) - return -5; + }, - var reason = ( reasonPtr ? UTF8ToString(reasonPtr) : undefined ); + /** + * Send text message over WebSocket + * + * @param instanceId Instance ID + * @param bufferPtr Pointer to the message buffer + * @param length Length of the message in the buffer + */ + WebSocketSendText: function (instanceId, message) { - try { - instance.ws.close(code, reason); - } catch(err) { - return -7; - } + var instance = webSocketState.instances[instanceId]; + if (!instance) + { + console.error(`Instance not found for instanceId: ${instanceId}`); + return -1; + } - return 0; + if (!instance.ws) + { + console.error(`WebSocket not initialized for instanceId: ${instanceId}`); + return -3; + } - }, + if (instance.ws.readyState !== 1) + { + console.log(`WebSocket readyState not equal to 1 for instanceId: ${instanceId}. Current readyState: ${instance.ws.readyState}`); + return -6; + } - /** - * Send message over WebSocket - * - * @param instanceId Instance ID - * @param bufferPtr Pointer to the message buffer - * @param length Length of the message in the buffer - */ - WebSocketSend: function(instanceId, bufferPtr, length) { + var utf8Message = UTF8ToString(message); + console.log(`Sending message : ${utf8Message} for instanceId: ${instanceId}`); + instance.ws.send(utf8Message); + console.log(`Message sent successfully for instanceId: ${instanceId}`); - var instance = webSocketState.instances[instanceId]; - if (!instance) return -1; + return 0; - if (!instance.ws) - return -3; + }, - if (instance.ws.readyState !== 1) - return -6; + /** + * Return WebSocket readyState + * + * @param instanceId Instance ID + */ + WebSocketGetState: function (instanceId) { + + var instance = webSocketState.instances[instanceId]; + if (!instance) return -1; - instance.ws.send(HEAPU8.buffer.slice(bufferPtr, bufferPtr + length)); + if (instance.ws) + return instance.ws.readyState; + else + return 3; - return 0; - - }, - - /** - * Send text message over WebSocket - * - * @param instanceId Instance ID - * @param bufferPtr Pointer to the message buffer - * @param length Length of the message in the buffer - */ - WebSocketSendText: function(instanceId, message) { - - var instance = webSocketState.instances[instanceId]; - if (!instance) return -1; - - if (!instance.ws) - return -3; - - if (instance.ws.readyState !== 1) - return -6; - - instance.ws.send(UTF8ToString(message)); - - return 0; - - }, - - /** - * Return WebSocket readyState - * - * @param instanceId Instance ID - */ - WebSocketGetState: function(instanceId) { - - var instance = webSocketState.instances[instanceId]; - if (!instance) return -1; - - if (instance.ws) - return instance.ws.readyState; - else - return 3; - - } + } }; diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/NativeWebSocketTransport.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/NativeWebSocketTransport.cs index cbc6c859..cb3e9775 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/NativeWebSocketTransport.cs +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/Network/NativeWebSocketTransport.cs @@ -112,13 +112,16 @@ public async UniTask SendMessage(NetworkMessage message) { if (!Connected) { + Debug.Log($"Enqueuing message: {message} and opening socket"); _queuedMessages.Enqueue(message); await OpenSocket(); } else { var finalJson = JsonConvert.SerializeObject(message); + Debug.Log($"SendText called for: {finalJson}"); await _client.SendText(finalJson); + Debug.Log($"SendText finished for: {finalJson}"); } } diff --git a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/WalletConnect.cs b/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/WalletConnect.cs index d0c0d02a..72cd72b3 100644 --- a/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/WalletConnect.cs +++ b/Assets/MirageSDK/Plugins/WalletConnectSharp.Unity/WalletConnect.cs @@ -9,17 +9,16 @@ using MirageSDK.WalletConnect.VersionShared.Infrastructure; using MirageSDK.WalletConnect.VersionShared.Models; using MirageSDK.WalletConnect.VersionShared.Models.DeepLink; -using MirageSDK.WalletConnect.VersionShared.Models.DeepLink.Helpers; using MirageSDK.WalletConnect.VersionShared.Models.Ethereum; using MirageSDK.WalletConnect.VersionShared.Models.Ethereum.Types; using MirageSDK.WalletConnect.VersionShared.Utils; using MirageSDK.WalletConnectSharp.Core; using MirageSDK.WalletConnectSharp.Core.Models; using MirageSDK.WalletConnectSharp.Core.Network; -using MirageSDK.WalletConnectSharp.Unity.Events; using MirageSDK.WalletConnectSharp.Unity.Network; using MirageSDK.WalletConnectSharp.Unity.Utils; using Cysharp.Threading.Tasks; +using MirageSDK.WalletConnectSharp.Core.StatusEvents; using UnityEngine; using Logger = AnkrSDK.InternalUtils.Logger; @@ -27,12 +26,14 @@ namespace MirageSDK.WalletConnectSharp.Unity { - public class WalletConnect : IWalletConnectable, IWalletConnectGenericRequester, IWalletConnectCommunicator, + public class WalletConnect : IWalletConnectable, IWalletConnectStatusHolder, + IWalletConnectGenericRequester, IWalletConnectCommunicator, IQuittable, IPausable, IUpdatable { private const string SettingsFilenameString = "WalletConnectSettings"; public event Action SessionStatusUpdated; public event Action OnSend; + public event Action OnAccountChanged; public event Action OnChainChanged; public bool CanSendRequests => _session?.CanSendRequests ?? false; @@ -66,6 +67,15 @@ public ClientMeta WalletMetadata } } + public string WalletName + { + get + { + CheckIfSessionCreated(); + return _session?.WalletMetadata?.Name; + } + } + public string[] Accounts { get @@ -167,7 +177,7 @@ public async UniTask OnApplicationPause(bool pauseStatus) await Connect(); } } - + public async UniTask ReconnectSession() { SessionSaveHandler.ClearSession(); @@ -179,9 +189,15 @@ public async UniTask ReconnectSession() public async UniTask Connect() { + //disabled for now because of CORS policy limitation + //we need to change the CORS policy in + //'https://usage-stats.game.ankr.com/collect + //endpoint to make it work in WebGL + #if !UNITY_WEBGL TryLoadOwnVersionKnowledge(); LogVersion(); - + #endif + var savedSession = SessionSaveHandler.GetSavedSession(); if (_session != null) @@ -293,10 +309,10 @@ public UniTask EthChainId() return _session.EthChainId(); } - //network argument is not used because WC1 - //only supports Ethereum network but still kept here to + //network argument is not used because WC1 + //only supports Ethereum network but still kept here to //support unified interface with WC2 - public string GetDefaultAccount(string network = null) + public UniTask GetDefaultAccount(string network = null) { CheckIfSessionCreated(); return _session.GetDefaultAccount(network); diff --git a/Assets/MirageSDK/Runtime/Core/Infrastructure/IMirageSDK.cs b/Assets/MirageSDK/Runtime/Core/Infrastructure/IMirageSDK.cs index 7aab720c..8e2baf55 100644 --- a/Assets/MirageSDK/Runtime/Core/Infrastructure/IMirageSDK.cs +++ b/Assets/MirageSDK/Runtime/Core/Infrastructure/IMirageSDK.cs @@ -5,7 +5,6 @@ namespace MirageSDK.Core.Infrastructure public interface IMirageSDK { IEthHandler Eth { get; } - IWalletHandler WalletHandler { get; } IContract GetContract(string contractAddress, string contractABI); IContract GetContract(ContractInformationSO contractInfo); IContractEventSubscriber CreateSubscriber(string wsUrl); diff --git a/Assets/MirageSDK/Runtime/Core/MirageSDKWrapper.cs b/Assets/MirageSDK/Runtime/Core/MirageSDKWrapper.cs index 3940c126..8f436473 100644 --- a/Assets/MirageSDK/Runtime/Core/MirageSDKWrapper.cs +++ b/Assets/MirageSDK/Runtime/Core/MirageSDKWrapper.cs @@ -8,19 +8,16 @@ namespace MirageSDK.Core public class MirageSDKWrapper : IMirageSDK { private readonly IContractFunctions _contractFunctions; - public IWalletHandler WalletHandler { get; } public IEthHandler Eth { get; } public ISilentSigningHandler SilentSigningHandler { get; } public MirageSDKWrapper( IContractFunctions contractFunctions, IEthHandler eth, - IWalletHandler disconnectHandler, ISilentSigningHandler silentSigningHandler = null ) { _contractFunctions = contractFunctions; - WalletHandler = disconnectHandler; SilentSigningHandler = silentSigningHandler; Eth = eth; } @@ -36,7 +33,7 @@ public IContract GetContract(ContractInformationSO contractInfo) { throw new InvalidDataException($"Invalid contract data: {contractInfo}"); } - + return new Contract(Eth, _contractFunctions, contractInfo.ContractAddress, contractInfo.ABI); } diff --git a/Assets/MirageSDK/Runtime/Mobile/EthHandler.cs b/Assets/MirageSDK/Runtime/Mobile/EthHandler.cs index 63344166..da75bfa7 100644 --- a/Assets/MirageSDK/Runtime/Mobile/EthHandler.cs +++ b/Assets/MirageSDK/Runtime/Mobile/EthHandler.cs @@ -74,7 +74,7 @@ public UniTask GetDefaultAccount() throw new Exception("Application is not linked to wallet"); } - return UniTask.FromResult(_walletConnect.GetDefaultAccount()); + return _walletConnect.GetDefaultAccount(); } public UniTask GetChainId() diff --git a/Assets/MirageSDK/Runtime/Provider/AoTSetup.cs b/Assets/MirageSDK/Runtime/Provider/AoTSetup.cs index 7632d6d6..9d363cff 100644 --- a/Assets/MirageSDK/Runtime/Provider/AoTSetup.cs +++ b/Assets/MirageSDK/Runtime/Provider/AoTSetup.cs @@ -2,6 +2,7 @@ using MirageSDK.Data.ContractMessages.ERC721; using MirageSDK.Data.ContractMessages.ERC721.RentableExtension; using JetBrains.Annotations; +using MirageSDK.Utils; using Nethereum.Web3; namespace MirageSDK.Provider @@ -14,8 +15,8 @@ private static void SetupAoT() var web3 = new Web3(); #if (UNITY_WEBGL && !UNITY_EDITOR) - var webGlWrapper = new WebGL.WebGLWrapper(); - var contractFunctions = new WebGL.Implementation.ContractFunctionsWebGL(webGlWrapper); + var webGlConnect = ConnectProvider.GetConnect(); + var contractFunctions = new WebGL.Implementation.ContractFunctionsWebGL(webGlConnect); #else var contractFunctions = new Mobile.ContractFunctions(web3); #endif diff --git a/Assets/MirageSDK/Runtime/Provider/MirageSDKFactory.cs b/Assets/MirageSDK/Runtime/Provider/MirageSDKFactory.cs index dee99edd..03704ae3 100644 --- a/Assets/MirageSDK/Runtime/Provider/MirageSDKFactory.cs +++ b/Assets/MirageSDK/Runtime/Provider/MirageSDKFactory.cs @@ -42,22 +42,20 @@ private static IMirageSDK CreateMirageSDKInstance(string providerURI, bool autoS ISilentSigningHandler silentSigningHandler = null; #if (UNITY_WEBGL && !UNITY_EDITOR) - var webGlWrapper = Utils.ConnectProvider.GetConnect().SessionWrapper; - var contractFunctions = new WebGL.Implementation.ContractFunctionsWebGL(webGlWrapper); - var eth = new WebGL.Implementation.EthHandlerWebGL(webGlWrapper); - var walletHandler = (IWalletHandler)webGlWrapper; + var webGlConnect = Utils.ConnectProvider.GetConnect(); + var webGlWrapper = new WebGL.WebGLWrapper(webGlConnect); + var contractFunctions = new WebGL.Implementation.ContractFunctionsWebGL(webGlConnect); + var eth = new WebGL.Implementation.EthHandlerWebGL(webGlConnect); #else silentSigningHandler = new MirageSDK.SilentSigning.Implementation.SilentSigningProtocol(); var web3Provider = new Mobile.MobileWeb3Provider().CreateWeb3(providerURI); var contractFunctions = new Mobile.ContractFunctions(web3Provider); var eth = new Mobile.EthHandler(web3Provider, silentSigningHandler); - var walletHandler = new Mobile.MobileWalletHandler(); #endif return new MirageSDKWrapper( contractFunctions, eth, - walletHandler, silentSigningHandler ); } diff --git a/Assets/MirageSDK/Runtime/SilentSigning/Implementation/SilentSigningProtocol.cs b/Assets/MirageSDK/Runtime/SilentSigning/Implementation/SilentSigningProtocol.cs index 78af1487..ce40e540 100644 --- a/Assets/MirageSDK/Runtime/SilentSigning/Implementation/SilentSigningProtocol.cs +++ b/Assets/MirageSDK/Runtime/SilentSigning/Implementation/SilentSigningProtocol.cs @@ -4,8 +4,8 @@ using MirageSDK.SilentSigning.Data.Responses; using MirageSDK.Utils; using MirageSDK.WalletConnect.VersionShared.Models; -using MirageSDK.WalletConnectSharp.Unity.Events; using Cysharp.Threading.Tasks; +using MirageSDK.WalletConnectSharp.Core.StatusEvents; using UnityEngine; namespace MirageSDK.SilentSigning.Implementation diff --git a/Assets/MirageSDK/Runtime/WebGL/Implementation/ContractFunctionsWebGL.cs b/Assets/MirageSDK/Runtime/WebGL/Implementation/ContractFunctionsWebGL.cs index a29de984..10fae401 100644 --- a/Assets/MirageSDK/Runtime/WebGL/Implementation/ContractFunctionsWebGL.cs +++ b/Assets/MirageSDK/Runtime/WebGL/Implementation/ContractFunctionsWebGL.cs @@ -11,11 +11,11 @@ namespace MirageSDK.WebGL.Implementation { public class ContractFunctionsWebGL : IContractFunctions { - private readonly WebGLWrapper _webGlWrapper; + private readonly WebGLConnect _webGlConnect; - public ContractFunctionsWebGL(WebGLWrapper webGlWrapper) + public ContractFunctionsWebGL(WebGLConnect webGlConnect) { - _webGlWrapper = webGlWrapper; + _webGlConnect = webGlConnect; } public async UniTask GetContractData(string contractAddress, @@ -23,14 +23,14 @@ public async UniTask GetContractData(strin { var methodEncoder = new FunctionMessageEncodingService(contractAddress); var txData = methodEncoder.CreateCallInput(requestData); - var response = await _webGlWrapper.GetContractData(txData.ToTransactionData()); + var response = await _webGlConnect.GetContractData(txData.ToTransactionData()); return methodEncoder.DecodeSimpleTypeOutput(response); } - + public async UniTask>> GetEvents(NewFilterInput filters, string contractAddress = null) where TEvDto : IEventDTO, new() { - var logs = await _webGlWrapper.GetEvents(filters); + var logs = await _webGlConnect.GetEvents(filters); var events = logs.DecodeAllEvents(); return events; } diff --git a/Assets/MirageSDK/Runtime/WebGL/Implementation/EthHandlerWebGL.cs b/Assets/MirageSDK/Runtime/WebGL/Implementation/EthHandlerWebGL.cs index ca6cb797..92bbd3e3 100644 --- a/Assets/MirageSDK/Runtime/WebGL/Implementation/EthHandlerWebGL.cs +++ b/Assets/MirageSDK/Runtime/WebGL/Implementation/EthHandlerWebGL.cs @@ -19,31 +19,31 @@ public class EthHandlerWebGL : IEthHandler private const string GetBlockNumberMethodName = "eth.getBlockNumber"; private const string GetBlockTransactionCountMethodName = "eth.getBlockTransactionCount"; private const bool ReturnTransactionObjects = true; - private readonly WebGLWrapper _webGlWrapper; + private readonly WebGLConnect _webGLConnect; - public EthHandlerWebGL(WebGLWrapper webGlWrapper) + public EthHandlerWebGL(WebGLConnect webGLConnect) { - _webGlWrapper = webGlWrapper; + _webGLConnect = webGLConnect; } public UniTask GetDefaultAccount() { - return _webGlWrapper.GetDefaultAccount(); + return _webGLConnect.GetDefaultAccount(); } public UniTask GetTransactionReceipt(string transactionHash) { - return _webGlWrapper.GetTransactionReceipt(transactionHash); + return _webGLConnect.GetTransactionReceipt(transactionHash); } public UniTask GetTransaction(string transactionHash) { - return _webGlWrapper.GetTransaction(transactionHash); + return _webGLConnect.GetTransaction(transactionHash); } public UniTask EstimateGas(TransactionInput transactionInput) { - return _webGlWrapper.EstimateGas(transactionInput.ToTransactionData()); + return _webGLConnect.EstimateGas(transactionInput.ToTransactionData()); } public UniTask EstimateGas( @@ -64,7 +64,7 @@ public UniTask EstimateGas( gasPrice = gasPrice != null ? MirageSDKHelper.StringToBigInteger(gasPrice) : null, nonce = nonce }; - return _webGlWrapper.EstimateGas(transactionData); + return _webGLConnect.EstimateGas(transactionData); } public UniTask Sign(string messageToSign, string address) @@ -74,7 +74,7 @@ public UniTask Sign(string messageToSign, string address) address = address, message = messageToSign }; - return _webGlWrapper.Sign(props); + return _webGLConnect.Sign(props); } public UniTask SendTransaction(string from, string to, string data = null, string value = null, @@ -89,7 +89,7 @@ public UniTask SendTransaction(string from, string to, string data = nul gasPrice = gasPrice != null ? MirageSDKHelper.StringToBigInteger(gasPrice) : null, nonce = nonce }; - return _webGlWrapper.SendTransaction(transactionData); + return _webGLConnect.SendTransaction(transactionData); } public async UniTask GetBalance(string address = null) @@ -104,33 +104,33 @@ public async UniTask GetBalance(string address = null) } : null }; - var balance = await _webGlWrapper.CallMethod(callObject); + var balance = await _webGLConnect.CallMethod(callObject); return balance; } public UniTask GetChainId() { - return _webGlWrapper.GetChainId(); + return _webGLConnect.GetChainId(); } public UniTask WalletSwitchEthChain(EthChain chain) { - return _webGlWrapper.SwitchChain(chain); + return _webGLConnect.SwitchChain(chain); } public UniTask WalletAddEthChain(EthChainData chain) { - return _webGlWrapper.AddChain(chain); + return _webGLConnect.AddChain(chain); } public UniTask WalletUpdateEthChain(EthUpdateChainData chain) { - return _webGlWrapper.UpdateChain(chain); + return _webGLConnect.UpdateChain(chain); } public UniTask EthChainId() { - return _webGlWrapper.GetChainId(); + return _webGLConnect.GetChainId(); } public UniTask GetBlockNumber() @@ -139,7 +139,7 @@ public UniTask GetBlockNumber() { Path = GetBlockNumberMethodName }; - return _webGlWrapper.CallMethod(callObject); + return _webGLConnect.CallMethod(callObject); } public UniTask GetTransactionCount(string hash) @@ -181,7 +181,7 @@ private UniTask GetTransactionCountCommon(string blockId) blockId } }; - return _webGlWrapper.CallMethod(callObject); + return _webGLConnect.CallMethod(callObject); } private UniTask GetBlock(string blockId, bool returnTransactionObjects = false) @@ -193,7 +193,7 @@ private UniTask GetBlock(string blockId, bool returnTr blockId, returnTransactionObjects } }; - return _webGlWrapper.CallMethod(callObject); + return _webGLConnect.CallMethod(callObject); } } } \ No newline at end of file diff --git a/Assets/MirageSDK/Runtime/WebGL/Resources/WebGLConnectSettings.asset b/Assets/MirageSDK/Runtime/WebGL/Resources/WebGLConnectSettings.asset index d15845c4..a9521ee9 100644 --- a/Assets/MirageSDK/Runtime/WebGL/Resources/WebGLConnectSettings.asset +++ b/Assets/MirageSDK/Runtime/WebGL/Resources/WebGLConnectSettings.asset @@ -13,6 +13,4 @@ MonoBehaviour: m_Name: WebGLConnectSettings m_EditorClassIdentifier: _defaultWallet: 1 - _defaultNetwork: 2 - _connectOnAwake: 1 - _connectOnStart: 0 + _defaultNetwork: 5 diff --git a/Assets/MirageSDK/Runtime/WebGL/WebGLConnect.cs b/Assets/MirageSDK/Runtime/WebGL/WebGLConnect.cs index 294a9ebf..f0f11d8f 100644 --- a/Assets/MirageSDK/Runtime/WebGL/WebGLConnect.cs +++ b/Assets/MirageSDK/Runtime/WebGL/WebGLConnect.cs @@ -1,37 +1,54 @@ using System; +using System.Linq; +using System.Numerics; using MirageSDK.Data; using MirageSDK.Utils; using MirageSDK.WalletConnect.VersionShared; using Cysharp.Threading.Tasks; +using MirageSDK.WalletConnect.VersionShared.Infrastructure; +using MirageSDK.WalletConnect.VersionShared.Models.Ethereum; +using MirageSDK.WalletConnectSharp.Core; +using MirageSDK.WalletConnectSharp.Core.StatusEvents; +using MirageSDK.WebGL.DTO; +using Nethereum.Hex.HexTypes; +using Nethereum.RPC.Eth.DTOs; +using Newtonsoft.Json; using UnityEngine; +using TransactionData = MirageSDK.WebGL.DTO.TransactionData; namespace MirageSDK.WebGL { - public class WebGLConnect : IWalletConnectable + public class WebGLConnect : IWalletConnectable, IWalletConnectStatusHolder, IWalletConnectTransitionDataProvider { private const string SettingsFilenameStr = "WebGLConnectSettings"; - - public Action OnLoginPanelRequested; - public Action OnConnect; - public WebGLWrapper SessionWrapper { get; private set; } + public event Action SessionStatusUpdated; + public event Action OnAccountChanged; + public WalletConnectStatus Status => _status; + + private WalletConnectStatus _status = WalletConnectStatus.Uninitialized; + private WebGLWrapper _sessionWrapper; private UniTaskCompletionSource _walletCompletionSource; private WebGLConnectSettingsSO _settings; private NetworkName _network; + private Wallet? _selectedWallet; + private readonly WebGLCommunicationProtocol _protocol; public string SettingsFilename => SettingsFilenameStr; + public string WalletName => _selectedWallet.HasValue ? _selectedWallet.Value.ToString() : ""; public WebGLConnect() { - + _protocol = new WebGLCommunicationProtocol(); } public void Initialize(ScriptableObject settings) { - SessionWrapper = new WebGL.WebGLWrapper(); + _protocol.StartReceiveCycle().Forget(); _settings = settings as WebGLConnectSettingsSO; if (_settings != null) { _network = _settings.DefaultNetwork; + _status = WalletConnectStatus.DisconnectedNoSession; } else { @@ -42,22 +59,50 @@ public void Initialize(ScriptableObject settings) public async UniTask Connect() { + UpdateStatus(WalletConnectStatus.TransportConnected); var wallet = _settings.DefaultWallet; if (wallet == Wallet.None) { - OnLoginPanelRequested?.Invoke(); _walletCompletionSource = new UniTaskCompletionSource(); wallet = await _walletCompletionSource.Task; } - await SessionWrapper.ConnectTo(wallet, EthereumNetworks.GetNetworkByName(_network)); - - OnConnect?.Invoke(SessionWrapper); + UpdateStatus(WalletConnectStatus.SessionRequestSent); + await ConnectTo(wallet, EthereumNetworks.GetNetworkByName(_network)); + + var account = await GetDefaultAccount(); + OnAccountChanged?.Invoke(new [] {account}); + UpdateStatus(WalletConnectStatus.WalletConnected); + } + + public UniTask CloseSession(bool connectNewSession = true) + { + _protocol.Disconnect(); + UpdateStatus(WalletConnectStatus.DisconnectedNoSession); + return UniTask.CompletedTask; } - public UniTask GetWalletsStatus() + public async UniTask ReconnectSession() { - return SessionWrapper.GetWalletsStatus(); + await CloseSession(); + await Connect(); + return ""; + } + + public async UniTask GetWalletsStatus() + { + var id = _protocol.GenerateId(); + WebGLInterlayer.GetWalletsStatus(id); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Success) + { + var payload = JsonConvert.DeserializeObject(answer.payload); + return payload; + } + + throw new Exception(answer.payload); } public void SetWallet(Wallet wallet) @@ -69,5 +114,240 @@ public void SetNetwork(NetworkName network) { _network = network; } + + public async UniTask SendTransaction(TransactionData transaction) + { + var id = _protocol.GenerateId(); + var payload = JsonConvert.SerializeObject(transaction); + WebGLInterlayer.SendTransaction(id, payload); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Success) + { + return answer.payload; + } + + throw new Exception(answer.payload); + } + + public async UniTask GetContractData(TransactionData transaction) + { + var id = _protocol.GenerateId(); + var payload = JsonConvert.SerializeObject(transaction); + WebGLInterlayer.GetContractData(id, payload); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Success) + { + return answer.payload; + } + + throw new Exception(answer.payload); + } + + public async UniTask EstimateGas(TransactionData transaction) + { + var id = _protocol.GenerateId(); + var payload = JsonConvert.SerializeObject(transaction); + WebGLInterlayer.EstimateGas(id, payload); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Success) + { + return new HexBigInteger(answer.payload); + } + + throw new Exception(answer.payload); + } + + public async UniTask Sign(DataSignaturePropsDTO signProps) + { + var id = _protocol.GenerateId(); + + WebGLInterlayer.SignMessage(id, JsonConvert.SerializeObject(signProps)); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Success) + { + return answer.payload; + } + + throw new Exception(answer.payload); + } + + public async UniTask GetDefaultAccount(string network = null) + { + var id = _protocol.GenerateId(); + WebGLInterlayer.GetAddresses(id); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Success) + { + var addresses = JsonConvert.DeserializeObject(answer.payload); + return addresses.First(); + } + + throw new Exception(answer.payload); + } + + public async UniTask GetChainId() + { + var id = _protocol.GenerateId(); + WebGLInterlayer.RequestChainId(id); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Success) + { + var payload = JsonConvert.DeserializeObject>(answer.payload); + return payload.Result.Value; + } + + throw new Exception(answer.payload); + } + + public async UniTask GetTransaction(string transactionHash) + { + var id = _protocol.GenerateId(); + WebGLInterlayer.GetTransaction(id, transactionHash); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Success) + { + var transactionData = JsonConvert.DeserializeObject(answer.payload); + return transactionData; + } + + throw new Exception(answer.payload); + } + + public async UniTask AddChain(EthChainData networkData) + { + var id = _protocol.GenerateId(); + var payload = JsonConvert.SerializeObject(networkData); + WebGLInterlayer.AddChain(id, payload); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Error) + { + throw new Exception(answer.payload); + } + } + + public async UniTask UpdateChain(EthUpdateChainData networkData) + { + var id = _protocol.GenerateId(); + var payload = JsonConvert.SerializeObject(networkData); + WebGLInterlayer.AddChain(id, payload); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Error) + { + throw new Exception(answer.payload); + } + } + + public async UniTask SwitchChain(EthChain networkData) + { + var id = _protocol.GenerateId(); + var payload = JsonConvert.SerializeObject(networkData); + WebGLInterlayer.SwitchChain(id, payload); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Error) + { + throw new Exception(answer.payload); + } + } + + public async UniTask CallMethod(WebGLCallObject callObject) + { + var id = _protocol.GenerateId(); + var payload = JsonConvert.SerializeObject(callObject); + WebGLInterlayer.CallMethod(id, payload); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Success) + { + var callAnswer = JsonConvert.DeserializeObject>(answer.payload); + + return callAnswer.Result; + } + + throw new Exception(answer.payload); + } + + public async UniTask GetEvents(NewFilterInput filters) + { + var id = _protocol.GenerateId(); + var payload = JsonConvert.SerializeObject(filters); + WebGLInterlayer.GetEvents(id, payload); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Success) + { + var logs = JsonConvert.DeserializeObject(answer.payload); + return logs; + } + + throw new Exception(answer.payload); + } + + public async UniTask GetTransactionReceipt(string transactionHash) + { + var id = _protocol.GenerateId(); + WebGLInterlayer.GetTransactionReceipt(id, transactionHash); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Success) + { + return JsonConvert.DeserializeObject(answer.payload); + } + + throw new Exception(answer.payload); + } + + private async UniTask ConnectTo(Wallet wallet, EthereumNetwork chain) + { + var id = _protocol.GenerateId(); + var connectionProps = new ConnectionProps + { + wallet = wallet.ToString(), + chain = chain + }; + + var payload = JsonConvert.SerializeObject(connectionProps); + WebGLInterlayer.CreateProvider(id, payload); + + var answer = await _protocol.WaitForAnswer(id); + + if (answer.status == WebGLMessageStatus.Error) + { + throw new Exception(answer.payload); + } + } + + private void UpdateStatus(WalletConnectStatus newStatus) + { + var prevStatus = _status; + _status = newStatus; + if (newStatus != prevStatus) + { + var transition = TransitionDataFactory.CreateTransitionObj(prevStatus, newStatus, this); + SessionStatusUpdated?.Invoke(transition); + } + } } } \ No newline at end of file diff --git a/Assets/MirageSDK/Runtime/WebGL/WebGLWrapper.cs b/Assets/MirageSDK/Runtime/WebGL/WebGLWrapper.cs index 2b2c5530..6c291150 100644 --- a/Assets/MirageSDK/Runtime/WebGL/WebGLWrapper.cs +++ b/Assets/MirageSDK/Runtime/WebGL/WebGLWrapper.cs @@ -1,272 +1,26 @@ -using MirageSDK.WebGL.DTO; -using Nethereum.Hex.HexTypes; using MirageSDK.Core.Infrastructure; using Cysharp.Threading.Tasks; -using Nethereum.RPC.Eth.DTOs; -using System.Linq; -using System; -using System.Numerics; using MirageSDK.Data; -using MirageSDK.WalletConnect.VersionShared.Models.Ethereum; -using Newtonsoft.Json; -using TransactionData = MirageSDK.WebGL.DTO.TransactionData; namespace MirageSDK.WebGL { public class WebGLWrapper : IWalletHandler { - private readonly WebGLCommunicationProtocol _protocol; + private readonly WebGLConnect _connect; - public WebGLWrapper() + public WebGLWrapper(WebGLConnect webGlConnect) { - _protocol = new WebGLCommunicationProtocol(); - _protocol.StartReceiveCycle().Forget(); - } - - public async UniTask ConnectTo(Wallet wallet, EthereumNetwork chain) - { - var id = _protocol.GenerateId(); - var connectionProps = new ConnectionProps - { - wallet = wallet.ToString(), - chain = chain - }; - - var payload = JsonConvert.SerializeObject(connectionProps); - WebGLInterlayer.CreateProvider(id, payload); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Error) - { - throw new Exception(answer.payload); - } - } - - public async UniTask GetWalletsStatus() - { - var id = _protocol.GenerateId(); - WebGLInterlayer.GetWalletsStatus(id); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Success) - { - var payload = JsonConvert.DeserializeObject(answer.payload); - return payload; - } - - throw new Exception(answer.payload); + _connect = webGlConnect; } public UniTask Disconnect(bool waitForNewSession = true) { - _protocol.Disconnect(); - return UniTask.CompletedTask; - } - - public async UniTask SendTransaction(TransactionData transaction) - { - var id = _protocol.GenerateId(); - var payload = JsonConvert.SerializeObject(transaction); - WebGLInterlayer.SendTransaction(id, payload); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Success) - { - return answer.payload; - } - - throw new Exception(answer.payload); - } - - public async UniTask GetContractData(TransactionData transaction) - { - var id = _protocol.GenerateId(); - var payload = JsonConvert.SerializeObject(transaction); - WebGLInterlayer.GetContractData(id, payload); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Success) - { - return answer.payload; - } - - throw new Exception(answer.payload); - } - - public async UniTask EstimateGas(TransactionData transaction) - { - var id = _protocol.GenerateId(); - var payload = JsonConvert.SerializeObject(transaction); - WebGLInterlayer.EstimateGas(id, payload); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Success) - { - return new HexBigInteger(answer.payload); - } - - throw new Exception(answer.payload); - } - - public async UniTask Sign(DataSignaturePropsDTO signProps) - { - var id = _protocol.GenerateId(); - - WebGLInterlayer.SignMessage(id, JsonConvert.SerializeObject(signProps)); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Success) - { - return answer.payload; - } - - throw new Exception(answer.payload); - } - - public async UniTask GetDefaultAccount() - { - var id = _protocol.GenerateId(); - WebGLInterlayer.GetAddresses(id); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Success) - { - var addresses = JsonConvert.DeserializeObject(answer.payload); - return addresses.First(); - } - - throw new Exception(answer.payload); + return _connect.CloseSession(waitForNewSession); } - public async UniTask GetChainId() + public UniTask GetWalletsStatus() { - var id = _protocol.GenerateId(); - WebGLInterlayer.RequestChainId(id); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Success) - { - var payload = JsonConvert.DeserializeObject>(answer.payload); - return payload.Result.Value; - } - - throw new Exception(answer.payload); - } - - public async UniTask GetTransaction(string transactionHash) - { - var id = _protocol.GenerateId(); - WebGLInterlayer.GetTransaction(id, transactionHash); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Success) - { - var transactionData = JsonConvert.DeserializeObject(answer.payload); - return transactionData; - } - - throw new Exception(answer.payload); - } - - public async UniTask AddChain(EthChainData networkData) - { - var id = _protocol.GenerateId(); - var payload = JsonConvert.SerializeObject(networkData); - WebGLInterlayer.AddChain(id, payload); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Error) - { - throw new Exception(answer.payload); - } - } - - public async UniTask UpdateChain(EthUpdateChainData networkData) - { - var id = _protocol.GenerateId(); - var payload = JsonConvert.SerializeObject(networkData); - WebGLInterlayer.AddChain(id, payload); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Error) - { - throw new Exception(answer.payload); - } - } - - public async UniTask SwitchChain(EthChain networkData) - { - var id = _protocol.GenerateId(); - var payload = JsonConvert.SerializeObject(networkData); - WebGLInterlayer.SwitchChain(id, payload); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Error) - { - throw new Exception(answer.payload); - } - } - - public async UniTask CallMethod(WebGLCallObject callObject) - { - var id = _protocol.GenerateId(); - var payload = JsonConvert.SerializeObject(callObject); - WebGLInterlayer.CallMethod(id, payload); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Success) - { - var callAnswer = JsonConvert.DeserializeObject>(answer.payload); - - return callAnswer.Result; - } - - throw new Exception(answer.payload); - } - - public async UniTask GetEvents(NewFilterInput filters) - { - var id = _protocol.GenerateId(); - var payload = JsonConvert.SerializeObject(filters); - WebGLInterlayer.GetEvents(id, payload); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Success) - { - var logs = JsonConvert.DeserializeObject(answer.payload); - return logs; - } - - throw new Exception(answer.payload); - } - - public async UniTask GetTransactionReceipt(string transactionHash) - { - var id = _protocol.GenerateId(); - WebGLInterlayer.GetTransactionReceipt(id, transactionHash); - - var answer = await _protocol.WaitForAnswer(id); - - if (answer.status == WebGLMessageStatus.Success) - { - return JsonConvert.DeserializeObject(answer.payload); - } - - throw new Exception(answer.payload); + return _connect.GetWalletsStatus(); } } } \ No newline at end of file diff --git a/Assets/MirageSDK/Runtime/WebGL/web3gl.jslib b/Assets/MirageSDK/Runtime/WebGL/web3gl.jslib index 1ac1290f..bcaad454 100644 --- a/Assets/MirageSDK/Runtime/WebGL/web3gl.jslib +++ b/Assets/MirageSDK/Runtime/WebGL/web3gl.jslib @@ -2,96 +2,96 @@ mergeInto(LibraryManager.library, { CreateProvider: function (id, payload) { window.ProviderFabric.createProvider( - Pointer_stringify(id), - Pointer_stringify(payload) + UTF8ToString(id), + UTF8ToString(payload) ); }, GetWalletsStatus: function (id) { window.ProviderFabric.getWalletsStatus( - Pointer_stringify(id) + UTF8ToString(id) ); }, SignMessage: function (id, payload) { window.WalletProvider.signMessage( - Pointer_stringify(id), - Pointer_stringify(payload) + UTF8ToString(id), + UTF8ToString(payload) ); }, SendTransaction: function (id, payload) { window.WalletProvider.sendTransaction( - Pointer_stringify(id), - Pointer_stringify(payload) + UTF8ToString(id), + UTF8ToString(payload) ); }, GetContractData: function (id, payload) { window.WalletProvider.getContractData( - Pointer_stringify(id), - Pointer_stringify(payload) + UTF8ToString(id), + UTF8ToString(payload) ); }, EstimateGas: function (id, payload) { window.WalletProvider.estimateGas( - Pointer_stringify(id), - Pointer_stringify(payload) + UTF8ToString(id), + UTF8ToString(payload) ); }, GetAddresses: function (id) { window.WalletProvider.getAccounts( - Pointer_stringify(id) + UTF8ToString(id) ); }, GetTransaction: function (id, transactionHash) { window.WalletProvider.getTransaction( - Pointer_stringify(id), - Pointer_stringify(transactionHash) + UTF8ToString(id), + UTF8ToString(transactionHash) ); }, GetTransactionReceipt: function (id, transactionHash) { window.WalletProvider.getTransactionReceipt( - Pointer_stringify(id), - Pointer_stringify(transactionHash) + UTF8ToString(id), + UTF8ToString(transactionHash) ); }, AddChain: function (id, networkData) { window.WalletProvider.addChain( - Pointer_stringify(id), - Pointer_stringify(networkData) + UTF8ToString(id), + UTF8ToString(networkData) ); }, SwitchChain: function (id, networkData) { window.WalletProvider.switchChain( - Pointer_stringify(id), - Pointer_stringify(networkData) + UTF8ToString(id), + UTF8ToString(networkData) ); }, GetEvents: function (id, filters) { window.WalletProvider.getEvents( - Pointer_stringify(id), - Pointer_stringify(filters) + UTF8ToString(id), + UTF8ToString(filters) ); }, RequestChainId: function (id) { window.WalletProvider.getChainId( - Pointer_stringify(id) + UTF8ToString(id) ); }, CallMethod: function (id, callObject) { window.WalletProvider.callMethod( - Pointer_stringify(id), - Pointer_stringify(callObject) + UTF8ToString(id), + UTF8ToString(callObject) ); }, diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 1925adc7..9413c68c 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -134,7 +134,7 @@ PlayerSettings: 16:10: 1 16:9: 1 Others: 1 - bundleVersion: 0.6.7 + bundleVersion: 0.6.6 preloadedAssets: [] metroInputSource: 0 wsaTransparentSwapchain: 0 @@ -156,7 +156,7 @@ PlayerSettings: androidMaxAspectRatio: 2.1 applicationIdentifier: Android: com.Mirage.MirageSDK - Standalone: com.Mirage.MirageSDK + Standalone: com.Mirage.Mirage-SDK iPhone: com.Mirage.MirageSDK buildNumber: Standalone: 0 @@ -783,7 +783,7 @@ PlayerSettings: webGLTemplate: PROJECT:ankr-web3-template-2020 webGLAnalyzeBuildSize: 0 webGLUseEmbeddedResources: 0 - webGLCompressionFormat: 1 + webGLCompressionFormat: 2 webGLWasmArithmeticExceptions: 0 webGLLinkerTarget: 1 webGLThreadsSupport: 0 @@ -794,7 +794,7 @@ PlayerSettings: platformArchitecture: {} scriptingBackend: Android: 0 - Standalone: 1 + Standalone: 0 il2cppCompilerConfiguration: {} managedStrippingLevel: {} incrementalIl2cppBuild: {}