# Path: keepkeylib/client.py
# class CallException(Exception):
#     def __init__(self, code, message):
#         super(CallException, self).__init__()
#         self.args = [code, message]
# 
# 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 unittest
import common
import binascii
import keepkeylib.messages_pb2 as proto
import keepkeylib.types_pb2 as proto_types
from keepkeylib.client import CallException
from keepkeylib.tx_api import TxApiZcashTestnet
                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=0)),
                proto.TxRequest(request_type=proto_types.TXFINISHED),

            ])

            (signatures, serialized_tx) = self.client.sign_tx('Zcash Testnet', [inp1, ], [out1, ])

        self.assertEqual(binascii.hexlify(serialized_tx), b'0100000001566b5e454d2b3443e01aab15b32f8870c9cb70098f6d149217c0802ed796ffc8000000006a4730440220226750799c61f8914df7cf8a7623bbcde3197e0eb83dac9905c2ff5f4a29c41a02203215a36c14ddbf82d57dc69476f7a08b6a4f2349960ae01a5a489b974f262a110121030e669acac1f280d1ddf441cd2ba5e97417bf2689e4bbec86df4f831bf9f7ffd0ffffffff016cd9f505000000001976a9145b157a678a10021243307e4bb58f36375aa80e1088ac00000000')

    def test_shieldedIn_one_one_fee_1(self):
        self.setup_mnemonic_allallall()

        # tx:  43d133a5bb5d1764368726707584c4eb1faf2a696a832325a7608d6b5e72aeca
        # input 0: 19.99740373 TAZ

        inp1 = proto_types.TxInputType(address_n=[2147483692, 2147483649, 2147483648, 0, 0],  # tmJ1xYxP8XNTtCoDgvdmQPSrxh5qZJgy65Z
                             # amount=1999740373,
                             prev_hash=binascii.unhexlify(b'43d133a5bb5d1764368726707584c4eb1faf2a696a832325a7608d6b5e72aeca'),
                             prev_index=0,
                             )

        out1 = proto_types.TxOutputType(address='tmJ1xYxP8XNTtCoDgvdmQPSrxh5qZJgy65Z',
                              amount=1999740373 - 1940,
                              script_type=proto_types.PAYTOADDRESS,
                              )

        with self.client:

# Path: keepkeylib/client.py
# class CallException(Exception):
#     def __init__(self, code, message):
#         super(CallException, self).__init__()
#         self.args = [code, message]
# 
# 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_zcash.py
import unittest
import common
import binascii
import keepkeylib.messages_pb2 as proto
import keepkeylib.types_pb2 as proto_types
from keepkeylib.client import CallException
from keepkeylib.tx_api import TxApiZcashTestnet

                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=0)),
                proto.TxRequest(request_type=proto_types.TXFINISHED),

            ])

            (signatures, serialized_tx) = self.client.sign_tx('Zcash Testnet', [inp1, ], [out1, ])

        self.assertEqual(binascii.hexlify(serialized_tx), b'0100000001566b5e454d2b3443e01aab15b32f8870c9cb70098f6d149217c0802ed796ffc8000000006a4730440220226750799c61f8914df7cf8a7623bbcde3197e0eb83dac9905c2ff5f4a29c41a02203215a36c14ddbf82d57dc69476f7a08b6a4f2349960ae01a5a489b974f262a110121030e669acac1f280d1ddf441cd2ba5e97417bf2689e4bbec86df4f831bf9f7ffd0ffffffff016cd9f505000000001976a9145b157a678a10021243307e4bb58f36375aa80e1088ac00000000')

    def test_shieldedIn_one_one_fee_1(self):
        self.setup_mnemonic_allallall()

        # tx:  43d133a5bb5d1764368726707584c4eb1faf2a696a832325a7608d6b5e72aeca
        # input 0: 19.99740373 TAZ

        inp1 = proto_types.TxInputType(address_n=[2147483692, 2147483649, 2147483648, 0, 0],  # tmJ1xYxP8XNTtCoDgvdmQPSrxh5qZJgy65Z
                             # amount=1999740373,
                             prev_hash=binascii.unhexlify(b'43d133a5bb5d1764368726707584c4eb1faf2a696a832325a7608d6b5e72aeca'),
                             prev_index=0,
                             )

        out1 = proto_types.TxOutputType(address='tmJ1xYxP8XNTtCoDgvdmQPSrxh5qZJgy65Z',
                              amount=1999740373 - 1940,
                              script_type=proto_types.PAYTOADDRESS,
                              )

        with self.client:

            self.client.set_tx_api(TxApiZcashTestnet)