Refactor getkeys
MODIFY
WalletWasabi.Gui/Controls/WalletExplorer/ReceiveTabViewModel.cs
WalletWasabi.Gui/Controls/WalletExplorer/ReceiveTabViewModel.cs
@@ -115,6 +115,15 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
{
_addresses.Add(new AddressViewModel(key));
}
+
+ foreach (HdPubKey key in Global.WalletService.KeyManager.GetKeys(x =>
+ x.HasLabel()
+ && !x.IsInternal()
+ && x.KeyState == KeyState.Clean)
+ .Reverse())
+ {
+ _addresses.Add(new AddressViewModel(key));
+ }
}
public ObservableCollection<AddressViewModel> Addresses

MODIFY
WalletWasabi.Tests/RegTests.cs
WalletWasabi.Tests/RegTests.cs
@@ -600,8 +600,8 @@ namespace WalletWasabi.Tests
Assert.Equal(42, keyManager.GetKeys(KeyState.Clean).Count());
Assert.Equal(58, keyManager.GetKeys().Count());
- Assert.Single(keyManager.GetKeys(KeyState.Used, false).Where(x => x.Label == "foo label"));
- Assert.Single(keyManager.GetKeys(KeyState.Used, false).Where(x => x.Label == "bar label"));
+ Assert.Single(keyManager.GetKeys(x => x.Label == "foo label" && x.KeyState == KeyState.Used && !x.IsInternal()));
+ Assert.Single(keyManager.GetKeys(x => x.Label == "bar label" && x.KeyState == KeyState.Used && !x.IsInternal()));
// REORG TESTS
var txid4 = await rpc.SendToAddressAsync(key2.GetP2wpkhAddress(network), Money.Coins(0.03m), replaceable: true);

MODIFY
WalletWasabi/KeyManagement/KeyManager.cs
WalletWasabi/KeyManagement/KeyManager.cs
@@ -7,6 +7,7 @@ using System.IO;
using System.Linq;
using System.Text;
using System.Security;
+using System;
namespace WalletWasabi.KeyManagement
{
@@ -154,7 +155,7 @@ namespace WalletWasabi.KeyManagement
km.SetFilePath(filePath);
lock (km.HdPubKeyScriptBytesLock)
{
- km.HdPubKeyScriptBytes.AddRange(km.GetKeys().Select(x => x.GetP2wpkhScript().ToCompressedBytes()));
+ km.HdPubKeyScriptBytes.AddRange(km.GetKeys(x => true).Select(x => x.GetP2wpkhScript().ToCompressedBytes()));
}
lock (km.ScriptHdPubkeyMapLock)
@@ -229,26 +230,38 @@ namespace WalletWasabi.KeyManagement
}
}
- public IEnumerable<HdPubKey> GetKeys(KeyState? keyState = null, bool? isInternal = null)
+ public IEnumerable<HdPubKey> GetKeys(Func<HdPubKey, bool> wherePredicate)
{
// BIP44-ish derivation scheme
// m / purpose' / coin_type' / account' / change / address_index
lock (HdPubKeysLock)
+ {
+ if (wherePredicate is null)
+ {
+ return HdPubKeys.ToList();
+ }
+ else
+ {
+ return HdPubKeys.Where(wherePredicate).ToList();
+ }
+ }
+ }
+
+ public IEnumerable<HdPubKey> GetKeys(KeyState? keyState = null, bool? isInternal = null)
{
if (keyState is null && isInternal is null)
{
- return HdPubKeys;
+ return GetKeys(x => true);
}
- if (isInternal is null && !(keyState is null))
+ if (isInternal is null && keyState != null)
{
- return HdPubKeys.Where(x => x.KeyState == keyState);
+ return GetKeys(x => x.KeyState == keyState);
}
else if (keyState is null)
{
- return HdPubKeys.Where(x => x.IsInternal() == isInternal);
- }
- return HdPubKeys.Where(x => x.IsInternal() == isInternal && x.KeyState == keyState);
+ return GetKeys(x => x.IsInternal() == isInternal);
}
+ return GetKeys(x => x.IsInternal() == isInternal && x.KeyState == keyState);
}
public IEnumerable<byte[]> GetPubKeyScriptBytes()

MODIFY
WalletWasabi/Services/CcjClient.cs
WalletWasabi/Services/CcjClient.cs
@@ -413,9 +413,7 @@ namespace WalletWasabi.Services
BitcoinAddress changeAddress = null;
var activeOutputAddresses = new List<BitcoinAddress>();
- if (changeAddress is null || !activeOutputAddresses.Any())
- {
- IEnumerable<HdPubKey> allUnusedInternalKeys = KeyManager.GetKeys(keyState: null, isInternal: true).Where(x => x.KeyState != KeyState.Used);
+ IEnumerable<HdPubKey> allUnusedInternalKeys = KeyManager.GetKeys(x => x.KeyState != KeyState.Used);
if (changeAddress is null)
{
@@ -500,7 +498,6 @@ namespace WalletWasabi.Services
AccessCache.AddOrReplace(ac, DateTimeOffset.UtcNow);
}
}
- }
KeyManager.ToFile();
@@ -834,7 +831,7 @@ namespace WalletWasabi.Services
if (coinWaitingForMix.Label == "ZeroLink Change" && coinWaitingForMix.Unspent)
{
coinWaitingForMix.Label = "ZeroLink Dequeued Change";
- var key = KeyManager.GetKeys().SingleOrDefault(x => x.GetP2wpkhScript() == coinWaitingForMix.ScriptPubKey);
+ var key = KeyManager.GetKeys(x => x.GetP2wpkhScript() == coinWaitingForMix.ScriptPubKey).SingleOrDefault();
if (!(key is null))
{
key.SetLabel(coinWaitingForMix.Label, KeyManager);

MODIFY
WalletWasabi/Services/WalletService.cs
WalletWasabi/Services/WalletService.cs
@@ -1008,7 +1008,7 @@ namespace WalletWasabi.Services
{
newLabel = Guard.Correct(newLabel);
coin.Label = newLabel;
- var key = KeyManager.GetKeys().SingleOrDefault(x => x.GetP2wpkhScript() == coin.ScriptPubKey);
+ var key = KeyManager.GetKeys(x => x.GetP2wpkhScript() == coin.ScriptPubKey).SingleOrDefault();
if (!(key is null))
{
key.SetLabel(newLabel, KeyManager);

Refactor getkeys