Skip to content

Commit

Permalink
Merge pull request #449 from NicolasDorier/trackctx
Browse files Browse the repository at this point in the history
Introduce TrackedSourceContext
  • Loading branch information
NicolasDorier authored Dec 5, 2023
2 parents 9b6c001 + 4260cb8 commit 8ec608f
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 257 deletions.
2 changes: 1 addition & 1 deletion NBXplorer.Tests/NBXplorer.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework Condition="'$(TargetFrameworkOverride)' == ''">net8.0</TargetFramework>
<LangVersion>10.0</LangVersion>
<LangVersion>12</LangVersion>
<TargetFramework Condition="'$(TargetFrameworkOverride)' != ''">$(TargetFrameworkOverride)</TargetFramework>
<SupportDBTrie Condition="'$(SupportDBTrie)' == ''">true</SupportDBTrie>
<DefineConstants Condition="'$(SupportDBTrie)' == 'true'">$(DefineConstants);SUPPORT_DBTRIE</DefineConstants>
Expand Down
1 change: 1 addition & 0 deletions NBXplorer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionFiles", "SolutionFi
Dockerfile.linuxamd64 = Dockerfile.linuxamd64
Dockerfile.linuxarm32v7 = Dockerfile.linuxarm32v7
Dockerfile.linuxarm64v8 = Dockerfile.linuxarm64v8
global.json = global.json
.circleci\run-tests.sh = .circleci\run-tests.sh
EndProjectSection
EndProject
Expand Down
12 changes: 12 additions & 0 deletions NBXplorer/Controllers/CommonRoutes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace NBXplorer.Controllers;

public static class CommonRoutes
{
public const string BaseCryptoEndpoint = "cryptos/{cryptoCode}";
public const string BaseDerivationEndpoint = $"{BaseCryptoEndpoint}/derivations";
public const string DerivationEndpoint = $"{BaseCryptoEndpoint}/derivations/{{derivationScheme}}";
public const string AddressEndpoint = $"{BaseCryptoEndpoint}/addresses/{{address}}";
public const string WalletEndpoint = $"{BaseCryptoEndpoint}/wallets/{{walletId}}";
public const string TrackedSourceEndpoint = $"{BaseCryptoEndpoint}/tracked-sources/{{trackedSource}}";
public const string TransactionsPath = "transactions/{txId?}";
}
9 changes: 0 additions & 9 deletions NBXplorer/Controllers/ControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ public ControllerBase(
public IRepositoryProvider RepositoryProvider { get; }
public IIndexers Indexers { get; }

internal static TrackedSource GetTrackedSource(DerivationStrategyBase derivationScheme, BitcoinAddress address)
{
TrackedSource trackedSource = null;
if (address != null)
trackedSource = new AddressTrackedSource(address);
if (derivationScheme != null)
trackedSource = new DerivationSchemeTrackedSource(derivationScheme);
return trackedSource;
}
internal NBXplorerNetwork GetNetwork(string cryptoCode, bool checkRPC)
{
if (cryptoCode == null)
Expand Down
21 changes: 9 additions & 12 deletions NBXplorer/Controllers/MainController.PSBT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,24 @@ namespace NBXplorer.Controllers
public partial class MainController
{
[HttpPost]
[Route("cryptos/{network}/derivations/{strategy}/psbt/create")]
[Route($"{CommonRoutes.DerivationEndpoint}/psbt/create")]
[TrackedSourceContext.TrackedSourceContextRequirement(allowedTrackedSourceTypes: typeof(DerivationSchemeTrackedSource))]
public async Task<IActionResult> CreatePSBT(
[ModelBinder(BinderType = typeof(NetworkModelBinder))]
NBXplorerNetwork network,
[ModelBinder(BinderType = typeof(DerivationStrategyModelBinder))]
DerivationStrategyBase strategy,
TrackedSourceContext trackedSourceContext,
[FromBody]
JObject body,
[FromServices]
IUTXOService utxoService)
{
if (body == null)
throw new ArgumentNullException(nameof(body));
var network = trackedSourceContext.Network;
CreatePSBTRequest request = ParseJObject<CreatePSBTRequest>(body, network);
if (strategy == null)
throw new ArgumentNullException(nameof(strategy));

var repo = RepositoryProvider.GetRepository(network);
var txBuilder = request.Seed is int s ? network.NBitcoinNetwork.CreateTransactionBuilder(s)
: network.NBitcoinNetwork.CreateTransactionBuilder();

var strategy = ((DerivationSchemeTrackedSource) trackedSourceContext.TrackedSource).DerivationStrategy;
CreatePSBTSuggestions suggestions = null;
if (!(request.DisableFingerprintRandomization is true) &&
fingerprintService.GetDistribution(network) is FingerprintDistribution distribution)
Expand Down Expand Up @@ -153,7 +150,7 @@ public async Task<IActionResult> CreatePSBT(
txBuilder.SetLockTime(new LockTime(0));
}
}
var utxoChanges = (await utxoService.GetUTXOs(network.CryptoCode, strategy)).As<UTXOChanges>();
var utxoChanges = (await utxoService.GetUTXOs(trackedSourceContext)).As<UTXOChanges>();
var utxos = utxoChanges.GetUnspentUTXOs(request.MinConfirmations);
var availableCoinsByOutpoint = utxos.ToDictionary(o => o.Outpoint);
if (request.IncludeOnlyOutpoints != null)
Expand Down Expand Up @@ -197,7 +194,7 @@ public async Task<IActionResult> CreatePSBT(
if (network.CryptoCode == "BTC" && unconfUtxos.Count > 0 && request.MinConfirmations == 0)
{
HashSet<uint256> requestedTxs = new HashSet<uint256>();
var rpc = RPCClients.Get(network);
var rpc = trackedSourceContext.RpcClient;
rpc = rpc.PrepareBatch();
var mempoolEntries =
unconfUtxos
Expand Down Expand Up @@ -270,7 +267,7 @@ public async Task<IActionResult> CreatePSBT(
bool hasChange = false;
if (request.ExplicitChangeAddress == null)
{
var keyInfo = (await GetUnusedAddress(network.CryptoCode, strategy, DerivationFeature.Change, autoTrack: true)).As<KeyPathInformation>();
var keyInfo = (await GetUnusedAddress(trackedSourceContext, DerivationFeature.Change, autoTrack: true)).As<KeyPathInformation>();
change = (keyInfo.ScriptPubKey, keyInfo.KeyPath);
}
else
Expand Down Expand Up @@ -351,7 +348,7 @@ public async Task<IActionResult> CreatePSBT(
// We made sure we can build the PSBT, so now we can reserve the change address if we need to
if (hasChange && request.ExplicitChangeAddress == null && request.ReserveChangeAddress)
{
var derivation = (await GetUnusedAddress(network.CryptoCode, strategy, DerivationFeature.Change, reserve: true, autoTrack: true)).As<KeyPathInformation>();
var derivation = (await GetUnusedAddress(trackedSourceContext, DerivationFeature.Change, reserve: true, autoTrack: true)).As<KeyPathInformation>();
// In most of the time, this is the same as previously, so no need to rebuild PSBT
if (derivation.ScriptPubKey != change.ScriptPubKey)
{
Expand Down
Loading

0 comments on commit 8ec608f

Please sign in to comment.