Ejemplo n.º 1
0
    def test_base_address(self):
        mnemonic = "test walk nut penalty hip pave soap entry language right filter choice"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        test_vectors = [
            # network id, account, expected result
            # data generated with code under test
            (network_ids.MAINNET, 4,
             "addr1q84sh2j72ux0l03fxndjnhctdg7hcppsaejafsa84vh7lwgmcs5wgus8qt4atk45lvt4xfxpjtwfhdmvchdf2m3u3hlsd5tq5r"
             ),
            (network_ids.TESTNET, 4,
             "addr_test1qr4sh2j72ux0l03fxndjnhctdg7hcppsaejafsa84vh7lwgmcs5wgus8qt4atk45lvt4xfxpjtwfhdmvchdf2m3u3hlswzkqcu"
             ),
        ]

        for network_id, account, expected_address in test_vectors:
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.BASE,
                address_n=[
                    1852 | HARDENED, 1815 | HARDENED, account | HARDENED, 0, 0
                ],
                address_n_staking=[
                    1852 | HARDENED, 1815 | HARDENED, account | HARDENED, 2, 0
                ])
            actual_address = derive_human_readable_address(
                keychain, address_parameters, protocol_magics.MAINNET,
                network_id)

            self.assertEqual(actual_address, expected_address)
    def test_get_public_key_scheme_18_words(self):
        mnemonic = "found differ bulb shadow wrist blue bind vessel deposit tip pelican action surprise weapon check fiction muscle this"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        derivation_paths = [
            [0x80000000 | 44, 0x80000000 | 1815, 0x80000000, 0, 0x80000000],
            [0x80000000 | 1852, 0x80000000 | 1815, 0x80000000, 0, 0],
        ]

        public_keys = [
            b'82f12f1916c0c35a412291e72204f17f033b0b7edf148dfd7d75acd3975c9ac0',
            b'd92d0590e76bbf0300112a9f283fba2f7f8af5cf1054d634b610d1e4f541df90',
        ]

        chain_codes = [
            b'974f9cd4336e23b976f934752026a2d4c32d2e23f0532f4f87152b45fa5ca81f',
            b'352499ed19b47f2bc8c164b47df0d55f428cc8c12f96b7e65d7563114ddfd75b',
        ]

        xpub_keys = [
            '82f12f1916c0c35a412291e72204f17f033b0b7edf148dfd7d75acd3975c9ac0974f9cd4336e23b976f934752026a2d4c32d2e23f0532f4f87152b45fa5ca81f',
            'd92d0590e76bbf0300112a9f283fba2f7f8af5cf1054d634b610d1e4f541df90352499ed19b47f2bc8c164b47df0d55f428cc8c12f96b7e65d7563114ddfd75b',
       ]

        for index, derivation_path in enumerate(derivation_paths):
            key = _get_public_key(keychain, derivation_path)

            self.assertEqual(hexlify(key.node.public_key), public_keys[index])
            self.assertEqual(hexlify(key.node.chain_code), chain_codes[index])
            self.assertEqual(key.xpub, xpub_keys[index])
Ejemplo n.º 3
0
async def cardano_get_address(ctx, msg):
    mnemonic = storage.get_mnemonic()
    root_node = bip32.from_mnemonic_cardano(mnemonic)

    try:
        address, _ = derive_address_and_node(root_node, msg.address_n)
    except ValueError as e:
        if __debug__:
            log.exception(__name__, e)
        raise wire.ProcessError("Deriving address failed")
    mnemonic = None
    root_node = None

    if msg.show_display:
        if not await show_swipable_with_confirmation(ctx,
                                                     address,
                                                     "Export address",
                                                     icon=ui.ICON_SEND,
                                                     icon_color=ui.GREEN):
            raise wire.ActionCancelled("Exporting cancelled")
        else:
            lines = _break_address_n_to_lines(msg.address_n)
            if not await show_swipable_with_confirmation(ctx,
                                                         lines,
                                                         "For BIP32 path",
                                                         icon=ui.ICON_SEND,
                                                         icon_color=ui.GREEN):
                raise wire.ActionCancelled("Exporting cancelled")

    return CardanoAddress(address=address)
Ejemplo n.º 4
0
    def test_enterprise_address(self):
        mnemonic = "test walk nut penalty hip pave soap entry language right filter choice"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        test_vectors = [
            # network id, expected result
            (network_ids.MAINNET,
             "addr1vx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzers66hrl8"),
            (network_ids.TESTNET,
             "addr_test1vz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzerspjrlsz")
        ]

        for network_id, expected_address in test_vectors:
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.ENTERPRISE,
                address_n=[
                    1852 | HARDENED, 1815 | HARDENED, 0 | HARDENED, 0, 0
                ],
            )
            actual_address = derive_human_readable_address(
                keychain, address_parameters, protocol_magics.MAINNET,
                network_id)

            self.assertEqual(actual_address, expected_address)
Ejemplo n.º 5
0
    def test_pointer_address(self):
        mnemonic = "test walk nut penalty hip pave soap entry language right filter choice"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        test_vectors = [
            # network id, pointer, expected result
            (network_ids.MAINNET,
             CardanoBlockchainPointerType(block_index=1,
                                          tx_index=2,
                                          certificate_index=3),
             "addr1gx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzerspqgpse33frd"
             ),
            (network_ids.TESTNET,
             CardanoBlockchainPointerType(block_index=24157,
                                          tx_index=177,
                                          certificate_index=42),
             "addr_test1gz2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzer5ph3wczvf2pfz4ly"
             )
        ]

        for network_id, pointer, expected_address in test_vectors:
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.POINTER,
                address_n=[
                    1852 | HARDENED, 1815 | HARDENED, 0 | HARDENED, 0, 0
                ],
                certificate_pointer=pointer,
            )
            actual_address = derive_human_readable_address(
                keychain, address_parameters, protocol_magics.MAINNET,
                network_id)

            self.assertEqual(actual_address, expected_address)
    def test_base_address_with_invalid_parameters(self):
        mnemonic = "test walk nut penalty hip pave soap entry language right filter choice"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        # both address_n_staking and staking_key_hash are None
        with self.assertRaises(wire.DataError):
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.BASE,
                address_n=[1852 | HARDENED, 1815 | HARDENED, 0 | HARDENED, 0, 0],
                address_n_staking=None,
                staking_key_hash=None,
            )
            derive_human_readable_address(keychain, address_parameters, 0, 0)

        # address_n_staking is not a staking path
        with self.assertRaises(wire.DataError):
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.BASE,
                address_n=[1852 | HARDENED, 1815 | HARDENED, 0 | HARDENED, 0, 0],
                address_n_staking=[1852 | HARDENED, 1815 | HARDENED, 0 | HARDENED, 0, 0],
                staking_key_hash=None,
            )
            derive_human_readable_address(keychain, address_parameters, 0, 0)
Ejemplo n.º 7
0
async def cardano_sign_message(ctx, msg):
    mnemonic = storage.get_mnemonic()
    root_node = bip32.from_mnemonic_cardano(mnemonic)

    try:
        signature = _sign_message(root_node, msg.message, msg.address_n)
    except ValueError as e:
        if __debug__:
            log.exception(__name__, e)
        raise wire.ProcessError("Signing failed")
    mnemonic = None
    root_node = None

    if not await show_swipable_with_confirmation(
        ctx, msg.message, "Signing message", ui.ICON_RECEIVE, ui.GREEN
    ):
        raise wire.ActionCancelled("Signing cancelled")

    if not await show_swipable_with_confirmation(
        ctx,
        _break_address_n_to_lines(msg.address_n),
        "With address",
        ui.ICON_RECEIVE,
        ui.GREEN,
    ):
        raise wire.ActionCancelled("Signing cancelled")

    return signature
    def test_root_address_derivation_scheme(self):
        mnemonic = "all all all all all all all all all all all all"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        # 44'/1815'
        address_parameters = CardanoAddressParametersType(
            address_type=CardanoAddressType.BYRON,
            address_n=[0x80000000 | 44, 0x80000000 | 1815],
        )
        address = derive_human_readable_address(keychain, address_parameters, protocol_magics.MAINNET, network_ids.MAINNET)
        self.assertEqual(address, "Ae2tdPwUPEZ2FGHX3yCKPSbSgyuuTYgMxNq652zKopxT4TuWvEd8Utd92w3")

        priv, ext, pub, chain = (
            b"204ec79cbb6502a141de60d274962010c7f1c94a2987b26506433184d228ed51",
            b"975cdd1c8610b44701567f05934c45c8716064263ccfe72ed2167ccb705c09b6",
            b"8c47ebce34234d04fd3dfbac33feaba6133e4e3d77c4b5ab18120ec6878ad4ce",
            b"02ac67c59a8b0264724a635774ca2c242afa10d7ab70e2bf0a8f7d4bb10f1f7a"
        )

        n = keychain.derive([0x80000000 | 44, 0x80000000 | 1815])
        self.assertEqual(hexlify(n.private_key()), priv)
        self.assertEqual(hexlify(n.private_key_ext()), ext)
        self.assertEqual(hexlify(seed.remove_ed25519_prefix(n.public_key())), pub)
        self.assertEqual(hexlify(n.chain_code()), chain)
    def test_base_address_with_staking_key_hash(self):
        mnemonic = "test walk nut penalty hip pave soap entry language right filter choice"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        test_vectors = [
            # network id, account, staking key hash, expected result
            # own staking key hash
            # data generated with code under test
            (network_ids.MAINNET, 4, unhexlify("1bc428e4720702ebd5dab4fb175324c192dc9bb76cc5da956e3c8dff"), "addr1q84sh2j72ux0l03fxndjnhctdg7hcppsaejafsa84vh7lwgmcs5wgus8qt4atk45lvt4xfxpjtwfhdmvchdf2m3u3hlsd5tq5r"),
            (network_ids.TESTNET, 4, unhexlify("1bc428e4720702ebd5dab4fb175324c192dc9bb76cc5da956e3c8dff"), "addr_test1qr4sh2j72ux0l03fxndjnhctdg7hcppsaejafsa84vh7lwgmcs5wgus8qt4atk45lvt4xfxpjtwfhdmvchdf2m3u3hlswzkqcu"),
            # staking key hash not owned - derived with "all all..." mnenomnic, data generated with code under test
            (network_ids.MAINNET, 4, unhexlify("122a946b9ad3d2ddf029d3a828f0468aece76895f15c9efbd69b4277"), "addr1q84sh2j72ux0l03fxndjnhctdg7hcppsaejafsa84vh7lwgj922xhxkn6twlq2wn4q50q352annk3903tj00h45mgfmsxrrvc2"),
            (network_ids.MAINNET, 0, unhexlify("122a946b9ad3d2ddf029d3a828f0468aece76895f15c9efbd69b4277"), "addr1qx2fxv2umyhttkxyxp8x0dlpdt3k6cwng5pxj3jhsydzersj922xhxkn6twlq2wn4q50q352annk3903tj00h45mgfms6xjnst"),
            (network_ids.TESTNET, 4, unhexlify("122a946b9ad3d2ddf029d3a828f0468aece76895f15c9efbd69b4277"), "addr_test1qr4sh2j72ux0l03fxndjnhctdg7hcppsaejafsa84vh7lwgj922xhxkn6twlq2wn4q50q352annk3903tj00h45mgfms947v54"),
        ]

        for network_id, account, staking_key_hash, expected_address in test_vectors:
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.BASE,
                address_n=[1852 | HARDENED, 1815 | HARDENED, account | HARDENED, 0, 0],
                staking_key_hash=staking_key_hash,
            )
            actual_address = derive_human_readable_address(keychain, address_parameters, protocol_magics.MAINNET, network_id)

            self.assertEqual(actual_address, expected_address)
Ejemplo n.º 10
0
    def test_reward_address(self):
        mnemonic = "test walk nut penalty hip pave soap entry language right filter choice"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        test_vectors = [
            # network id, expected result
            (network_ids.MAINNET,
             "stake1uyevw2xnsc0pvn9t9r9c7qryfqfeerchgrlm3ea2nefr9hqxdekzz"),
            (network_ids.TESTNET,
             "stake_test1uqevw2xnsc0pvn9t9r9c7qryfqfeerchgrlm3ea2nefr9hqp8n5xl"
             )
        ]

        for network_id, expected_address in test_vectors:
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.REWARD,
                address_n=[
                    1852 | HARDENED, 1815 | HARDENED, 0 | HARDENED, 2, 0
                ],
            )
            actual_address = derive_human_readable_address(
                keychain, address_parameters, protocol_magics.MAINNET,
                network_id)

            self.assertEqual(actual_address, expected_address)
    def test_derive_hd_passphrase(self):
        mnemonic = "plastic that delay conduct police ticket swim gospel intact harsh obtain entire"
        root_node = bip32.from_mnemonic_cardano(mnemonic)

        self.assertEqual(
            hexlify(_derive_hd_passphrase(root_node)).decode('utf8'),
            "8ee689a22e1ec569d2ada515c4ee712ad089901b7fe0afb94fe196de944ee814")
    def test_sign_message(self):
        mnemonic = "plastic that delay conduct police ticket swim gospel intact harsh obtain entire"
        node = bip32.from_mnemonic_cardano(mnemonic)

        messages = [
            ('Test message to sign',
             [0x80000000 | 44, 0x80000000 | 1815, 0x80000000, 0, 0x80000000],
             '07f226da2a59c3083e80f01ef7e0ec46fc726ebe6bd15d5e9040031c342d8651bee9aee875019c41a7719674fd417ad43990988ffd371527604b6964df75960d'
             ),
            ('New Test message to sign', [0x80000000 | 44, 0x80000000 | 1815],
             '8fd3b9d8a4c30326b720de76f8de2bbf57b29b7593576eac4a3017ea23046812017136520dc2f24e9fb4da56bd87c77ea49265686653b36859b5e1e56ba9eb0f'
             ),
            ('Another Test message to sign',
             [0x80000000 | 44, 0x80000000 | 1815, 0, 0, 0],
             '89d63bd32c2eb92aa418b9ce0383a7cf489bc56284876c19246b70be72070d83d361fcb136e8e257b7e66029ef4a566405cda0143d251f851debd62c3c38c302'
             ),
            ('Just another Test message to sign',
             [0x80000000 | 44, 0x80000000 | 1815, 0x80000000, 0, 0],
             '49d948090d30e35a88a26d8fb07aca5d68936feba2d5bd49e0d0f7c027a0c8c2955b93a7c930a3b36d23c2502c18bf39cf9b17bbba1a0965090acfb4d10a9305'
             ),
        ]

        for (message, derivation_path, expected_signature) in messages:
            signature = _sign_message(node, message, derivation_path)
            self.assertEqual(expected_signature,
                             hexlify(signature.signature).decode('utf8'))
Ejemplo n.º 13
0
async def get_address(ctx, msg):
    await paths.validate_path(ctx, validate_full_path, path=msg.address_n)

    mnemonic = storage.get_mnemonic()
    passphrase = await seed._get_cached_passphrase(ctx)
    root_node = bip32.from_mnemonic_cardano(mnemonic, passphrase)

    try:
        address, _ = derive_address_and_node(root_node, msg.address_n)
    except ValueError as e:
        if __debug__:
            log.exception(__name__, e)
        raise wire.ProcessError("Deriving address failed")
    mnemonic = None
    root_node = None

    if msg.show_display:
        if not await confirm_with_pagination(ctx,
                                             address,
                                             "Export address",
                                             icon=ui.ICON_SEND,
                                             icon_color=ui.GREEN):
            raise wire.ActionCancelled("Exporting cancelled")

    return CardanoAddress(address=address)
    def test_get_public_key_scheme_12_words(self):
        mnemonic = "all all all all all all all all all all all all"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        derivation_paths = [
            [0x80000000 | 44, 0x80000000 | 1815, 0x80000000, 0, 0x80000000],
            [0x80000000 | 44, 0x80000000 | 1815],
            [0x80000000 | 44, 0x80000000 | 1815, 0, 0, 0],
            [0x80000000 | 44, 0x80000000 | 1815, 0x80000000, 0, 0],

            [0x80000000 | 1852, 0x80000000 | 1815, 0x80000000, 0, 0x80000000],
            [0x80000000 | 1852, 0x80000000 | 1815],
            [0x80000000 | 1852, 0x80000000 | 1815, 0, 0, 0],
            [0x80000000 | 1852, 0x80000000 | 1815, 0x80000000, 0, 0],
        ]

        public_keys = [
            b'a938c8554ae04616cfaae7cd0eb557475082c4e910242ce774967e0bd7492408',
            b'8c47ebce34234d04fd3dfbac33feaba6133e4e3d77c4b5ab18120ec6878ad4ce',
            b'17cc0bf978756d0d5c76f931629036a810c61801b78beecb44555773d13e3791',
            b'b90fb812a2268e9569ff1172e8daed1da3dc7e72c7bded7c5bcb7282039f90d5',

            b'f698a764b23aa6667b1157fc4247c6a1b58c21a3865ac6a47a3590167a9e0211',
            b'e9c46841be76e3be0289694fd5c7503c04f40e5b036abac200b98a9006cf6647',
            b'6d225f078ca611f00d86cbfd8ba6c6ac7826721434eae6525686efb878b72370',
            b'5d010cf16fdeff40955633d6c565f3844a288a24967cf6b76acbeb271b4f13c1',
        ]

        chain_codes = [
            b'cbf6ab47c8eb1a0477fc40b25dbb6c4a99454edb97d6fe5acedd3e238ef46fe0',
            b'02ac67c59a8b0264724a635774ca2c242afa10d7ab70e2bf0a8f7d4bb10f1f7a',
            b'646ac4a6295326bae6831be05921edfbcb362de48dfd37b12e74c227dfad768d',
            b'fd8e71c1543de2cdc7f7623130c5f2cceb53549055fa1f5bc88199989e08cce7',

            b'13cfb6de37a568aae56cadac907e6469b121464fe1b70a10c213eaea2cbb6636',
            b'58f3f46f4a93e7a4431e75b10af7497b747c3053cb7466ed53f4277e78a63c52',
            b'f72b3c361381db2d88289440268c94c5e7467c9414375e6b63d03026750f3c66',
            b'f123474e140a2c360b01f0fa66f2f22e2e965a5b07a80358cf75f77abbd66088',
        ]

        xpub_keys = [
            'a938c8554ae04616cfaae7cd0eb557475082c4e910242ce774967e0bd7492408cbf6ab47c8eb1a0477fc40b25dbb6c4a99454edb97d6fe5acedd3e238ef46fe0',
            '8c47ebce34234d04fd3dfbac33feaba6133e4e3d77c4b5ab18120ec6878ad4ce02ac67c59a8b0264724a635774ca2c242afa10d7ab70e2bf0a8f7d4bb10f1f7a',
            '17cc0bf978756d0d5c76f931629036a810c61801b78beecb44555773d13e3791646ac4a6295326bae6831be05921edfbcb362de48dfd37b12e74c227dfad768d',
            'b90fb812a2268e9569ff1172e8daed1da3dc7e72c7bded7c5bcb7282039f90d5fd8e71c1543de2cdc7f7623130c5f2cceb53549055fa1f5bc88199989e08cce7',

            'f698a764b23aa6667b1157fc4247c6a1b58c21a3865ac6a47a3590167a9e021113cfb6de37a568aae56cadac907e6469b121464fe1b70a10c213eaea2cbb6636',
            'e9c46841be76e3be0289694fd5c7503c04f40e5b036abac200b98a9006cf664758f3f46f4a93e7a4431e75b10af7497b747c3053cb7466ed53f4277e78a63c52',
            '6d225f078ca611f00d86cbfd8ba6c6ac7826721434eae6525686efb878b72370f72b3c361381db2d88289440268c94c5e7467c9414375e6b63d03026750f3c66',
            '5d010cf16fdeff40955633d6c565f3844a288a24967cf6b76acbeb271b4f13c1f123474e140a2c360b01f0fa66f2f22e2e965a5b07a80358cf75f77abbd66088',
        ]

        for index, derivation_path in enumerate(derivation_paths):
            key = _get_public_key(keychain, derivation_path)

            self.assertEqual(hexlify(key.node.public_key), public_keys[index])
            self.assertEqual(hexlify(key.node.chain_code), chain_codes[index])
            self.assertEqual(key.xpub, xpub_keys[index])
Ejemplo n.º 15
0
    def test_root_address_derivation_scheme(self):
        mnemonic = "all all all all all all all all all all all all"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)

        # 44'/1815'
        address, _ = derive_address_and_node(
            node, [0x80000000 | 44, 0x80000000 | 1815])
        self.assertEqual(
            address,
            "Ae2tdPwUPEZ2FGHX3yCKPSbSgyuuTYgMxNq652zKopxT4TuWvEd8Utd92w3")

        priv, ext, pub, chain = (
            b"204ec79cbb6502a141de60d274962010c7f1c94a2987b26506433184d228ed51",
            b"975cdd1c8610b44701567f05934c45c8716064263ccfe72ed2167ccb705c09b6",
            b"8c47ebce34234d04fd3dfbac33feaba6133e4e3d77c4b5ab18120ec6878ad4ce",
            b"02ac67c59a8b0264724a635774ca2c242afa10d7ab70e2bf0a8f7d4bb10f1f7a"
        )

        _, n = derive_address_and_node(node,
                                       [0x80000000 | 44, 0x80000000 | 1815])
        self.assertEqual(hexlify(n.private_key()), priv)
        self.assertEqual(hexlify(n.private_key_ext()), ext)
        self.assertEqual(hexlify(seed.remove_ed25519_prefix(n.public_key())),
                         pub)
        self.assertEqual(hexlify(n.chain_code()), chain)
    def test_get_public_key_scheme_24_words(self):
        mnemonic = "balance exotic ranch knife glory slow tape favorite yard gym awake ill exist useless parent aim pig stay effort into square gasp credit butter"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        derivation_paths = [
            [0x80000000 | 44, 0x80000000 | 1815, 0x80000000, 0, 0x80000000],
            [0x80000000 | 1852, 0x80000000 | 1815, 0x80000000, 0, 0],
        ]

        public_keys = [
            b'9d45d1e979bd0b942adb1896019c85d08fbc562f012775a1f72fc7be8fe9e4b6',
            b'a85a339897354931d584f828f6d79d4227ed16f3468990687ab42f13a87c9ea8',
        ]

        chain_codes = [
            b'aad67fd6d620f7af88ad816a229de09cfacff3e28008a528759b2e2cf28d859a',
            b'e6f844931e7e2ec724e6e62efde662ae2669355322dc3eb9b307bc1c8e75e219',
        ]

        xpub_keys = [
            '9d45d1e979bd0b942adb1896019c85d08fbc562f012775a1f72fc7be8fe9e4b6aad67fd6d620f7af88ad816a229de09cfacff3e28008a528759b2e2cf28d859a',
            'a85a339897354931d584f828f6d79d4227ed16f3468990687ab42f13a87c9ea8e6f844931e7e2ec724e6e62efde662ae2669355322dc3eb9b307bc1c8e75e219',
       ]

        for index, derivation_path in enumerate(derivation_paths):
            key = _get_public_key(keychain, derivation_path)

            self.assertEqual(hexlify(key.node.public_key), public_keys[index])
            self.assertEqual(hexlify(key.node.chain_code), chain_codes[index])
            self.assertEqual(key.xpub, xpub_keys[index])
Ejemplo n.º 17
0
    def test_get_address_root_scheme(self):
        mnemonic = "all all all all all all all all all all all all"
        passphrase = ""
        root_node = bip32.from_mnemonic_cardano(mnemonic, passphrase)

        address_root = _get_address_root(root_node, {1: b'X\x1cr,zu\x81?\xaf\xde\x9f\xf9\xe4\xd4\x90\xadH$\xe9\xf3\x88\x16\xcb\xd2)\x02M\x0c#\xde'})
        self.assertEqual(address_root, b'\xb3\xbbS\xa8;uN:E=\xe8\xe5\x9c\x18\xbcn\xcf\xd0c\xba\x0e\xba\xaelL}\xba\xbb')
    def test_get_native_script_hash(self):
        mnemonic = "all all all all all all all all all all all all"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        for script, expected_hash in VALID_NATIVE_SCRIPTS:
            actual_hash = get_native_script_hash(keychain, script)
            self.assertEqual(hexlify(actual_hash), expected_hash)
Ejemplo n.º 19
0
    def test_hardened_address_derivation_scheme(self):
        mnemonic = "all all all all all all all all all all all all"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        addresses = [
            "Ae2tdPwUPEZ98eHFwxSsPBDz73amioKpr58Vw85mP1tMkzq8siaftiejJ3j",
            "Ae2tdPwUPEZKA971NCHuHqaEnxZDFWPzH3fEsLpDnbEpG6UeMRHnRzCzEwK",
            "Ae2tdPwUPEZL9Ag1ouS4b1zjuPxKpvEUgjpVpG1KQFs5pNewQb65F1WXVQ2",
        ]

        for i, expected in enumerate(addresses):
            # 44'/1815'/0'/0/i'
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.BYRON,
                address_n=[
                    0x80000000 | 44, 0x80000000 | 1815, 0x80000000, 0,
                    0x80000000 + i
                ],
            )
            address = derive_human_readable_address(keychain,
                                                    address_parameters,
                                                    protocol_magics.MAINNET,
                                                    network_ids.MAINNET)
            self.assertEqual(expected, address)

        nodes = [
            (b"3881a8de77d069001010d7f7d5211552e7d539b0e253add710367f95e528ed51",
             b"9b77608b38e0a0c7861aa234557c81482f42aae2d17993a8ddaec1868fb04d60",
             b"a938c8554ae04616cfaae7cd0eb557475082c4e910242ce774967e0bd7492408",
             b"cbf6ab47c8eb1a0477fc40b25dbb6c4a99454edb97d6fe5acedd3e238ef46fe0"
             ),
            (b"3003aca659846540b9ed04f2b844f2d8ea964856ca38a7dffedef4f6e528ed51",
             b"8844ccc81d633e1c7126f30c2524c1652617cf58da755014070215bf5070ba38",
             b"be28c00ed6cb9b70310f78028f8e3a2db935baf482d84afa590b0b5b864571cc",
             b"584b4631d752023a249e980779517280e6c0b3ac7a7f27c6e9456bfd228ca60b"
             ),
            (b"68e4482add0a741e14c8f2306bf83206a623e3729dd24175915eedece428ed51",
             b"3165a80c5efe846224d46a0427cdb2be4f31ea3585c51f4131faefc4328ad95a",
             b"9a32499976ffb582daa9988dfc42a303de5ed00c320c929f496be3c6eb1cf405",
             b"da07ca30a3d1c5fe3c34ce5fa197722446a646624a10bdf8889a4b9c347b2ef2"
             ),
        ]

        for i, (priv, ext, pub, chain) in enumerate(nodes):
            n = keychain.derive([
                0x80000000 | 44, 0x80000000 | 1815, 0x80000000, 0,
                0x80000000 + i
            ])
            self.assertEqual(hexlify(n.private_key()), priv)
            self.assertEqual(hexlify(n.private_key_ext()), ext)
            self.assertEqual(
                hexlify(seed.remove_ed25519_prefix(n.public_key())), pub)
            self.assertEqual(hexlify(n.chain_code()), chain)
    def test_reward_address_with_non_staking_path(self):
        mnemonic = "test walk nut penalty hip pave soap entry language right filter choice"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        with self.assertRaises(wire.DataError):
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.REWARD,
                address_n=[44 | HARDENED, 1815 | HARDENED, 0 | HARDENED, 0, 0])
            derive_human_readable_address(keychain, address_parameters, 0, 0)
Ejemplo n.º 21
0
async def _get_keychain_bip39(ctx: wire.Context) -> Keychain:
    if not device.is_initialized():
        raise wire.NotInitialized("Device is not initialized")

    # ask for passphrase, loading from cache if necessary
    passphrase = await _get_passphrase(ctx)
    # derive the root node from mnemonic and passphrase via Cardano Icarus algorithm
    secret_bytes = mnemonic.get_secret()
    assert secret_bytes is not None
    root = bip32.from_mnemonic_cardano(secret_bytes.decode(), passphrase.decode())
    return Keychain(root)
    def test_pointer_address_invalid_pointers(self):
        mnemonic = "test walk nut penalty hip pave soap entry language right filter choice"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        # pointer is None
        with self.assertRaises(wire.DataError):
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.POINTER,
                address_n=[
                    1852 | HARDENED, 1815 | HARDENED, 0 | HARDENED, 0, 0
                ],
                certificate_pointer=None,
            )
            derive_human_readable_address(keychain, address_parameters, 0, 0)

        # block index is None
        with self.assertRaises(wire.DataError):
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.POINTER,
                address_n=[
                    1852 | HARDENED, 1815 | HARDENED, 0 | HARDENED, 0, 0
                ],
                certificate_pointer=CardanoBlockchainPointerType(
                    block_index=None, tx_index=2, certificate_index=3),
            )
            derive_human_readable_address(keychain, address_parameters, 0, 0)

        # tx index is None
        with self.assertRaises(wire.DataError):
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.POINTER,
                address_n=[
                    1852 | HARDENED, 1815 | HARDENED, 0 | HARDENED, 0, 0
                ],
                certificate_pointer=CardanoBlockchainPointerType(
                    block_index=1, tx_index=None, certificate_index=3),
            )
            derive_human_readable_address(keychain, address_parameters, 0, 0)

        # certificate index is None
        with self.assertRaises(wire.DataError):
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.POINTER,
                address_n=[
                    1852 | HARDENED, 1815 | HARDENED, 0 | HARDENED, 0, 0
                ],
                certificate_pointer=CardanoBlockchainPointerType(
                    block_index=1, tx_index=2, certificate_index=None),
            )
            derive_human_readable_address(keychain, address_parameters, 0, 0)
Ejemplo n.º 23
0
    def test_non_hardened_address_derivation_scheme(self):
        mnemonic = "all all all all all all all all all all all all"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        addresses = [
            "Ae2tdPwUPEZ5YUb8sM3eS8JqKgrRLzhiu71crfuH2MFtqaYr5ACNRdsswsZ",
            "Ae2tdPwUPEZJb8r1VZxweSwHDTYtqeYqF39rZmVbrNK62JHd4Wd7Ytsc8eG",
            "Ae2tdPwUPEZFm6Y7aPZGKMyMAK16yA5pWWKU9g73ncUQNZsAjzjhszenCsq",
        ]

        for i, expected in enumerate(addresses):
            # 44'/1815'/0'/0/i
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.BYRON,
                address_n=[
                    0x80000000 | 44, 0x80000000 | 1815, 0x80000000, 0, i
                ],
            )
            address = derive_human_readable_address(keychain,
                                                    address_parameters,
                                                    protocol_magics.MAINNET,
                                                    network_ids.MAINNET)
            self.assertEqual(address, expected)

        nodes = [
            (b"d03ba81163fd55af97bd132bf651a0da5b5e6201b15b1caca60b0be8e028ed51",
             b"493f44aa8d25fe0d3fe2935c76ea6b3e9e41c79e9dbcbe7131357c5aa1b6cac5",
             b"b90fb812a2268e9569ff1172e8daed1da3dc7e72c7bded7c5bcb7282039f90d5",
             b"fd8e71c1543de2cdc7f7623130c5f2cceb53549055fa1f5bc88199989e08cce7"
             ),
            (b"08b6438c8dd49d34b71c8e914d6ac3184e5ab3dcc8af023d08503a7edf28ed51",
             b"3fee605fdfaddc1ee2ea0b246b02c9abc54ad741054bc83943e8b21487b5a053",
             b"89053545a6c254b0d9b1464e48d2b5fcf91d4e25c128afb1fcfc61d0843338ea",
             b"26308151516f3b0e02bb1638142747863c520273ce9bd3e5cd91e1d46fe2a635"
             ),
            (b"088f0275bf4a1bd18f08d7ef06c6ddb6ce7e3dc415fb4e89fe21bf39e628ed51",
             b"4c44563c7df519ea9b4d1801c1ab98b449db28b87f1c3837759c20f68c4c1e65",
             b"52548cb98e6f46a592bdf7f3598a9abc0126c78dfa3f46d1894ee52a5213e833",
             b"91af0668ee449e613e61bbb2482e5ddee1d9b15785727ec3e362c36861bff923"
             ),
        ]

        for i, (priv, ext, pub, chain) in enumerate(nodes):
            n = keychain.derive(
                [0x80000000 | 44, 0x80000000 | 1815, 0x80000000, 0, i])
            self.assertEqual(hexlify(n.private_key()), priv)
            self.assertEqual(hexlify(n.private_key_ext()), ext)
            self.assertEqual(
                hexlify(seed.remove_ed25519_prefix(n.public_key())), pub)
            self.assertEqual(hexlify(n.chain_code()), chain)
    def test_get_address_root(self):
        mnemonic = "plastic that delay conduct police ticket swim gospel intact harsh obtain entire"
        root_node = bip32.from_mnemonic_cardano(mnemonic)

        address_root = _get_address_root(
            root_node, {
                1:
                b'X\x1cr,zu\x81?\xaf\xde\x9f\xf9\xe4\xd4\x90\xadH$\xe9\xf3\x88\x16\xcb\xd2)\x02M\x0c#\xde'
            })
        self.assertEqual(
            address_root,
            b'\xca\x9bbQ\xa5\xaa}\x01U\xba\xe5\xa5\xaa~\x84M\x0b;\x1dM\xd8z\xe7Y\x01\xc8\x92\x91'
        )
Ejemplo n.º 25
0
async def cardano_get_public_key(ctx, msg):
    mnemonic = storage.get_mnemonic()
    root_node = bip32.from_mnemonic_cardano(mnemonic)

    try:
        key = _get_public_key(root_node, msg.address_n)
    except ValueError as e:
        if __debug__:
            log.exception(__name__, e)
        raise wire.ProcessError("Deriving public key failed")
    mnemonic = None
    root_node = None

    return key
Ejemplo n.º 26
0
async def get_keychain(ctx: wire.Context) -> Keychain:
    if not device.is_initialized():
        raise wire.NotInitialized("Device is not initialized")

    passphrase = await get_passphrase(ctx)
    if mnemonic.is_bip39():
        # derive the root node from mnemonic and passphrase via Cardano Icarus algorithm
        root = bip32.from_mnemonic_cardano(mnemonic.get_secret().decode(), passphrase)
    else:
        # derive the root node via SLIP-0023
        seed = mnemonic.get_seed(passphrase)
        root = bip32.from_seed(seed, "ed25519 cardano seed")

    keychain = Keychain(root)
    return keychain
Ejemplo n.º 27
0
    def test_various_paths_at_once(self):
        mnemonic = "test walk nut penalty hip pave soap entry language right filter choice"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        derivation_paths = [
            [44 | HARDENED, 1815 | HARDENED, HARDENED, 0, 0],
            [44 | HARDENED, 1815 | HARDENED, HARDENED, 0, 1],
            [1852 | HARDENED, 1815 | HARDENED, HARDENED, 0, 0],
            [1852 | HARDENED, 1815 | HARDENED, HARDENED, 0, 1],
            [44 | HARDENED, 1815 | HARDENED, HARDENED, 0, 2],
            [1852 | HARDENED, 1815 | HARDENED, HARDENED, 0, 2]
        ]

        public_keys = [
            b'badd2852ccda7492364be0f88f2ba0b78c5f2d7179a941f1d19f756112b66afa',
            b'34377409140c061d76778626d43456880d5471c1cbade8c372cb6a3be9678072',
            b'73fea80d424276ad0978d4fe5310e8bc2d485f5f6bb3bf87612989f112ad5a7d',
            b'f626ab887eb5f40b502463ccf2ec5a7311676ee9e5d55c492059a366c0b4d4a1',
            b'408ee7b2d1c84d7899dba07150fae88c5411974f1762cb659dd928db8aac206b',
            b'86e8a3880767e1ed521a47de1e031d47f33d5a8095be467bffbbd3295e27258e'
        ]

        chain_codes = [
            b"e1c5d15875d3ed68667978af38fe3fe586511d87a784c0962a333c21e63a865d",
            b"15c987276326a82defa4cb6762d43442f09e5dcbcc37fa0c58f24ae2dba3d3eb",
            b"dd75e154da417becec55cdd249327454138f082110297d5e87ab25e15fad150f",
            b"f7ab126f2884db9059fa09ca83be6b8bd0250426aeb62191bdd9861457b8bc91",
            b"18d5c9d20c8d23bed068c9ff3a1126b940f0e537f9d94891828a999dda6fafd1",
            b"580bba4bb0b9c56974e16a6998322a91e857e2fac28674404da993f6197fd29f"
        ]

        xpub_keys = [
            "badd2852ccda7492364be0f88f2ba0b78c5f2d7179a941f1d19f756112b66afae1c5d15875d3ed68667978af38fe3fe586511d87a784c0962a333c21e63a865d",
            "34377409140c061d76778626d43456880d5471c1cbade8c372cb6a3be967807215c987276326a82defa4cb6762d43442f09e5dcbcc37fa0c58f24ae2dba3d3eb",
            "73fea80d424276ad0978d4fe5310e8bc2d485f5f6bb3bf87612989f112ad5a7ddd75e154da417becec55cdd249327454138f082110297d5e87ab25e15fad150f",
            "f626ab887eb5f40b502463ccf2ec5a7311676ee9e5d55c492059a366c0b4d4a1f7ab126f2884db9059fa09ca83be6b8bd0250426aeb62191bdd9861457b8bc91",
            "408ee7b2d1c84d7899dba07150fae88c5411974f1762cb659dd928db8aac206b18d5c9d20c8d23bed068c9ff3a1126b940f0e537f9d94891828a999dda6fafd1",
            "86e8a3880767e1ed521a47de1e031d47f33d5a8095be467bffbbd3295e27258e580bba4bb0b9c56974e16a6998322a91e857e2fac28674404da993f6197fd29f"
        ]

        for index, derivation_path in enumerate(derivation_paths):
            key = _get_public_key(keychain, derivation_path)

            self.assertEqual(hexlify(key.node.public_key), public_keys[index])
            self.assertEqual(hexlify(key.node.chain_code), chain_codes[index])
            self.assertEqual(key.xpub, xpub_keys[index])
    def test_byron_address_with_shelley_namespace(self):
        """
        It shouldn't be possible to derive Byron addresses
        with a Shelley namespace (1852')
        """
        mnemonic = "all all all all all all all all all all all all"
        passphrase = ""
        node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
        keychain = Keychain(node)

        with self.assertRaises(wire.DataError):
            address_parameters = CardanoAddressParametersType(
                address_type=CardanoAddressType.BYRON,
                address_n=[1852 | HARDENED, 1815 | HARDENED, 0 | HARDENED, 0, 0],
            )
            derive_human_readable_address(keychain, address_parameters, 0, 0)
Ejemplo n.º 29
0
async def sign_tx(ctx, msg):
    mnemonic = storage.get_mnemonic()
    passphrase = await seed._get_cached_passphrase(ctx)
    root_node = bip32.from_mnemonic_cardano(mnemonic, passphrase)

    progress.init(msg.transactions_count, "Loading data")

    try:
        # request transactions
        transactions = []
        tx_req = CardanoTxRequest()
        for index in range(msg.transactions_count):
            progress.advance()
            tx_ack = await request_transaction(ctx, tx_req, index)
            transactions.append(tx_ack.transaction)

        # clear progress bar
        display_homescreen()

        for i in msg.inputs:
            await validate_path(ctx, validate_full_path, path=i.address_n)

        # sign the transaction bundle and prepare the result
        transaction = Transaction(msg.inputs, msg.outputs, transactions,
                                  root_node, msg.network)
        tx_body, tx_hash = transaction.serialise_tx()
        tx = CardanoSignedTx(tx_body=tx_body, tx_hash=tx_hash)

    except ValueError as e:
        if __debug__:
            log.exception(__name__, e)
        raise wire.ProcessError("Signing failed")

    # display the transaction in UI
    if not await show_tx(
            ctx,
            transaction.output_addresses,
            transaction.outgoing_coins,
            transaction.change_derivation_paths,
            transaction.change_coins,
            transaction.fee,
            len(tx_body),
            transaction.network_name,
    ):
        raise wire.ActionCancelled("Signing cancelled")

    return tx
Ejemplo n.º 30
0
async def get_keychain(ctx: wire.Context) -> Keychain:
    if not storage.is_initialized():
        raise wire.ProcessError("Device is not initialized")

    # derive the root node from mnemonic and passphrase
    passphrase = cache.get_passphrase()
    if passphrase is None:
        passphrase = await protect_by_passphrase(ctx)
        cache.set_passphrase(passphrase)
    root = bip32.from_mnemonic_cardano(mnemonic.restore(), passphrase)

    # derive the namespaced root node
    for i in SEED_NAMESPACE[0]:
        root.derive_cardano(i)

    keychain = Keychain(SEED_NAMESPACE[0], root)
    return keychain