Skip to content

Commit

Permalink
Add hierarchy API
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasDorier committed Dec 11, 2023
1 parent eca963d commit 58c9fb8
Show file tree
Hide file tree
Showing 11 changed files with 880 additions and 110 deletions.
66 changes: 66 additions & 0 deletions NBXplorer.Client/ExplorerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using NBitcoin.RPC;
using System.Runtime.CompilerServices;
using System.Linq;
using System.Diagnostics;

namespace NBXplorer
{
Expand Down Expand Up @@ -394,6 +395,24 @@ public Task<TransactionInformation> GetTransactionAsync(TrackedSource trackedSou
return SendAsync<TransactionInformation>(HttpMethod.Get, null, $"{GetBasePath(trackedSource)}/transactions/{txId}", cancellation);
}

public async Task AssociateScriptsAsync(TrackedSource trackedSource, AssociateScriptRequest[] scripts, CancellationToken cancellation = default)
{
if (scripts == null)
throw new ArgumentNullException(nameof(scripts));
if (trackedSource == null)
throw new ArgumentNullException(nameof(trackedSource));
await SendAsync(HttpMethod.Post, scripts, $"{GetBasePath(trackedSource)}/associate", cancellation);
}

public async Task ImportUTXOs(TrackedSource trackedSource, ImportUTXORequest request, CancellationToken cancellation = default)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
if (trackedSource == null)
throw new ArgumentNullException(nameof(trackedSource));
await SendAsync(HttpMethod.Post, request, $"{GetBasePath(trackedSource)}/import-utxos", cancellation);
}

public Task RescanAsync(RescanRequest rescanRequest, CancellationToken cancellation = default)
{
if (rescanRequest == null)
Expand Down Expand Up @@ -547,6 +566,53 @@ public GenerateWalletResponse GenerateWallet(GenerateWalletRequest request = nul
return GenerateWalletAsync(request, cancellationToken).GetAwaiter().GetResult();
}

public async Task<TrackedSource[]> GetChildWallets(TrackedSource trackedSource,
CancellationToken cancellation = default)
{
return await GetAsync<TrackedSource[]>($"{GetBasePath(trackedSource)}/children", cancellation);
}
public async Task<TrackedSource[]> GetParentWallets(TrackedSource trackedSource,
CancellationToken cancellation = default)
{
return await GetAsync<TrackedSource[]>($"{GetBasePath(trackedSource)}/parents", cancellation);
}
public async Task AddChildWallet(TrackedSource trackedSource, TrackedSource childWallet, CancellationToken cancellation = default)
{
var request = new TrackedSourceRequest()
{
TrackedSource = childWallet
};
await SendAsync(HttpMethod.Post, request, $"{GetBasePath(trackedSource)}/children", cancellation);
}

public async Task AddParentWallet(TrackedSource trackedSource, TrackedSource parentWallet,
CancellationToken cancellation = default)
{
var request = new TrackedSourceRequest()
{
TrackedSource = parentWallet
};
await SendAsync(HttpMethod.Post, request, $"{GetBasePath(trackedSource)}/parents", cancellation);
}
public async Task RemoveChildWallet(TrackedSource trackedSource, TrackedSource childWallet, CancellationToken cancellation = default)
{
var request = new TrackedSourceRequest()
{
TrackedSource = childWallet
};
await SendAsync(HttpMethod.Delete, request, $"{GetBasePath(trackedSource)}/children", cancellation);
}

public async Task RemoveParentWallet(TrackedSource trackedSource, TrackedSource parentWallet,
CancellationToken cancellation = default)
{
var request = new TrackedSourceRequest()
{
TrackedSource = parentWallet
};
await SendAsync(HttpMethod.Delete, request, $"{GetBasePath(trackedSource)}/parents", cancellation);
}

private static readonly HttpClient SharedClient = new HttpClient();
internal HttpClient Client = SharedClient;

Expand Down
2 changes: 2 additions & 0 deletions NBXplorer.Client/Models/GenerateWalletRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public class GenerateWalletRequest
public bool ImportKeysToRPC { get; set; }
public bool SavePrivateKeys { get; set; }
public Dictionary<string, string> AdditionalOptions { get; set; }

public TrackedSource ParentWallet { get; set; }
}
}
25 changes: 25 additions & 0 deletions NBXplorer.Client/Models/ImportUTXORequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NBitcoin;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;

namespace NBXplorer.Models;

public class ImportUTXORequest
{
[JsonProperty("UTXOs")]
public OutPoint[] Utxos { get; set; }
}

public class AssociateScriptRequest
{
public Script ScriptPubKey { get; set; }
public IDestination Destination { get; set; }

public BitcoinAddress GetAddress(Network network)
{
return GetScriptPubKey().GetDestinationAddress(network);
}

public Script GetScriptPubKey() => ScriptPubKey ?? Destination.ScriptPubKey;
}
1 change: 1 addition & 0 deletions NBXplorer.Client/Models/TrackWalletRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class TrackWalletRequest
{
public TrackDerivationOption[] DerivationOptions { get; set; }
public bool Wait { get; set; } = false;
public TrackedSource ParentWallet { get; set; }
}

public class TrackDerivationOption
Expand Down
6 changes: 6 additions & 0 deletions NBXplorer.Client/Models/TrackedSourceRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace NBXplorer.Models;

public class TrackedSourceRequest
{
public TrackedSource TrackedSource { get; set; }
}
Loading

0 comments on commit 58c9fb8

Please sign in to comment.