From 56e1dd45782f66744ed7b054c66966d1ba4229fe Mon Sep 17 00:00:00 2001 From: Don Kackman Date: Tue, 1 Aug 2023 16:07:34 -0500 Subject: [PATCH] Key management (#88) * get_key * get_keys * set_label * delete_label --- src/chia-dotnet/ChiaTypes/KeyData.cs | 10 ++++ src/chia-dotnet/ChiaTypes/KeyDataSecrets.cs | 11 ++++ src/chia-dotnet/DaemonProxy.cs | 63 +++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 src/chia-dotnet/ChiaTypes/KeyData.cs create mode 100644 src/chia-dotnet/ChiaTypes/KeyDataSecrets.cs diff --git a/src/chia-dotnet/ChiaTypes/KeyData.cs b/src/chia-dotnet/ChiaTypes/KeyData.cs new file mode 100644 index 00000000..7e43baf5 --- /dev/null +++ b/src/chia-dotnet/ChiaTypes/KeyData.cs @@ -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; } + } +} diff --git a/src/chia-dotnet/ChiaTypes/KeyDataSecrets.cs b/src/chia-dotnet/ChiaTypes/KeyDataSecrets.cs new file mode 100644 index 00000000..6a0ed872 --- /dev/null +++ b/src/chia-dotnet/ChiaTypes/KeyDataSecrets.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace chia.dotnet +{ + public record KeyDataSecrets + { + public IEnumerable Mnemonic { get; init; } = new List(); + public string Bytes { get; init; } = string.Empty; + public PrivateKey PrivateKey { get; init; } = new(); + } +} diff --git a/src/chia-dotnet/DaemonProxy.cs b/src/chia-dotnet/DaemonProxy.cs index e24ffdbf..146bdff6 100644 --- a/src/chia-dotnet/DaemonProxy.cs +++ b/src/chia-dotnet/DaemonProxy.cs @@ -3,6 +3,7 @@ using System.Threading; using System.Threading.Tasks; using System.Collections.Generic; +using System.Reflection.Emit; namespace chia.dotnet { @@ -333,6 +334,68 @@ public async Task CheckKeys(string rootPath, CancellationToken cancellationToken _ = await SendMessage("check_keys", data, cancellationToken).ConfigureAwait(false); } + /// + /// Locates and returns KeyData matching the provided fingerprint + /// + /// The fingerprint + /// Include secrets + /// + /// + public async Task GetKey(uint fingerprint, bool includeSecrets = false, CancellationToken cancellationToken = default) + { + dynamic data = new ExpandoObject(); + data.fingerprint = fingerprint; + data.include_secrets = includeSecrets; + + return await SendMessage("get_key", data, "key", cancellationToken).ConfigureAwait(false); + } + + /// + /// Returns the KeyData of all keys which can be retrieved + /// + /// The fingerprint + /// Include secrets + /// + /// + public async Task GetKeys(uint fingerprint, bool includeSecrets = false, CancellationToken cancellationToken = default) + { + dynamic data = new ExpandoObject(); + data.fingerprint = fingerprint; + data.include_secrets = includeSecrets; + + return await SendMessage>("get_keys", data, "keys", cancellationToken).ConfigureAwait(false); + } + + /// + /// Assigns the given label to the first key with the given fingerprint. + /// + /// The fingerprint + /// The label + /// + /// + 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); + } + + /// + /// Removes the label assigned to the key with the given fingerprint. + /// + /// The fingerprint + /// + /// + 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))