diff --git a/ChainSafe.Gaming.sln b/ChainSafe.Gaming.sln index 454ef47d6..dee5b62e9 100644 --- a/ChainSafe.Gaming.sln +++ b/ChainSafe.Gaming.sln @@ -43,8 +43,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainSafe.Gaming.SygmaClien EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainSafe.Gaming.Marketplace", "src/ChainSafe.Gaming.Marketplace\ChainSafe.Gaming.Marketplace.csproj", "{E7A9FE35-BEFD-4569-B848-495E0B85B674}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainSafe.Gaming.InProcessTransactionExecutor.Unity", "src\ChainSafe.Gaming.InProcessTransactionExecutor.Unity\ChainSafe.Gaming.InProcessTransactionExecutor.Unity.csproj", "{46C16E64-DC72-446D-B153-A2142691A151}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChainSafe.Gaming.HyperPlay", "src\ChainSafe.Gaming.HyperPlay\ChainSafe.Gaming.HyperPlay.csproj", "{0859E3E2-5AB9-4997-9DE7-368EAB89313B}" EndProject Global @@ -149,12 +147,6 @@ Global {E7A9FE35-BEFD-4569-B848-495E0B85B674}.Release|Any CPU.Build.0 = Release|Any CPU {E7A9FE35-BEFD-4569-B848-495E0B85B674}.Test|Any CPU.ActiveCfg = Debug|Any CPU {E7A9FE35-BEFD-4569-B848-495E0B85B674}.Test|Any CPU.Build.0 = Debug|Any CPU - {46C16E64-DC72-446D-B153-A2142691A151}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {46C16E64-DC72-446D-B153-A2142691A151}.Debug|Any CPU.Build.0 = Debug|Any CPU - {46C16E64-DC72-446D-B153-A2142691A151}.Release|Any CPU.ActiveCfg = Release|Any CPU - {46C16E64-DC72-446D-B153-A2142691A151}.Release|Any CPU.Build.0 = Release|Any CPU - {46C16E64-DC72-446D-B153-A2142691A151}.Test|Any CPU.ActiveCfg = Test|Any CPU - {46C16E64-DC72-446D-B153-A2142691A151}.Test|Any CPU.Build.0 = Test|Any CPU {0859E3E2-5AB9-4997-9DE7-368EAB89313B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {0859E3E2-5AB9-4997-9DE7-368EAB89313B}.Debug|Any CPU.Build.0 = Debug|Any CPU {0859E3E2-5AB9-4997-9DE7-368EAB89313B}.Release|Any CPU.ActiveCfg = Release|Any CPU diff --git a/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs b/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs index 37683cd3b..c34757203 100644 --- a/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs +++ b/Packages/io.chainsafe.web3-unity.web3auth/Runtime/Web3AuthWallet.cs @@ -9,6 +9,7 @@ using ChainSafe.Gaming.Web3.Core; using ChainSafe.Gaming.Web3.Core.Evm; using ChainSafe.Gaming.Web3.Environment; +using Nethereum.JsonRpc.Client; using Nethereum.Signer; using UnityEngine; using TWeb3Auth = Web3Auth; @@ -26,20 +27,21 @@ public class Web3AuthWallet : ISigner, ITransactionExecutor, ILifecycleParticipa private TWeb3Auth coreInstance; private InProcessSigner signer; private InProcessTransactionExecutor transactionExecutor; - private IMainThreadRunner mainThreadRunner; + private IClient rpcClient; private readonly IAnalyticsClient analyticsClient; /// /// Initializes a new instance of the class. /// /// The configuration for the Web3Auth wallet. - /// The configuration for the target blockchain. /// The RPC provider for blockchain interaction. - public Web3AuthWallet(Web3AuthWalletConfig config, IRpcProvider rpcProvider, IMainThreadRunner mainThreadRunner, IAnalyticsClient analyticsClient) + /// + /// + public Web3AuthWallet(Web3AuthWalletConfig config, IRpcProvider rpcProvider, IClient rpcClient, IAnalyticsClient analyticsClient) { this.config = config; this.rpcProvider = rpcProvider; - this.mainThreadRunner = mainThreadRunner; + this.rpcClient = rpcClient; this.analyticsClient = analyticsClient; } @@ -72,7 +74,7 @@ public async ValueTask WillStartAsync() var signerConfig = new InProcessSignerConfig { PrivateKey = privateKey }; signer = new InProcessSigner(signerConfig); - transactionExecutor = new InProcessTransactionExecutor(signer, analyticsClient.ChainConfig, rpcProvider, mainThreadRunner, new RpcClientWrapper(analyticsClient.ChainConfig)); + transactionExecutor = new InProcessTransactionExecutor(signer, analyticsClient.ChainConfig, rpcProvider, rpcClient); void Web3Auth_OnLogin(Web3AuthResponse response) { diff --git a/scripts/data/published_dependencies.txt b/scripts/data/published_dependencies.txt index 95a06a814..96a73ea71 100644 --- a/scripts/data/published_dependencies.txt +++ b/scripts/data/published_dependencies.txt @@ -1,3 +1,3 @@ -Packages/io.chainsafe.web3-unity/Runtime/Libraries/:ADRaffy.ENSNormalize;Nethereum.Model;BouncyCastle.Crypto;Nethereum.RLP;ChainSafe.Gaming.Debugging;Nethereum.RPC;ChainSafe.Gaming.Gelato;ChainSafe.Gaming.SygmaClient;Nethereum.Signer.EIP712;ChainSafe.Gaming.InProcessSigner;Nethereum.Signer;ChainSafe.Gaming.InProcessTransactionExecutor;Nethereum.Util;ChainSafe.Gaming.Unity.ThirdParty;Nethereum.Web3;ChainSafe.Gaming.Unity;System.Buffers;ChainSafe.Gaming.WalletConnect;System.Memory;ChainSafe.Gaming;System.Numerics.Vectors;Microsoft.Bcl.AsyncInterfaces;System.Reactive;Microsoft.Extensions.DependencyInjection.Abstractions;System.Runtime.CompilerServices.Unsafe;Microsoft.Extensions.DependencyInjection;System.Runtime.InteropServices.WindowsRuntime;Microsoft.Extensions.Logging.Abstractions;System.Security.Cryptography.Cng;Microsoft.IdentityModel.Abstractions;System.Text.Encodings.Web;Microsoft.IdentityModel.Logging;System.Text.Json;Microsoft.IdentityModel.Tokens;System.Threading.Channels;NBitcoin;System.Threading.Tasks.Extensions;Nethereum.ABI;WalletConnectSharp.Auth;Nethereum.Accounts;WalletConnectSharp.Common;WalletConnectSharp.Events;Nethereum.BlockchainProcessing;WalletConnectSharp.Core;Nethereum.Contracts;WalletConnectSharp.Crypto;Nethereum.Hex;Nethereum.JsonRpc.Client;WalletConnectSharp.Network.Websocket;Nethereum.JsonRpc.IpcClient;WalletConnectSharp.Network;Nethereum.JsonRpc.RpcClient;WalletConnectSharp.Sign;Nethereum.KeyStore;WalletConnectSharp.Storage;Nethereum.Merkle.Patricia;WalletConnectSharp.Web3Wallet;Nethereum.Merkle;Websocket.Client;Nethereum.Metamask;Nethereum.Siwe.Core;Nethereum.Siwe;Nethereum.UI;Nethereum.Unity.Metamask;Nethereum.Unity;ChainSafe.Gaming.MetaMask;ChainSafe.Gaming.MetaMask.Unity;ChainSafe.Gaming.InProcessTransactionExecutor.Unity;ChainSafe.Gaming.Marketplace +Packages/io.chainsafe.web3-unity/Runtime/Libraries/:ADRaffy.ENSNormalize;Nethereum.Model;BouncyCastle.Crypto;Nethereum.RLP;ChainSafe.Gaming.Debugging;Nethereum.RPC;ChainSafe.Gaming.Gelato;ChainSafe.Gaming.SygmaClient;Nethereum.Signer.EIP712;ChainSafe.Gaming.InProcessSigner;Nethereum.Signer;ChainSafe.Gaming.InProcessTransactionExecutor;Nethereum.Util;ChainSafe.Gaming.Unity.ThirdParty;Nethereum.Web3;ChainSafe.Gaming.Unity;System.Buffers;ChainSafe.Gaming.WalletConnect;System.Memory;ChainSafe.Gaming;System.Numerics.Vectors;Microsoft.Bcl.AsyncInterfaces;System.Reactive;Microsoft.Extensions.DependencyInjection.Abstractions;System.Runtime.CompilerServices.Unsafe;Microsoft.Extensions.DependencyInjection;System.Runtime.InteropServices.WindowsRuntime;Microsoft.Extensions.Logging.Abstractions;System.Security.Cryptography.Cng;Microsoft.IdentityModel.Abstractions;System.Text.Encodings.Web;Microsoft.IdentityModel.Logging;System.Text.Json;Microsoft.IdentityModel.Tokens;System.Threading.Channels;NBitcoin;System.Threading.Tasks.Extensions;Nethereum.ABI;WalletConnectSharp.Auth;Nethereum.Accounts;WalletConnectSharp.Common;WalletConnectSharp.Events;Nethereum.BlockchainProcessing;WalletConnectSharp.Core;Nethereum.Contracts;WalletConnectSharp.Crypto;Nethereum.Hex;Nethereum.JsonRpc.Client;WalletConnectSharp.Network.Websocket;Nethereum.JsonRpc.IpcClient;WalletConnectSharp.Network;Nethereum.JsonRpc.RpcClient;WalletConnectSharp.Sign;Nethereum.KeyStore;WalletConnectSharp.Storage;Nethereum.Merkle.Patricia;WalletConnectSharp.Web3Wallet;Nethereum.Merkle;Websocket.Client;Nethereum.Metamask;Nethereum.Siwe.Core;Nethereum.Siwe;Nethereum.UI;Nethereum.Unity.Metamask;Nethereum.Unity;ChainSafe.Gaming.MetaMask;ChainSafe.Gaming.MetaMask.Unity;ChainSafe.Gaming.Marketplace Packages/io.chainsafe.web3-unity.lootboxes/Chainlink/Runtime/Libraries/:Chainsafe.Gaming.Chainlink;ChainSafe.Gaming.Lootboxes.Chainlink Packages/io.chainsafe.web3-unity.hyperplay/Runtime/Libraries/:ChainSafe.Gaming.HyperPlay \ No newline at end of file diff --git a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/ChainSafe.Gaming.InProcessTransactionExecutor.Unity.csproj b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/ChainSafe.Gaming.InProcessTransactionExecutor.Unity.csproj deleted file mode 100644 index 36dd894ec..000000000 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/ChainSafe.Gaming.InProcessTransactionExecutor.Unity.csproj +++ /dev/null @@ -1,22 +0,0 @@ - - - - netstandard2.1 - 9.0 - True - True - ../../global.ruleset - Debug;Release;Test - AnyCPU - ChainSafe.Gaming.InProcessTransactionExecutor.Unity - - - - - - - - - - - diff --git a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs b/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs deleted file mode 100644 index ed40e4e8a..000000000 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/RpcClientWrapper.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Net.Http; -using ChainSafe.Gaming.Web3; -using ChainSafe.Gaming.Web3.Environment; -using Nethereum.JsonRpc.Client; -using Nethereum.Unity.Rpc; - -namespace ChainSafe.Gaming.InProcessTransactionExecutor.Unity -{ - public class RpcClientWrapper : IRpcClientWrapper - { - private readonly IChainConfig chainConfig; - - public RpcClientWrapper(IChainConfig chainConfig) - { - this.chainConfig = chainConfig; - } - - public IClient Client => new RpcClient(new Uri(chainConfig.Rpc)); - } -} \ No newline at end of file diff --git a/src/ChainSafe.Gaming.InProcessTransactionExecutor/IRpcClientWrapper.cs b/src/ChainSafe.Gaming.InProcessTransactionExecutor/IRpcClientWrapper.cs deleted file mode 100644 index f0372ec30..000000000 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor/IRpcClientWrapper.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Nethereum.JsonRpc.Client; - -namespace ChainSafe.Gaming.InProcessTransactionExecutor -{ - public interface IRpcClientWrapper - { - public IClient Client { get; } - } -} \ No newline at end of file diff --git a/src/ChainSafe.Gaming.InProcessTransactionExecutor/InProcessTransactionExecutor.cs b/src/ChainSafe.Gaming.InProcessTransactionExecutor/InProcessTransactionExecutor.cs index 403780e4e..4d7cecc15 100644 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor/InProcessTransactionExecutor.cs +++ b/src/ChainSafe.Gaming.InProcessTransactionExecutor/InProcessTransactionExecutor.cs @@ -7,6 +7,7 @@ using ChainSafe.Gaming.Web3.Core.Evm; using ChainSafe.Gaming.Web3.Environment; using Nethereum.Hex.HexTypes; +using Nethereum.JsonRpc.Client; using Nethereum.RPC.Eth.DTOs; using Nethereum.Web3.Accounts; using NIpcClient = Nethereum.JsonRpc.IpcClient.IpcClient; @@ -30,10 +31,9 @@ public class InProcessTransactionExecutor : ITransactionExecutor /// Injected . /// Injected . /// Injected . - /// Injected . - /// Injected . + /// Injected . /// Throws exception if initializing instance fails. - public InProcessTransactionExecutor(ISigner signer, IChainConfig chainConfig, IRpcProvider rpcProvider, IMainThreadRunner mainThreadRunner, IRpcClientWrapper rpcClientWrapper) + public InProcessTransactionExecutor(ISigner signer, IChainConfig chainConfig, IRpcProvider rpcProvider, IClient rpcClient) { // It should be possible to set up other signers to work with this as well. // However, does it make sense to let a remote wallet sign a transaction, but @@ -43,23 +43,19 @@ public InProcessTransactionExecutor(ISigner signer, IChainConfig chainConfig, IR accountAddress = privateKey.GetPublicAddress(); var account = new Account(privateKey); - // Initialize Web3 on the main thread. - mainThreadRunner.Enqueue(() => + if (chainConfig.Rpc is not null && !string.Empty.Equals(chainConfig.Rpc)) { - if (chainConfig.Rpc is not null && !string.Empty.Equals(chainConfig.Rpc)) - { - web3 = new NWeb3(account, rpcClientWrapper.Client); - } - else if (chainConfig.Ipc is not null && !string.Empty.Equals(chainConfig.Ipc)) - { - var client = new NIpcClient(chainConfig.Rpc); - web3 = new NWeb3(client); - } - else - { - throw new Web3Exception($"{nameof(IChainConfig)} should have at least one communication method set."); - } - }); + web3 = new NWeb3(account, rpcClient); + } + else if (chainConfig.Ipc is not null && !string.Empty.Equals(chainConfig.Ipc)) + { + var client = new NIpcClient(chainConfig.Rpc); + web3 = new NWeb3(client); + } + else + { + throw new Web3Exception($"{nameof(IChainConfig)} should have at least one communication method set."); + } this.rpcProvider = rpcProvider; } diff --git a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/InProcessTransactionExecutorExtensions.cs b/src/ChainSafe.Gaming.InProcessTransactionExecutor/InProcessTransactionExecutorExtensions.cs similarity index 88% rename from src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/InProcessTransactionExecutorExtensions.cs rename to src/ChainSafe.Gaming.InProcessTransactionExecutor/InProcessTransactionExecutorExtensions.cs index 5a913192a..ec360d166 100644 --- a/src/ChainSafe.Gaming.InProcessTransactionExecutor.Unity/InProcessTransactionExecutorExtensions.cs +++ b/src/ChainSafe.Gaming.InProcessTransactionExecutor/InProcessTransactionExecutorExtensions.cs @@ -1,6 +1,7 @@ using ChainSafe.Gaming.Web3.Build; using ChainSafe.Gaming.Web3.Core.Evm; using Microsoft.Extensions.DependencyInjection; +using Nethereum.JsonRpc.Client; namespace ChainSafe.Gaming.InProcessTransactionExecutor.Unity { @@ -19,8 +20,6 @@ public static IWeb3ServiceCollection UseInProcessTransactionExecutor(this IWeb3S collection.AssertServiceNotBound(); collection.AddSingleton(); - collection.AssertServiceNotBound(); - collection.AddSingleton(); return collection; } } diff --git a/src/ChainSafe.Gaming.Unity/ChainSafe.Gaming.Unity.csproj b/src/ChainSafe.Gaming.Unity/ChainSafe.Gaming.Unity.csproj index 542bbb428..9b61d799a 100644 --- a/src/ChainSafe.Gaming.Unity/ChainSafe.Gaming.Unity.csproj +++ b/src/ChainSafe.Gaming.Unity/ChainSafe.Gaming.Unity.csproj @@ -18,7 +18,7 @@ - + diff --git a/src/ChainSafe.Gaming/Web3/Evm/JsonRpc/RpcClientExtensions.cs b/src/ChainSafe.Gaming/Web3/Evm/JsonRpc/RpcClientExtensions.cs index 6374350eb..a9438e58d 100644 --- a/src/ChainSafe.Gaming/Web3/Evm/JsonRpc/RpcClientExtensions.cs +++ b/src/ChainSafe.Gaming/Web3/Evm/JsonRpc/RpcClientExtensions.cs @@ -3,6 +3,7 @@ using ChainSafe.Gaming.Web3.Core; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; +using Nethereum.JsonRpc.Client; namespace ChainSafe.Gaming.Evm.JsonRpc { @@ -40,6 +41,9 @@ public static IWeb3ServiceCollection UseRpcProvider(this IWeb3ServiceCollection collection.AssertServiceNotBound(); collection.TryAddSingleton(DefaultClientConfig); collection.AddSingleton(); + + collection.AssertServiceNotBound(); + collection.AddSingleton(); return collection; } } diff --git a/src/ChainSafe.Gaming/Web3/Evm/JsonRpc/RpcClientProvider.cs b/src/ChainSafe.Gaming/Web3/Evm/JsonRpc/RpcClientProvider.cs index 67dbbef26..0c3ea01e7 100644 --- a/src/ChainSafe.Gaming/Web3/Evm/JsonRpc/RpcClientProvider.cs +++ b/src/ChainSafe.Gaming/Web3/Evm/JsonRpc/RpcClientProvider.cs @@ -5,13 +5,14 @@ using ChainSafe.Gaming.Web3.Core; using ChainSafe.Gaming.Web3.Environment; using Nethereum.Hex.HexTypes; +using Nethereum.JsonRpc.Client; using Nethereum.JsonRpc.Client.RpcMessages; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace ChainSafe.Gaming.Evm.Providers { - public class RpcClientProvider : IRpcProvider, ILifecycleParticipant + public class RpcClientProvider : ClientBase, IRpcProvider, ILifecycleParticipant { private readonly RpcClientConfig config; private readonly Web3Environment environment; @@ -100,19 +101,9 @@ public async Task Perform(string method, params object[] parameters) try { - var httpClient = environment.HttpClient; var request = new RpcRequestMessage(Guid.NewGuid().ToString(), method, parameters); - var response = - (await httpClient.Post(config.RpcNodeUrl, request)) - .AssertSuccess(); - if (response.HasError) - { - var error = response.Error; - var errorMessage = - $"RPC returned error for \"{method}\": {error.Code} {error.Message} {error.Data}"; - throw new Web3Exception(errorMessage); - } + var response = await SendAsync(request); var serializer = JsonSerializer.Create(); return serializer.Deserialize(new JTokenReader(response.Result))!; @@ -134,5 +125,26 @@ public async Task Perform(string method, params object[] parameters) }); } } + + protected override async Task SendAsync(RpcRequestMessage request, string route = null) + { + string body = JsonConvert.SerializeObject(request); + + return await SendAsyncInternally(body); + } + + protected override async Task SendAsync(RpcRequestMessage[] requests) + { + string body = JsonConvert.SerializeObject(requests); + + return await SendAsyncInternally(body); + } + + private async Task SendAsyncInternally(string body) + { + var result = await environment.HttpClient.PostRaw(config.RpcNodeUrl, body, "application/json"); + + return JsonConvert.DeserializeObject(result.Response); + } } } \ No newline at end of file