Last active
December 23, 2021 03:35
-
-
Save richardkiss/b52b0d1f5050a882b80d9ff039d5a03d to your computer and use it in GitHub Desktop.
offer-zlib-compression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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