# -*- coding: utf-8 -*-
"""
Asserts that BtcqMainnet/BtcqTestnet/BtcqRegtest in electrum/constants.py
match the daemon's btcq-beta finalized chainparams. Catches drift between
wallet and daemon — see packaging spec §0.

Reference: ~/code/btcq/src/kernel/chainparams.cpp on branch btcq-beta.
"""
import unittest

from electrum import constants
from electrum.constants import BtcqMainnet, BtcqTestnet, BtcqRegtest


class BtcqMainnetChainparams(unittest.TestCase):
    def test_address_prefixes(self):
        # Per daemon CMainParams::base58Prefixes (btcq-beta)
        self.assertEqual(BtcqMainnet.WIF_PREFIX, 0x9A)
        self.assertEqual(BtcqMainnet.ADDRTYPE_P2PKH, 0x1A)
        self.assertEqual(BtcqMainnet.ADDRTYPE_P2SH, 0x3A)

    def test_bip44_coin_type(self):
        # Per daemon walletutil.cpp GenerateWalletDescriptor (btcq-beta)
        self.assertEqual(BtcqMainnet.BIP44_COIN_TYPE, 2827)

    def test_xprv_xpub_standard_headers(self):
        # Per daemon EXT_SECRET_KEY / EXT_PUBLIC_KEY (btcq-beta)
        self.assertEqual(BtcqMainnet.XPRV_HEADERS['standard'], 0x09C58519)
        self.assertEqual(BtcqMainnet.XPUB_HEADERS['standard'], 0x09C59A73)

    def test_segwit_hrp(self):
        self.assertEqual(BtcqMainnet.SEGWIT_HRP, 'btcq')

    def test_genesis_deferred(self):
        # Daemon mainnet genesis nNonce + nTime are placeholders pending
        # launch day; wallet matches that "TBD" posture with empty string.
        self.assertEqual(BtcqMainnet.GENESIS, '')


class BtcqTestnetChainparams(unittest.TestCase):
    def test_address_prefixes(self):
        self.assertEqual(BtcqTestnet.WIF_PREFIX, 0xE1)
        self.assertEqual(BtcqTestnet.ADDRTYPE_P2PKH, 0x55)
        self.assertEqual(BtcqTestnet.ADDRTYPE_P2SH, 0x78)

    def test_bip44_coin_type(self):
        self.assertEqual(BtcqTestnet.BIP44_COIN_TYPE, 1)

    def test_xprv_xpub_standard_headers(self):
        self.assertEqual(BtcqTestnet.XPRV_HEADERS['standard'], 0x140663A7)
        self.assertEqual(BtcqTestnet.XPUB_HEADERS['standard'], 0x14067901)

    def test_segwit_hrp(self):
        self.assertEqual(BtcqTestnet.SEGWIT_HRP, 'btcqt')

    def test_genesis_mined(self):
        # Per daemon CTestNetParams::genesis. Bumped to the v2 hard-fork
        # genesis on 2026-04-29 (BTCQ-015 uniform SHA3-256 merkle).
        # See electrum/constants.py:221 and CLAUDE.md "Past chainparams
        # gaps" for the context on why this constant lagged behind the
        # btcq.5 wallet release.
        self.assertEqual(
            BtcqTestnet.GENESIS,
            'fcd9a20fc86d9316f80996bd9e05d0a2817511e13e6d5aab7e651bb37c95750b',
        )


class BtcqRegtestChainparams(unittest.TestCase):
    def test_inherits_testnet_address_prefixes(self):
        self.assertEqual(BtcqRegtest.WIF_PREFIX, 0xE1)
        self.assertEqual(BtcqRegtest.ADDRTYPE_P2PKH, 0x55)
        self.assertEqual(BtcqRegtest.ADDRTYPE_P2SH, 0x78)

    def test_inherits_bip44_coin_type(self):
        self.assertEqual(BtcqRegtest.BIP44_COIN_TYPE, 1)

    def test_overrides_segwit_hrp(self):
        self.assertEqual(BtcqRegtest.SEGWIT_HRP, 'btcqrt')

    def test_genesis_mined(self):
        # Per daemon CRegTestParams::genesis assertion (btcq-beta)
        self.assertEqual(
            BtcqRegtest.GENESIS,
            '3c75fbfdccf783067c26930a6b584bbd466ba123a5be49413e5f2fecd19c8cca',
        )


class InheritedBitcoinClassesRemoved(unittest.TestCase):
    """Per packaging spec §0 decision (b): inherited Bitcoin classes
    removed so users can't accidentally land on Bitcoin mainnet via a
    CLI flag (they're currently in NETS_LIST via all_subclasses)."""

    def test_bitcoin_classes_absent(self):
        for cls_name in [
            'BitcoinMainnet', 'BitcoinTestnet', 'BitcoinTestnet4',
            'BitcoinRegtest', 'BitcoinSimnet', 'BitcoinSignet',
            'BitcoinMutinynet',
        ]:
            self.assertFalse(
                hasattr(constants, cls_name),
                f"{cls_name} should be removed per packaging spec §0",
            )


class NoSegwitXprvXpubRows(unittest.TestCase):
    """Per packaging spec §0 decision (b): only 'standard' XPRV/XPUB
    headers remain. BTCQ uses all-hardened ML-DSA-65 derivation; the
    yprv/zprv/Yprv/Zprv segwit-style rows don't apply."""

    def test_btcq_mainnet_only_standard(self):
        self.assertEqual(set(BtcqMainnet.XPRV_HEADERS.keys()), {'standard'})
        self.assertEqual(set(BtcqMainnet.XPUB_HEADERS.keys()), {'standard'})

    def test_btcq_testnet_only_standard(self):
        self.assertEqual(set(BtcqTestnet.XPRV_HEADERS.keys()), {'standard'})
        self.assertEqual(set(BtcqTestnet.XPUB_HEADERS.keys()), {'standard'})


if __name__ == '__main__':
    unittest.main()
