# needs to run against simulator with "--multi-mig" flag as multisigs need to be there before login sequence executes

import base64, pytest, time
from test_640_miniscript_migration import (msc0, msc1, msc2, msc3, msc4, msc5, msc6, msc7, msc8, msc9, msc10,
                                           msc11, msc12, msc14, msc15, msc16, msc17, msc18, msc19, msc20)

# MULTISIGS
ms0 = ['ms0', (2, 2), [(1130956047, 1, 'tpubDF2rnouQaaYrXF4noGTv6rQYmx87cQ4GrUdhpvXkhtChwQPbdGTi8GA88NUaSrwZBwNsTkC9bFkkC8vDyGBVVAQTZ2AS6gs68RQXtXcCvkP'), (4118082990, 0, 'tpubDDX85PzueTZjod816TDBdJPk8vWhqyZkSAXJ5xUjvSd1PyuEKnjt5UxiinKJSZzTTFVGSsSEm57LtpxQGdmSjQJtBmz1KUKtA9H63EzZmbA')], {'d': ['m/44h/1h/0h', 'm/48h/1h/0h/2h'], 'ch': 'XTN', 'ft': 14}, 0]
ms_psbt0 = 'cHNidP8BAgQCAAAAAQMEAAAAAAEEAQEBBQEBAfsEAgAAAAABAJACAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/////wMBZgD/////AgDyBSoBAAAAIgAgmpn9BiIVcQF8SNxOBdxHZnr4zV50wqEfgao3H2nXwQYAAAAAAAAAACZqJKohqe3i9hw/cdHe/T+pmd+jaVN1XGkGiXmZYrSL69g2l06M+QAAAAABASsA8gUqAQAAACIAIJqZ/QYiFXEBfEjcTgXcR2Z6+M1edMKhH4GqNx9p18EGAQVHUiEDpu0LHSZwTffTfIc4jmXAz2wEHnpdj8wqeEmXnjhsLmQhA2TIP6eApQSJp8iL+LUKERNbAllqpwd359L99FBGOPdtUq4iAgNkyD+ngKUEiafIi/i1ChETWwJZaqcHd+fS/fRQRjj3bUcwRAIgWd1qBvLJ3w7BCnKnf/lqC2NWx+k2ZckyogR7jvIa6Z0CIEGD4eI8QB34FHub2MqS6+V4pKbAVPW9Yb2f3e+3u6uhAQEDBAEAAAAiBgNkyD+ngKUEiafIi/i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jaVN1XGkGiXmZYrSL69g2l06M+QAAAAABASsA8gUqAQAAACIAIDuxLdT3NLSdNvgYc0siGgMlJyNPlw3iUhtfvclWGCoUAQVHUiEDgwkO0ZNeLYTc0jARk0ubSvJSVLUeWXbOJI5gspAixFghA6btCx0mcE3303yHOI5lwM9sBB56XY/MKnhJl544bC5kUq4iAgODCQ7Rk14thNzSMBGTS5tK8lJUtR5Zds4kjmCykCLEWEcwRAIgI2cs69L4CIcU83erd/vww+0gfITnEDGSVTfCl55d33ICIDujRu9l8AUSkHUaz7syn5mJwnP81D3pxUYIBvoVmX30AQEDBAEAAAAiBgODCQ7Rk14thNzSMBGTS5tK8lJUtR5Zds4kjmCykCLEWBgWL00mLAAAgAEAAIAAAACAAAAAAAAAAAAiBgOm7QsdJnBN99N8hziOZcDPbAQeel2PzCp4SZeeOGwuZBwPBWlDMAAAgAEAAIAAAACAAgAAgAAAAAAAAAAAAQ4g1dxbBflVNuLZ2Ul1HWuYv3YvB+1WVb8try3mMtNWnpEBDwQAAAAAARAE/f///wABAUdSIQI1DhBwGxC7cQhnJ80CPFsg5dA/8ZVi447B1hj12FYq8yEDo6dsb749Oh2LqODbufgw5FiStoOjcNyOM9FOuOai1vpSriICAjUOEHAbELtxCGcnzQI8WyDl0D/xlWLjjsHWGPXYVirzGBYvTSYsAACAAQAAgAAAAIAAAAAAAQAAACICA6OnbG++PTodi6jg27n4MORYkraDo3DcjjPRTrjmotb6HA8FaUMwAACAAQAAgAAAAIACAACAAAAAAAEAAAABBCIAIBzbGbUkOtQUlU758Be6etJ319rIzQhJ2CMnsdGC4PFQAQMISOYFKgEAAAAA'

ms2 = ['ms2', (2, 2), [(1130956047, 1, 'tpubDF2rnouQaaYrUEy2JM1YD3RFzew4onawGM4X2Re67gguTf5CbHonBRiFGe3Xjz7DK88dxBFGf2i7K1hef3PM4cFKyUjcbJXddaY9F5tJBoP'), (2783214288, 0, 'tpubDCqWSUR4xtNPhMrVjQ2h5rdN2BACCHfviVnUrAynei9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c4Rl9fGA8FaUNUAACAAQAAgAAAAIAAAAAACgAAACICA7bKzo4U5agufymR41/EHE2ocQu5rcSUbxU7tSzWgubmDEFpIYcAAAAACgAAAAAA'

ms4 = ["ms4", [3, 5], [[1130956047, 1, "tpubDF2rnouQaaYrXF4noGTv6rQYmx87cQ4GrUdhpvXkhtChwQPbdGTi8GA88NUaSrwZBwNsTkC9bFkkC8vDyGBVVAQTZ2AS6gs68RQXtXcCvkP"], [4061361782, 0, "tpubDCmGdTYoJo9JwhbqhsFKu9V4CZpUNUq5girUxkhqhk1GzgNRawhoqLBAntdYahF9vhkib1zjQSQPiV61ZJ6J2tpa28Vt5kgvY7RuhBSE8Hm"], [109302970, 0, "tpubDDgf715teaqpZnc6uBnFc5Sz5WAh3595Avp1Nw9wqFmHCFSxhuv2zY6oB6JJidjB8qVMeGMpsAGBw1KjDy5uhvJ9zmvMpDz2w3Zk8UJCay8"], [2963526823, 0, "tpubDCLbYkfCtNo2XbFuwNJC3fQ2UBxhJJNpDVdtEBL87nxCRXYoynBabhzkCz94xFe84GtYS6hNxqh34X8SfdXFkg7E7CHDXfjiATp8EQT1DFK"], [17824383, 0, "tpubDCRBBeN8XiDyNDazsGvcWUQ6rUkbp86tkFzHckdXGPyVP8oaHm9DWt4CGQk79ZQj6JTTFmQGCQeFJbk3ULknu8mqyyaVP1guAAh9ocBzNdP"]], {"d": ["m/44h/1h/0h", "m/48h/1h/0h/2h"], "ch": "XTN", "ft": 14}, 0]
ms_psbt4 = "cHNidP8BAgQCAAAAAQMEAAAAAAEEAQEBBQEBAfsEAgAAAAABAJACAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/////wMBZgD/////AgDyBSoBAAAAIgAgs8DZXkorAp7+aGi3KRhfm6t/BdA+Dgm8bQZWnJ82iXgAAAAAAAAAACZqJKohqe3i9hw/cdHe/T+pmd+jaVN1XGkGiXmZYrSL69g2l06M+QAAAAABASsA8gUqAQAAACIAILPA2V5KKwKe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NRHtSrm0xdep1NEhA79sK90WXZCKIiP5B4ZGT3itlhA+LQc5uh2yHfr+Ph1ZU64iBgI5IfPOIeqRJEZbJbw90VpvHLhoT7mhGNflnY9nH5RpaRhx0Vm3LAAAgAEAAIAAAACAAAAAAAAAAAAiBgNADORkKHWjlIwstIoCPgL3g4YJpD81Ee1KubTF16nU0RwPBWlDMAAAgAEAAIAAAACAAQAAgAAAAAAAAAAAIgYDv2wr3RZdkIoiI/kHhkZPeK2WED4tBzm6HbId+v4+HVkY+zzV8SwAAIABAACAAAAAgAAAAAAAAAAAAAEAIgAgGRsELUF4c3z6lfN0TBxMdtdIcfY3RmVLUFgdSOiGkw8BAWlRIQJRPpoNbvU/xmH/V7O+MoeYusljQuoK+lo2AQlq0fxBMyEC/Qc6zjeVbsNxejwzmxsKDAFaFFSaroUnhXNgToKTjuUhA4mgQByfozPkgmIIhTWOPBs3dPU0X6FoXoJSvAM0VIHJU64iAgJRPpoNbvU/xmH/V7O+MoeYusljQuoK+lo2AQlq0fxBMxj7PNXxLAAAgAEAAIAAAACAAAAAAAEAAAAiAgL9BzrON5Vuw3F6PDObGwoMAVoUVJquhSeFc2BOgpOO5Rhx0Vm3LAAAgAEAAIAAAACAAAAAAAEAAAAiAgOJoEAcn6Mz5IJiCIU1jjwbN3T1NF+haF6CUrwDNFSByRwPBWlDMAAAgAEAAIAAAACAAQAAgAAAAAABAAAAAA=="


def test_multisig(settings_set, settings_get, start_sign, end_sign, goto_home, pick_menu_item, cap_menu,
                  clear_miniscript, cap_story):
    # # try one by one
    # for ms, psbt in [(ms0, ms_psbt0), (ms1, ms_psbt1), (ms2, ms_psbt2), (ms3, ms_psbt3)]:
    #     clear_miniscript()
    #     name = ms[0]
    #     settings_set("multisig", [ms])
    #     goto_home()
    #     pick_menu_item("Settings")
    #     pick_menu_item("Multisig/Miniscript")  # migration happens here
    #     time.sleep(.1)
    #     assert name in cap_menu()
    #     assert settings_get("multisig", None) is None
    #     msc = settings_get("miniscript")
    #     assert len(msc) == 1
    #     assert len(msc[0]) == 4  # new format (name, policy, keys, opts)
    #     assert msc[0][0] == name
    #     try_sign(base64.b64decode(psbt))

    # now try bulk migration
    # clear_miniscript()
    # settings_set("multisig", [ms0, ms1, ms2, ms3])
    goto_home()
    pick_menu_item("Settings")
    pick_menu_item("Multisig/Miniscript")  # migration happens here
    menu = cap_menu()
    for name in ["ms0", "ms1", "ms2", "ms3", "ms4", "ms5"]:
        assert name in menu

    assert len(settings_get("multisig")) == 6  # preserved
    msc = settings_get("miniscript")
    assert len(msc) == 6
    for x in msc:
        assert len(x) == 4  # new format (name, policy, keys, opts)

    for i, psbt in enumerate([ms_psbt0, ms_psbt1, ms_psbt2, ms_psbt3, ms_psbt4, ms_psbt5]):
        start_sign(base64.b64decode(psbt), miniscript="ms%d" % i)
        time.sleep(.1)
        title, story = cap_story()
        assert title == "OK TO SEND?"
        assert "WARNING" not in story
        assert "Limited Signing" not in story
        end_sign()


def test_multisig_miniscript_migration(settings_append, clear_miniscript, settings_get,
                                       settings_remove, settings_set, goto_home, pick_menu_item):

    # clear_miniscript()
    # settings_remove("multisig")

    for msc in [msc0, msc1, msc2, msc3, msc4, msc5, msc6, msc7, msc8, msc9, msc10,
                msc11, msc12, msc14, msc15, msc16, msc17, msc18, msc19, msc20]:
        settings_append("miniscript", msc)

    # settings_set("multisig", [ms0, ms1, ms2, ms3])

    goto_home()
    pick_menu_item("Settings")
    pick_menu_item("Multisig/Miniscript")  # migration happened here

    miniscripts = settings_get("miniscript")
    assert len(miniscripts) == 26  # 20 miniscript wallets & 6 multisigs
    for m in miniscripts:
        assert len(m) == 4

    assert len(settings_get("multisig")) == 6


def test_nunchuk_bug_report(settings_set, goto_home, pick_menu_item, need_keypress, cap_story,
                            microsd_path, settings_remove):
    name = "Off chain"
    settings_remove("multisig")
    settings_remove("multi_mig")
    ms = [["Off chain", [2, 4], [[1073312282, 1, "xpub6DnZdnMX95n19Lu5oDJmC15k8f18FDzinf4NthgUXQpEaEN2jcdWJz8vkSF1NWBAEfxHzTHxZimH8d4pjJ1Uy6AxhpSku1Ro1trJk6RwARa"], [4082991556, 0, "xpub6FF8WuaE5MX5XAv4whPmDVdTtoGs1CMczszwSRM93ELySmy5KYWbwSBevsaBTmdrABrgQvSdKnwKVi9b9oFxidxbJechpuu3TvhCiJ5c1Rb"], [254963148, 0, "xpub6ErsS8s6zHjePcQ679sSqdfQaXSQ2GbSkUxDRjsKkwLViB39zG4ubgeQYSVM8Qkq3Y7A9qTjQifinUJYHM3XbSoP4dJoU2DsUHvj8rFaZpG"], [173494037, 0, "xpub6E7H774dkWsVLxGuzXmiqi5UwbzKD4BSYfymczKEZNUGft5ZXhoNVcEpCA7RRiSALbdec6fSS5DCke6yH8U96JdLmBq9YwJDDwce5k5VDNZ"]], {"d": ["m/48h/0h/0h/2h", "m/48h/0h/1h/2h"], "ft": 14}]]
    target_desc = "wsh(sortedmulti(2,[1a72f93f/48h/0h/1h/2h]xpub6DnZdnMX95n19Lu5oDJmC15k8f18FDzinf4NthgUXQpEaEN2jcdWJz8vkSF1NWBAEfxHzTHxZimH8d4pjJ1Uy6AxhpSku1Ro1trJk6RwARa/0/*,[c4815df3/48h/0h/0h/2h]xpub6FF8WuaE5MX5XAv4whPmDVdTtoGs1CMczszwSRM93ELySmy5KYWbwSBevsaBTmdrABrgQvSdKnwKVi9b9oFxidxbJechpuu3TvhCiJ5c1Rb/0/*,[cc6d320f/48h/0h/0h/2h]xpub6ErsS8s6zHjePcQ679sSqdfQaXSQ2GbSkUxDRjsKkwLViB39zG4ubgeQYSVM8Qkq3Y7A9qTjQifinUJYHM3XbSoP4dJoU2DsUHvj8rFaZpG/0/*,[154f570a/48h/0h/0h/2h]xpub6E7H774dkWsVLxGuzXmiqi5UwbzKD4BSYfymczKEZNUGft5ZXhoNVcEpCA7RRiSALbdec6fSS5DCke6yH8U96JdLmBq9YwJDDwce5k5VDNZ/0/*))"
    settings_set("multisig", ms)
    settings_set("chain", "BTC")
    goto_home()
    pick_menu_item("Settings")
    pick_menu_item("Multisig/Miniscript")  # migration happen here
    pick_menu_item(name)
    pick_menu_item("Descriptors")
    pick_menu_item("Export")
    need_keypress("1")
    time.sleep(.1)
    _, story = cap_story()
    split_story = story.split("\n\n")
    fname = split_story[1]
    with open(microsd_path(fname), "r") as f:
        desc_w_csum = f.read().strip()

    desc = desc_w_csum.split("#")[0]
    assert desc == target_desc.replace("/0/*", "/<0;1>/*")
