# Path: keepkeylib/client.py
# class CallException(Exception):
#     def __init__(self, code, message):
#         super(CallException, self).__init__()
#         self.args = [code, message]
# 
# Path: keepkeylib/tools.py
# def parse_path(nstr):
#     """
#     Convert BIP32 path string to list of uint32 integers with hardened flags.
#     Several conventions are supported to set the hardened flag: -1, 1', 1h
#     e.g.: "0/1h/1" -> [0, 0x80000001, 1]
#     :param nstr: path string
#     :return: list of integers
#     """
#     if not nstr:
#         return []
# 
#     n = nstr.split('/')
# 
#     # m/a/b/c => a/b/c
#     if n[0] == 'm':
#         n = n[1:]
# 
#     # coin_name/a/b/c => 44'/SLIP44_constant'/a/b/c
#     #if n[0] in slip44:
#     #    coin_id = slip44[n[0]]
#     #    n[0:1] = ['44h', '{}h'.format(coin_id)]
# 
#     def str_to_harden(x):
#         if x.startswith('-'):
#             return H_(abs(int(x)))
#         elif x.endswith(('h', "'")):
#             return H_(int(x[:-1]))
#         else:
#             return int(x)
# 
#     try:
#         return list(str_to_harden(x) for x in n)
#     except Exception:
#         raise ValueError('Invalid BIP32 path', nstr)
# 
# Path: keepkeylib/nano.py
# def encode_balance(balance):


import unittest
import common
from binascii import hexlify, unhexlify
from keepkeylib.client import CallException
from keepkeylib.tools import parse_path
from keepkeylib import messages_pb2 as proto
from keepkeylib import messages_nano_pb2 as proto_nano
from keepkeylib import types_pb2 as proto_types
from keepkeylib import nano
    #             balance=86242336390000000000000000000,
    #         )
    #         self.assertIsInstance(res, proto_nano.NanoSignedTx)
    #         self.assertEqual(hexlify(res.block_hash), '5d732d843c22f806011127655790484dbabd38dda20b24900c053c3dfc12523f')
    #         self.assertEqual(hexlify(res.signature), '3fb596c34db1241201983cbf613fe9b68a6eae2420c7f294c7e883574fda10d5cc19c9e516b57ed0cbc5e7d3438f70f2ddd7a45bf3e693ff800b97e187de5701')

    # def test_block_6(self):
    #     # https://www.nanode.co/block/a7e59d38b001d9348dbe16fa866d0b435259d381af1db019f3ff83fd7590e226
    #     self.setup_mnemonic_nopin_nopassphrase()
    #     with self.client:
    #         self.client.set_expected_responses([
    #             proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx),
    #             proto_nano.NanoSignedTx(),
    #         ])
    #         res = self.client.nano_sign_tx(
    #             'Nano', NANO_ACCOUNT_0_PATH,
    #             grandparent_hash=unhexlify('32ac7d8f5a16a498abf203b8dfee623c9e111ff25e7339f8cd69ec7492b23edd'),
    #             parent_link=unhexlify(NANO_ACCOUNT_1_PUBLICKEY),
    #             parent_representative=REP_NANODE,
    #             parent_balance=86242336390000000000000000000,
    #             link_recipient_n=OTHER_ACCOUNT_3_PATH,
    #             representative=REP_NANODE,
    #             balance=40000760000000000000000000000,
    #         )
    #         self.assertIsInstance(res, proto_nano.NanoSignedTx)
    #         self.assertEqual(hexlify(res.block_hash), 'a7e59d38b001d9348dbe16fa866d0b435259d381af1db019f3ff83fd7590e226')
    #         self.assertEqual(hexlify(res.signature), '1dcd8a27aeac1cab9a2054d5cc6df1b80be46290596dcf6d195c2c286b1615d4139276f9be9c6f202ee1ee8a5569b4a4fc838b1d7306aa71c8e431a6b8075707')

    def test_invalid_block_1(self):
        self.setup_mnemonic_nopin_nopassphrase()

# Path: keepkeylib/client.py
# class CallException(Exception):
#     def __init__(self, code, message):
#         super(CallException, self).__init__()
#         self.args = [code, message]
# 
# Path: keepkeylib/tools.py
# def parse_path(nstr):
#     """
#     Convert BIP32 path string to list of uint32 integers with hardened flags.
#     Several conventions are supported to set the hardened flag: -1, 1', 1h
#     e.g.: "0/1h/1" -> [0, 0x80000001, 1]
#     :param nstr: path string
#     :return: list of integers
#     """
#     if not nstr:
#         return []
# 
#     n = nstr.split('/')
# 
#     # m/a/b/c => a/b/c
#     if n[0] == 'm':
#         n = n[1:]
# 
#     # coin_name/a/b/c => 44'/SLIP44_constant'/a/b/c
#     #if n[0] in slip44:
#     #    coin_id = slip44[n[0]]
#     #    n[0:1] = ['44h', '{}h'.format(coin_id)]
# 
#     def str_to_harden(x):
#         if x.startswith('-'):
#             return H_(abs(int(x)))
#         elif x.endswith(('h', "'")):
#             return H_(int(x[:-1]))
#         else:
#             return int(x)
# 
#     try:
#         return list(str_to_harden(x) for x in n)
#     except Exception:
#         raise ValueError('Invalid BIP32 path', nstr)
# 
# Path: keepkeylib/nano.py
# def encode_balance(balance):


# Path: tests/test_msg_nano_signtx.py
import unittest
import common
from binascii import hexlify, unhexlify
from keepkeylib.client import CallException
from keepkeylib.tools import parse_path
from keepkeylib import messages_pb2 as proto
from keepkeylib import messages_nano_pb2 as proto_nano
from keepkeylib import types_pb2 as proto_types
from keepkeylib import nano

    #             balance=86242336390000000000000000000,
    #         )
    #         self.assertIsInstance(res, proto_nano.NanoSignedTx)
    #         self.assertEqual(hexlify(res.block_hash), '5d732d843c22f806011127655790484dbabd38dda20b24900c053c3dfc12523f')
    #         self.assertEqual(hexlify(res.signature), '3fb596c34db1241201983cbf613fe9b68a6eae2420c7f294c7e883574fda10d5cc19c9e516b57ed0cbc5e7d3438f70f2ddd7a45bf3e693ff800b97e187de5701')

    # def test_block_6(self):
    #     # https://www.nanode.co/block/a7e59d38b001d9348dbe16fa866d0b435259d381af1db019f3ff83fd7590e226
    #     self.setup_mnemonic_nopin_nopassphrase()
    #     with self.client:
    #         self.client.set_expected_responses([
    #             proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx),
    #             proto_nano.NanoSignedTx(),
    #         ])
    #         res = self.client.nano_sign_tx(
    #             'Nano', NANO_ACCOUNT_0_PATH,
    #             grandparent_hash=unhexlify('32ac7d8f5a16a498abf203b8dfee623c9e111ff25e7339f8cd69ec7492b23edd'),
    #             parent_link=unhexlify(NANO_ACCOUNT_1_PUBLICKEY),
    #             parent_representative=REP_NANODE,
    #             parent_balance=86242336390000000000000000000,
    #             link_recipient_n=OTHER_ACCOUNT_3_PATH,
    #             representative=REP_NANODE,
    #             balance=40000760000000000000000000000,
    #         )
    #         self.assertIsInstance(res, proto_nano.NanoSignedTx)
    #         self.assertEqual(hexlify(res.block_hash), 'a7e59d38b001d9348dbe16fa866d0b435259d381af1db019f3ff83fd7590e226')
    #         self.assertEqual(hexlify(res.signature), '1dcd8a27aeac1cab9a2054d5cc6df1b80be46290596dcf6d195c2c286b1615d4139276f9be9c6f202ee1ee8a5569b4a4fc838b1d7306aa71c8e431a6b8075707')

    def test_invalid_block_1(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.assertRaises(CallException):