File: test_stellar.py

package info (click to toggle)
python-trezor 0.13.10-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,596 kB
  • sloc: python: 14,947; xml: 33; sh: 16; makefile: 3
file content (944 lines) | stat: -rw-r--r-- 35,084 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
# This file is part of the Trezor project.
#
# Copyright (C) 2012-2022 SatoshiLabs and contributors
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# 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 License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.

import warnings

import pytest

try:
    from stellar_sdk import (
        Account,
        Asset,
        AuthorizationFlag,
        MuxedAccount,
        Network,
        TransactionBuilder,
        TrustLineEntryFlag,
    )
    from stellar_sdk.strkey import StrKey
except ImportError:
    pytest.skip("stellar_sdk not installed", allow_module_level=True)

from trezorlib import messages, stellar

TX_SOURCE = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
SEQUENCE = 123456
TIMEBOUNDS_START = 461535181
TIMEBOUNDS_END = 1575234180
BASE_FEE = 200


def make_default_tx(default_op: bool = False, **kwargs) -> TransactionBuilder:
    source_account = Account(account=TX_SOURCE, sequence=SEQUENCE)
    default_params = {
        "source_account": source_account,
        "network_passphrase": Network.TESTNET_NETWORK_PASSPHRASE,
        "base_fee": BASE_FEE,
    }
    default_params.update(kwargs)
    builder = TransactionBuilder(**default_params)
    builder.add_time_bounds(TIMEBOUNDS_START, TIMEBOUNDS_END)

    if default_op:
        builder.append_manage_data_op(data_name="Trezor", data_value=b"Hello, Stellar")

    return builder


def test_simple():
    envelope = make_default_tx(default_op=True).build()

    tx, operations = stellar.from_envelope(envelope)
    assert tx.source_account == TX_SOURCE
    assert tx.fee == envelope.transaction.fee
    assert tx.sequence_number == SEQUENCE + 1
    assert tx.timebounds_start is TIMEBOUNDS_START
    assert tx.timebounds_end is TIMEBOUNDS_END
    assert tx.memo_type == messages.StellarMemoType.NONE
    assert tx.memo_text is None
    assert tx.memo_id is None
    assert tx.memo_hash is None
    assert len(operations) == 1


def test_memo_text():
    memo_text = "Have a nice day!"
    envelope = (
        make_default_tx(default_op=True).add_text_memo(memo_text.encode()).build()
    )

    tx, operations = stellar.from_envelope(envelope)
    assert tx.memo_type == messages.StellarMemoType.TEXT
    assert tx.memo_text == memo_text
    assert tx.memo_id is None
    assert tx.memo_hash is None


def test_memo_id():
    memo_id = 123456789
    envelope = make_default_tx(default_op=True).add_id_memo(memo_id).build()

    tx, operations = stellar.from_envelope(envelope)
    assert tx.memo_type == messages.StellarMemoType.ID
    assert tx.memo_text is None
    assert tx.memo_id == memo_id
    assert tx.memo_hash is None


def test_memo_hash():
    memo_hash = "b77cd735095e1b58da2d7415c1f51f423a722b34d7d5002d8896608a9130a74b"
    envelope = (
        make_default_tx(v1=False, default_op=True).add_hash_memo(memo_hash).build()
    )

    tx, operations = stellar.from_envelope(envelope)
    assert tx.memo_type == messages.StellarMemoType.HASH
    assert tx.memo_text is None
    assert tx.memo_id is None
    assert tx.memo_hash.hex() == memo_hash


def test_memo_return_hash():
    memo_return = "b77cd735095e1b58da2d7415c1f51f423a722b34d7d5002d8896608a9130a74b"
    envelope = (
        make_default_tx(v1=False, default_op=True)
        .add_return_hash_memo(memo_return)
        .build()
    )

    tx, operations = stellar.from_envelope(envelope)
    assert tx.memo_type == messages.StellarMemoType.RETURN
    assert tx.memo_text is None
    assert tx.memo_id is None
    assert tx.memo_hash.hex() == memo_return


def test_time_bounds_missing():
    tx = make_default_tx(default_op=True)
    tx.time_bounds = None
    with warnings.catch_warnings():
        # ignore warning about missing time bounds
        warnings.filterwarnings("ignore", message=r".*TimeBounds.*")
        envelope = tx.build()

    with pytest.raises(ValueError):
        stellar.from_envelope(envelope)


def test_multiple_operations():
    tx = make_default_tx()
    data_name = "Trezor"
    data_value = b"Hello, Stellar"
    operation1_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"
    destination = "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6"
    amount = "50.0111"
    asset_code = "XLM"
    asset_issuer = None
    operation2_source = "GBHWKBPP3O4H2BUUKSFXE4PK5WHLQYVZIZUNUJ4AU5VUZZEVBDMXISAS"

    envelope = (
        tx.append_manage_data_op(
            data_name=data_name, data_value=data_value, source=operation1_source
        )
        .append_payment_op(
            destination=destination,
            amount=amount,
            asset=Asset(asset_code, asset_issuer),
            source=operation2_source,
        )
        .build()
    )

    tx, operations = stellar.from_envelope(envelope)
    assert tx.source_account == TX_SOURCE
    assert tx.fee == envelope.transaction.fee
    assert tx.sequence_number == SEQUENCE + 1
    assert tx.timebounds_start is TIMEBOUNDS_START
    assert tx.timebounds_end is TIMEBOUNDS_END
    assert tx.memo_type == messages.StellarMemoType.NONE
    assert tx.memo_text is None
    assert tx.memo_id is None
    assert tx.memo_hash is None
    assert len(operations) == 2

    assert isinstance(operations[0], messages.StellarManageDataOp)
    assert operations[0].source_account == operation1_source
    assert operations[0].key == data_name
    assert operations[0].value == data_value

    assert isinstance(operations[1], messages.StellarPaymentOp)
    assert operations[1].source_account == operation2_source
    assert operations[1].destination_account == destination
    assert operations[1].asset.type == messages.StellarAssetType.NATIVE
    assert operations[1].asset.code is None
    assert operations[1].asset.issuer is None
    assert operations[1].amount == 500111000


def test_create_account():
    tx = make_default_tx()
    destination = "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6"
    starting_balance = "100.0333"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_create_account_op(
        destination=destination,
        starting_balance=starting_balance,
        source=operation_source,
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarCreateAccountOp)
    assert operations[0].source_account == operation_source
    assert operations[0].new_account == destination
    assert operations[0].starting_balance == 1000333000


def test_payment_native_asset():
    tx = make_default_tx()
    destination = "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6"
    amount = "50.0111"
    asset_code = "XLM"
    asset_issuer = None
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_payment_op(
        destination=destination,
        amount=amount,
        asset=Asset(asset_code, asset_issuer),
        source=operation_source,
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarPaymentOp)
    assert operations[0].source_account == operation_source
    assert operations[0].destination_account == destination
    assert operations[0].asset.type == messages.StellarAssetType.NATIVE
    assert operations[0].asset.code is None
    assert operations[0].asset.issuer is None
    assert operations[0].amount == 500111000


def test_payment_alpha4_asset():
    tx = make_default_tx()
    destination = "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6"
    amount = "50.0111"
    asset_code = "USD"
    asset_issuer = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_payment_op(
        destination=destination,
        amount=amount,
        asset=Asset(asset_code, asset_issuer),
        source=operation_source,
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarPaymentOp)
    assert operations[0].source_account == operation_source
    assert operations[0].destination_account == destination
    assert operations[0].asset.type == messages.StellarAssetType.ALPHANUM4
    assert operations[0].asset.code == asset_code
    assert operations[0].asset.issuer == asset_issuer
    assert operations[0].amount == 500111000


def test_payment_alpha12_asset():
    tx = make_default_tx()
    destination = "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6"
    amount = "50.0111"
    asset_code = "BANANA"
    asset_issuer = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_payment_op(
        destination=destination,
        amount=amount,
        asset=Asset(asset_code, asset_issuer),
        source=operation_source,
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarPaymentOp)
    assert operations[0].source_account == operation_source
    assert operations[0].destination_account == destination
    assert operations[0].asset.type == messages.StellarAssetType.ALPHANUM12
    assert operations[0].asset.code == asset_code
    assert operations[0].asset.issuer == asset_issuer
    assert operations[0].amount == 500111000


def test_path_payment_strict_receive():
    tx = make_default_tx()
    destination = "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6"
    send_max = "50.0111"
    dest_amount = "100"
    send_code = "XLM"
    send_issuer = None
    dest_code = "USD"
    dest_issuer = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"
    path_asset1 = Asset(
        "JPY", "GD6PV7DXQJX7AGVXFQ2MTCLTCH6LR3E6IO2EO2YDZD7F7IOZZCCB5DSQ"
    )
    path_asset2 = Asset(
        "BANANA", "GC7EKO37HNSKQ3V6RZ274EO7SFOWASQRHLX3OR5FIZK6UMV6LIEDXHGZ"
    )

    envelope = tx.append_path_payment_strict_receive_op(
        destination=destination,
        send_asset=Asset(send_code, send_issuer),
        send_max=send_max,
        dest_asset=Asset(dest_code, dest_issuer),
        dest_amount=dest_amount,
        path=[path_asset1, path_asset2],
        source=operation_source,
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1

    assert isinstance(operations[0], messages.StellarPathPaymentStrictReceiveOp)
    assert operations[0].source_account == operation_source
    assert operations[0].destination_account == destination
    assert operations[0].send_asset.type == messages.StellarAssetType.NATIVE
    assert operations[0].send_max == 500111000
    assert operations[0].destination_amount == 1000000000
    assert operations[0].destination_asset.type == messages.StellarAssetType.ALPHANUM4
    assert operations[0].destination_asset.code == dest_code
    assert operations[0].destination_asset.issuer == dest_issuer
    assert len(operations[0].paths) == 2
    assert operations[0].paths[0].type == messages.StellarAssetType.ALPHANUM4
    assert operations[0].paths[0].code == path_asset1.code
    assert operations[0].paths[0].issuer == path_asset1.issuer
    assert operations[0].paths[1].type == messages.StellarAssetType.ALPHANUM12
    assert operations[0].paths[1].code == path_asset2.code
    assert operations[0].paths[1].issuer == path_asset2.issuer


def test_manage_sell_offer_new_offer():
    tx = make_default_tx()
    price = "0.5"
    amount = "50.0111"
    selling_code = "XLM"
    selling_issuer = None
    buying_code = "USD"
    buying_issuer = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_manage_sell_offer_op(
        selling=Asset(selling_code, selling_issuer),
        buying=Asset(buying_code, buying_issuer),
        amount=amount,
        price=price,
        source=operation_source,
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarManageSellOfferOp)
    assert operations[0].source_account == operation_source
    assert operations[0].selling_asset.type == messages.StellarAssetType.NATIVE
    assert operations[0].buying_asset.type == messages.StellarAssetType.ALPHANUM4
    assert operations[0].buying_asset.code == buying_code
    assert operations[0].buying_asset.issuer == buying_issuer
    assert operations[0].amount == 500111000
    assert operations[0].price_n == 1
    assert operations[0].price_d == 2
    assert operations[0].offer_id == 0  # indicates a new offer


def test_manage_sell_offer_update_offer():
    tx = make_default_tx()
    price = "0.5"
    amount = "50.0111"
    selling_code = "XLM"
    selling_issuer = None
    buying_code = "USD"
    buying_issuer = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
    offer_id = 12345
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_manage_sell_offer_op(
        selling=Asset(selling_code, selling_issuer),
        buying=Asset(buying_code, buying_issuer),
        amount=amount,
        price=price,
        offer_id=offer_id,
        source=operation_source,
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarManageSellOfferOp)
    assert operations[0].source_account == operation_source
    assert operations[0].selling_asset.type == messages.StellarAssetType.NATIVE
    assert operations[0].buying_asset.type == messages.StellarAssetType.ALPHANUM4
    assert operations[0].buying_asset.code == buying_code
    assert operations[0].buying_asset.issuer == buying_issuer
    assert operations[0].amount == 500111000
    assert operations[0].price_n == 1
    assert operations[0].price_d == 2
    assert operations[0].offer_id == offer_id


def test_create_passive_sell_offer():
    tx = make_default_tx()
    price = "0.5"
    amount = "50.0111"
    selling_code = "XLM"
    selling_issuer = None
    buying_code = "USD"
    buying_issuer = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_create_passive_sell_offer_op(
        selling=Asset(selling_code, selling_issuer),
        buying=Asset(buying_code, buying_issuer),
        amount=amount,
        price=price,
        source=operation_source,
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarCreatePassiveSellOfferOp)
    assert operations[0].source_account == operation_source
    assert operations[0].selling_asset.type == messages.StellarAssetType.NATIVE
    assert operations[0].buying_asset.type == messages.StellarAssetType.ALPHANUM4
    assert operations[0].buying_asset.code == buying_code
    assert operations[0].buying_asset.issuer == buying_issuer
    assert operations[0].amount == 500111000
    assert operations[0].price_n == 1
    assert operations[0].price_d == 2


def test_set_options():
    tx = make_default_tx()
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"
    inflation_dest = "GAXN7HZQTHIPW7N2HGPAXMR42LPJ5VLYXMCCOX4D3JC4CQZGID3UYUPF"
    clear_flags = AuthorizationFlag.AUTHORIZATION_REQUIRED
    set_flags = (
        AuthorizationFlag.AUTHORIZATION_IMMUTABLE
        | AuthorizationFlag.AUTHORIZATION_REVOCABLE
    )
    master_weight = 255
    low_threshold = 10
    med_threshold = 20
    high_threshold = 30
    home_domain = "example.com"

    envelope = tx.append_set_options_op(
        inflation_dest=inflation_dest,
        clear_flags=clear_flags,
        set_flags=set_flags,
        master_weight=master_weight,
        low_threshold=low_threshold,
        med_threshold=med_threshold,
        high_threshold=high_threshold,
        home_domain=home_domain,
        source=operation_source,
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarSetOptionsOp)
    assert operations[0].source_account == operation_source
    assert operations[0].inflation_destination_account == inflation_dest
    assert operations[0].clear_flags == clear_flags
    assert operations[0].set_flags == set_flags
    assert operations[0].master_weight == master_weight
    assert operations[0].low_threshold == low_threshold
    assert operations[0].medium_threshold == med_threshold
    assert operations[0].high_threshold == high_threshold
    assert operations[0].home_domain == home_domain
    assert operations[0].signer_type is None
    assert operations[0].signer_key is None
    assert operations[0].signer_weight is None


def test_set_options_ed25519_signer():
    tx = make_default_tx()
    signer = "GAXN7HZQTHIPW7N2HGPAXMR42LPJ5VLYXMCCOX4D3JC4CQZGID3UYUPF"
    weight = 10
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_ed25519_public_key_signer(
        account_id=signer, weight=weight, source=operation_source
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarSetOptionsOp)
    assert operations[0].source_account == operation_source
    assert operations[0].inflation_destination_account is None
    assert operations[0].clear_flags is None
    assert operations[0].set_flags is None
    assert operations[0].master_weight is None
    assert operations[0].low_threshold is None
    assert operations[0].medium_threshold is None
    assert operations[0].high_threshold is None
    assert operations[0].home_domain is None
    assert operations[0].signer_type == messages.StellarSignerType.ACCOUNT
    assert operations[0].signer_key == StrKey.decode_ed25519_public_key(signer)
    assert operations[0].signer_weight == weight


def test_set_options_pre_auth_tx_signer():
    tx = make_default_tx()
    signer = bytes.fromhex(
        "2db4b22ca018119c5027a80578813ffcf582cda4aa9e31cd92b43cfa4fc5a000"
    )
    weight = 30
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_pre_auth_tx_signer(
        pre_auth_tx_hash=signer, weight=weight, source=operation_source
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarSetOptionsOp)
    assert operations[0].signer_type == messages.StellarSignerType.PRE_AUTH
    assert operations[0].signer_key == signer
    assert operations[0].signer_weight == weight


def test_set_options_hashx_signer():
    tx = make_default_tx()
    signer = bytes.fromhex(
        "3389e9f0f1a65f19736cacf544c2e825313e8447f569233bb8db39aa607c8000"
    )
    weight = 20
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_hashx_signer(
        sha256_hash=signer, weight=weight, source=operation_source
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarSetOptionsOp)
    assert operations[0].signer_type == messages.StellarSignerType.HASH
    assert operations[0].signer_key == signer
    assert operations[0].signer_weight == weight


def test_change_trust():
    tx = make_default_tx()
    asset_code = "USD"
    asset_issuer = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
    limit = "1000"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_change_trust_op(
        asset=Asset(asset_code, asset_issuer),
        limit=limit,
        source=operation_source,
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarChangeTrustOp)
    assert operations[0].source_account == operation_source
    assert operations[0].asset.type == messages.StellarAssetType.ALPHANUM4
    assert operations[0].asset.code == asset_code
    assert operations[0].asset.issuer == asset_issuer
    assert operations[0].limit == 10000000000


def test_allow_trust():
    tx = make_default_tx()
    asset_code = "USD"
    trustor = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    with warnings.catch_warnings():
        # ignore warnings about append_trust_line_flags being a deprecated op,
        # Trezor doesn't currently support the alternative
        warnings.filterwarnings("ignore", message=r".*append_set_trust_line_flags_op.*")
        warnings.filterwarnings("ignore", message=r".*SetTrustLineFlags.*")
        envelope = tx.append_allow_trust_op(
            trustor=trustor,
            asset_code=asset_code,
            authorize=TrustLineEntryFlag.AUTHORIZED_FLAG,
            source=operation_source,
        ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarAllowTrustOp)
    assert operations[0].source_account == operation_source
    assert operations[0].asset_type == messages.StellarAssetType.ALPHANUM4
    assert operations[0].asset_code == asset_code
    assert operations[0].trusted_account == trustor
    assert operations[0].is_authorized is True


def test_account_merge():
    tx = make_default_tx()
    destination = "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_account_merge_op(
        destination=destination, source=operation_source
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarAccountMergeOp)
    assert operations[0].source_account == operation_source
    assert operations[0].destination_account == destination


def test_manage_data():
    tx = make_default_tx()
    data_name = "Trezor"
    data_value = b"Hello, Stellar"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_manage_data_op(
        data_name=data_name, data_value=data_value, source=operation_source
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarManageDataOp)
    assert operations[0].source_account == operation_source
    assert operations[0].key == data_name
    assert operations[0].value == data_value


def test_manage_data_remove_data_entity():
    tx = make_default_tx()
    data_name = "Trezor"
    data_value = None  # remove data entity
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_manage_data_op(
        data_name=data_name, data_value=data_value, source=operation_source
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarManageDataOp)
    assert operations[0].source_account == operation_source
    assert operations[0].key == data_name
    assert operations[0].value is None


def test_bump_sequence():
    tx = make_default_tx()
    bump_to = 143487250972278900
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_bump_sequence_op(
        bump_to=bump_to, source=operation_source
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarBumpSequenceOp)
    assert operations[0].source_account == operation_source
    assert operations[0].bump_to == bump_to


def test_manage_buy_offer_new_offer():
    tx = make_default_tx()
    price = "0.5"
    amount = "50.0111"
    selling_code = "XLM"
    selling_issuer = None
    buying_code = "USD"
    buying_issuer = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_manage_buy_offer_op(
        selling=Asset(selling_code, selling_issuer),
        buying=Asset(buying_code, buying_issuer),
        amount=amount,
        price=price,
        source=operation_source,
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarManageBuyOfferOp)
    assert operations[0].source_account == operation_source
    assert operations[0].selling_asset.type == messages.StellarAssetType.NATIVE
    assert operations[0].buying_asset.type == messages.StellarAssetType.ALPHANUM4
    assert operations[0].buying_asset.code == buying_code
    assert operations[0].buying_asset.issuer == buying_issuer
    assert operations[0].amount == 500111000
    assert operations[0].price_n == 1
    assert operations[0].price_d == 2
    assert operations[0].offer_id == 0  # indicates a new offer


def test_manage_buy_offer_update_offer():
    tx = make_default_tx()
    price = "0.5"
    amount = "50.0111"
    selling_code = "XLM"
    selling_issuer = None
    buying_code = "USD"
    buying_issuer = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
    offer_id = 12345
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_manage_buy_offer_op(
        selling=Asset(selling_code, selling_issuer),
        buying=Asset(buying_code, buying_issuer),
        amount=amount,
        price=price,
        offer_id=offer_id,
        source=operation_source,
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarManageBuyOfferOp)
    assert operations[0].source_account == operation_source
    assert operations[0].selling_asset.type == messages.StellarAssetType.NATIVE
    assert operations[0].buying_asset.type == messages.StellarAssetType.ALPHANUM4
    assert operations[0].buying_asset.code == buying_code
    assert operations[0].buying_asset.issuer == buying_issuer
    assert operations[0].amount == 500111000
    assert operations[0].price_n == 1
    assert operations[0].price_d == 2
    assert operations[0].offer_id == offer_id


def test_path_payment_strict_send():
    tx = make_default_tx()
    destination = "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6"
    send_amount = "50.0112"
    dest_min = "120"
    send_code = "XLM"
    send_issuer = None
    dest_code = "USD"
    dest_issuer = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"
    path_asset1 = Asset(
        "JPY", "GD6PV7DXQJX7AGVXFQ2MTCLTCH6LR3E6IO2EO2YDZD7F7IOZZCCB5DSQ"
    )
    path_asset2 = Asset(
        "BANANA", "GC7EKO37HNSKQ3V6RZ274EO7SFOWASQRHLX3OR5FIZK6UMV6LIEDXHGZ"
    )

    envelope = tx.append_path_payment_strict_send_op(
        destination=destination,
        send_asset=Asset(send_code, send_issuer),
        send_amount=send_amount,
        dest_asset=Asset(dest_code, dest_issuer),
        dest_min=dest_min,
        path=[path_asset1, path_asset2],
        source=operation_source,
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1

    assert isinstance(operations[0], messages.StellarPathPaymentStrictSendOp)
    assert operations[0].source_account == operation_source
    assert operations[0].destination_account == destination
    assert operations[0].send_asset.type == messages.StellarAssetType.NATIVE
    assert operations[0].send_amount == 500112000
    assert operations[0].destination_min == 1200000000
    assert operations[0].destination_asset.type == messages.StellarAssetType.ALPHANUM4
    assert operations[0].destination_asset.code == dest_code
    assert operations[0].destination_asset.issuer == dest_issuer
    assert len(operations[0].paths) == 2
    assert operations[0].paths[0].type == messages.StellarAssetType.ALPHANUM4
    assert operations[0].paths[0].code == path_asset1.code
    assert operations[0].paths[0].issuer == path_asset1.issuer
    assert operations[0].paths[1].type == messages.StellarAssetType.ALPHANUM12
    assert operations[0].paths[1].code == path_asset2.code
    assert operations[0].paths[1].issuer == path_asset2.issuer


def test_payment_muxed_account_not_support_raise():
    tx = make_default_tx()
    destination = MuxedAccount(
        "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6", 1
    )
    amount = "50.0111"
    asset_code = "XLM"
    asset_issuer = None
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_payment_op(
        destination=destination,
        amount=amount,
        asset=Asset(asset_code, asset_issuer),
        source=operation_source,
    ).build()

    with pytest.raises(ValueError, match="MuxedAccount is not supported"):
        stellar.from_envelope(envelope)


def test_path_payment_strict_send_muxed_account_not_support_raise():
    tx = make_default_tx()
    destination = MuxedAccount(
        "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6", 1
    )
    send_amount = "50.0112"
    dest_min = "120"
    send_code = "XLM"
    send_issuer = None
    dest_code = "USD"
    dest_issuer = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"
    path_asset1 = Asset(
        "JPY", "GD6PV7DXQJX7AGVXFQ2MTCLTCH6LR3E6IO2EO2YDZD7F7IOZZCCB5DSQ"
    )
    path_asset2 = Asset(
        "BANANA", "GC7EKO37HNSKQ3V6RZ274EO7SFOWASQRHLX3OR5FIZK6UMV6LIEDXHGZ"
    )

    envelope = tx.append_path_payment_strict_send_op(
        destination=destination,
        send_asset=Asset(send_code, send_issuer),
        send_amount=send_amount,
        dest_asset=Asset(dest_code, dest_issuer),
        dest_min=dest_min,
        path=[path_asset1, path_asset2],
        source=operation_source,
    ).build()

    with pytest.raises(ValueError, match="MuxedAccount is not supported"):
        stellar.from_envelope(envelope)


def test_path_payment_strict_receive_muxed_account_not_support_raise():
    tx = make_default_tx()
    destination = MuxedAccount(
        "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6", 1
    )
    send_max = "50.0111"
    dest_amount = "100"
    send_code = "XLM"
    send_issuer = None
    dest_code = "USD"
    dest_issuer = "GCSJ7MFIIGIRMAS4R3VT5FIFIAOXNMGDI5HPYTWS5X7HH74FSJ6STSGF"
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"
    path_asset1 = Asset(
        "JPY", "GD6PV7DXQJX7AGVXFQ2MTCLTCH6LR3E6IO2EO2YDZD7F7IOZZCCB5DSQ"
    )
    path_asset2 = Asset(
        "BANANA", "GC7EKO37HNSKQ3V6RZ274EO7SFOWASQRHLX3OR5FIZK6UMV6LIEDXHGZ"
    )

    envelope = tx.append_path_payment_strict_receive_op(
        destination=destination,
        send_asset=Asset(send_code, send_issuer),
        send_max=send_max,
        dest_asset=Asset(dest_code, dest_issuer),
        dest_amount=dest_amount,
        path=[path_asset1, path_asset2],
        source=operation_source,
    ).build()

    with pytest.raises(ValueError, match="MuxedAccount is not supported"):
        stellar.from_envelope(envelope)


def test_account_merge_muxed_account_not_support_raise():
    tx = make_default_tx()
    destination = MuxedAccount(
        "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6", 1
    )
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_account_merge_op(
        destination=destination, source=operation_source
    ).build()

    with pytest.raises(ValueError, match="MuxedAccount is not supported"):
        stellar.from_envelope(envelope)


def test_op_source_muxed_account_not_support_raise():
    tx = make_default_tx()
    destination = "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6"
    amount = "50.0111"
    asset_code = "XLM"
    asset_issuer = None
    operation_source = MuxedAccount(
        "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V", 2
    )

    envelope = tx.append_payment_op(
        destination=destination,
        amount=amount,
        asset=Asset(asset_code, asset_issuer),
        source=operation_source,
    ).build()

    with pytest.raises(ValueError, match="MuxedAccount is not supported"):
        stellar.from_envelope(envelope)


def test_tx_source_muxed_account_not_support_raise():
    source_account = Account(account=MuxedAccount(TX_SOURCE, 123456), sequence=SEQUENCE)
    destination = "GDNSSYSCSSJ76FER5WEEXME5G4MTCUBKDRQSKOYP36KUKVDB2VCMERS6"
    amount = "50.0111"
    asset_code = "XLM"
    asset_issuer = None
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = (
        TransactionBuilder(
            source_account=source_account,
            network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE,
            base_fee=BASE_FEE,
        )
        .add_time_bounds(TIMEBOUNDS_START, TIMEBOUNDS_END)
        .append_payment_op(
            destination=destination,
            amount=amount,
            asset=Asset(asset_code, asset_issuer),
            source=operation_source,
        )
        .build()
    )

    with pytest.raises(ValueError, match="MuxedAccount is not supported"):
        stellar.from_envelope(envelope)


def test_claim_claimable_balance():
    tx = make_default_tx()
    balance_id = (
        "00000000178826fbfe339e1f5c53417c6fedfe2c05e8bec14303143ec46b38981b09c3f9"
    )
    operation_source = "GAEB4MRKRCONK4J7MVQXAHTNDPAECUCCCNE7YC5CKM34U3OJ673A4D6V"

    envelope = tx.append_claim_claimable_balance_op(
        balance_id=balance_id, source=operation_source
    ).build()

    tx, operations = stellar.from_envelope(envelope)
    assert len(operations) == 1
    assert isinstance(operations[0], messages.StellarClaimClaimableBalanceOp)
    assert operations[0].source_account == operation_source
    assert operations[0].balance_id == bytes.fromhex(balance_id)