test(wallet): cover ImportedKeyAccount edge cases and fund-safety fallbacks

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.
This commit is contained in:
2026-07-07 16:24:23 +02:00
parent db5b65ca0d
commit 5d74038aad
@@ -82,6 +82,55 @@ public class ImportedKeyAccountTests
Assert.Null(account.GetPrivateKey(false, 0));
}
[Fact]
public void Lista_vuota_viene_rifiutata()
{
Assert.Throws<ArgumentException>(
() => 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()
{