# 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
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library.  If not, see <http://www.gnu.org/licenses/>.
#
# The script has been modified for KeepKey Device.


NANO_ACCOUNT_0_PATH = parse_path("m/44'/165'/0'")
NANO_ACCOUNT_0_ADDRESS = 'xrb_1bhbsc9yuh15anq3owu1izw1nk7bhhqefrkhfo954fyt8dk1q911buk1kk4c'
NANO_ACCOUNT_1_PATH = parse_path("m/44'/165'/1'")
NANO_ACCOUNT_1_ADDRESS = 'xrb_3p9ws1t6nx7r5xunf7khtbzqwa9ncjte9fmiy59eiyjkkfds6z5zgpom1cxs'
NANO_ACCOUNT_1_PUBLICKEY = 'd8fcc8344a74b81f7746964fd27f7e20f45474c3b670f0cec87a329357927c7f'
OTHER_ACCOUNT_3_PATH = parse_path("m/44'/100'/3'")
OTHER_ACCOUNT_3_ADDRESS = 'xrb_1x9k73o5icut7pr8khu9xcgtbaau6z6fh5fxxb5m9s3fpzuoe6aio9xjz4et'
OTHER_ACCOUNT_3_PUBLICKEY = '74f2286a382b7a2db0693f67ea9da4a11b27c8d78dbdea4733e42db7f7561110'
REP_OFFICIAL_1 = 'xrb_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4'
REP_NANODE = 'xrb_1nanode8ngaakzbck8smq6ru9bethqwyehomf79sae1k7xd47dkidjqzffeg'
RECIPIENT_DONATIONS = 'xrb_3wm37qz19zhei7nzscjcopbrbnnachs4p1gnwo5oroi3qonw6inwgoeuufdp'
RECIPIENT_DONATIONS_PUBLICKEY = 'f2612dfe03fdec8169fcaa2aad9384d28853f22b01d4e5475c5601bd69c2429c'

class TestMsgNanoSignTx(common.KeepKeyTest):

    def test_encode_balance(self):
        self.assertEqual(hexlify(nano.encode_balance(0)), '00000000000000000000000000000000')
        self.assertEqual(hexlify(nano.encode_balance(4440329590121742105910495447534801366)), '03572d26b8163ca8016a76280cb011d6')

# 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

#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library.  If not, see <http://www.gnu.org/licenses/>.
#
# The script has been modified for KeepKey Device.


NANO_ACCOUNT_0_PATH = parse_path("m/44'/165'/0'")
NANO_ACCOUNT_0_ADDRESS = 'xrb_1bhbsc9yuh15anq3owu1izw1nk7bhhqefrkhfo954fyt8dk1q911buk1kk4c'
NANO_ACCOUNT_1_PATH = parse_path("m/44'/165'/1'")
NANO_ACCOUNT_1_ADDRESS = 'xrb_3p9ws1t6nx7r5xunf7khtbzqwa9ncjte9fmiy59eiyjkkfds6z5zgpom1cxs'
NANO_ACCOUNT_1_PUBLICKEY = 'd8fcc8344a74b81f7746964fd27f7e20f45474c3b670f0cec87a329357927c7f'
OTHER_ACCOUNT_3_PATH = parse_path("m/44'/100'/3'")
OTHER_ACCOUNT_3_ADDRESS = 'xrb_1x9k73o5icut7pr8khu9xcgtbaau6z6fh5fxxb5m9s3fpzuoe6aio9xjz4et'
OTHER_ACCOUNT_3_PUBLICKEY = '74f2286a382b7a2db0693f67ea9da4a11b27c8d78dbdea4733e42db7f7561110'
REP_OFFICIAL_1 = 'xrb_3arg3asgtigae3xckabaaewkx3bzsh7nwz7jkmjos79ihyaxwphhm6qgjps4'
REP_NANODE = 'xrb_1nanode8ngaakzbck8smq6ru9bethqwyehomf79sae1k7xd47dkidjqzffeg'
RECIPIENT_DONATIONS = 'xrb_3wm37qz19zhei7nzscjcopbrbnnachs4p1gnwo5oroi3qonw6inwgoeuufdp'
RECIPIENT_DONATIONS_PUBLICKEY = 'f2612dfe03fdec8169fcaa2aad9384d28853f22b01d4e5475c5601bd69c2429c'

class TestMsgNanoSignTx(common.KeepKeyTest):

    def test_encode_balance(self):
        self.assertEqual(hexlify(nano.encode_balance(0)), '00000000000000000000000000000000')
        self.assertEqual(hexlify(nano.encode_balance(4440329590121742105910495447534801366)), '03572d26b8163ca8016a76280cb011d6')

        self.assertEqual(hexlify(nano.encode_balance(340282366920938463463374607431768211455)), 'ffffffffffffffffffffffffffffffff')