Skip to content

Commit

Permalink
add more complex test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kukks committed Nov 17, 2023
1 parent a009d19 commit ff942dd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
6 changes: 5 additions & 1 deletion NBXplorer.Client/ExplorerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,11 @@ public KeyPathInformation GetKeyInformation(DerivationStrategyBase strategy, Scr

public async Task<KeyPathInformation> GetKeyInformationAsync(DerivationStrategyBase strategy, Script script, CancellationToken cancellation = default)
{
return await SendAsync<KeyPathInformation>(HttpMethod.Get, null, $"v1/cryptos/{CryptoCode}/derivations/{strategy}/scripts/{script.ToHex()}", cancellation).ConfigureAwait(false);
return await GetKeyInformationAsync(new DerivationSchemeTrackedSource(strategy), script, cancellation).ConfigureAwait(false);
}
public async Task<KeyPathInformation> GetKeyInformationAsync(TrackedSource trackedSource, Script script, CancellationToken cancellation = default)
{
return await SendAsync<KeyPathInformation>(HttpMethod.Get, null, $"{GetBasePath(trackedSource)}/scripts/{script.ToHex()}", cancellation).ConfigureAwait(false);
}

[Obsolete("Use GetKeyInformationAsync(DerivationStrategyBase strategy, Script script) instead")]
Expand Down
30 changes: 28 additions & 2 deletions NBXplorer.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4488,7 +4488,7 @@ public async Task CanAssociateIndependentScripts(Backend backend)
var parentWallet = Guid.NewGuid().ToString();
var parentWalletTS = new WalletTrackedSource(parentWallet);


#if SUPPORT_DBTRIE
//this should create both wallets
if (backend == Backend.DBTrie)
{
Expand All @@ -4507,7 +4507,7 @@ await Assert.ThrowsAsync<NBXplorerException>(async () =>await tester.Client.Gene
await Assert.ThrowsAsync<HttpRequestException>(async () =>await tester.Client.ImportUTXOs(parentWalletTS, Array.Empty<ImportUTXORequest>()));
return;
}

#endif
await tester.Client.TrackAsync(wallet1TS, new TrackWalletRequest()
{
ParentWallet = parentWalletTS
Expand Down Expand Up @@ -4580,6 +4580,32 @@ await Eventually(async () =>
scriptBagUtxos = await tester.Client.GetUTXOsAsync(wallet1TS);
Assert.Equal(2, scriptBagUtxos.GetUnspentUTXOs().Length);
});

//create wallet A
//create wallet b using generate and make it child of A
// create address using unused on B
// creat wallet C tracking address from B, make it child of A, B
var walletA = new WalletTrackedSource(Guid.NewGuid().ToString());
await tester.Client.TrackAsync(walletA);
var generatResponse = await tester.Client.GenerateWalletAsync(new GenerateWalletRequest()
{
ParentWallet = walletA
});
var walletB = TrackedSource.Create(generatResponse.DerivationScheme);
var addressA = await tester.Client.GetUnusedAsync(generatResponse.DerivationScheme, DerivationFeature.Deposit, 0, true);
var walletC = AddressTrackedSource.Create(addressA.Address);
await tester.Client.TrackAsync(walletC, new TrackWalletRequest()
{
ParentWallet = walletB
});
await tester.Client.TrackAsync(walletC, new TrackWalletRequest()
{
ParentWallet = walletA
});

var kpi = await tester.Client.GetKeyInformationsAsync(addressA.ScriptPubKey, CancellationToken.None);
var tss = kpi.Select(information => information.TrackedSource);
Assert.True(tss.Distinct().Count() == tss.Count(), "The result should only distinct tracked source matches. While this endpoint is marked obsolete, the same logic is used to trigger events, which means there will be duplicated events when the script is matched against");
}
}
}
12 changes: 9 additions & 3 deletions NBXplorer/Controllers/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,14 @@ public async Task<IActionResult> GetKeyInformations(string cryptoCode,

[HttpGet]
[Route($"{CommonRoutes.DerivationEndpoint}/scripts/{{script}}")]
[TrackedSourceContext.TrackedSourceContextRequirement(allowedTrackedSourceTypes:typeof(DerivationSchemeTrackedSource))]
[Route($"{CommonRoutes.AddressEndpoint}/scripts/{{script}}")]
[Route($"{CommonRoutes.WalletEndpoint}/scripts/{{script}}")]
[Route($"{CommonRoutes.TrackedSourceEndpoint}/scripts/{{script}}")]
public async Task<IActionResult> GetKeyInformations(TrackedSourceContext trackedSourceContext, [ModelBinder(BinderType = typeof(ScriptModelBinder))] Script script)
{
var derivationScheme = ((DerivationSchemeTrackedSource ) trackedSourceContext.TrackedSource).DerivationStrategy;
var result = (await trackedSourceContext.Repository.GetKeyInformations(new[] { script }))
.SelectMany(k => k.Value)
.FirstOrDefault(k => k.DerivationStrategy == derivationScheme);
.FirstOrDefault(k => k.TrackedSource == trackedSourceContext.TrackedSource);
if (result == null)
throw new NBXplorerError(404, "script-not-found", "The script does not seem to be tracked").AsException();
return Json(result, trackedSourceContext.Network.Serializer.Settings);
Expand Down Expand Up @@ -518,6 +519,11 @@ public async Task<IActionResult> TrackWallet(
(trackedSourceContext.TrackedSource is WalletTrackedSource ||
request?.ParentWallet is not null))
{
if (request?.ParentWallet == trackedSourceContext.TrackedSource)
{
throw new NBXplorerException(new NBXplorerError(400, "parent-wallet-same-as-tracked-source",
"Parent wallets cannot be the same as the tracked source"));
}
await postgresRepository.EnsureWalletCreated(trackedSourceContext.TrackedSource, request?.ParentWallet is null? null: new []{request?.ParentWallet });
}
if (repo is not PostgresRepository && request.ParentWallet is not null)
Expand Down
7 changes: 5 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,12 @@ Returns:

Note: `redeem` is returning the segwit redeem if the derivation scheme is a P2SH-P2WSH or P2WSH, or the p2sh redeem if just a p2sh.

## <a name="scriptPubKey"></a>Get scriptPubKey information of a Derivation Scheme
## <a name="scriptPubKey"></a>Get scriptPubKey information of a tracked source

`HTTP GET v1/cryptos/{cryptoCode}/derivations/{derivationScheme}/scripts/{script}`

`HTTP GET v1/cryptos/{cryptoCode}/addresses/{address}/scripts/{script}`
`HTTP GET v1/cryptos/{cryptoCode}/wallets/{walletId}/scripts/{script}`
`HTTP GET v1/cryptos/{cryptoCode}/tracked-sources/{trackedSource}/scripts/{script}`
Error codes:

* HTTP 404: `cryptoCode-not-supported`
Expand All @@ -498,6 +500,7 @@ Returns:
`HTTP GET v1/cryptos/{cryptoCode}/derivations/{derivationScheme}/utxos`
`HTTP GET v1/cryptos/{cryptoCode}/addresses/{address}/utxos`
`HTTP GET v1/cryptos/{cryptoCode}/wallets/{walletId}/utxos`
`HTTP GET v1/cryptos/{cryptoCode}/tracked-sources/{trackedSource}/utxos`

Error:

Expand Down

0 comments on commit ff942dd

Please sign in to comment.