Ejemplo n.º 1
0
    def test_ethereum_sanity_checks(self, client):
        # gas overflow
        with pytest.raises(TrezorFailure):
            ethereum.sign_tx(
                client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=123456,
                gas_price=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
                gas_limit=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
                to=TO_ADDR,
                value=12345678901234567890,
            )

        # no gas price
        with pytest.raises(TrezorFailure):
            client.call(
                messages.EthereumSignTx(
                    address_n=parse_path("44'/60'/0'/0/0"),
                    nonce=b"AAA",
                    gas_limit=ethereum.int_to_big_endian(10000),
                    to=TO_ADDR,
                    value=ethereum.int_to_big_endian(12345678901234567890),
                ))

        # no gas limit
        with pytest.raises(TrezorFailure):
            client.call(
                messages.EthereumSignTx(
                    address_n=parse_path("44'/60'/0'/0/0"),
                    nonce=b"AAA",
                    gas_price=ethereum.int_to_big_endian(10000),
                    to=TO_ADDR,
                    value=ethereum.int_to_big_endian(12345678901234567890),
                ))
Ejemplo n.º 2
0
def test_signtx_data_pagination(client: Client, flow):
    with client:
        client.watch_layout()
        client.set_input_flow(flow(client))
        ethereum.sign_tx(
            client,
            n=parse_path("m/44h/60h/0h/0/0"),
            nonce=0x0,
            gas_price=0x14,
            gas_limit=0x14,
            to="0x1d1c328764a41bda0492b66baa30c4a339ff85ef",
            chain_id=1,
            value=0xA,
            tx_type=None,
            data=bytes.fromhex(HEXDATA),
        )

    with client, pytest.raises(exceptions.Cancelled):
        client.watch_layout()
        client.set_input_flow(flow(client, cancel=True))
        ethereum.sign_tx(
            client,
            n=parse_path("m/44h/60h/0h/0/0"),
            nonce=0x0,
            gas_price=0x14,
            gas_limit=0x14,
            to="0x1d1c328764a41bda0492b66baa30c4a339ff85ef",
            chain_id=1,
            value=0xA,
            tx_type=None,
            data=bytes.fromhex(HEXDATA),
        )
    def test_ethereum_signtx_nodata(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.client:
            self.client.set_expected_responses(
                [
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.EthereumTxRequest(data_length=None),  # v,r,s checked later
                ]
            )

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/100"),
                nonce=0,
                gas_price=20,
                gas_limit=20,
                to=TO_ADDR,
                value=10,
            )

        assert sig_v == 27
        assert (
            sig_r.hex()
            == "2f548f63ddb4cf19b6b9f922da58ff71833b967d590f3b4dcc2a70810338a982"
        )
        assert (
            sig_s.hex()
            == "428d35f0dca963b5196b63e7aa5e0405d8bff77d6aee1202183f1f68dacb4483"
        )

        with self.client:
            self.client.set_expected_responses(
                [
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.EthereumTxRequest(data_length=None),
                ]
            )

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/100"),
                nonce=123456,
                gas_price=20000,
                gas_limit=20000,
                to=TO_ADDR,
                value=12345678901234567890,
            )
        assert sig_v == 27
        assert (
            sig_r.hex()
            == "3bf0470cd7f5ad8d82613199f73deadc55c3c9f32f91b1a21b5ef644144ebd58"
        )
        assert (
            sig_s.hex()
            == "48b3ef1b2502febdf35e9ff4df0ba1fda62f042fad639eb4852a297fc9872ebd"
        )
Ejemplo n.º 4
0
    def test_ethereum_signtx_nodata(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.client:
            self.client.set_expected_responses(
                [
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.EthereumTxRequest(data_length=None),  # v,r,s checked later
                ]
            )

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/100"),
                nonce=0,
                gas_price=20,
                gas_limit=20,
                to=TO_ADDR,
                value=10,
            )

        assert sig_v == 27
        assert (
            sig_r.hex()
            == "2f548f63ddb4cf19b6b9f922da58ff71833b967d590f3b4dcc2a70810338a982"
        )
        assert (
            sig_s.hex()
            == "428d35f0dca963b5196b63e7aa5e0405d8bff77d6aee1202183f1f68dacb4483"
        )

        with self.client:
            self.client.set_expected_responses(
                [
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.EthereumTxRequest(data_length=None),
                ]
            )

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/100"),
                nonce=123456,
                gas_price=20000,
                gas_limit=20000,
                to=TO_ADDR,
                value=12345678901234567890,
            )
        assert sig_v == 27
        assert (
            sig_r.hex()
            == "3bf0470cd7f5ad8d82613199f73deadc55c3c9f32f91b1a21b5ef644144ebd58"
        )
        assert (
            sig_s.hex()
            == "48b3ef1b2502febdf35e9ff4df0ba1fda62f042fad639eb4852a297fc9872ebd"
        )
Ejemplo n.º 5
0
    def test_ethereum_signtx_nodata(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.client:
            self.client.set_expected_responses([
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.EthereumTxRequest(
                    data_length=None),  # v,r,s checked later
            ])

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=0,
                gas_price=20,
                gas_limit=20,
                to=unhexlify("1d1c328764a41bda0492b66baa30c4a339ff85ef"),
                value=10,
            )

        assert sig_v == 27
        assert (
            hexlify(sig_r) ==
            b"9b61192a161d056c66cfbbd331edb2d783a0193bd4f65f49ee965f791d898f72"
        )
        assert (
            hexlify(sig_s) ==
            b"49c0bbe35131592c6ed5c871ac457feeb16a1493f64237387fab9b83c1a202f7"
        )

        with self.client:
            self.client.set_expected_responses([
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.EthereumTxRequest(data_length=None),
            ])

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=123456,
                gas_price=20000,
                gas_limit=20000,
                to=unhexlify("1d1c328764a41bda0492b66baa30c4a339ff85ef"),
                value=12345678901234567890,
            )
        assert sig_v == 28
        assert (
            hexlify(sig_r) ==
            b"6de597b8ec1b46501e5b159676e132c1aa78a95bd5892ef23560a9867528975a"
        )
        assert (
            hexlify(sig_s) ==
            b"6e33c4230b1ecf96a8dbb514b4aec0a6d6ba53f8991c8143f77812aa6daa993f"
        )
Ejemplo n.º 6
0
    def test_ethereum_signtx_newcontract(self):
        self.setup_mnemonic_allallall()

        # contract creation without data should fail.
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=123456,
                gas_price=20000,
                gas_limit=20000,
                to="",
                value=12345678901234567890,
            )

        with self.client:
            self.client.set_expected_responses(
                [
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.EthereumTxRequest(
                        data_length=1024,
                        signature_r=None,
                        signature_s=None,
                        signature_v=None,
                    ),
                    proto.EthereumTxRequest(data_length=1024),
                    proto.EthereumTxRequest(data_length=1024),
                    proto.EthereumTxRequest(data_length=3),
                    proto.EthereumTxRequest(),
                ]
            )

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=0,
                gas_price=20000,
                gas_limit=20000,
                to="",
                value=12345678901234567890,
                data=b"ABCDEFGHIJKLMNOP" * 256 + b"!!!",
            )
        assert sig_v == 28
        assert (
            sig_r.hex()
            == "c86bda9de238b1c602648996561e7270a3be208da96bbf23474cb8e4014b9f93"
        )
        assert (
            sig_s.hex()
            == "18742403f75a05e7fa9868c30b36f1e55628de02d01c03084c1ff6775a13137c"
        )
    def test_ethereum_signtx_newcontract(self):
        self.setup_mnemonic_allallall()

        # contract creation without data should fail.
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=123456,
                gas_price=20000,
                gas_limit=20000,
                to="",
                value=12345678901234567890,
            )

        with self.client:
            self.client.set_expected_responses(
                [
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.EthereumTxRequest(
                        data_length=1024,
                        signature_r=None,
                        signature_s=None,
                        signature_v=None,
                    ),
                    proto.EthereumTxRequest(data_length=1024),
                    proto.EthereumTxRequest(data_length=1024),
                    proto.EthereumTxRequest(data_length=3),
                    proto.EthereumTxRequest(),
                ]
            )

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=0,
                gas_price=20000,
                gas_limit=20000,
                to="",
                value=12345678901234567890,
                data=b"ABCDEFGHIJKLMNOP" * 256 + b"!!!",
            )
        assert sig_v == 28
        assert (
            sig_r.hex()
            == "c86bda9de238b1c602648996561e7270a3be208da96bbf23474cb8e4014b9f93"
        )
        assert (
            sig_s.hex()
            == "18742403f75a05e7fa9868c30b36f1e55628de02d01c03084c1ff6775a13137c"
        )
Ejemplo n.º 8
0
    def test_ethereum_signtx_newcontract(self):
        self.setup_mnemonic_nopin_nopassphrase()

        # contract creation without data should fail.
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=123456,
                gas_price=20000,
                gas_limit=20000,
                to="",
                value=12345678901234567890,
            )

        with self.client:
            self.client.set_expected_responses([
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.EthereumTxRequest(
                    data_length=1024,
                    signature_r=None,
                    signature_s=None,
                    signature_v=None,
                ),
                proto.EthereumTxRequest(data_length=1024),
                proto.EthereumTxRequest(data_length=1024),
                proto.EthereumTxRequest(data_length=3),
                proto.EthereumTxRequest(),
            ])

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=0,
                gas_price=20000,
                gas_limit=20000,
                to="",
                value=12345678901234567890,
                data=b"ABCDEFGHIJKLMNOP" * 256 + b"!!!",
            )
        assert sig_v == 28
        assert (
            hexlify(sig_r) ==
            b"b401884c10ae435a2e792303b5fc257a09f94403b2883ad8c0ac7a7282f5f1f9"
        )
        assert (
            hexlify(sig_s) ==
            b"4742fc9e6a5fa8db3db15c2d856914a7f3daab21603a6c1ce9e9927482f8352e"
        )
Ejemplo n.º 9
0
def test_data_streaming(client: Client):
    """Only verifying the expected responses, the signatures are
    checked in vectorized function above.
    """
    with client:
        client.set_expected_responses(
            [
                messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                message_filters.EthereumTxRequest(
                    data_length=1_024,
                    signature_r=None,
                    signature_s=None,
                    signature_v=None,
                ),
                message_filters.EthereumTxRequest(
                    data_length=1_024,
                    signature_r=None,
                    signature_s=None,
                    signature_v=None,
                ),
                message_filters.EthereumTxRequest(
                    data_length=1_024,
                    signature_r=None,
                    signature_s=None,
                    signature_v=None,
                ),
                message_filters.EthereumTxRequest(
                    data_length=3,
                    signature_r=None,
                    signature_s=None,
                    signature_v=None,
                ),
                message_filters.EthereumTxRequest(data_length=None),
            ]
        )

        ethereum.sign_tx(
            client,
            n=parse_path("m/44h/60h/0h/0/0"),
            nonce=0,
            gas_price=20_000,
            gas_limit=20_000,
            to=TO_ADDR,
            value=0,
            data=b"ABCDEFGHIJKLMNOP" * 256 + b"!!!",
            chain_id=1,
        )
    def test_ethereum_signtx_wanchain(self, client):
        with client:
            client.set_expected_responses(
                [
                    messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                    messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                    messages.EthereumTxRequest(data_length=None),
                ]
            )
            sig_v, sig_r, sig_s = ethereum.sign_tx(
                client,
                n=parse_path("44'/5718350'/0'/0/0"),
                nonce=0,
                gas_price=20,
                gas_limit=20,
                # ADT token address
                to="0xd0d6d6c5fe4a677d343cc433536bb717bae167dd",
                chain_id=1,
                tx_type=1,
                # value needs to be 0, token value is set in the contract (data)
                value=100,
            )

            # ad-hoc generated signature. might not be valid.
            assert (
                sig_r.hex()
                == "d6e197029031ec90b53ed14e8233aa78b592400513ac0386d2d55cdedc3d796f"
            )
            assert (
                sig_s.hex()
                == "326e0d600dd1b7ee606eb531b998a6a3b3293d4995fb8cfe0677962e8a43cff6"
            )
Ejemplo n.º 11
0
    def test_ethereum_signtx_message(self, client):
        with client:
            client.set_expected_responses([
                messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                messages.EthereumTxRequest(
                    data_length=1024,
                    signature_r=None,
                    signature_s=None,
                    signature_v=None,
                ),
                messages.EthereumTxRequest(data_length=1024),
                messages.EthereumTxRequest(data_length=1024),
                messages.EthereumTxRequest(data_length=3),
                messages.EthereumTxRequest(),
            ])

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=0,
                gas_price=20000,
                gas_limit=20000,
                to=TO_ADDR,
                value=0,
                data=b"ABCDEFGHIJKLMNOP" * 256 + b"!!!",
            )
        assert sig_v == 27
        assert (
            sig_r.hex() ==
            "81af16020d3c6ad820cab2e2b0834fa37f4a9b0c2443f151a4e2f12fe1081b09")
        assert (
            sig_s.hex() ==
            "7b34b5d8a43771d493cd9fa0c7b27a9563e2a31799fb9f0c2809539a848b9f47")
    def test_ethereum_signtx_unknown_erc20_token(self, client):
        with client:
            expected_responses = [
                messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
            ]
            # TT asks for contract address confirmation
            if client.features.model == "T":
                expected_responses.append(
                    messages.ButtonRequest(
                        code=messages.ButtonRequestType.SignTx))

            expected_responses.append(
                messages.EthereumTxRequest(data_length=None))
            client.set_expected_responses(expected_responses)

            data = bytearray()
            # method id signalizing `transfer(address _to, uint256 _value)` function
            data.extend(bytes.fromhex("a9059cbb"))
            # 1st function argument (to - the receiver)
            data.extend(
                bytes.fromhex(
                    "000000000000000000000000574bbb36871ba6b78e27f4b4dcfb76ea0091880b"
                ))
            # 2nd function argument (value - amount to be transferred)
            data.extend(
                bytes.fromhex(
                    "0000000000000000000000000000000000000000000000000000000000000123"
                ))
            # since this token is unknown trezor should display "unknown token value"

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                client,
                n=parse_path("44'/60'/0'/0/1"),
                nonce=0,
                gas_price=20,
                gas_limit=20,
                # unknown token address (Grzegorz Brzęczyszczykiewicz Token)
                to="0xfc6b5d6af8a13258f7cbd0d39e11b35e01a32f93",
                chain_id=1,
                # value needs to be 0, token value is set in the contract (data)
                value=0,
                data=data,
            )

            # taken from T1 might not be 100% correct but still better than nothing
            assert (
                sig_r.hex() ==
                "2559bbf1bcb80992b6eaa96f0074b19606d8ea7bf4219e1c9ac64a12855c0cce"
            )
            assert (
                sig_s.hex() ==
                "633a74429eb6d3aeec4ed797542236a85daab3cab15e37736b87a45697541d7a"
            )
Ejemplo n.º 13
0
    def test_ethereum_signtx_known_erc20_token(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.client:
            self.client.set_expected_responses(
                [
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.EthereumTxRequest(data_length=None),
                ]
            )

            data = bytearray()
            # method id signalizing `transfer(address _to, uint256 _value)` function
            data.extend(bytes.fromhex("a9059cbb"))
            # 1st function argument (to - the receiver)
            data.extend(
                bytes.fromhex(
                    "000000000000000000000000574bbb36871ba6b78e27f4b4dcfb76ea0091880b"
                )
            )
            # 2nd function argument (value - amount to be transferred)
            data.extend(
                bytes.fromhex(
                    "000000000000000000000000000000000000000000000000000000000bebc200"
                )
            )
            # 200 000 000 in dec, divisibility of ADT = 9, trezor1 displays 0.2 ADT, Trezor T 200 000 000 Wei ADT

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=0,
                gas_price=20,
                gas_limit=20,
                # ADT token address
                to="0xd0d6d6c5fe4a677d343cc433536bb717bae167dd",
                chain_id=1,
                # value needs to be 0, token value is set in the contract (data)
                value=0,
                data=data,
            )

            # taken from T1 might not be 100% correct but still better than nothing
            assert (
                sig_r.hex()
                == "ec1df922115d256745410fbc2070296756583c8786e4d402a88d4e29ec513fa9"
            )
            assert (
                sig_s.hex()
                == "7001bfe3ba357e4a9f9e0d3a3f8a8962257615a4cf215db93e48b98999fc51b7"
            )
    def test_ethereum_signtx_known_erc20_token(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.client:
            self.client.set_expected_responses(
                [
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.EthereumTxRequest(data_length=None),
                ]
            )

            data = bytearray()
            # method id signalizing `transfer(address _to, uint256 _value)` function
            data.extend(bytes.fromhex("a9059cbb"))
            # 1st function argument (to - the receiver)
            data.extend(
                bytes.fromhex(
                    "000000000000000000000000574bbb36871ba6b78e27f4b4dcfb76ea0091880b"
                )
            )
            # 2nd function argument (value - amount to be transferred)
            data.extend(
                bytes.fromhex(
                    "000000000000000000000000000000000000000000000000000000000bebc200"
                )
            )
            # 200 000 000 in dec, divisibility of ADT = 9, trezor1 displays 0.2 ADT, Trezor T 200 000 000 Wei ADT

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=0,
                gas_price=20,
                gas_limit=20,
                # ADT token address
                to="0xd0d6d6c5fe4a677d343cc433536bb717bae167dd",
                chain_id=1,
                # value needs to be 0, token value is set in the contract (data)
                value=0,
                data=data,
            )

            # taken from T1 might not be 100% correct but still better than nothing
            assert (
                sig_r.hex()
                == "ec1df922115d256745410fbc2070296756583c8786e4d402a88d4e29ec513fa9"
            )
            assert (
                sig_s.hex()
                == "7001bfe3ba357e4a9f9e0d3a3f8a8962257615a4cf215db93e48b98999fc51b7"
            )
    def test_ethereum_signtx_unknown_erc20_token(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.client:
            self.client.set_expected_responses(
                [
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.EthereumTxRequest(data_length=None),
                ]
            )

            data = bytearray()
            # method id signalizing `transfer(address _to, uint256 _value)` function
            data.extend(bytes.fromhex("a9059cbb"))
            # 1st function argument (to - the receiver)
            data.extend(
                bytes.fromhex(
                    "000000000000000000000000574bbb36871ba6b78e27f4b4dcfb76ea0091880b"
                )
            )
            # 2nd function argument (value - amount to be transferred)
            data.extend(
                bytes.fromhex(
                    "0000000000000000000000000000000000000000000000000000000000000123"
                )
            )
            # since this token is unknown trezor should display "unknown token value"

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/1"),
                nonce=0,
                gas_price=20,
                gas_limit=20,
                # unknown token address (Grzegorz Brzęczyszczykiewicz Token)
                to="0xfc6b5d6af8a13258f7cbd0d39e11b35e01a32f93",
                chain_id=1,
                # value needs to be 0, token value is set in the contract (data)
                value=0,
                data=data,
            )

            # taken from T1 might not be 100% correct but still better than nothing
            assert (
                sig_r.hex()
                == "2559bbf1bcb80992b6eaa96f0074b19606d8ea7bf4219e1c9ac64a12855c0cce"
            )
            assert (
                sig_s.hex()
                == "633a74429eb6d3aeec4ed797542236a85daab3cab15e37736b87a45697541d7a"
            )
def test_chain_ids(client, chain_id, slip44, sig):
    sig_v, sig_r, sig_s = ethereum.sign_tx(
        client,
        n=parse_path(f"m/44h/{slip44}h/0h/0/0"),
        nonce=0,
        gas_price=20000000000,
        gas_limit=21000,
        to="0x8eA7a3fccC211ED48b763b4164884DDbcF3b0A98",
        value=10000000000,
        chain_id=chain_id,
    )
    expected_v = 2 * chain_id + 35
    assert sig_v in (expected_v, expected_v + 1)
    assert (sig_v, sig_r.hex(), sig_s.hex()) == sig
    def test_ethereum_signtx_known_erc20_token(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.client:
            self.client.set_expected_responses([
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.EthereumTxRequest(data_length=None),
            ])

            data = bytearray()
            # method id signalizing `transfer(address _to, uint256 _value)` function
            data.extend(bytes.fromhex("a9059cbb"))
            # 1st function argument (to - the receiver)
            data.extend(
                bytes.fromhex(
                    "000000000000000000000000574bbb36871ba6b78e27f4b4dcfb76ea0091880b"
                ))
            # 2nd function argument (value - amount to be transferred)
            data.extend(
                bytes.fromhex(
                    "000000000000000000000000000000000000000000000000000000000bebc200"
                ))
            # 200 000 000 in dec, divisibility of ADT = 9, trezor1 displays 0.2 ADT, Trezor T 200 000 000 Wei ADT

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=0,
                gas_price=20,
                gas_limit=20,
                # ADT token address
                to=
                b"\xd0\xd6\xd6\xc5\xfe\x4a\x67\x7d\x34\x3c\xc4\x33\x53\x6b\xb7\x17\xba\xe1\x67\xdd",
                chain_id=1,
                # value needs to be 0, token value is set in the contract (data)
                value=0,
                data=data,
            )

            # taken from T1 might not be 100% correct but still better than nothing
            assert (
                sig_r.hex() ==
                "75cf48fa173d8ceb68af9e4fb6b78ef69e6ed5e7679ba6f8e3e91d74b2fb0f96"
            )
            assert (
                sig_s.hex() ==
                "65de4a8c35263b2cfff3954b12146e8e568aa67a1c2461d6865e74ef75c7e190"
            )
    def test_ethereum_signtx_unknown_erc20_token(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.client:
            self.client.set_expected_responses([
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.EthereumTxRequest(data_length=None),
            ])

            data = bytearray()
            # method id signalizing `transfer(address _to, uint256 _value)` function
            data.extend(bytes.fromhex("a9059cbb"))
            # 1st function argument (to - the receiver)
            data.extend(
                bytes.fromhex(
                    "000000000000000000000000574bbb36871ba6b78e27f4b4dcfb76ea0091880b"
                ))
            # 2nd function argument (value - amount to be transferred)
            data.extend(
                bytes.fromhex(
                    "0000000000000000000000000000000000000000000000000000000000000123"
                ))
            # since this token is unknown trezor should display "unknown token value"

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/1"),
                nonce=0,
                gas_price=20,
                gas_limit=20,
                # unknown token address (Grzegorz Brzęczyszczykiewicz Token)
                to=
                b"\xfc\x6b\x5d\x6a\xf8\xa1\x32\x58\xf7\xcb\xd0\xd3\x9e\x11\xb3\x5e\x01\xa3\x2f\x93",
                chain_id=1,
                # value needs to be 0, token value is set in the contract (data)
                value=0,
                data=data,
            )

            # taken from T1 might not be 100% correct but still better than nothing
            assert (
                sig_r.hex() ==
                "2559bbf1bcb80992b6eaa96f0074b19606d8ea7bf4219e1c9ac64a12855c0cce"
            )
            assert (
                sig_s.hex() ==
                "633a74429eb6d3aeec4ed797542236a85daab3cab15e37736b87a45697541d7a"
            )
    def test_ethereum_signtx_unknown_erc20_token(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.client:
            self.client.set_expected_responses([
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.EthereumTxRequest(data_length=None),
            ])

            data = bytearray()
            # method id signalizing `transfer(address _to, uint256 _value)` function
            data.extend(bytes.fromhex("a9059cbb"))
            # 1st function argument (to - the receiver)
            data.extend(
                bytes.fromhex(
                    "000000000000000000000000574bbb36871ba6b78e27f4b4dcfb76ea0091880b"
                ))
            # 2nd function argument (value - amount to be transferred)
            data.extend(
                bytes.fromhex(
                    "0000000000000000000000000000000000000000000000000000000000000123"
                ))
            # since this token is unknown trezor should display "unknown token value"

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=0,
                gas_price=20,
                gas_limit=20,
                # unknown token address (Grzegorz Brzęczyszczykiewicz Token)
                to=
                b"\xfc\x6b\x5d\x6a\xf8\xa1\x32\x58\xf7\xcb\xd0\xd3\x9e\x11\xb3\x5e\x01\xa3\x2f\x93",
                chain_id=1,
                # value needs to be 0, token value is set in the contract (data)
                value=0,
                data=data,
            )

            # taken from T1 might not be 100% correct but still better than nothing
            assert (
                sig_r.hex() ==
                "1707471fbf632e42d18144157aaf4cde101cd9aa9782ad8e30583cfc95ddeef6"
            )
            assert (
                sig_s.hex() ==
                "3d2e52ba5904a4bf131abde3f79db826199f5d6f4d241d531d7e8a30a3b9cfd9"
            )
    def test_ethereum_sanity_checks(self):
        # gas overflow
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=123456,
                gas_price=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
                gas_limit=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
                to=TO_ADDR,
                value=12345678901234567890,
            )

        # no gas price
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=123456,
                gas_limit=10000,
                to=TO_ADDR,
                value=12345678901234567890,
            )

        # no gas limit
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=123456,
                gas_price=10000,
                to=TO_ADDR,
                value=12345678901234567890,
            )

        # no nonce
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=[0, 0],
                gas_price=10000,
                gas_limit=123456,
                to=TO_ADDR,
                value=12345678901234567890,
            )
    def test_ethereum_sanity_checks(self):
        # gas overflow
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=123456,
                gas_price=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
                gas_limit=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
                to=TO_ADDR,
                value=12345678901234567890,
            )

        # no gas price
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=123456,
                gas_limit=10000,
                to=TO_ADDR,
                value=12345678901234567890,
            )

        # no gas limit
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=123456,
                gas_price=10000,
                to=TO_ADDR,
                value=12345678901234567890,
            )

        # no nonce
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=[0, 0],
                gas_price=10000,
                gas_limit=123456,
                to=TO_ADDR,
                value=12345678901234567890,
            )
Ejemplo n.º 22
0
    def test_ethereum_sanity_checks(self):
        # gas overflow
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=123456,
                gas_price=0xffffffffffffffffffffffffffffffff,
                gas_limit=0xffffffffffffffffffffffffffffff,
                to=unhexlify("1d1c328764a41bda0492b66baa30c4a339ff85ef"),
                value=12345678901234567890,
            )

        # no gas price
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=123456,
                gas_limit=10000,
                to=unhexlify("1d1c328764a41bda0492b66baa30c4a339ff85ef"),
                value=12345678901234567890,
            )

        # no gas limit
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=123456,
                gas_price=10000,
                to=unhexlify("1d1c328764a41bda0492b66baa30c4a339ff85ef"),
                value=12345678901234567890,
            )

        # no nonce
        with pytest.raises(Exception):
            ethereum.sign_tx(
                self.client,
                n=[0, 0],
                gas_price=10000,
                gas_limit=123456,
                to=unhexlify("1d1c328764a41bda0492b66baa30c4a339ff85ef"),
                value=12345678901234567890,
            )
def test_with_data(client):
    sig_v, sig_r, sig_s = ethereum.sign_tx(
        client,
        n=parse_path("m/44h/60h/0h/0/0"),
        nonce=0,
        gas_price=20000000000,
        gas_limit=21000,
        to="0x8eA7a3fccC211ED48b763b4164884DDbcF3b0A98",
        value=10000000000,
        chain_id=1,
        data=b"ABCDEFGHIJKLM",
    )
    assert sig_v == 37
    assert (sig_r.hex() ==
            "c57556e308f49c84adc614042bf443381676fd2797f9512a85dc529f7acb7fa8")
    assert (sig_s.hex() ==
            "60a82338641fd4a924a6ec31da4aadc0a43ac77d3a021307ec07bb6982ccbe8d")
Ejemplo n.º 24
0
def test_signtx(client: Client, parameters, result):
    with client:
        sig_v, sig_r, sig_s = ethereum.sign_tx(
            client,
            n=parse_path(parameters["path"]),
            nonce=int(parameters["nonce"], 16),
            gas_price=int(parameters["gas_price"], 16),
            gas_limit=int(parameters["gas_limit"], 16),
            to=parameters["to_address"],
            chain_id=parameters["chain_id"],
            value=int(parameters["value"], 16),
            tx_type=parameters["tx_type"],
            data=bytes.fromhex(parameters["data"]),
        )

    expected_v = 2 * parameters["chain_id"] + 35
    assert sig_v in (expected_v, expected_v + 1)
    assert sig_r.hex() == result["sig_r"]
    assert sig_s.hex() == result["sig_s"]
    assert sig_v == result["sig_v"]
    def test_ethereum_signtx_message(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.client:
            self.client.set_expected_responses(
                [
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.EthereumTxRequest(
                        data_length=1024,
                        signature_r=None,
                        signature_s=None,
                        signature_v=None,
                    ),
                    proto.EthereumTxRequest(data_length=1024),
                    proto.EthereumTxRequest(data_length=1024),
                    proto.EthereumTxRequest(data_length=3),
                    proto.EthereumTxRequest(),
                ]
            )

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=0,
                gas_price=20000,
                gas_limit=20000,
                to=TO_ADDR,
                value=0,
                data=b"ABCDEFGHIJKLMNOP" * 256 + b"!!!",
            )
        assert sig_v == 27
        assert (
            sig_r.hex()
            == "81af16020d3c6ad820cab2e2b0834fa37f4a9b0c2443f151a4e2f12fe1081b09"
        )
        assert (
            sig_s.hex()
            == "7b34b5d8a43771d493cd9fa0c7b27a9563e2a31799fb9f0c2809539a848b9f47"
        )
Ejemplo n.º 26
0
    def test_ethereum_signtx_message(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.client:
            self.client.set_expected_responses([
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.EthereumTxRequest(
                    data_length=1024,
                    signature_r=None,
                    signature_s=None,
                    signature_v=None,
                ),
                proto.EthereumTxRequest(data_length=1024),
                proto.EthereumTxRequest(data_length=1024),
                proto.EthereumTxRequest(data_length=3),
                proto.EthereumTxRequest(),
            ])

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=0,
                gas_price=20000,
                gas_limit=20000,
                to=unhexlify("1d1c328764a41bda0492b66baa30c4a339ff85ef"),
                value=0,
                data=b"ABCDEFGHIJKLMNOP" * 256 + b"!!!",
            )
        assert sig_v == 28
        assert (
            hexlify(sig_r) ==
            b"070e9dafda4d9e733fa7b6747a75f8a4916459560efb85e3e73cd39f31aa160d"
        )
        assert (
            hexlify(sig_s) ==
            b"7842db33ef15c27049ed52741db41fe3238a6fa3a6a0888fcfb74d6917600e41"
        )
Ejemplo n.º 27
0
    def sign_transaction(self, tx):
        """Sign a provided transaction, either with our private key or a Trezor hardware wallet.

        :param tx: Transaction to sign
        :return: Signed transaction
        """
        logger.info('Signing transaction: %s', tx)
        if self.trezor is not None:
            chain_id = tx['chainId']
            txobj = Transaction(
                nonce=tx['nonce'],
                gasprice=tx['gasPrice'],
                startgas=tx['gas'],
                to=tx['to'],
                value=tx['value'],
                data=bytes.fromhex(tx['data'][2:]),
            )

            v, r, s = trezoreth.sign_tx(
                self.trezor,
                n=self.address_n,
                nonce=txobj.nonce,
                gas_price=txobj.gasprice,
                gas_limit=txobj.startgas,
                to=txobj.to,
                value=txobj.value,
                data=txobj.data,
                chain_id=chain_id,
            )

            r = int.from_bytes(r, byteorder='big')
            s = int.from_bytes(s, byteorder='big')

            return rlp.encode(txobj.copy(v=v, r=r, s=s))
        else:
            return self.w3.eth.account.signTransaction(
                tx, self.priv_key).rawTransaction
Ejemplo n.º 28
0
def test_sanity_checks(client: Client):
    """Is not vectorized because these are internal-only tests that do not
    need to be exposed to the public.
    """
    # contract creation without data should fail.
    with pytest.raises(TrezorFailure, match=r"DataError"):
        ethereum.sign_tx(
            client,
            n=parse_path("m/44h/60h/0h/0/0"),
            nonce=123_456,
            gas_price=20_000,
            gas_limit=20_000,
            to="",
            value=12_345_678_901_234_567_890,
            chain_id=1,
        )

    # gas overflow
    with pytest.raises(TrezorFailure, match=r"DataError"):
        ethereum.sign_tx(
            client,
            n=parse_path("m/44h/60h/0h/0/0"),
            nonce=123_456,
            gas_price=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
            gas_limit=0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF,
            to=TO_ADDR,
            value=12_345_678_901_234_567_890,
            chain_id=1,
        )

    # bad chain ID
    with pytest.raises(TrezorFailure, match=r"Chain ID out of bounds"):
        ethereum.sign_tx(
            client,
            n=parse_path("m/44h/60h/0h/0/0"),
            nonce=123_456,
            gas_price=20_000,
            gas_limit=20_000,
            to=TO_ADDR,
            value=12_345_678_901_234_567_890,
            chain_id=0,
        )
    def test_ethereum_signtx_data(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.client:
            self.client.set_expected_responses([
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.EthereumTxRequest(data_length=None),
            ])

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=0,
                gas_price=20,
                gas_limit=20,
                to=TO_ADDR,
                value=10,
                data=b"abcdefghijklmnop" * 16,
            )
        assert sig_v == 28
        assert (
            sig_r.hex() ==
            "6da89ed8627a491bedc9e0382f37707ac4e5102e25e7a1234cb697cedb7cd2c0")
        assert (
            sig_s.hex() ==
            "691f73b145647623e2d115b208a7c3455a6a8a83e3b4db5b9c6d9bc75825038a")

        with self.client:
            self.client.set_expected_responses([
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                proto.EthereumTxRequest(
                    data_length=1024,
                    signature_r=None,
                    signature_s=None,
                    signature_v=None,
                ),
                proto.EthereumTxRequest(data_length=1024),
                proto.EthereumTxRequest(data_length=1024),
                proto.EthereumTxRequest(data_length=3),
                proto.EthereumTxRequest(),
            ])

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=[0, 0],
                nonce=123456,
                gas_price=20000,
                gas_limit=20000,
                to=TO_ADDR,
                value=12345678901234567890,
                data=b"ABCDEFGHIJKLMNOP" * 256 + b"!!!",
            )
        assert sig_v == 28
        assert (
            sig_r.hex() ==
            "4e90b13c45c6a9bf4aaad0e5427c3e62d76692b36eb727c78d332441b7400404")
        assert (
            sig_s.hex() ==
            "3ff236e7d05f0f9b1ee3d70599bb4200638f28388a8faf6bb36db9e04dc544be")
    def test_ethereum_signtx_data(self, client):
        with client:
            client.set_expected_responses(
                [
                    messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                    messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                    messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                    messages.EthereumTxRequest(data_length=None),
                ]
            )

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=0,
                gas_price=20,
                gas_limit=20,
                to=TO_ADDR,
                value=10,
                data=b"abcdefghijklmnop" * 16,
            )
        assert sig_v == 27
        assert (
            sig_r.hex()
            == "e90f9e3dbfb34861d40d67570cb369049e675c6eebfdda6b08413a2283421b85"
        )
        assert (
            sig_s.hex()
            == "763912b8801f76cbea7792d98123a245514beeab2f3afebb4bab637888e8393a"
        )

        with client:
            client.set_expected_responses(
                [
                    messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                    messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                    messages.ButtonRequest(code=messages.ButtonRequestType.SignTx),
                    messages.EthereumTxRequest(
                        data_length=1024,
                        signature_r=None,
                        signature_s=None,
                        signature_v=None,
                    ),
                    messages.EthereumTxRequest(data_length=1024),
                    messages.EthereumTxRequest(data_length=1024),
                    messages.EthereumTxRequest(data_length=3),
                    messages.EthereumTxRequest(),
                ]
            )

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=123456,
                gas_price=20000,
                gas_limit=20000,
                to=TO_ADDR,
                value=12345678901234567890,
                data=b"ABCDEFGHIJKLMNOP" * 256 + b"!!!",
            )
        assert sig_v == 27
        assert (
            sig_r.hex()
            == "dd96d82d791118a55601dfcede237760d2e9734b76c373ede5362a447c42ac48"
        )
        assert (
            sig_s.hex()
            == "60a77558f28d483d476f9507cd8a6a4bb47b86611aaff95fd5499b9ee9ebe7ee"
        )
Ejemplo n.º 31
0
            ],
            "outputs": [{"name": "", "type": "bool"}],
        }
    ]
    contract = w3.eth.contract(address=token_address, abi=min_abi)
    data = contract.encodeABI("transfer", [to_address, amount])
    data = bytes.fromhex(data[2:])
    to_address = token_address
    amount     = 0

sig = ethereum.sign_tx(
    client,
    n=address_n,
    nonce=nonce,
    gas_price=gas_price,
    gas_limit=gas_limit,
    to=to_address,
    value=amount,
    data=data,
    chain_id=chain_id,
)
client.close()

to = bytes.fromhex(to_address[2:])
transaction = rlp_encode((nonce, gas_price, gas_limit, to, amount, data) + sig)
print(f'{{"hex": "0x{transaction.hex()}"}}')

"""
txn_id = 0xdcaf3eba690a3cdbad8c2926a8f5a95cd20003c5ba2aace91d8c5fe8048e395b
https://etherscan.io/getRawTx?tx=${txn_id}
Ejemplo n.º 32
0
    def test_ethereum_signtx_eip155(self):

        # chain_id, nonce, sig_v, sig_r, sig_s, value, gas_limit, data
        VECTORS = [
            (
                3,
                0,
                41,
                b"a90d0bc4f8d63be69453dd62f2bb5fff53c610000abf956672564d8a654d401a",
                b"544a2e57bc8b4da18660a1e6036967ea581cc635f5137e3ba97a750867c27cf2",
                100000000000000000,
                21000,
                None,
            ),
            (
                3,
                1,
                42,
                b"699428a6950e23c6843f1bf3754f847e64e047e829978df80d55187d19a401ce",
                b"087343d0a3a2f10842218ffccb146b59a8431b6245ab389fde22dc833f171e6e",
                100000000000000000,
                21000,
                None,
            ),
            (
                3,
                2,
                42,
                b"ba85b622a8bb82606ba96c132e81fa8058172192d15bc41d7e57c031bca17df4",
                b"6473b75997634b6f692f8d672193591d299d5bf1c2d6e51f1a14ed0530b91c7d",
                100000000000000000,
                21004,
                b"\0",
            ),
            (
                3,
                3,
                42,
                b"d021c98f92859c8db5e4de2f0e410a8deb0c977eb1a631e323ebf7484bd0d79a",
                b"2c0e9defc9b1e895dc9520ff25ba3c635b14ad70aa86a5ad6c0a3acb82b569b6",
                100000000000000000,
                299732,
                b"ABCDEFGHIJKLMNOP" * 256 + b"!!!",
            ),
            (
                3,
                4,
                42,
                b"dd52f026972a83c56b7dea356836fcfc70a68e3b879cdc8ef2bb5fea23e0a7aa",
                b"079285fe579c9a2da25c811b1c5c0a74cd19b6301ee42cf20ef7b3b1353f7242",
                0,
                21004,
                b"\0",
            ),
            (
                1,
                1,
                37,
                b"bae6198fdc87ccad6256e543617a34d052bfd17ae3be0bec7fbf8ea34bf9c930",
                b"7d12f625f3e54700b6ed14ab669f45a8a2b5552c39f0781b0ab3796f19e6b4d1",
                0,
                21004,
                b"\0",
            ),
            (
                255,
                1,
                546,
                b"7597a40719509ae3850d2eba808b7b2f7d272fda316e1321e5ebcc911e9f1b0d",
                b"269dd69248273820f65b43d8824bb7aff1aa4e35ee663a5433a5df8f0c47dc31",
                0,
                21004,
                b"\0",
            ),
            (
                256,
                1,
                547,
                b"64e9821db2001ff5dff13c9d8c7fb0701ff860f5f95155d378fb9fcc06088f28",
                b"4d03f339afed717e2155f044a6b0a895b5ac98343f1745e7525870c2046c36bc",
                0,
                21004,
                b"\0",
            ),
            (
                65535,
                1,
                131106,
                b"6f2275808dc328184d7aa019d0a68f8dd8234969576a477670934145bb358969",
                b"2be1ff9045bccff9ba3d6d5c7789a52c52c9679526dd3ec349caa318c3d055ff",
                0,
                21004,
                b"\0",
            ),
            (
                65536,
                1,
                131107,
                b"e16e35afe534a46e3e4cf09f355cbf02edc01937c2b444238162c2aca79037b8",
                b"1083b84e21b1cbad95c7ea9792818c18fa716aa25951c5341b48732d611a396a",
                0,
                21004,
                b"\0",
            ),
            (
                16777215,
                1,
                33554466,
                b"f9753ee68cf2af20638cc753945d157039504f82d6d6fe0ec98806b64366c551",
                b"056b57a69d88a4b71fba993c580d8bbf04f2c857f97a8b7d4b2892b5dafa9114",
                0,
                21004,
                b"\0",
            ),
            (
                16777216,
                1,
                33554468,
                b"23a5399650c6efa46a25a0a966a29119830d9c587b6b93da43cb0be6d3c69059",
                b"5a6eddffc62317a6a3801608071655a9c43423aef9705b2f5df4212942265c37",
                0,
                21004,
                b"\0",
            ),
            (
                2147483629,
                1,
                4294967294,
                b"6a996586f1ea19afe9cb0ca44dec6bb8643cdf53b5cf148323c94a32a04b087d",
                b"0d086b208df6826657edf98010972b2649b323466a7ea4b67e7285fb9e829481",
                0,
                21004,
                b"\0",
            ),
            (
                2147483630,
                1,
                4294967296,
                b"fd0377ff429a51ae284c4b09b7d7c26c78944c86bb311b5988d70be4fc59eeba",
                b"2ad183858ac6b1efa820b9ee2c2dbf6659a73cc5b714d32c380b263d024f2ee9",
                0,
                21004,
                None,
            ),
            (
                2147483631,
                1,
                4294967298,
                b"a4e89720285b179f679ecec1c79e4948b18ee4cf08f76b11d701cbead5e81a70",
                b"094b32d3e53833c8085dadfe169782db4d32856d7b556f832f64dfdcfa1dd3b8",
                100000000000000000,
                21000,
                None,
            ),
            (
                3125659152,
                1,
                6251318340,
                b"a39e51d16cb10c81a9c1d9b071b714c4ecf112702407f1cc7aae35c85eced3d3",
                b"0b0654665e4677c77510fc2a43026ee66c13261d3d893895bc49e6f4eaa5c8bd",
                1,
                21005,
                None,
            ),
            (
                4294967295,
                1,
                8589934625,
                b"3367230b5f506426f075f3137f4fd6a5fc4198326dd4936bbc38bf0c5ff43a5e",
                b"6e48c3c95b3a534f7853a3b5dd72cdeffe9b7e93503eb7a793c9d5fdc14c4e99",
                100000000000000000,
                21000,
                None,
            ),
        ]

        self.setup_mnemonic_allallall()

        for ci, n, sv, sr, ss, v, gl, d in VECTORS:
            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=[H_(44), H_(1), H_(0), 0, 0],
                nonce=n,
                gas_price=20000000000,
                gas_limit=gl,
                to=unhexlify("8ea7a3fccc211ed48b763b4164884ddbcf3b0a98"),
                value=v,
                chain_id=ci,
                data=d,
            )
            assert sig_v == sv
            assert hexlify(sig_r) == sr
            assert hexlify(sig_s) == ss
    def test_ethereum_signtx_eip155(self):

        # chain_id, nonce, sig_v, sig_r, sig_s, value, gas_limit, data
        VECTORS = [
            (
                3,
                0,
                42,
                "cde31d8ab07d423d5e52aeb148180528ea54974cdb4c5578499c0137ec24d892",
                "41fc58955b3b3e3f3b2aced65e11e8a3cb6339027f943bec3d504d6398b69dd2",
                100000000000000000,
                21000,
                None,
            ),
            (
                3,
                1,
                41,
                "57951fed170f3765dea164d65acd31373799db32ec572e213b1d9a1209956b98",
                "0971f8830c0e2e89919309f217ed2eadb0c63d647e016d220729ce79d27c24a0",
                100000000000000000,
                21000,
                None,
            ),
            (
                3,
                2,
                42,
                "73744f66231690edd9eed2ab3c2b56ec4f6c4b9aabc633ae7f3f4ea94223d52c",
                "7f500afbe2b2b4e4e57f22511e3a42b3596b85cad7fe1eca700cdae1905d3555",
                100000000000000000,
                21004,
                b"\0",
            ),
            (
                3,
                3,
                42,
                "1a4fc1ec5f98bf874d5336aaf1fa9069ce68dc36c3f77e93465c9ac2c8b4b741",
                "13007c9b1df6a0d2f2ffa9d0ebcdec189122a5e781eb64967eb0d6a6def95b7a",
                100000000000000000,
                299732,
                b"ABCDEFGHIJKLMNOP" * 256 + b"!!!",
            ),
            (
                3,
                4,
                42,
                "8da0358d780df542f767d977f99ad034b6d9fa808fe50997141be2a1b93542c0",
                "2dafe1ead8aae1051e6662c5d553b34067bda9c8fa7314ae8693ec61ddfc96d4",
                0,
                21004,
                b"\0",
            ),
            (
                1,
                1,
                38,
                "b72707f0f5a38339c9dd0359720312c739a8ac6554659c7af48456f06ba33374",
                "75a431c046046942f9c1f3305cd08f34302164811c675ac0a0ac0b73cb30a90e",
                0,
                21004,
                b"\0",
            ),
            (
                255,
                1,
                545,
                "529172fb644a6d29b7218fb783f3d666021fc29cc4bf9bffbcfb3b84ab8d6181",
                "30980c6102a12872ef9cd888f2bf90c81bbbdc8878ff7d1d1382f8983b0d0c49",
                0,
                21004,
                b"\0",
            ),
            (
                256,
                1,
                548,
                "db53c05c679bdfdf3ded787ce9607d3f109ae46c87b1dcc9ab34053e5ed0eace",
                "39645dd48118d369b588dbf279f1a8c01051fabf65bf8eaa633c6433ff120cce",
                0,
                21004,
                b"\0",
            ),
            (
                65535,
                1,
                131105,
                "b520fa77767cdf07b6014d4a8fb35eebe5ed7c0edab97132b0dc74e3e1f13ed9",
                "78735b2db4cf95fb651c5c1f5529e60542019e456c6cb7a9f4bd9bbb83418d99",
                0,
                21004,
                b"\0",
            ),
            (
                65536,
                1,
                131107,
                "4b6122ba875b57ce084bd5f08e9ae1944e998726a4056c9b7746432d8f46ba99",
                "6812c2668ac9c9927b69ef7cf9baec54436f7319ccc14f0f664e1e94e6109e06",
                0,
                21004,
                b"\0",
            ),
            (
                16777215,
                1,
                33554465,
                "68a8c6f2336a8e3296f17a307d84a1e6d3ab1383fdcc62611c2e8426f2e2777e",
                "2d4ce900077ab40aac26064945998dbac5a014baadae2d3cb629cdeb9452db61",
                0,
                21004,
                b"\0",
            ),
            (
                16777216,
                1,
                33554468,
                "b6c42c584ef69621a2e5f3e1ab9dad890dbff3c92a599230dd0e394cd29d1c68",
                "497eec05ea52773d0f05e7fdf4f7993b3a06ef958804b39af699ef09ed0f5d7e",
                0,
                21004,
                b"\0",
            ),
            (
                2147483629,
                1,
                4294967294,
                "1a31f886c0bba527e622a731270dc29e62a607ff63558fca38745e5b9a672686",
                "0f3fce8a70598bbb54387cde7e8f957a27e4a816cbc9408717b27d8666222bd9",
                0,
                21004,
                b"\0",
            ),
            (
                2147483630,
                1,
                4294967296,
                "ba6cb6e2ebbac3726db9a3e4a939454009108f6515330e567aeada14ecebe074",
                "2bbfba1154cae32e3e6c6bbf3ce41cba6cc8c6b764245ba6026605506838e690",
                0,
                21004,
                None,
            ),
            (
                2147483631,
                1,
                4294967298,
                "3c743528e9ce315db02e487de93f2b2cfc93421e43f1d519f77a2f05bd2ce190",
                "16c1fec1495fe5da89d1a026f1a575ff354e18ff0fb9d04a6cfb0413267ab2bc",
                100000000000000000,
                21000,
                None,
            ),
            (
                3125659152,
                1,
                6251318340,
                "82cde0c9e1a94c1305791b09e1bcd021a49b036a16d9733acbc1a08bb30f3410",
                "472c8897519ba410b86f80993236d992e18e94d1f59c3d8760d2d7c90914dfc6",
                1,
                21005,
                None,
            ),
            (
                4294967295,
                1,
                8589934625,
                "67788e892fb372bba16823e16d3186f67494d7b1128555248f3661ad87e9d7ef",
                "2faf9f06dfdf23ceca2796cf0d55c88187f199e98a94dfb15722824b244d81a1",
                100000000000000000,
                21000,
                None,
            ),
        ]

        self.setup_mnemonic_allallall()

        for ci, n, sv, sr, ss, v, gl, d in VECTORS:
            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=[H_(44), H_(60), H_(0), 0, 0],
                nonce=n,
                gas_price=20000000000,
                gas_limit=gl,
                to="0x8eA7a3fccC211ED48b763b4164884DDbcF3b0A98",
                value=v,
                chain_id=ci,
                data=d,
            )
            assert sig_v == sv
            assert sig_r.hex() == sr
            assert sig_s.hex() == ss
    def test_ethereum_signtx_data(self):
        self.setup_mnemonic_nopin_nopassphrase()

        with self.client:
            self.client.set_expected_responses(
                [
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.EthereumTxRequest(data_length=None),
                ]
            )

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=0,
                gas_price=20,
                gas_limit=20,
                to=TO_ADDR,
                value=10,
                data=b"abcdefghijklmnop" * 16,
            )
        assert sig_v == 27
        assert (
            sig_r.hex()
            == "e90f9e3dbfb34861d40d67570cb369049e675c6eebfdda6b08413a2283421b85"
        )
        assert (
            sig_s.hex()
            == "763912b8801f76cbea7792d98123a245514beeab2f3afebb4bab637888e8393a"
        )

        with self.client:
            self.client.set_expected_responses(
                [
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.ButtonRequest(code=proto.ButtonRequestType.SignTx),
                    proto.EthereumTxRequest(
                        data_length=1024,
                        signature_r=None,
                        signature_s=None,
                        signature_v=None,
                    ),
                    proto.EthereumTxRequest(data_length=1024),
                    proto.EthereumTxRequest(data_length=1024),
                    proto.EthereumTxRequest(data_length=3),
                    proto.EthereumTxRequest(),
                ]
            )

            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=parse_path("44'/60'/0'/0/0"),
                nonce=123456,
                gas_price=20000,
                gas_limit=20000,
                to=TO_ADDR,
                value=12345678901234567890,
                data=b"ABCDEFGHIJKLMNOP" * 256 + b"!!!",
            )
        assert sig_v == 27
        assert (
            sig_r.hex()
            == "dd96d82d791118a55601dfcede237760d2e9734b76c373ede5362a447c42ac48"
        )
        assert (
            sig_s.hex()
            == "60a77558f28d483d476f9507cd8a6a4bb47b86611aaff95fd5499b9ee9ebe7ee"
        )
Ejemplo n.º 35
0
    def test_ethereum_signtx_eip155(self):

        # chain_id, nonce, sig_v, sig_r, sig_s, value, gas_limit, data
        VECTORS = [
            (
                3,
                0,
                42,
                "cde31d8ab07d423d5e52aeb148180528ea54974cdb4c5578499c0137ec24d892",
                "41fc58955b3b3e3f3b2aced65e11e8a3cb6339027f943bec3d504d6398b69dd2",
                100000000000000000,
                21000,
                None,
            ),
            (
                3,
                1,
                41,
                "57951fed170f3765dea164d65acd31373799db32ec572e213b1d9a1209956b98",
                "0971f8830c0e2e89919309f217ed2eadb0c63d647e016d220729ce79d27c24a0",
                100000000000000000,
                21000,
                None,
            ),
            (
                3,
                2,
                42,
                "73744f66231690edd9eed2ab3c2b56ec4f6c4b9aabc633ae7f3f4ea94223d52c",
                "7f500afbe2b2b4e4e57f22511e3a42b3596b85cad7fe1eca700cdae1905d3555",
                100000000000000000,
                21004,
                b"\0",
            ),
            (
                3,
                3,
                42,
                "1a4fc1ec5f98bf874d5336aaf1fa9069ce68dc36c3f77e93465c9ac2c8b4b741",
                "13007c9b1df6a0d2f2ffa9d0ebcdec189122a5e781eb64967eb0d6a6def95b7a",
                100000000000000000,
                299732,
                b"ABCDEFGHIJKLMNOP" * 256 + b"!!!",
            ),
            (
                3,
                4,
                42,
                "8da0358d780df542f767d977f99ad034b6d9fa808fe50997141be2a1b93542c0",
                "2dafe1ead8aae1051e6662c5d553b34067bda9c8fa7314ae8693ec61ddfc96d4",
                0,
                21004,
                b"\0",
            ),
            (
                1,
                1,
                38,
                "b72707f0f5a38339c9dd0359720312c739a8ac6554659c7af48456f06ba33374",
                "75a431c046046942f9c1f3305cd08f34302164811c675ac0a0ac0b73cb30a90e",
                0,
                21004,
                b"\0",
            ),
            (
                255,
                1,
                545,
                "529172fb644a6d29b7218fb783f3d666021fc29cc4bf9bffbcfb3b84ab8d6181",
                "30980c6102a12872ef9cd888f2bf90c81bbbdc8878ff7d1d1382f8983b0d0c49",
                0,
                21004,
                b"\0",
            ),
            (
                256,
                1,
                548,
                "db53c05c679bdfdf3ded787ce9607d3f109ae46c87b1dcc9ab34053e5ed0eace",
                "39645dd48118d369b588dbf279f1a8c01051fabf65bf8eaa633c6433ff120cce",
                0,
                21004,
                b"\0",
            ),
            (
                65535,
                1,
                131105,
                "b520fa77767cdf07b6014d4a8fb35eebe5ed7c0edab97132b0dc74e3e1f13ed9",
                "78735b2db4cf95fb651c5c1f5529e60542019e456c6cb7a9f4bd9bbb83418d99",
                0,
                21004,
                b"\0",
            ),
            (
                65536,
                1,
                131107,
                "4b6122ba875b57ce084bd5f08e9ae1944e998726a4056c9b7746432d8f46ba99",
                "6812c2668ac9c9927b69ef7cf9baec54436f7319ccc14f0f664e1e94e6109e06",
                0,
                21004,
                b"\0",
            ),
            (
                16777215,
                1,
                33554465,
                "68a8c6f2336a8e3296f17a307d84a1e6d3ab1383fdcc62611c2e8426f2e2777e",
                "2d4ce900077ab40aac26064945998dbac5a014baadae2d3cb629cdeb9452db61",
                0,
                21004,
                b"\0",
            ),
            (
                16777216,
                1,
                33554468,
                "b6c42c584ef69621a2e5f3e1ab9dad890dbff3c92a599230dd0e394cd29d1c68",
                "497eec05ea52773d0f05e7fdf4f7993b3a06ef958804b39af699ef09ed0f5d7e",
                0,
                21004,
                b"\0",
            ),
            (
                2147483629,
                1,
                4294967294,
                "1a31f886c0bba527e622a731270dc29e62a607ff63558fca38745e5b9a672686",
                "0f3fce8a70598bbb54387cde7e8f957a27e4a816cbc9408717b27d8666222bd9",
                0,
                21004,
                b"\0",
            ),
            (
                2147483630,
                1,
                4294967296,
                "ba6cb6e2ebbac3726db9a3e4a939454009108f6515330e567aeada14ecebe074",
                "2bbfba1154cae32e3e6c6bbf3ce41cba6cc8c6b764245ba6026605506838e690",
                0,
                21004,
                None,
            ),
            (
                2147483631,
                1,
                4294967298,
                "3c743528e9ce315db02e487de93f2b2cfc93421e43f1d519f77a2f05bd2ce190",
                "16c1fec1495fe5da89d1a026f1a575ff354e18ff0fb9d04a6cfb0413267ab2bc",
                100000000000000000,
                21000,
                None,
            ),
            (
                3125659152,
                1,
                6251318340,
                "82cde0c9e1a94c1305791b09e1bcd021a49b036a16d9733acbc1a08bb30f3410",
                "472c8897519ba410b86f80993236d992e18e94d1f59c3d8760d2d7c90914dfc6",
                1,
                21005,
                None,
            ),
            (
                4294967295,
                1,
                8589934625,
                "67788e892fb372bba16823e16d3186f67494d7b1128555248f3661ad87e9d7ef",
                "2faf9f06dfdf23ceca2796cf0d55c88187f199e98a94dfb15722824b244d81a1",
                100000000000000000,
                21000,
                None,
            ),
        ]

        self.setup_mnemonic_allallall()

        for ci, n, sv, sr, ss, v, gl, d in VECTORS:
            sig_v, sig_r, sig_s = ethereum.sign_tx(
                self.client,
                n=[H_(44), H_(60), H_(0), 0, 0],
                nonce=n,
                gas_price=20000000000,
                gas_limit=gl,
                to=bytes.fromhex("8ea7a3fccc211ed48b763b4164884ddbcf3b0a98"),
                value=v,
                chain_id=ci,
                data=d,
            )
            assert sig_v == sv
            assert sig_r.hex() == sr
            assert sig_s.hex() == ss
Ejemplo n.º 36
0
 def __sign_transaction(self, n: List[int], trezor_transaction: dict) -> Tuple[bytes, bytes, bytes]:
     """Internal wrapper for trezorlib transaction signing calls"""
     v, r, s = ethereum.sign_tx(client=self.__client, n=n, **trezor_transaction)
     return v, r, s