1# This file is part of the Trezor project.
2#
3# Copyright (C) 2012-2018 SatoshiLabs and contributors
4#
5# This library is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License version 3
7# as published by the Free Software Foundation.
8#
9# This library is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU Lesser General Public License for more details.
13#
14# You should have received a copy of the License along with this library.
15# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
16
17import common
18
19from binascii import hexlify, unhexlify
20import unittest
21
22from keepkeylib import ckd_public as bip32
23from common import KeepKeyTest
24
25from keepkeylib import messages_pb2 as proto
26from keepkeylib import types_pb2 as proto_types
27from keepkeylib.client import CallException
28from keepkeylib.tools import parse_path
29from keepkeylib.tx_api import TxApiGroestlcoinTestnet
30
31# https://blockbook-test.groestlcoin.org/tx/4ce0220004bdfe14e3dd49fd8636bcb770a400c0c9e9bff670b6a13bb8f15c72
32class TestMsgSigntxSegwitGRS(KeepKeyTest):
33
34    def test_send_p2sh(self):
35        self.setup_mnemonic_allallall()
36        self.client.set_tx_api(TxApiGroestlcoinTestnet)
37        inp1 = proto_types.TxInputType(
38            address_n=parse_path("49'/1'/0'/1/0"), # 2N1LGaGg836mqSQqiuUBLfcyGBhyZYBtBZ7
39            amount=123456789,
40            prev_hash=unhexlify('09a48bce2f9d5c6e4f0cb9ea1b32d0891855e8acfe5334f9ebd72b9ad2de60cf'),
41            prev_index=0,
42            sequence=0xfffffffe,
43            script_type=proto_types.SPENDP2SHWITNESS,
44        )
45        out1 = proto_types.TxOutputType(
46            address='mvbu1Gdy8SUjTenqerxUaZyYjmvedc787y',
47            amount=12300000,
48            script_type=proto_types.PAYTOADDRESS,
49        )
50        out2 = proto_types.TxOutputType(
51            address='2N1LGaGg836mqSQqiuUBLfcyGBhyZYBtBZ7',
52            script_type=proto_types.PAYTOADDRESS,
53            amount=123456789 - 11000 - 12300000,
54        )
55        with self.client:
56            self.client.set_expected_responses([
57                proto.TxRequest(request_type=proto_types.TXINPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
58                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
59                proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput),
60                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=1)),
61                proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput),
62                proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx),
63                proto.TxRequest(request_type=proto_types.TXINPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
64                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
65                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=1)),
66                proto.TxRequest(request_type=proto_types.TXINPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
67                proto.TxRequest(request_type=proto_types.TXFINISHED),
68            ])
69            (signatures, serialized_tx) = self.client.sign_tx('GRS Testnet', [inp1], [out1, out2], lock_time=650756)
70
71        self.assertEqual(hexlify(serialized_tx), b'01000000000101cf60ded29a2bd7ebf93453feace8551889d0321beab90c4f6e5c9d2fce8ba4090000000017160014d16b8c0680c61fc6ed2e407455715055e41052f5feffffff02e0aebb00000000001976a914a579388225827d9f2fe9014add644487808c695d88ac3df39f060000000017a91458b53ea7f832e8f096e896b8713a8c6df0e892ca8702483045022100b7ce2972bcbc3a661fe320ba901e680913b2753fcb47055c9c6ba632fc4acf81022001c3cfd6c2fe92eb60f5176ce0f43707114dd7223da19c56f2df89c13c2fef80012103e7bfe10708f715e8538c92d46ca50db6f657bbc455b7494e6a0303ccdb868b7904ee0900')
72
73    def test_send_p2sh_change(self):
74        self.setup_mnemonic_allallall()
75        self.client.set_tx_api(TxApiGroestlcoinTestnet)
76        inp1 = proto_types.TxInputType(
77            address_n=parse_path("49'/1'/0'/1/0"), # 2N1LGaGg836mqSQqiuUBLfcyGBhyZYBtBZ7
78            amount=123456789,
79            prev_hash=unhexlify('09a48bce2f9d5c6e4f0cb9ea1b32d0891855e8acfe5334f9ebd72b9ad2de60cf'),
80            prev_index=0,
81            sequence=0xfffffffe,
82            script_type=proto_types.SPENDP2SHWITNESS,
83        )
84        out1 = proto_types.TxOutputType(
85            address='mvbu1Gdy8SUjTenqerxUaZyYjmvedc787y',
86            amount=12300000,
87            script_type=proto_types.PAYTOADDRESS,
88        )
89        out2 = proto_types.TxOutputType(
90            address_n=parse_path("49'/1'/0'/1/0"),
91            script_type=proto_types.PAYTOP2SHWITNESS,
92            amount=123456789 - 11000 - 12300000,
93        )
94        with self.client:
95            self.client.set_expected_responses([
96                proto.TxRequest(request_type=proto_types.TXINPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
97                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
98                proto.ButtonRequest(code=proto_types.ButtonRequest_ConfirmOutput),
99                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=1)),
100                proto.ButtonRequest(code=proto_types.ButtonRequest_SignTx),
101                proto.TxRequest(request_type=proto_types.TXINPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
102                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
103                proto.TxRequest(request_type=proto_types.TXOUTPUT, details=proto_types.TxRequestDetailsType(request_index=1)),
104                proto.TxRequest(request_type=proto_types.TXINPUT, details=proto_types.TxRequestDetailsType(request_index=0)),
105                proto.TxRequest(request_type=proto_types.TXFINISHED),
106            ])
107            (signatures, serialized_tx) = self.client.sign_tx('GRS Testnet', [inp1], [out1, out2], lock_time=650756)
108
109        self.assertEqual(hexlify(serialized_tx), b'01000000000101cf60ded29a2bd7ebf93453feace8551889d0321beab90c4f6e5c9d2fce8ba4090000000017160014d16b8c0680c61fc6ed2e407455715055e41052f5feffffff02e0aebb00000000001976a914a579388225827d9f2fe9014add644487808c695d88ac3df39f060000000017a91458b53ea7f832e8f096e896b8713a8c6df0e892ca8702483045022100b7ce2972bcbc3a661fe320ba901e680913b2753fcb47055c9c6ba632fc4acf81022001c3cfd6c2fe92eb60f5176ce0f43707114dd7223da19c56f2df89c13c2fef80012103e7bfe10708f715e8538c92d46ca50db6f657bbc455b7494e6a0303ccdb868b7904ee0900')
110
111if __name__ == '__main__':
112    unittest.main()
113