Added some new class methods to the Program objects (#8041)

This commit is contained in:
Matt Hauff 2021-08-13 10:15:25 -07:00 committed by GitHub
parent 7b2aab99c8
commit 950e78e1f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,6 +12,7 @@ from clvm_tools.curry import curry, uncurry
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.hash import std_hash
from chia.util.byte_types import hexstr_to_bytes
from .tree_hash import sha256_treehash
@ -54,9 +55,17 @@ class Program(SExp):
assert f.read() == b""
return result
@classmethod
def fromhex(cls, hexstr: str) -> "Program":
return cls.from_bytes(hexstr_to_bytes(hexstr))
def to_serialized_program(self) -> "SerializedProgram":
return SerializedProgram.from_bytes(bytes(self))
@classmethod
def from_serialized_program(cls, sp: "SerializedProgram") -> "Program":
return cls.from_bytes(bytes(sp))
def __bytes__(self) -> bytes:
f = io.BytesIO()
self.stream(f) # type: ignore # noqa
@ -166,6 +175,10 @@ class SerializedProgram:
ret._buf = bytes(blob)
return ret
@classmethod
def fromhex(cls, hexstr: str) -> "SerializedProgram":
return cls.from_bytes(hexstr_to_bytes(hexstr))
@classmethod
def from_program(cls, p: Program) -> "SerializedProgram":
ret = SerializedProgram()