From 5d74038aad12a3469530127698b54bdd1ba8b4ed Mon Sep 17 00:00:00 2001 From: Davide Grilli Date: Tue, 7 Jul 2026 16:24:23 +0200 Subject: [PATCH] test(wallet): cover ImportedKeyAccount edge cases and fund-safety fallbacks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Empty entry list rejection, GetPublicKey boundaries (mirroring the existing GetPrivateKey tests), and explicit assertions that change and out-of-range indices always fall back to the first imported address — never to an address the wallet doesn't hold keys for. --- .../Wallet/ImportedKeyAccountTests.cs | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/PalladiumWallet.Tests/Wallet/ImportedKeyAccountTests.cs b/tests/PalladiumWallet.Tests/Wallet/ImportedKeyAccountTests.cs index 591a49f..f0950d4 100644 --- a/tests/PalladiumWallet.Tests/Wallet/ImportedKeyAccountTests.cs +++ b/tests/PalladiumWallet.Tests/Wallet/ImportedKeyAccountTests.cs @@ -82,6 +82,55 @@ public class ImportedKeyAccountTests Assert.Null(account.GetPrivateKey(false, 0)); } + [Fact] + public void Lista_vuota_viene_rifiutata() + { + Assert.Throws( + () => new ImportedKeyAccount([], ScriptKind.NativeSegwit, Profile)); + } + + [Fact] + public void GetAddress_fuori_range_o_change_ricade_sul_primo_indirizzo() + { + var keys = Enumerable.Range(0, 2).Select(_ => GenerateKey()).ToList(); + var entries = keys + .Select(k => (k.PubKey.GetAddress(ScriptPubKeyType.Segwit, Network), (Key?)k)) + .ToList(); + var account = new ImportedKeyAccount(entries, ScriptKind.NativeSegwit, Profile); + var first = entries[0].Item1.ToString(); + + // Fund safety: change and any out-of-range index must map to the first + // address, never to an address the wallet does not control. + Assert.Equal(first, account.GetAddress(isChange: true, 1).ToString()); + Assert.Equal(first, account.GetAddress(isChange: false, -1).ToString()); + Assert.Equal(first, account.GetAddress(isChange: false, 99).ToString()); + Assert.Equal(first, account.GetChangeAddress(5).ToString()); + Assert.Equal(entries[1].Item1.ToString(), account.GetAddress(isChange: false, 1).ToString()); + } + + [Fact] + public void GetPublicKey_segue_gli_stessi_confini_di_GetPrivateKey() + { + var key = GenerateKey(); + var addr = key.PubKey.GetAddress(ScriptPubKeyType.Segwit, Network); + var account = new ImportedKeyAccount([(addr, key)], ScriptKind.NativeSegwit, Profile); + + Assert.Equal(key.PubKey, account.GetPublicKey(false, 0)); + Assert.Null(account.GetPublicKey(true, 0)); + Assert.Null(account.GetPublicKey(false, -1)); + Assert.Null(account.GetPublicKey(false, 99)); + } + + [Fact] + public void GetPublicKey_e_null_per_una_entry_watch_only() + { + var key = GenerateKey(); + var addr = key.PubKey.GetAddress(ScriptPubKeyType.Segwit, Network); + var account = new ImportedKeyAccount([(addr, (Key?)null)], ScriptKind.NativeSegwit, Profile); + + Assert.Null(account.GetPublicKey(false, 0)); + } + [Fact] public void FixedAddresses_copre_tutti_gli_indirizzi() {