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

QoL changes #134

Merged
merged 1 commit into from
Jun 30, 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
11 changes: 8 additions & 3 deletions src/Solnet.Rpc/Core/Http/RequestResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class RequestResult<T>
/// <summary>
/// Returns <c>true</c> if the HTTP request was successful (e.g. Code 200).
/// </summary>
public bool WasHttpRequestSuccessful { get; }
public bool WasHttpRequestSuccessful { get; set; }

/// <summary>
/// Returns <c>true</c> if the request was successfully handled by the server and no error parameters are found in the result.
Expand All @@ -38,13 +38,13 @@ public class RequestResult<T>
/// <summary>
/// Returns the <see cref="HttpStatusCode"/> of the request.
/// </summary>
public HttpStatusCode HttpStatusCode { get; }
public HttpStatusCode HttpStatusCode { get; set; }

/// <summary>
/// Returns the error code if one was found in the error object when the server is unable to handle the request.
/// </summary>
public int ServerErrorCode { get; set; }

/// <summary>
/// Initialize the request result.
/// <param name="resultMsg">An http request result.</param>
Expand All @@ -58,6 +58,11 @@ public class RequestResult<T>
Result = result;
}

/// <summary>
/// Added default constructor to facilitate unit tests.
/// </summary>
public RequestResult() { }

internal RequestResult(HttpStatusCode code, string reason)
{
HttpStatusCode = code;
Expand Down
2 changes: 1 addition & 1 deletion src/Solnet.Rpc/Core/Sockets/SubscriptionEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class SubscriptionEvent : EventArgs
/// <param name="status">The new status.</param>
/// <param name="error">The possible error message.</param>
/// <param name="code">The possible error code.</param>
internal SubscriptionEvent(SubscriptionStatus status, string error = default, string code = default)
public SubscriptionEvent(SubscriptionStatus status, string error = default, string code = default)
{
Status = status;
Error = error;
Expand Down
19 changes: 12 additions & 7 deletions src/Solnet.Rpc/Core/Sockets/SubscriptionState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class SubscriptionState
/// <summary>
/// Streaming client reference for easy unsubscription.
/// </summary>
private readonly SolanaStreamingRpcClient _rpcClient;
private readonly IStreamingRpcClient _rpcClient;

/// <summary>
/// The subscription ID as confirmed by the node.
Expand All @@ -23,27 +23,27 @@ public abstract class SubscriptionState
/// <summary>
/// The channel subscribed.
/// </summary>
public SubscriptionChannel Channel { get; }
public SubscriptionChannel Channel { get; protected set; }

/// <summary>
/// The current state of the subscription.
/// </summary>
public SubscriptionStatus State { get; private set; }
public SubscriptionStatus State { get; protected set; }

/// <summary>
/// The last error message.
/// </summary>
public string LastError { get; private set; }
public string LastError { get; protected set; }

/// <summary>
/// The last error code.
/// </summary>
public string LastCode { get; private set; }
public string LastCode { get; protected set; }

/// <summary>
/// The collection of parameters that were submitted for this subscription.
/// </summary>
public ImmutableList<object> AdditionalParameters { get; }
public ImmutableList<object> AdditionalParameters { get; protected set; }

/// <summary>
/// Event fired when the state of the subcription changes.
Expand All @@ -56,13 +56,18 @@ public abstract class SubscriptionState
/// <param name="rpcClient">The streaming rpc client reference.</param>
/// <param name="chan">The channel of this subscription.</param>
/// <param name="aditionalParameters">Aditional parameters for this given subscription.</param>
internal SubscriptionState(SolanaStreamingRpcClient rpcClient, SubscriptionChannel chan, IList<object> aditionalParameters = default)
protected SubscriptionState(IStreamingRpcClient rpcClient, SubscriptionChannel chan, IList<object> aditionalParameters = default)
{
_rpcClient = rpcClient;
Channel = chan;
AdditionalParameters = aditionalParameters?.ToImmutableList();
}

/// <summary>
/// Default constructor to help setup tests.
/// </summary>
protected SubscriptionState() { }

/// <summary>
/// Changes the state of the subscription and invokes the event.
/// </summary>
Expand Down
64 changes: 50 additions & 14 deletions src/Solnet.Wallet/PrivateKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,77 @@ public class PrivateKey
/// </summary>
private const int PrivateKeyLength = 64;

private string _key;

/// <summary>
/// The key as base-58 encoded string.
/// </summary>
public string Key;
public string Key
{
get
{
if (_key == null)
{
Key = Encoders.Base58.EncodeData(KeyBytes);
}
return _key;
}
set { _key = value; }
}


private byte[] _keyBytes;

/// <summary>
/// The bytes of the key.
/// </summary>
public byte[] KeyBytes;
public byte[] KeyBytes
{
get
{
if (_keyBytes == null)
{
KeyBytes = Encoders.Base58.DecodeData(Key);
}
return _keyBytes;
}
set { _keyBytes = value; }
}


/// <summary>
/// Initialize the private key from the given byte array.
/// Initialize the public key from the given byte array.
/// </summary>
/// <param name="key">The private key as byte array.</param>
/// <param name="key">The public key as byte array.</param>
public PrivateKey(byte[] key)
{
if (key.Length != PrivateKeyLength)
throw new ArgumentException("invalid key length", nameof(key));
KeyBytes = key;
Key = Encoders.Base58.EncodeData(key);
KeyBytes = new byte[PrivateKeyLength];
Array.Copy(key, KeyBytes, PrivateKeyLength);
}

/// <summary>
/// Initialize the private key from the given string.
/// Initialize the public key from the given string.
/// </summary>
/// <param name="key">The private key as base58 encoded string.</param>
/// <param name="key">The public key as base58 encoded string.</param>
public PrivateKey(string key)
{
byte[] decodedKey = Encoders.Base58.DecodeData(key);
if (decodedKey.Length != PrivateKeyLength)
throw new ArgumentException("invalid key length", nameof(key));
KeyBytes = decodedKey;
Key = key;
}


/// <summary>
/// Initialize the public key from the given string.
/// </summary>
/// <param name="key">The public key as base58 encoded string.</param>
public PrivateKey(ReadOnlySpan<byte> key)
{
if (key.Length != PrivateKeyLength)
throw new ArgumentException("invalid key length", nameof(key));
KeyBytes = new byte[PrivateKeyLength];
key.CopyTo(KeyBytes.AsSpan());
}

/// <summary>
/// Conversion between a <see cref="PrivateKey"/> object and the corresponding base-58 encoded private key.
/// </summary>
Expand Down
66 changes: 51 additions & 15 deletions src/Solnet.Wallet/PublicKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,43 @@ public class PublicKey
/// </summary>
private const int PublicKeyLength = 32;

private string _key;

/// <summary>
/// The key as base-58 encoded string.
/// </summary>
public string Key;
public string Key
{
get
{
if (_key == null)
{
Key = Encoders.Base58.EncodeData(KeyBytes);
}
return _key;
}
set { _key = value; }
}


private byte[] _keyBytes;

/// <summary>
/// The bytes of the key.
/// </summary>
public byte[] KeyBytes;
public byte[] KeyBytes
{
get
{
if (_keyBytes == null)
{
KeyBytes = Encoders.Base58.DecodeData(Key);
}
return _keyBytes;
}
set { _keyBytes = value; }
}


/// <summary>
/// Initialize the public key from the given byte array.
Expand All @@ -35,49 +63,57 @@ public PublicKey(byte[] key)
{
if (key.Length != PublicKeyLength)
throw new ArgumentException("invalid key length", nameof(key));
KeyBytes = key;
Key = Encoders.Base58.EncodeData(key);
KeyBytes = new byte[PublicKeyLength];
Array.Copy(key, KeyBytes, PublicKeyLength);
}

/// <summary>
/// Initialize the public key from the given string.
/// </summary>
/// <param name="key">The public key as base58 encoded string.</param>
public PublicKey(string key)
{
byte[] decodedKey = Encoders.Base58.DecodeData(key);
if (decodedKey.Length != PublicKeyLength)
throw new ArgumentException("invalid key length", nameof(key));
KeyBytes = decodedKey;
Key = key;
}


/// <summary>
/// Initialize the public key from the given string.
/// </summary>
/// <param name="key">The public key as base58 encoded string.</param>
public PublicKey(ReadOnlySpan<byte> key)
{
if (key.Length != PublicKeyLength)
throw new ArgumentException("invalid key length", nameof(key));
KeyBytes = new byte[PublicKeyLength];
key.CopyTo(KeyBytes.AsSpan());
}

/// <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);
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);
public static explicit operator PublicKey(byte[] keyBytes) => new(keyBytes);
}
}
16 changes: 0 additions & 16 deletions test/Solnet.Wallet.Test/KeysTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ namespace Solnet.Wallet.Test
[TestClass]
public class KeysTest
{
private static readonly string InvalidPublicKeyLength = "ALSzrjtGi8MZGmAZa6ZhtUZq3rwurWuJqWFdgcj9MMFLasdasasd";
private const string InvalidPrivateKeyLength = "5ZD7ntKtyHrnqMhfSuKBLdqHzT5N3a2aYnCGBasdcz4N78b84TKpasdwQ4QBsapEnpnZFchM7F1BpqDasdkSuLdwMZwM8hLi";
private const string PrivateKey = "5ZD7ntKtyHrnqMhfSuKBLdqHzT5N3a2aYnCGBcz4N78b84TKpjwQ4QBsapEnpnZFchM7F1BpqDkSuLdwMZwM8hLi";
private static readonly byte[] ExpectedPrivateKeyBytes =
{
Expand All @@ -20,20 +18,6 @@ public class KeysTest
119, 181, 171, 113, 120, 224, 0, 118, 155, 61, 246, 56, 178, 47
};

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void TestPublicKeyStringException()
{
_ = new PublicKey(InvalidPublicKeyLength);
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void TestPrivateKeyStringException()
{
_ = new PublicKey(InvalidPrivateKeyLength);
}

[TestMethod]
public void TestPrivateKeyBytes()
{
Expand Down