Files
chia-blockchain/chia/util/casts.py
matt-o-howandGitHub 80d38200a3 Migrate away from clvm imports (#19730)
* migrate casts to wallet util

* reset mempool_check_conditions

* ruff to re-order imports

* reset full_node_rpc_api

* add tests for casts

* ruff and type

* move to util to avoid consensus depending on wallet

* ruff again

* add a deterministic seed for random
2025-06-26 10:18:04 -07:00

22 lines
611 B
Python

# This file converts between chialisp bytes and Python integer
from __future__ import annotations
def int_from_bytes(blob: bytes) -> int:
size = len(blob)
if size == 0:
return 0
return int.from_bytes(blob, "big", signed=True)
def int_to_bytes(v: int) -> bytes:
byte_count = (v.bit_length() + 8) >> 3
if v == 0:
return b""
r = v.to_bytes(byte_count, "big", signed=True)
# make sure the string returned is minimal
# ie. no leading 00 or ff bytes that are unnecessary
while len(r) > 1 and r[0] == (0xFF if r[1] & 0x80 else 0):
r = r[1:]
return r