# Path: keepkeylib/ckd_public.py
# PRIME_DERIVATION_FLAG = 0x80000000
#     I64 = hmac.HMAC(key=node.chain_code, msg=data, digestmod=hashlib.sha512).digest()
# def point_to_pubkey(point):
# def sec_to_public_pair(pubkey):
#     def public_pair_for_x(generator, x, is_even):
# def is_prime(n):
# def fingerprint(pubkey):
# def get_address(public_node, address_type):
# def public_ckd(public_node, n):
# def get_subnode(node, i):
# def serialize(node, version=0x0488B21E):
# def deserialize(xpub):
# 
# 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/tx_api.py
# def pack_varint(n):
#     def __init__(self, network, url):
#     def fetch_json(self, url, resource, resourceid):
#     def get_tx(self, txhash):
#     def __init__(self, network, url, zcash=None):
#     def get_tx(self, txhash):
#     def get_raw_tx(self, txhash):
# class TxApi(object):
# class TxApiInsight(TxApi):


import common
import unittest
from binascii import hexlify, unhexlify
from keepkeylib import ckd_public as bip32
from common import KeepKeyTest
from keepkeylib import messages_pb2 as proto
from keepkeylib import types_pb2 as proto_types
from keepkeylib.client import CallException
from keepkeylib.tools import parse_path
from keepkeylib.tx_api import TxApiGroestlcoinTestnet
                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=1)),
                proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput),
                proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx),
                proto.TxRequest(request_type=proto_types.TXINPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=1)),
                proto.TxRequest(request_type=proto_types.TXINPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
                proto.TxRequest(request_type=proto_types.TXFINISHED),
            ])
            (signatures, serialized_tx) = self.client.sign_tx('GRS Testnet', [inp1], [out1, out2], lock_time=650713)

        self.assertEqual(hexlify(serialized_tx), b'01000000000101d1613f483f2086d076c82fe34674385a86beb08f052d5405fe1aed397f852f4f0000000000feffffff02404b4c000000000017a9147a55d61848e77ca266e79a39bfc85c580a6426c987a8386f0000000000160014cc8067093f6f843d6d3e22004a4290cd0c0f336b02483045022100ea8780bc1e60e14e945a80654a41748bbf1aa7d6f2e40a88d91dfc2de1f34bd10220181a474a3420444bd188501d8d270736e1e9fe379da9970de992ff445b0972e3012103adc58245cf28406af0ef5cc24b8afba7f1be6c72f279b642d85c48798685f862d9ed0900')

    def test_send_native_change(self):
        self.setup_mnemonic_allallall()
        self.client.set_tx_api(TxApiGroestlcoinTestnet)
        inp1 = proto_types.TxInputType(
            address_n=parse_path("84'/1'/0'/0/0"), # tgrs1qkvwu9g3k2pdxewfqr7syz89r3gj557l3ued7ja
            amount=12300000,
            prev_hash=unhexlify('4f2f857f39ed1afe05542d058fb0be865a387446e32fc876d086203f483f61d1'),
            prev_index=0,
            sequence=0xfffffffe,
            script_type=proto_types.SPENDWITNESS,
        )
        out1 = proto_types.TxOutputType(
            address='2N4Q5FhU2497BryFfUgbqkAJE87aKDv3V3e',
            amount=5000000,
            script_type=proto_types.PAYTOADDRESS,
        )
        out2 = proto_types.TxOutputType(

# Path: keepkeylib/ckd_public.py
# PRIME_DERIVATION_FLAG = 0x80000000
#     I64 = hmac.HMAC(key=node.chain_code, msg=data, digestmod=hashlib.sha512).digest()
# def point_to_pubkey(point):
# def sec_to_public_pair(pubkey):
#     def public_pair_for_x(generator, x, is_even):
# def is_prime(n):
# def fingerprint(pubkey):
# def get_address(public_node, address_type):
# def public_ckd(public_node, n):
# def get_subnode(node, i):
# def serialize(node, version=0x0488B21E):
# def deserialize(xpub):
# 
# 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/tx_api.py
# def pack_varint(n):
#     def __init__(self, network, url):
#     def fetch_json(self, url, resource, resourceid):
#     def get_tx(self, txhash):
#     def __init__(self, network, url, zcash=None):
#     def get_tx(self, txhash):
#     def get_raw_tx(self, txhash):
# class TxApi(object):
# class TxApiInsight(TxApi):


# Path: tests/test_msg_signtx_segwit_native_grs.py
import common
import unittest
from binascii import hexlify, unhexlify
from keepkeylib import ckd_public as bip32
from common import KeepKeyTest
from keepkeylib import messages_pb2 as proto
from keepkeylib import types_pb2 as proto_types
from keepkeylib.client import CallException
from keepkeylib.tools import parse_path
from keepkeylib.tx_api import TxApiGroestlcoinTestnet

                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=1)),
                proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput),
                proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx),
                proto.TxRequest(request_type=proto_types.TXINPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=1)),
                proto.TxRequest(request_type=proto_types.TXINPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
                proto.TxRequest(request_type=proto_types.TXFINISHED),
            ])
            (signatures, serialized_tx) = self.client.sign_tx('GRS Testnet', [inp1], [out1, out2], lock_time=650713)

        self.assertEqual(hexlify(serialized_tx), b'01000000000101d1613f483f2086d076c82fe34674385a86beb08f052d5405fe1aed397f852f4f0000000000feffffff02404b4c000000000017a9147a55d61848e77ca266e79a39bfc85c580a6426c987a8386f0000000000160014cc8067093f6f843d6d3e22004a4290cd0c0f336b02483045022100ea8780bc1e60e14e945a80654a41748bbf1aa7d6f2e40a88d91dfc2de1f34bd10220181a474a3420444bd188501d8d270736e1e9fe379da9970de992ff445b0972e3012103adc58245cf28406af0ef5cc24b8afba7f1be6c72f279b642d85c48798685f862d9ed0900')

    def test_send_native_change(self):
        self.setup_mnemonic_allallall()
        self.client.set_tx_api(TxApiGroestlcoinTestnet)
        inp1 = proto_types.TxInputType(
            address_n=parse_path("84'/1'/0'/0/0"), # tgrs1qkvwu9g3k2pdxewfqr7syz89r3gj557l3ued7ja
            amount=12300000,
            prev_hash=unhexlify('4f2f857f39ed1afe05542d058fb0be865a387446e32fc876d086203f483f61d1'),
            prev_index=0,
            sequence=0xfffffffe,
            script_type=proto_types.SPENDWITNESS,
        )
        out1 = proto_types.TxOutputType(
            address='2N4Q5FhU2497BryFfUgbqkAJE87aKDv3V3e',
            amount=5000000,
            script_type=proto_types.PAYTOADDRESS,
        )
        out2 = proto_types.TxOutputType(

            address_n=parse_path("84'/1'/0'/1/0"), # tgrs1qejqxwzfld7zr6mf7ygqy5s5se5xq7vmt9lkd57