Use TryGetKeyForScriptPubKey properly
MODIFY
WalletWasabi.Tests/UnitTests/Transactions/TransactionFactoryTests.cs
WalletWasabi.Tests/UnitTests/Transactions/TransactionFactoryTests.cs
@@ -296,8 +296,7 @@ namespace WalletWasabi.Tests.UnitTests.Transactions
Assert.Equal(2, tx.Outputs.Count);
var changeOutput = Assert.Single<TxOut>(tx.Outputs, x => x.ScriptPubKey == changeKey.ScriptPubKey);
- transactionFactory.KeyManager.TryGetKeyForScriptPubKey(changeOutput.ScriptPubKey, out HdPubKey? hdPubKey);
- Assert.Null(hdPubKey);
+ Assert.False(transactionFactory.KeyManager.TryGetKeyForScriptPubKey(changeOutput.ScriptPubKey, out _));
Assert.Equal(Money.Coins(0.9m), changeOutput.Value + result.Fee);
}
@@ -332,7 +331,7 @@ namespace WalletWasabi.Tests.UnitTests.Transactions
var destination2Output = Assert.Single<TxOut>(tx.Outputs, x => x.ScriptPubKey == key2.ScriptPubKey);
Assert.Equal(Money.Coins(0.3m), destination2Output.Value + result.Fee);
- var changeOutput = Assert.Single(tx.Outputs, x => transactionFactory.KeyManager.TryGetKeyForScriptPubKey(x.ScriptPubKey, out HdPubKey? _));
+ var changeOutput = Assert.Single(tx.Outputs, x => transactionFactory.KeyManager.TryGetKeyForScriptPubKey(x.ScriptPubKey, out _));
Assert.Equal(Money.Coins(0.1m), changeOutput.Value);
}

MODIFY
WalletWasabi/Blockchain/Keys/KeyManager.cs
WalletWasabi/Blockchain/Keys/KeyManager.cs
@@ -418,7 +418,7 @@ namespace WalletWasabi.Blockchain.Keys
}
}
- public bool TryGetKeyForScriptPubKey(Script scriptPubKey, out HdPubKey? hdPubKey)
+ public bool TryGetKeyForScriptPubKey(Script scriptPubKey, [NotNullWhen(true)] out HdPubKey? hdPubKey)
{
hdPubKey = default;

MODIFY
WalletWasabi/Blockchain/TransactionProcessing/TransactionProcessor.cs
WalletWasabi/Blockchain/TransactionProcessing/TransactionProcessor.cs
@@ -204,8 +204,7 @@ namespace WalletWasabi.Blockchain.TransactionProcessing
{
// If transaction received to any of the wallet keys:
var output = tx.Transaction.Outputs[i];
- KeyManager.TryGetKeyForScriptPubKey(output.ScriptPubKey, out HdPubKey? foundKey);
- if (foundKey is { })
+ if (KeyManager.TryGetKeyForScriptPubKey(output.ScriptPubKey, out HdPubKey? foundKey))
{
if (!foundKey.IsInternal)
{

MODIFY
WalletWasabi/Blockchain/Transactions/TransactionFactory.cs
WalletWasabi/Blockchain/Transactions/TransactionFactory.cs
@@ -280,8 +280,7 @@ namespace WalletWasabi.Blockchain.Transactions
for (var i = 0U; i < tx.Outputs.Count; i++)
{
TxOut output = tx.Outputs[i];
- KeyManager.TryGetKeyForScriptPubKey(output.ScriptPubKey, out HdPubKey? foundKey);
- if (foundKey is not null)
+ if (KeyManager.TryGetKeyForScriptPubKey(output.ScriptPubKey, out HdPubKey? foundKey))
{
var smartCoin = new SmartCoin(smartTransaction, i, foundKey);
label = SmartLabel.Merge(label, smartCoin.HdPubKey.Label); // foundKey's label is already added to the coinlabel.

MODIFY
WalletWasabi/Extensions/NBitcoinExtensions.cs
WalletWasabi/Extensions/NBitcoinExtensions.cs
@@ -400,8 +400,7 @@ namespace NBitcoin
{
if (script is { })
{
- keyManager.TryGetKeyForScriptPubKey(script, out HdPubKey? hdPubKey);
- if (hdPubKey is { })
+ if (keyManager.TryGetKeyForScriptPubKey(script, out HdPubKey? hdPubKey))
{
psbt.AddKeyPath(fp, hdPubKey, script);
}
@@ -411,8 +410,7 @@ namespace NBitcoin
// Add output keypaths.
foreach (var script in psbt.Outputs.Select(x => x.ScriptPubKey).ToArray())
{
- keyManager.TryGetKeyForScriptPubKey(script, out HdPubKey? hdPubKey);
- if (hdPubKey is { })
+ if (keyManager.TryGetKeyForScriptPubKey(script, out HdPubKey? hdPubKey))
{
psbt.AddKeyPath(fp, hdPubKey, script);
}

Use TryGetKeyForScriptPubKey properly