Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b535637
Revert "automated duplicating imported samples modifications (#595)"
robGG1997 Sep 14, 2023
95fb832
Merge branch 'main' of https://github.com/ChainSafe/web3.unity
robGG1997 Sep 14, 2023
e3ab3c2
Merge branch 'main' of https://github.com/ChainSafe/web3.unity
robGG1997 Sep 14, 2023
a536df0
Merge branch 'main' of https://github.com/ChainSafe/web3.unity
robGG1997 Sep 15, 2023
c12a0de
Merge branch 'main' of https://github.com/ChainSafe/web3.unity
robGG1997 Sep 18, 2023
8e1ea0a
Merge branch 'main' of https://github.com/ChainSafe/web3.unity
robGG1997 Sep 18, 2023
1318cd7
Merge branch 'main' of https://github.com/ChainSafe/web3.unity
robGG1997 Sep 21, 2023
33b8e52
Merge branch 'main' of https://github.com/ChainSafe/web3.unity
robGG1997 Sep 22, 2023
0a205bd
Merge branch 'main' of https://github.com/ChainSafe/web3.unity
robGG1997 Sep 25, 2023
df4795a
Merge branch 'main' of https://github.com/ChainSafe/web3.unity
robGG1997 Sep 29, 2023
abc34f6
Merge branch 'main' of https://github.com/ChainSafe/web3.unity
robGG1997 Sep 29, 2023
031e8d3
Merge branch 'main' of https://github.com/ChainSafe/web3.unity
robGG1997 Oct 18, 2023
1dbb557
Added Docs to Wallet Connect
robGG1997 Oct 19, 2023
c2ad8a8
Inline docs for NetCore and improved WC inline docs.
robGG1997 Oct 19, 2023
890c0ee
Added Inline docs for NetCore and improved docs.
robGG1997 Oct 19, 2023
d715bb0
Added inline docs for InProcessSigner and improved previously added docs
robGG1997 Oct 20, 2023
22917c9
Merge branch 'main' into rob/add-inline-docstring-669
robGG1997 Oct 23, 2023
ce3aca8
Made requested docs changes
robGG1997 Oct 24, 2023
e6c166c
Merge branch 'main' into rob/add-inline-docstring-669
robGG1997 Oct 25, 2023
cfeb6dc
Merge branch 'main' into rob/add-inline-docstring-669
kalambet Oct 25, 2023
8ba84d0
switched response for mint721 with a newer transaction hash
robGG1997 Oct 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public IEnumerator TestTransactionStatus()
[UnityTest]
public IEnumerator TestMint721()
{
config.TestResponse = "0xa9f953f9845e7d49d778d6fed622d566daf09e8e1c793297c7cab54782e1aae9";
config.TestResponse = "0xd3027fbfd9d5ddb5ea0ef75f5b128581d9268ad67728d150657f915c8910f9f0";

var mint721 = _sample.Mint721(Mint721Abi, Mint721Address, MintUri);

Expand Down
28 changes: 28 additions & 0 deletions src/ChainSafe.Gaming.InProcessSigner/InProcessSigner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,47 @@

namespace ChainSafe.Gaming.InProcessSigner
{
/// <summary>
/// Concrete Implementation of <see cref="ISigner"/> that uses a Private Key to sign messages and typed data.
/// </summary>
public class InProcessSigner : ISigner
{
private EthECKey privateKey;
private EthereumMessageSigner messageSigner;

/// <summary>
/// Initializes a new instance of the <see cref="InProcessSigner"/> class.
/// </summary>
/// <param name="config">Injected Config for signer containing a private key.</param>
/// <exception cref="Web3Exception">Throws Exception if initializing instance fails.</exception>
public InProcessSigner(InProcessSignerConfig config)
{
privateKey = config.PrivateKey ??
throw new Web3Exception($"{nameof(InProcessSignerConfig)}.{nameof(InProcessSignerConfig.PrivateKey)} must be set");
messageSigner = new();
}

/// <summary>
/// Implementation of <see cref="ISigner.GetAddress"/> using In Process.
/// </summary>
/// <returns>Public address of signer.</returns>
public Task<string> GetAddress() => Task.FromResult(privateKey.GetPublicAddress());

/// <summary>
/// Implementation of <see cref="ISigner.SignMessage"/> using In Process.
/// </summary>
/// <param name="message">Message to be signed.</param>
/// <returns>Hash response of a successfully signed message.</returns>
public Task<string> SignMessage(string message) =>
Task.FromResult(messageSigner.EncodeUTF8AndSign(message, privateKey));

/// <summary>
/// Implementation of <see cref="ISigner.SignTypedData{TStructType}"/> using In Process.
/// </summary>
/// <param name="domain">Serializable domain for signing typed data.</param>
/// <param name="message">Message/Data to be signed.</param>
/// <typeparam name="TStructType">Type of Data to be signed.</typeparam>
/// <returns>Hash response of a successfully signed typed data.</returns>
public Task<string> SignTypedData<TStructType>(SerializableDomain domain, TStructType message)
{
var primaryType = typeof(TStructType).Name;
Expand All @@ -45,6 +69,10 @@ public Task<string> SignTypedData<TStructType>(SerializableDomain domain, TStruc
return Task.FromResult(Eip712TypedDataSigner.Current.SignTypedDataV4(typedData, privateKey));
}

/// <summary>
/// Get private key of <see cref="ISigner"/>.
/// </summary>
/// <returns>Private key of <see cref="ISigner"/>.</returns>
public EthECKey GetKey() => privateKey;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
using Nethereum.Signer;
using ChainSafe.Gaming.Evm.Signers;
using Nethereum.Signer;

namespace ChainSafe.Gaming.InProcessSigner
{
/// <summary>
/// Config for <see cref="InProcessSigner"/>.
/// </summary>
public class InProcessSignerConfig
{
/// <summary>
/// Private key of <see cref="ISigner"/>.
/// </summary>
public EthECKey? PrivateKey { get; set; }
}
}
20 changes: 20 additions & 0 deletions src/ChainSafe.Gaming.InProcessSigner/InProcessSignerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,42 @@

namespace ChainSafe.Gaming.InProcessSigner
{
/// <summary>
/// <see cref="InProcessSigner"/> extension methods.
/// </summary>
public static class InProcessSignerExtensions
{
/// <summary>
/// Bind <see cref="InProcessSigner"/> as <see cref="ISigner"/> and a <see cref="configuration"/> to <see cref="collection"/>.
/// </summary>
/// <param name="collection">Service Collection to bind services to.</param>
/// <param name="configuration">Config used by <see cref="InProcessSigner"/>.</param>
/// <returns>The same service collection that was passed in. This enables fluent style.</returns>
public static IWeb3ServiceCollection UseInProcessSigner(this IWeb3ServiceCollection collection, InProcessSignerConfig configuration)
{
collection.UseInProcessSigner();
collection.ConfigureInProcessSigner(configuration);
return collection;
}

/// <summary>
/// Bind <see cref="InProcessSigner"/> as <see cref="ISigner"/> to <see cref="collection"/>.
/// </summary>
/// <param name="collection">Service Collection to bind services to.</param>
/// <returns>The same service collection that was passed in. This enables fluent style.</returns>
public static IWeb3ServiceCollection UseInProcessSigner(this IWeb3ServiceCollection collection)
{
collection.AssertServiceNotBound<ISigner>();
collection.AddSingleton<ISigner, InProcessSigner>();
return collection;
}

/// <summary>
/// Binds a <see cref="configuration"/> to <see cref="collection"/>.
/// </summary>
/// <param name="collection">Service Collection to bind services to.</param>
/// <param name="configuration">Config used by <see cref="InProcessSigner"/>.</param>
/// <returns>The same service collection that was passed in. This enables fluent style.</returns>
public static IWeb3ServiceCollection ConfigureInProcessSigner(this IWeb3ServiceCollection collection, InProcessSignerConfig configuration)
{
collection.Replace(ServiceDescriptor.Singleton(typeof(InProcessSignerConfig), configuration));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@

namespace ChainSafe.Gaming.InProcessTransactionExecutor
{
/// <summary>
/// Concrete Implementation of <see cref="ITransactionExecutor"/> that uses a private key to execute transactions.
/// </summary>
public class InProcessTransactionExecutor : ITransactionExecutor
{
private readonly NWeb3 web3;
private readonly IRpcProvider rpcProvider;
private readonly string accountAddress;

/// <summary>
/// Initializes a new instance of the <see cref="InProcessTransactionExecutor"/> class.
/// </summary>
/// <param name="signer">Injected <see cref="ISigner"/>.</param>
/// <param name="chainConfig">Injected <see cref="IChainConfig"/>.</param>
/// <param name="rpcProvider">Injected <see cref="IRpcProvider"/>.</param>
/// <exception cref="Web3Exception">Throws exception if initializing instance fails.</exception>
public InProcessTransactionExecutor(ISigner signer, IChainConfig chainConfig, IRpcProvider rpcProvider)
{
// It should be possible to set up other signers to work with this as well.
Expand All @@ -45,6 +55,14 @@ public InProcessTransactionExecutor(ISigner signer, IChainConfig chainConfig, IR
this.rpcProvider = rpcProvider;
}

/// <summary>
/// Implementation of <see cref="ITransactionExecutor.SendTransaction"/>.
/// Send a transaction using Wallet Connect.
/// This prompts user to approve a transaction on a connected wallet.
/// </summary>
/// <param name="transaction">Transaction to send.</param>
/// <returns>Hash response of a successfully executed transaction.</returns>
/// <exception cref="Web3Exception">Throws Exception if executing transaction fails.</exception>
public async Task<TransactionResponse> SendTransaction(TransactionRequest transaction)
{
if (string.IsNullOrEmpty(transaction.From))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

namespace ChainSafe.Gaming.InProcessTransactionExecutor
{
/// <summary>
/// Extension methods for <see cref="InProcessTransactionExecutor"/>.
/// </summary>
public static class InProcessTransactionExecutorExtensions
{
/// <summary>
/// Binds implementation of <see cref="ITransactionExecutor"/> as <see cref="InProcessTransactionExecutor"/> to Web3 as a service.
/// </summary>
/// <param name="collection">Service collection to bind implementations to.</param>
/// <returns>The same service collection that was passed in. This enables fluent style.</returns>
public static IWeb3ServiceCollection UseInProcessSigner(this IWeb3ServiceCollection collection)
{
collection.AssertServiceNotBound<ITransactionExecutor>();
Expand Down
28 changes: 28 additions & 0 deletions src/ChainSafe.Gaming.NetCore/ChainConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,46 @@

namespace ChainSafe.Gaming.NetCore
{
/// <summary>
/// Concrete Implementation of <see cref="IChainConfig"/>.
/// Holds all config files related to chain and network.
/// </summary>
public class ChainConfig : IChainConfig
{
/// <summary>
/// Implementation of <see cref="IChainConfig.ChainId"/>
/// Chain Id, eg. "5" for Goerli.
/// </summary>
public string ChainId { get; set; }

/// <summary>
/// Implementation of <see cref="IChainConfig.Chain"/>
/// Chain, for eg. "Ethereum".
/// </summary>
public string Chain { get; set; }

/// <summary>
/// Implementation of <see cref="IChainConfig.Network"/>
/// Specific Chain Network, eg. "mainnet" or "goerli".
/// </summary>
public string Network { get; set; }

/// <summary>
/// Implementation of <see cref="IChainConfig.Rpc"/>
/// RPC node link.
/// </summary>
public string Rpc { get; set; }

/// <summary>
/// Implementation of <see cref="IChainConfig.Ipc"/>
/// IPC link.
/// </summary>
public string Ipc { get; set; }

/// <summary>
/// Implementation of <see cref="IChainConfig.Ws"/>
/// WebSocket link.
/// </summary>
public string Ws { get; set; }
}
}
9 changes: 9 additions & 0 deletions src/ChainSafe.Gaming.NetCore/NetCoreEnvironmentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@

namespace ChainSafe.Gaming.NetCore
{
/// <summary>
/// NetCore environment extension.
/// Binds implementations to Web3 as a service for NetCore environment.
/// </summary>
public static class NetCoreEnvironmentExtensions
{
/// <summary>
/// Binds implementation of <see cref="IHttpClient"/>, <see cref="ILogWriter"/> and <see cref="IOperatingSystemMediator"/> to Web3 as a service for NetCore environment.
/// </summary>
/// <param name="services">Service collection to bind implementations to.</param>
/// <returns>The same service collection that was passed in. This enables fluent style.</returns>
public static IWeb3ServiceCollection UseNetCoreEnvironment(this IWeb3ServiceCollection services)
{
services.UseApiAnalytics();
Expand Down
32 changes: 32 additions & 0 deletions src/ChainSafe.Gaming.NetCore/NetCoreHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@

namespace ChainSafe.Gaming.NetCore
{
/// <summary>
/// Implementation of <see cref="IHttpClient"/> for NetCore environment.
/// </summary>
public class NetCoreHttpClient : IHttpClient
{
private readonly HttpClient originalClient;

/// <summary>
/// Initializes a new instance of the <see cref="NetCoreHttpClient"/> class.
/// </summary>
public NetCoreHttpClient()
{
originalClient = new HttpClient();
Expand All @@ -28,25 +34,51 @@ private static async ValueTask<NetworkResponse<string>> ResponseToNetworkRespons
}
}

/// <summary>
/// Get Raw response from http request with a GET method.
/// </summary>
/// <param name="url">Url of request to be made.</param>
/// <returns>Raw response to the request.</returns>
public async ValueTask<NetworkResponse<string>> GetRaw(string url)
{
var response = await originalClient.GetAsync(url);
return await ResponseToNetworkResponse(response);
}

/// <summary>
/// Get Raw response from http request with a POST method.
/// </summary>
/// <param name="url">Url of request to be made.</param>
/// <param name="data">Request data/body.</param>
/// <param name="contentType">Content type request header.</param>
/// <returns>Raw response to the request.</returns>
public async ValueTask<NetworkResponse<string>> PostRaw(string url, string data, string contentType)
{
var requestContent = new StringContent(data, Encoding.UTF8, contentType);
var response = await originalClient.PostAsync(url, requestContent);
return await ResponseToNetworkResponse(response);
}

/// <summary>
/// Get response as <see cref="TResponse"/> from http request with a GET method.
/// </summary>
/// <param name="url">Url of request to be made.</param>
/// <typeparam name="TResponse">Response body model type.</typeparam>
/// <returns>Response as a <see cref="TResponse"/>.</returns>
public async ValueTask<NetworkResponse<TResponse>> Get<TResponse>(string url)
{
var response = await GetRaw(url);
return response.Map(x => JsonConvert.DeserializeObject<TResponse>(x));
}

/// <summary>
/// Get response as <see cref="TResponse"/> from http request with a GET method.
/// </summary>
/// <param name="url">Url of request to be made.</param>
/// <param name="data">Request data/body.</param>
/// <typeparam name="TRequest">Request data/body model type.</typeparam>
/// <typeparam name="TResponse">Response data/body type.</typeparam>
/// <returns>Response as a <see cref="TResponse"/>.</returns>
public async ValueTask<NetworkResponse<TResponse>> Post<TRequest, TResponse>(string url, TRequest data)
{
var requestJson = JsonConvert.SerializeObject(data);
Expand Down
11 changes: 11 additions & 0 deletions src/ChainSafe.Gaming.NetCore/NetCoreLogWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,24 @@

namespace ChainSafe.Gaming.NetCore
{
/// <summary>
/// Implementation of <see cref="ILogWriter"/> for NetCore environment.
/// </summary>
public class NetCoreLogWriter : ILogWriter
{
/// <summary>
/// Log message.
/// </summary>
/// <param name="message">Message to be logged.</param>
public void Log(string message)
{
Console.WriteLine(FormatMessage(message, "INFO"));
}

/// <summary>
/// Log error message.
/// </summary>
/// <param name="message">Error message to be logged.</param>
public void LogError(string message)
{
Console.WriteLine(FormatMessage(message, "ERROR"));
Expand Down
16 changes: 16 additions & 0 deletions src/ChainSafe.Gaming.NetCore/NetCoreOperatingSystemMediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@

namespace ChainSafe.Gaming.NetCore
{
/// <summary>
/// Implementation of <see cref="IOperatingSystemMediator"/> for NetCore environment.
/// </summary>
public class NetCoreOperatingSystemMediator : IOperatingSystemMediator
{
/// <summary>
/// Is platform on Mobile or not.
/// </summary>
/// <exception cref="System.NotImplementedException">Not Implemented.</exception>
public bool IsMobilePlatform => throw new System.NotImplementedException();

/// <summary>
/// Current Platform enum.
/// </summary>
/// <exception cref="System.NotImplementedException">Not Implemented.</exception>
public Platform Platform => throw new System.NotImplementedException();

/// <summary>
/// Open a Url using Http for .NetCore environment.
/// </summary>
/// <param name="url">Url to open.</param>
/// <exception cref="System.NotImplementedException">Not Implemented.</exception>
public void OpenUrl(string url) => throw new System.NotImplementedException();
}
}
7 changes: 7 additions & 0 deletions src/ChainSafe.Gaming.NetCore/ProjectConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

namespace ChainSafe.Gaming.NetCore
{
/// <summary>
/// Implementation of <see cref="IProjectConfig"/> for NetCore environment.
/// </summary>
public class ProjectConfig : IProjectConfig
{
/// <summary>
/// Implementation of <see cref="IProjectConfig.ProjectId"/>
/// Project Id fetched from the ChainSafe Gaming web dashboard.
/// </summary>
public string ProjectId { get; set; }
}
}
Loading