Skip to content

Commit

Permalink
Key management (#88)
Browse files Browse the repository at this point in the history
* get_key

* get_keys

* set_label

* delete_label
  • Loading branch information
dkackman authored Aug 1, 2023
1 parent 541299c commit 56e1dd4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/chia-dotnet/ChiaTypes/KeyData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace chia.dotnet
{
public record KeyData
{
public uint Fingerprint { get; init; }
public string PublicKey { get; init; } = string.Empty;
public string? Label { get; init; }
public KeyDataSecrets? Secrets { get; init; }
}
}
11 changes: 11 additions & 0 deletions src/chia-dotnet/ChiaTypes/KeyDataSecrets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;

namespace chia.dotnet
{
public record KeyDataSecrets
{
public IEnumerable<string> Mnemonic { get; init; } = new List<string>();
public string Bytes { get; init; } = string.Empty;
public PrivateKey PrivateKey { get; init; } = new();
}
}
63 changes: 63 additions & 0 deletions src/chia-dotnet/DaemonProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Reflection.Emit;

namespace chia.dotnet
{
Expand Down Expand Up @@ -333,6 +334,68 @@ public async Task CheckKeys(string rootPath, CancellationToken cancellationToken
_ = await SendMessage("check_keys", data, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Locates and returns KeyData matching the provided fingerprint
/// </summary>
/// <param name="fingerprint">The fingerprint</param>
/// <param name="includeSecrets">Include secrets</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<KeyData> GetKey(uint fingerprint, bool includeSecrets = false, CancellationToken cancellationToken = default)
{
dynamic data = new ExpandoObject();
data.fingerprint = fingerprint;
data.include_secrets = includeSecrets;

return await SendMessage<KeyData>("get_key", data, "key", cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Returns the KeyData of all keys which can be retrieved
/// </summary>
/// <param name="fingerprint">The fingerprint</param>
/// <param name="includeSecrets">Include secrets</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<KeyData> GetKeys(uint fingerprint, bool includeSecrets = false, CancellationToken cancellationToken = default)
{
dynamic data = new ExpandoObject();
data.fingerprint = fingerprint;
data.include_secrets = includeSecrets;

return await SendMessage<IEnumerable<KeyData>>("get_keys", data, "keys", cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Assigns the given label to the first key with the given fingerprint.
/// </summary>
/// <param name="fingerprint">The fingerprint</param>
/// <param name="label">The label</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task SetLabel(uint fingerprint, string label, CancellationToken cancellationToken = default)
{
dynamic data = new ExpandoObject();
data.fingerprint = fingerprint;
data.label = label;

_ = await SendMessage("set_label", data, cancellationToken).ConfigureAwait(false);
}

/// <summary>
/// Removes the label assigned to the key with the given fingerprint.
/// </summary>
/// <param name="fingerprint">The fingerprint</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task DeleteLabel(uint fingerprint, CancellationToken cancellationToken = default)
{
dynamic data = new ExpandoObject();
data.fingerprint = fingerprint;

_ = await SendMessage("delete_label", data, cancellationToken).ConfigureAwait(false);
}

private static object CreateDataObject(string service)
{
if (string.IsNullOrEmpty(service))
Expand Down

0 comments on commit 56e1dd4

Please sign in to comment.