Use TryGet pattern
MODIFY
WalletWasabi.Tests/UnitTests/Transactions/TransactionFactoryTests.cs
WalletWasabi.Tests/UnitTests/Transactions/TransactionFactoryTests.cs
@@ -296,7 +296,8 @@ namespace WalletWasabi.Tests.UnitTests.Transactions
Assert.Equal(2, tx.Outputs.Count);
var changeOutput = Assert.Single<TxOut>(tx.Outputs, x => x.ScriptPubKey == changeKey.ScriptPubKey);
- Assert.Null(transactionFactory.KeyManager.GetKeyForScriptPubKey(changeOutput.ScriptPubKey));
+ transactionFactory.KeyManager.TryGetKeyForScriptPubKey(changeOutput.ScriptPubKey, out HdPubKey? hdPubKey);
+ Assert.Null(hdPubKey);
Assert.Equal(Money.Coins(0.9m), changeOutput.Value + result.Fee);
}
@@ -331,7 +332,11 @@ 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.GetKeyForScriptPubKey(x.ScriptPubKey) is { });
+ var changeOutput = Assert.Single(tx.Outputs, x =>
+ {
+ transactionFactory.KeyManager.TryGetKeyForScriptPubKey(x.ScriptPubKey, out HdPubKey? hdPubKey);
+ return hdPubKey is { };
+ });
Assert.Equal(Money.Coins(0.1m), changeOutput.Value);
}

MODIFY
WalletWasabi/Blockchain/Keys/KeyManager.cs
WalletWasabi/Blockchain/Keys/KeyManager.cs
@@ -418,16 +418,19 @@ namespace WalletWasabi.Blockchain.Keys
}
}
- public HdPubKey? GetKeyForScriptPubKey(Script scriptPubKey)
+ public bool TryGetKeyForScriptPubKey(Script scriptPubKey, out HdPubKey? hdPubKey)
{
+ hdPubKey = default;
+
lock (ScriptHdPubKeyMapLock)
{
if (ScriptHdPubKeyMap.TryGetValue(scriptPubKey, out var key))
{
- return key;
+ hdPubKey = key;
+ return true;
}
- return default;
+ return false;
}
}

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

MODIFY
WalletWasabi/Blockchain/Transactions/TransactionFactory.cs
WalletWasabi/Blockchain/Transactions/TransactionFactory.cs
@@ -126,7 +126,8 @@ namespace WalletWasabi.Blockchain.Transactions
if (payments.TryGetCustomRequest(out DestinationRequest? custChange))
{
var changeScript = custChange.Destination.ScriptPubKey;
- changeHdPubKey = KeyManager.GetKeyForScriptPubKey(changeScript);
+ KeyManager.TryGetKeyForScriptPubKey(changeScript, out HdPubKey? hdPubKey);
+ changeHdPubKey = hdPubKey;
var changeStrategy = payments.ChangeStrategy;
if (changeStrategy == ChangeStrategy.Custom)
@@ -279,7 +280,7 @@ namespace WalletWasabi.Blockchain.Transactions
for (var i = 0U; i < tx.Outputs.Count; i++)
{
TxOut output = tx.Outputs[i];
- var foundKey = KeyManager.GetKeyForScriptPubKey(output.ScriptPubKey);
+ KeyManager.TryGetKeyForScriptPubKey(output.ScriptPubKey, out HdPubKey? foundKey);
if (foundKey is not null)
{
var smartCoin = new SmartCoin(smartTransaction, i, foundKey);

MODIFY
WalletWasabi/CoinJoin/Client/Clients/CoinJoinClient.cs
WalletWasabi/CoinJoin/Client/Clients/CoinJoinClient.cs
@@ -445,7 +445,9 @@ namespace WalletWasabi.CoinJoin.Client.Clients
else
{
// Should never happen, but oh well we can autocorrect it so why not.
- ExposedLinks[input] = ExposedLinks[input].Append(new HdPubKeyBlindedPair(DestinationKeyManager.GetKeyForScriptPubKey(activeOutput.Address.ScriptPubKey) ?? KeyManager.GetKeyForScriptPubKey(activeOutput.Address.ScriptPubKey), false));
+ DestinationKeyManager.TryGetKeyForScriptPubKey(activeOutput.Address.ScriptPubKey, out HdPubKey? hdPubKey);
+ KeyManager.TryGetKeyForScriptPubKey(activeOutput.Address.ScriptPubKey, out HdPubKey? hdPubK);
+ ExposedLinks[input] = ExposedLinks[input].Append(new HdPubKeyBlindedPair(hdPubKey ?? hdPubK, false));
}
}
}

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

Use TryGet pattern