chia 0.28.0

A meta-crate that exports all of the Chia crates in the workspace.
Documentation
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
from chia_rs import (
    SpendConditions,
    SpendBundleConditions,
    Coin,
    G1Element,
    G2Element,
    Program,
    AugSchemeMPL,
)
from chia_rs.sized_ints import uint64
from chia_rs.sized_bytes import bytes32
import pytest
import copy
import random

rng = random.Random(1337)
sk = AugSchemeMPL.key_gen(bytes32.random(rng))
pk = sk.get_g1()

coin = b"bcbcbcbcbcbcbcbcbcbcbcbcbcbcbcbc"
parent = b"edededededededededededededededed"
ph = b"abababababababababababababababab"
ph2 = b"cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd"


def test_hash_spend() -> None:

    a1 = SpendConditions(
        coin,
        parent,
        ph,
        123,
        None,
        0,
        None,
        None,
        None,
        None,
        [(ph2, 1000000, None)],
        [(pk, b"msg")],
        [],
        [],
        [],
        [],
        [],
        [],
        False,
        0,
        0,
    )
    a2 = SpendConditions(
        coin,
        parent,
        ph,
        123,
        None,
        1,
        None,
        None,
        None,
        None,
        [(ph2, 1000000, None)],
        [(pk, b"msg")],
        [],
        [],
        [],
        [],
        [],
        [],
        False,
        0,
        0,
    )
    b = hash(a1)
    c = hash(a2)
    assert type(b) is int
    assert type(c) is int
    assert b != c

    assert a1.get_hash() == bytes32.fromhex(
        "aee5968a44ae4f1c01fe79c688a757121c61b9f3e6d2267610bda93a27aaa502"
    )
    assert (
        str(a1.get_hash())
        == "aee5968a44ae4f1c01fe79c688a757121c61b9f3e6d2267610bda93a27aaa502"
    )


def test_hash_spend_bundle_conditions() -> None:

    a1 = SpendBundleConditions(
        [],
        1000,
        1337,
        42,
        None,
        None,
        [(pk, b"msg")],
        12345678,
        123,
        456,
        False,
        4321,
        8765,
    )
    a2 = SpendBundleConditions(
        [],
        1001,
        1337,
        42,
        None,
        None,
        [(pk, b"msg")],
        12345678,
        123,
        456,
        False,
        4321,
        8765,
    )
    b = hash(a1)
    c = hash(a2)
    assert type(b) is int
    assert type(c) is int
    assert b != c


def test_json_spend() -> None:

    a = SpendConditions(
        coin,
        parent,
        ph,
        123,
        None,
        0,
        None,
        None,
        None,
        None,
        [(ph2, 1000000, None)],
        [(pk, b"msg")],
        [],
        [],
        [],
        [],
        [],
        [],
        False,
        0,
        0,
    )

    assert a.to_json_dict() == {
        "coin_id": "0x" + coin.hex(),
        "parent_id": "0x" + parent.hex(),
        "puzzle_hash": "0x" + ph.hex(),
        "coin_amount": 123,
        "height_relative": None,
        "seconds_relative": 0,
        "before_height_relative": None,
        "before_seconds_relative": None,
        "birth_height": None,
        "birth_seconds": None,
        "create_coin": [["0x" + ph2.hex(), 1000000, None]],
        "agg_sig_me": [["0x" + bytes(pk).hex(), "0x6d7367"]],
        "agg_sig_parent": [],
        "agg_sig_puzzle": [],
        "agg_sig_amount": [],
        "agg_sig_puzzle_amount": [],
        "agg_sig_parent_amount": [],
        "agg_sig_parent_puzzle": [],
        "flags": 0,
        "execution_cost": 0,
        "condition_cost": 0,
    }


def test_from_json_spend() -> None:

    a = SpendConditions(
        coin,
        parent,
        ph,
        123,
        None,
        0,
        None,
        None,
        None,
        None,
        [(ph2, 1000000, None)],
        [(pk, b"msg")],
        [],
        [],
        [],
        [],
        [],
        [],
        False,
        0,
        0,
    )

    b = SpendConditions.from_json_dict(
        {
            "coin_id": "0x" + coin.hex(),
            "parent_id": "0x" + parent.hex(),
            "puzzle_hash": "0x" + ph.hex(),
            "coin_amount": 123,
            "height_relative": None,
            "seconds_relative": 0,
            "before_height_relative": None,
            "before_seconds_relative": None,
            "birth_height": None,
            "birth_seconds": None,
            "create_coin": [["0x" + ph2.hex(), 1000000, None]],
            "agg_sig_me": [["0x" + bytes(pk).hex(), "0x6d7367"]],
            "agg_sig_parent": [],
            "agg_sig_puzzle": [],
            "agg_sig_amount": [],
            "agg_sig_puzzle_amount": [],
            "agg_sig_parent_amount": [],
            "agg_sig_parent_puzzle": [],
            "flags": 0,
            "execution_cost": 0,
            "condition_cost": 0,
        }
    )
    assert a == b


def test_from_json_spend_set_optional() -> None:

    a = SpendConditions(
        coin,
        parent,
        ph,
        123,
        1337,
        0,
        None,
        None,
        None,
        None,
        [(ph2, 1000000, None)],
        [(pk, b"msg")],
        [],
        [],
        [],
        [],
        [],
        [],
        False,
        0,
        0,
    )

    b = SpendConditions.from_json_dict(
        {
            "coin_id": "0x" + coin.hex(),
            "parent_id": "0x" + parent.hex(),
            "puzzle_hash": "0x" + ph.hex(),
            "coin_amount": 123,
            "height_relative": 1337,
            "seconds_relative": 0,
            "before_height_relative": None,
            "before_seconds_relative": None,
            "birth_height": None,
            "birth_seconds": None,
            "create_coin": [["0x" + ph2.hex(), 1000000, None]],
            "agg_sig_me": [["0x" + bytes(pk).hex(), "0x6d7367"]],
            "agg_sig_parent": [],
            "agg_sig_puzzle": [],
            "agg_sig_amount": [],
            "agg_sig_puzzle_amount": [],
            "agg_sig_parent_amount": [],
            "agg_sig_parent_puzzle": [],
            "flags": 0,
            "execution_cost": 0,
            "condition_cost": 0,
        }
    )
    assert a == b


def test_invalid_hex_prefix() -> None:

    with pytest.raises(ValueError, match="bytes object is expected to start with 0x"):
        a = SpendConditions.from_json_dict(
            {
                # this field is missing the 0x prefix
                "coin_id": coin.hex(),
                "parent_id": "0x" + parent.hex(),
                "puzzle_hash": "0x" + ph.hex(),
                "coin_amount": 123,
                "height_relative": None,
                "seconds_relative": 0,
                "before_height_relative": None,
                "before_seconds_relative": None,
                "birth_height": None,
                "birth_seconds": None,
                "create_coin": [["0x" + ph2.hex(), 1000000, None]],
                "agg_sig_me": [["0x" + bytes(pk).hex(), "0x6d7367"]],
                "agg_sig_parent": [],
                "agg_sig_puzzle": [],
                "agg_sig_amount": [],
                "agg_sig_puzzle_amount": [],
                "agg_sig_parent_amount": [],
                "agg_sig_parent_puzzle": [],
                "flags": 0,
            }
        )


def test_invalid_hex_prefix_bytes() -> None:

    with pytest.raises(ValueError, match="bytes object is expected to start with 0x"):
        a = SpendConditions.from_json_dict(
            {
                "coin_id": "0x" + coin.hex(),
                "parent_id": "0x" + parent.hex(),
                "puzzle_hash": "0x" + ph.hex(),
                "coin_amount": 123,
                "height_relative": None,
                "seconds_relative": 0,
                "before_height_relative": None,
                "before_seconds_relative": None,
                "birth_height": None,
                "birth_seconds": None,
                "create_coin": [["0x" + ph2.hex(), 1000000, None]],
                # the message field is missing the 0x prefix and is variable length bytes
                "agg_sig_me": [["0x" + bytes(pk).hex(), "6d7367"]],
                "agg_sig_parent": [],
                "agg_sig_puzzle": [],
                "agg_sig_amount": [],
                "agg_sig_puzzle_amount": [],
                "agg_sig_parent_amount": [],
                "agg_sig_parent_puzzle": [],
                "flags": 0,
            }
        )


def test_invalid_hex_digit() -> None:

    with pytest.raises(ValueError, match="invalid hex"):
        a = SpendConditions.from_json_dict(
            {
                # this field is has an invalid hex digit (the last one)
                "coin_id": "0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdeg",
                "parent_id": "0x" + parent.hex(),
                "puzzle_hash": "0x" + ph.hex(),
                "coin_amount": 123,
                "height_relative": None,
                "seconds_relative": 0,
                "before_height_relative": None,
                "before_seconds_relative": None,
                "birth_height": None,
                "birth_seconds": None,
                "create_coin": [["0x" + ph2.hex(), 1000000, None]],
                "agg_sig_me": [["0x" + bytes(pk).hex(), "0x6d7367"]],
                "agg_sig_parent": [],
                "agg_sig_puzzle": [],
                "agg_sig_amount": [],
                "agg_sig_puzzle_amount": [],
                "agg_sig_parent_amount": [],
                "agg_sig_parent_puzzle": [],
                "flags": 0,
            }
        )


def test_invalid_hex_length() -> None:

    with pytest.raises(ValueError, match="invalid length 33 expected 32"):
        a = SpendConditions.from_json_dict(
            {
                # this field is has invalid length
                "coin_id": "0x" + coin.hex() + "ff",
                "parent_id": "0x" + parent.hex(),
                "puzzle_hash": "0x" + ph.hex(),
                "coin_amount": 123,
                "height_relative": None,
                "seconds_relative": 0,
                "before_height_relative": None,
                "before_seconds_relative": None,
                "birth_height": None,
                "birth_seconds": None,
                "create_coin": [["0x" + ph2.hex(), 1000000, None]],
                "agg_sig_me": [["0x" + bytes(pk).hex(), "0x6d7367"]],
                "agg_sig_parent": [],
                "agg_sig_puzzle": [],
                "agg_sig_amount": [],
                "agg_sig_puzzle_amount": [],
                "agg_sig_parent_amount": [],
                "agg_sig_parent_puzzle": [],
                "flags": 0,
            }
        )


def test_missing_field() -> None:

    with pytest.raises(KeyError, match="coin_id"):
        a = SpendConditions.from_json_dict(
            {
                # coin_id is missing
                "parent_id": "0x" + parent.hex(),
                "puzzle_hash": "0x" + ph.hex(),
                "coin_amount": 123,
                "height_relative": None,
                "seconds_relative": 0,
                "before_height_relative": None,
                "before_seconds_relative": None,
                "birth_height": None,
                "birth_seconds": None,
                "create_coin": [["0x" + ph2.hex(), 1000000, None]],
                "agg_sig_me": [["0x" + bytes(pk).hex(), "0x6d7367"]],
                "agg_sig_parent": [],
                "agg_sig_puzzle": [],
                "agg_sig_amount": [],
                "agg_sig_puzzle_amount": [],
                "agg_sig_parent_amount": [],
                "agg_sig_parent_puzzle": [],
                "flags": 0,
            }
        )


def test_json_spend_bundle_conditions() -> None:

    a = SpendBundleConditions(
        [],
        1000,
        1337,
        42,
        None,
        None,
        [(pk, b"msg")],
        12345678,
        123,
        456,
        False,
        4321,
        8765,
    )

    assert a.to_json_dict() == {
        "spends": [],
        "reserve_fee": 1000,
        "height_absolute": 1337,
        "seconds_absolute": 42,
        "before_height_absolute": None,
        "before_seconds_absolute": None,
        "agg_sig_unsafe": [["0x" + bytes(pk).hex(), "0x6d7367"]],
        "cost": 12345678,
        "removal_amount": 123,
        "addition_amount": 456,
        "validated_signature": False,
        "execution_cost": 4321,
        "condition_cost": 8765,
    }


def test_from_json_spend_bundle_conditions() -> None:

    a = SpendBundleConditions(
        [],
        1000,
        1337,
        42,
        None,
        None,
        [(pk, b"msg")],
        12345678,
        123,
        456,
        False,
        4321,
        8765,
    )
    b = SpendBundleConditions.from_json_dict(
        {
            "spends": [],
            "reserve_fee": 1000,
            "height_absolute": 1337,
            "seconds_absolute": 42,
            "before_height_absolute": None,
            "before_seconds_absolute": None,
            "agg_sig_unsafe": [["0x" + bytes(pk).hex(), "0x6d7367"]],
            "cost": 12345678,
            "removal_amount": 123,
            "addition_amount": 456,
            "validated_signature": False,
            "execution_cost": 4321,
            "condition_cost": 8765,
        }
    )
    assert a == b


def test_copy_spend() -> None:

    a = SpendConditions(
        coin,
        parent,
        ph,
        123,
        None,
        0,
        None,
        None,
        None,
        None,
        [(ph2, 1000000, None)],
        [(pk, b"msg")],
        [],
        [],
        [],
        [],
        [],
        [],
        False,
        0,
        0,
    )
    b = copy.copy(a)

    assert a == b
    assert a is not b

    b = copy.deepcopy(a)
    assert a == b
    assert a is not b


def test_copy_spend_bundle_conditions() -> None:

    a = SpendBundleConditions(
        [],
        1000,
        1337,
        42,
        None,
        None,
        [(pk, b"msg")],
        12345678,
        123,
        456,
        False,
        4321,
        8765,
    )
    b = copy.copy(a)

    assert a == b
    assert a is not b

    b = copy.deepcopy(a)
    assert a == b
    assert a is not b


def coin_roundtrip(c: Coin) -> bool:
    buf = c.to_bytes()
    # make sure c.to_bytes() and bytes(c) are synonyms
    assert buf == bytes(c)
    buf = c.stream_to_bytes()
    assert buf == bytes(c)
    c2 = Coin.from_bytes(buf)
    return c == c2


def test_coin_serialize() -> None:

    c1 = Coin(coin, ph, uint64(1000000))
    assert c1.to_bytes() == coin + ph + (1000000).to_bytes(8, byteorder="big")
    assert coin_roundtrip(c1)

    c2 = Coin(coin, ph2, uint64(0))
    assert c2.to_bytes() == coin + ph2 + (0).to_bytes(8, byteorder="big")
    assert coin_roundtrip(c2)

    c3 = Coin(coin, ph2, uint64(0xFFFFFFFFFFFFFFFF))
    assert c3.to_bytes() == coin + ph2 + (0xFFFFFFFFFFFFFFFF).to_bytes(
        8, byteorder="big"
    )
    assert coin_roundtrip(c3)


def test_coin_parse_rust() -> None:

    buffer = (
        coin
        + ph2
        + (0xFFFFFFFFFFFFFFFF).to_bytes(8, byteorder="big")
        + b"more bytes following, that should be ignored"
    )

    c1, consumed = Coin.parse_rust(buffer)
    assert buffer[consumed:] == b"more bytes following, that should be ignored"
    assert c1 == Coin(coin, ph2, uint64(0xFFFFFFFFFFFFFFFF))


def sha2(buf: bytes) -> bytes:
    from hashlib import sha256

    ctx = sha256()
    ctx.update(buf)
    return ctx.digest()


def test_coin_get_hash() -> None:

    c1 = Coin(coin, ph, uint64(1000000))
    assert sha2(c1.to_bytes()) == c1.get_hash()

    c2 = Coin(coin, ph2, uint64(0))
    assert sha2(c2.to_bytes()) == c2.get_hash()

    c3 = Coin(coin, ph2, uint64(0xFFFFFFFFFFFFFFFF))
    assert sha2(c3.to_bytes()) == c3.get_hash()


def test_g1_element() -> None:

    a = G1Element.from_bytes(
        bytes.fromhex(
            "a24d88ce995cea579675377728938eeb3956d5da608414efc9064774dc9653764edeb4823fc8da22c810917bf389c127"
        )
    )
    b = bytes(a)
    assert b == bytes.fromhex(
        "a24d88ce995cea579675377728938eeb3956d5da608414efc9064774dc9653764edeb4823fc8da22c810917bf389c127"
    )
    c = G1Element.from_bytes(b)
    assert a == c

    assert (
        a.to_json_dict()
        == "0xa24d88ce995cea579675377728938eeb3956d5da608414efc9064774dc9653764edeb4823fc8da22c810917bf389c127"
    )

    d = G1Element.from_json_dict(
        "0xa24d88ce995cea579675377728938eeb3956d5da608414efc9064774dc9653764edeb4823fc8da22c810917bf389c127"
    )
    assert d == a

    d = G1Element.from_json_dict(
        bytes.fromhex(
            "a24d88ce995cea579675377728938eeb3956d5da608414efc9064774dc9653764edeb4823fc8da22c810917bf389c127"
        )
    )
    assert d == a


def test_g2_element() -> None:

    a = G2Element.from_bytes(
        bytes.fromhex(
            "a566b4d972db20765c668ce7fdcd76a4a5a8201dc2d5b1e747e2993fcdd99c8c96c1ca0503ade72809ae6d19c5e8400e10900a24ae56b7c9c84231ed5b7dd4c0790dd1aef56e0820e86994aa02c33bd409d3f17ace74c7fa40b00fe5022cc6d6"
        )
    )
    b = bytes(a)
    assert b == bytes.fromhex(
        "a566b4d972db20765c668ce7fdcd76a4a5a8201dc2d5b1e747e2993fcdd99c8c96c1ca0503ade72809ae6d19c5e8400e10900a24ae56b7c9c84231ed5b7dd4c0790dd1aef56e0820e86994aa02c33bd409d3f17ace74c7fa40b00fe5022cc6d6"
    )
    c = G2Element.from_bytes(b)
    assert a == c

    assert (
        a.to_json_dict()
        == "0xa566b4d972db20765c668ce7fdcd76a4a5a8201dc2d5b1e747e2993fcdd99c8c96c1ca0503ade72809ae6d19c5e8400e10900a24ae56b7c9c84231ed5b7dd4c0790dd1aef56e0820e86994aa02c33bd409d3f17ace74c7fa40b00fe5022cc6d6"
    )

    d = G2Element.from_json_dict(
        "0xa566b4d972db20765c668ce7fdcd76a4a5a8201dc2d5b1e747e2993fcdd99c8c96c1ca0503ade72809ae6d19c5e8400e10900a24ae56b7c9c84231ed5b7dd4c0790dd1aef56e0820e86994aa02c33bd409d3f17ace74c7fa40b00fe5022cc6d6"
    )
    assert a == d

    d = G2Element.from_json_dict(
        bytes.fromhex(
            "a566b4d972db20765c668ce7fdcd76a4a5a8201dc2d5b1e747e2993fcdd99c8c96c1ca0503ade72809ae6d19c5e8400e10900a24ae56b7c9c84231ed5b7dd4c0790dd1aef56e0820e86994aa02c33bd409d3f17ace74c7fa40b00fe5022cc6d6"
        )
    )
    assert a == d


def test_program() -> None:
    p = Program.from_json_dict("0xff8080")
    assert str(p) == "Program(ff8080)"
    assert p.to_bytes() == bytes.fromhex("ff8080")
    assert p.stream_to_bytes() == bytes.fromhex("ff8080")

    p = Program.from_bytes(bytes.fromhex("ff8080"))
    assert str(p) == "Program(ff8080)"
    assert p.to_bytes() == bytes.fromhex("ff8080")
    assert p.stream_to_bytes() == bytes.fromhex("ff8080")

    # make sure we can pass in a slice/memoryview
    p = Program.from_bytes(bytes.fromhex("00ff8080")[1:])
    assert str(p) == "Program(ff8080)"

    # truncated serialization
    with pytest.raises(ValueError, match="unexpected end of buffer"):
        Program.from_bytes(bytes.fromhex("ff80"))

    with pytest.raises(ValueError, match="unexpected end of buffer"):
        Program.parse_rust(bytes.fromhex("ff80"))

    # garbage at the end of the serialization
    # from_bytes() requires all input to be consumed
    with pytest.raises(ValueError, match="input buffer too large"):
        Program.from_bytes(bytes.fromhex("ff808080"))

    # But the (lower level) parse() function doesn't, because it's meant to
    # consume only its part of the stream.
    p, consumed = Program.parse_rust(bytes.fromhex("ff808080"))
    assert str(p) == "Program(ff8080)"
    assert consumed == 3

    # truncated serialization
    with pytest.raises(ValueError, match="unexpected end of buffer"):
        Program.from_json_dict("0xff80")

    # garbage at the end of the serialization
    with pytest.raises(ValueError, match="invalid CLVM serialization"):
        Program.from_json_dict("0xff808080")