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

Introduce TrackedSourceContext #449

Merged
merged 1 commit into from
Dec 5, 2023
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
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