def test_extended_key_string(self):
        # Also tests extended_key, WIF and privkey_bytes
        assert mprivkey.extended_key_string(Bitcoin) == MXPRV
        chg_master = mprivkey.child(1)
        chg5 = chg_master.child(5)
        assert chg5.WIF(Bitcoin) == 'L5kTYMuajTGWdYiMoD4V8k6LS4Bg3HFMA5UGTfxG9Wh7UKu9CHFC'
        ext_key_base58 = chg5.extended_key_string(Bitcoin)
        assert ext_key_base58 == 'xprv9x12y3UYL4ZhYxiFzf3k2xwRmN9w6Ah9MRQSqpPC67WBFMTA2m1evkCKidz7UYBa5i8QwxmU9Ju7giqEmcPRXKXwzgAJwssNeZNQLPT3LAY'

        # Check can recreate
        dup, coin = bip32.from_extended_key_string(ext_key_base58)
        assert coin is Bitcoin
        assert dup.chain_code == chg5.chain_code
        assert dup.n == chg5.n == 5
        assert dup.depth == chg5.depth == 2
        assert dup.ec_point() == chg5.ec_point()
    def test_extended_key_string(self):
        # Implictly tests extended_key()
        assert mpubkey.extended_key_string(Bitcoin) == MXPUB
        chg_master = mpubkey.child(1)
        chg5 = chg_master.child(5)
        assert chg5.address(Bitcoin) == '1BsEFqGtcZnVBbPeimcfAFTitQdTLvUXeX'
        assert chg5.extended_key_string(Bitcoin) == 'xpub6AzPNZ1SAS7zmSnj6gakQ6tAKPzRVdQzieL3eCnoeT3A89nJaJKuUYWoZuYp8xWhCs1gF9yXAwGg7zKYhvCfhk9jrb1bULhLkQCwtB1Nnn1'

        ext_key_base58 = chg5.extended_key_string(Bitcoin)
        assert ext_key_base58 == 'xpub6AzPNZ1SAS7zmSnj6gakQ6tAKPzRVdQzieL3eCnoeT3A89nJaJKuUYWoZuYp8xWhCs1gF9yXAwGg7zKYhvCfhk9jrb1bULhLkQCwtB1Nnn1'

        # Check can recreate
        dup, coin = bip32.from_extended_key_string(ext_key_base58)
        assert coin is Bitcoin
        assert dup.chain_code == chg5.chain_code
        assert dup.n == chg5.n == 5
        assert dup.depth == chg5.depth == 2
        assert dup.ec_point() == chg5.ec_point()
#
# Tests of wallet/bip32.py
#

import pytest

import wallet.bip32 as bip32
from lib.coins import BitcoinCash as Bitcoin, CoinError
from lib.hash import Base58

MXPRV = 'xprv9s21ZrQH143K2gMVrSwwojnXigqHgm1khKZGTCm7K8w4PmuDEUrudk11ZBxhGPUiUeVcrfGLoZmt8rFNRDLp18jmKMcVma89z7PJd2Vn7R9'
MPRIVKEY = b';\xf4\xbfH\xd20\xea\x94\x01_\x10\x1b\xc3\xb0\xff\xc9\x17$?K\x02\xe5\x82R\xe5\xb3A\xdb\x87&E\x00'
MXPUB = 'xpub661MyMwAqRbcFARxxUUxAsjGGifn6Djc4YUsFbAisUU3GaEMn2BABYKVQTHrDtwvSfgY2bK8aFGyCNmB52SKjkFGP18sSRTNn1sCeez7Utd'

mpubkey, mpubcoin = bip32.from_extended_key_string(MXPUB)
mprivkey, mprivcoin = bip32.from_extended_key_string(MXPRV)


def test_from_extended_key():
    # Tests the failure modes of from_extended_key.
    with pytest.raises(TypeError):
        bip32._from_extended_key('')
    with pytest.raises(ValueError):
        bip32._from_extended_key(b'')
    with pytest.raises(CoinError):
        bip32._from_extended_key(bytes(78))
    # Invalid prefix byte
    raw = Base58.decode_check(MXPRV)
    with pytest.raises(ValueError):
        bip32._from_extended_key(raw[:45] + b'\1' + raw[46:])