# 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 int_to_big_endian(value):
#     import struct
# 
#     res = b''
#     while 0 < value:
#         res = struct.pack("B", value & 0xff) + res
#         value = value >> 8
# 
#     return res


import unittest
import common
import binascii
import struct
import keepkeylib.messages_pb2 as proto
import keepkeylib.types_pb2 as proto_types
import keepkeylib.exchange_pb2 as proto_exchange
from keepkeylib.client import CallException
from keepkeylib.tools import int_to_big_endian
# 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.



class TestMsgEthereumSigntx(common.KeepKeyTest):
    def test_ethereum_tx_xfer_acc1(self):
        self.setup_mnemonic_nopin_nopassphrase()
        self.client.apply_policy('ShapeShift', 1)

        sig_v, sig_r, sig_s, hash, signature_der = self.client.ethereum_sign_tx(
            n=[0, 0],
            nonce=0,
            gas_price=20,
            gas_limit=20,
            value=1234567890,
            to_n=[0x8000002c, 0x8000003c, 0x80000001, 0, 0],
            address_type=proto_types.TRANSFER,
            chain_id=1,
            )

        self.assertEqual(sig_v, 37)
        self.assertEqual(binascii.hexlify(sig_r), '72b87b41a92dbad6dffcd7f9441102029adad4864291f867726abaaa3572dee7')
        self.assertEqual(binascii.hexlify(sig_s), '26814243d29bea4dddaef6da5b8af6836e499c2c7ccaa0877204f5465edc8290')

# 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 int_to_big_endian(value):
#     import struct
# 
#     res = b''
#     while 0 < value:
#         res = struct.pack("B", value & 0xff) + res
#         value = value >> 8
# 
#     return res


# Path: tests/test_msg_ethereum_signtx_xfer.py
import unittest
import common
import binascii
import struct
import keepkeylib.messages_pb2 as proto
import keepkeylib.types_pb2 as proto_types
import keepkeylib.exchange_pb2 as proto_exchange
from keepkeylib.client import CallException
from keepkeylib.tools import int_to_big_endian

# 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.



class TestMsgEthereumSigntx(common.KeepKeyTest):
    def test_ethereum_tx_xfer_acc1(self):
        self.setup_mnemonic_nopin_nopassphrase()
        self.client.apply_policy('ShapeShift', 1)

        sig_v, sig_r, sig_s, hash, signature_der = self.client.ethereum_sign_tx(
            n=[0, 0],
            nonce=0,
            gas_price=20,
            gas_limit=20,
            value=1234567890,
            to_n=[0x8000002c, 0x8000003c, 0x80000001, 0, 0],
            address_type=proto_types.TRANSFER,
            chain_id=1,
            )

        self.assertEqual(sig_v, 37)
        self.assertEqual(binascii.hexlify(sig_r), '72b87b41a92dbad6dffcd7f9441102029adad4864291f867726abaaa3572dee7')
        self.assertEqual(binascii.hexlify(sig_s), '26814243d29bea4dddaef6da5b8af6836e499c2c7ccaa0877204f5465edc8290')

        self.assertEqual(binascii.hexlify(hash), 'f8cb6ffe80afaa7f651e66933e62667c3250a0168fcc29b1f46529511bf53fe6')