Add receive function
MODIFY
MagicalCryptoWallet.Tests/RegTests.cs
MagicalCryptoWallet.Tests/RegTests.cs
@@ -445,7 +445,7 @@ namespace MagicalCryptoWallet.Tests
var wallet = new WalletService(keyManager, indexDownloader, nodes, blocksFolderPath);
// Get some money, make it confirm.
- var key = keyManager.GenerateNewKey("foo Label", KeyState.Clean, isInternal: false);
+ var key = wallet.GetReceiveKey("foo Label");
var txid = await Global.RpcClient.SendToAddressAsync(key.GetP2wpkhAddress(network), new Money(0.1m, MoneyUnit.BTC));
await Global.RpcClient.GenerateAsync(1);
@@ -484,9 +484,11 @@ namespace MagicalCryptoWallet.Tests
Assert.NotNull(firstCoin.SpentOutputs);
Assert.NotEmpty(firstCoin.SpentOutputs);
Assert.Equal(txid, firstCoin.TransactionId);
+ Assert.Single(keyManager.GetKeys(KeyState.Used, false));
+ Assert.Equal("foo Label", keyManager.GetKeys(KeyState.Used, false).First().Label);
// Get some money, make it confirm.
- key = keyManager.GenerateNewKey("", KeyState.Clean, isInternal: false);
+ key = wallet.GetReceiveKey("");
await Global.RpcClient.SendToAddressAsync(key.GetP2wpkhAddress(network), new Money(0.1m, MoneyUnit.BTC));
await Global.RpcClient.GenerateAsync(1);

MODIFY
MagicalCryptoWallet/Services/WalletService.cs
MagicalCryptoWallet/Services/WalletService.cs
@@ -175,6 +175,31 @@ namespace MagicalCryptoWallet.Services
}
}
+ public HdPubKey GetReceiveKey(string label)
+ {
+ // ToDo, put this correction pattern into the Guard class: Guard.Correct(string ...)
+ if(string.IsNullOrWhiteSpace(label))
+ {
+ label = "";
+ }
+ else
+ {
+ label = label.Trim();
+ }
+
+ // Make sure there's always 21 clean keys generated and indexed.
+ while (KeyManager.GetKeys(KeyState.Clean, false).Count() < 21)
+ {
+ KeyManager.GenerateNewKey("", KeyState.Clean, false);
+ }
+
+ var ret = KeyManager.GetKeys(KeyState.Clean, false).RandomElement();
+
+ ret.Label = label;
+
+ return ret;
+ }
+
private void ProcessBlock(Height height, Block block)
{
var keys = KeyManager.GetKeys().ToList();

Add receive function