Skip to content

Instantly share code, notes, and snippets.

@richardkiss
Last active December 23, 2021 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardkiss/b52b0d1f5050a882b80d9ff039d5a03d to your computer and use it in GitHub Desktop.
Save richardkiss/b52b0d1f5050a882b80d9ff039d5a03d to your computer and use it in GitHub Desktop.
offer-zlib-compression
import zlib
from chia.wallet.puzzles.load_clvm import load_clvm
from chia.wallet.puzzles import p2_delegated_puzzle_or_hidden_puzzle as standard_puzzle
from chia.wallet.puzzles.cc_loader import CC_MOD
OFFER_MOD = load_clvm("settlement_payments.clvm")
puzzle_dict = bytes(OFFER_MOD) + bytes(standard_puzzle.MOD) + bytes(CC_MOD)
offer_stream = bytes.fromhex(open("test.offer").read())
comp_obj = zlib.compressobj(zdict=puzzle_dict)
comp_offer = comp_obj.compress(offer_stream)
comp_offer += comp_obj.flush()
print(len(comp_offer))
do = zlib.decompressobj(zdict = puzzle_dict)
o1 = do.decompress(comp_offer)
print(len(o1))
print(o1 == offer_stream)
# this code is rough and not tested and quite ugly
ZDICT = [
bytes(OFFER_MOD) + bytes(standard_puzzle.MOD) + bytes(CC_MOD),
# more dictionaries go here
]
def zdict_for_version(version: int) -> bytes:
assert version == 1 # expand more later
return ZDICT[version]
def decompress_offer_blob(offer_blob: bytes) -> SpendBundle:
assert offer_blob[:4] == "OFFR"
version = int.from_bytes(offer_blob[4:6], "big")
zdict = zdict_for_version(version)
sb_bytes = decompress_magic(zdict, offer_blob[6:]) # do the decompression, as in the other python file
return SpendBundle.deserialize(sb_bytes)
def compress_offer(sb: SpendBundle, version: int) -> bytes:
version_blob = version.to_bytes(length=2, byteorder="big")
zdict = zdict_for_version(version)
offer_blob_sb = compress_magic(zdict, bytes(sb)) # do the compression, as in the other python file
return b"OFFR" + version_blob + offer_blob_sb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment