Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements implicit conversions and debugger display attributes. #116

Merged
merged 1 commit into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/Solnet.Rpc/Core/Http/JsonRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Solnet.Rpc.Core.Http
/// <summary>
/// Base Rpc client class that abstracts the HttpClient handling.
/// </summary>
public abstract class JsonRpcClient
internal abstract class JsonRpcClient
{
/// <summary>
/// The Json serializer options to be reused between calls.
Expand Down
4 changes: 3 additions & 1 deletion src/Solnet.Rpc/SolanaRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Solnet.Rpc.Types;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
Expand All @@ -15,7 +16,8 @@ namespace Solnet.Rpc
/// <summary>
/// Implements functionality to interact with the Solana JSON RPC API.
/// </summary>
public class SolanaRpcClient : JsonRpcClient, IRpcClient
[DebuggerDisplay("Cluster = {" + nameof(NodeAddress) + "}")]
internal class SolanaRpcClient : JsonRpcClient, IRpcClient
{
/// <summary>
/// Message Id generator.
Expand Down
2 changes: 2 additions & 0 deletions src/Solnet.Rpc/SolanaStreamingRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Solnet.Rpc.Types;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.WebSockets;
using System.Text;
using System.Text.Json;
Expand All @@ -18,6 +19,7 @@ namespace Solnet.Rpc
/// <summary>
/// Implementation of the Solana streaming RPC API abstraction client.
/// </summary>
[DebuggerDisplay("Cluster = {" + nameof(NodeAddress) + "}")]
internal class SolanaStreamingRpcClient : StreamingRpcClient, IStreamingRpcClient
{
/// <summary>
Expand Down
16 changes: 16 additions & 0 deletions src/Solnet.Wallet/Account.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using Chaos.NaCl;
using Org.BouncyCastle.Security;
using Solnet.Wallet.Utilities;
using System.Diagnostics;

namespace Solnet.Wallet
{
/// <summary>
/// Implements account functionality.
/// </summary>
[DebuggerDisplay("PubKey = {" + nameof(Account.PublicKey.Key) + "}")]
public class Account
{
/// <summary>
Expand Down Expand Up @@ -83,5 +85,19 @@ private static byte[] GenerateRandomSeed()
RandomUtils.GetBytes(bytes);
return bytes;
}

/// <summary>
/// Conversion between a <see cref="Account"/> object and the corresponding private key.
/// </summary>
/// <param name="account">The Account object.</param>
/// <returns>The private key.</returns>
public static implicit operator PrivateKey(Account account) => account.PrivateKey;

/// <summary>
/// Conversion between a <see cref="Account"/> object and the corresponding public key.
/// </summary>
/// <param name="account">The Account object.</param>
/// <returns>The public key as a byte array.</returns>
public static implicit operator PublicKey(Account account) => account.PublicKey;
}
}
2 changes: 2 additions & 0 deletions src/Solnet.Wallet/Bip39/Mnemonic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Solnet.Wallet.Utilities;
using System;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Text;

Expand All @@ -14,6 +15,7 @@ namespace Solnet.Wallet.Bip39
/// BIP39 specification used as reference located here: https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
/// This implementation was taken from NBitcoin: https://github.com/MetacoSA/NBitcoin/
/// </summary>
[DebuggerDisplay("Mnemonic = {" + nameof(_mnemonic) + "}")]
public class Mnemonic
{
/// <summary>
Expand Down
30 changes: 30 additions & 0 deletions src/Solnet.Wallet/PrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

using Solnet.Wallet.Utilities;
using System;
using System.Diagnostics;

namespace Solnet.Wallet
{
/// <summary>
/// Implements the private key functionality.
/// </summary>
[DebuggerDisplay("Key = {" + nameof(Key) + "}")]
public class PrivateKey
{
/// <summary>
Expand Down Expand Up @@ -49,5 +51,33 @@ public PrivateKey(string key)
KeyBytes = decodedKey;
Key = key;
}

/// <summary>
/// Conversion between a <see cref="PrivateKey"/> object and the corresponding base-58 encoded private key.
/// </summary>
/// <param name="privateKey">The PrivateKey object.</param>
/// <returns>The base-58 encoded private key.</returns>
public static implicit operator string(PrivateKey privateKey) => privateKey.Key;

/// <summary>
/// Conversion between a base-58 encoded private key and the <see cref="PrivateKey"/> object.
/// </summary>
/// <param name="address">The base-58 encoded private key.</param>
/// <returns>The PrivateKey object.</returns>
public static explicit operator PrivateKey(string address) => new (address);

/// <summary>
/// Conversion between a <see cref="PrivateKey"/> object and the private key as a byte array.
/// </summary>
/// <param name="privateKey">The PrivateKey object.</param>
/// <returns>The private key as a byte array.</returns>
public static implicit operator byte[](PrivateKey privateKey) => privateKey.KeyBytes;

/// <summary>
/// Conversion between a private key as a byte array and the corresponding <see cref="PrivateKey"/> object.
/// </summary>
/// <param name="keyBytes">The private key as a byte array.</param>
/// <returns>The PrivateKey object.</returns>
public static explicit operator PrivateKey(byte[] keyBytes) => new (keyBytes);
}
}
30 changes: 30 additions & 0 deletions src/Solnet.Wallet/PublicKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

using Solnet.Wallet.Utilities;
using System;
using System.Diagnostics;

namespace Solnet.Wallet
{
/// <summary>
/// Implements the public key functionality.
/// </summary>
[DebuggerDisplay("Key = {" + nameof(Key) + "}")]
public class PublicKey
{
/// <summary>
Expand Down Expand Up @@ -49,5 +51,33 @@ public PublicKey(string key)
KeyBytes = decodedKey;
Key = key;
}

/// <summary>
/// Conversion between a <see cref="PublicKey"/> object and the corresponding base-58 encoded public key.
/// </summary>
/// <param name="publicKey">The PublicKey object.</param>
/// <returns>The base-58 encoded public key.</returns>
public static implicit operator string(PublicKey publicKey) => publicKey.Key;

/// <summary>
/// Conversion between a base-58 encoded public key and the <see cref="PublicKey"/> object.
/// </summary>
/// <param name="address">The base-58 encoded public key.</param>
/// <returns>The PublicKey object.</returns>
public static explicit operator PublicKey(string address) => new (address);

/// <summary>
/// Conversion between a <see cref="PublicKey"/> object and the public key as a byte array.
/// </summary>
/// <param name="publicKey">The PublicKey object.</param>
/// <returns>The public key as a byte array.</returns>
public static implicit operator byte[](PublicKey publicKey) => publicKey.KeyBytes;

/// <summary>
/// Conversion between a public key as a byte array and the corresponding <see cref="PublicKey"/> object.
/// </summary>
/// <param name="keyBytes">The public key as a byte array.</param>
/// <returns>The PublicKey object.</returns>
public static explicit operator PublicKey(byte[] keyBytes) => new (keyBytes);
}
}