Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snake string utils #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tonsdk/boc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
from ._builder import Builder, begin_cell
from ._dict_builder import DictBuilder, begin_dict
from ._slice import Slice
from ._string_utils import string_to_cell, cell_to_string, read_string_tail, write_string_tail
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixme: Remove


__all__ = [
'Cell', 'Slice',
'Builder', 'begin_cell',
'DictBuilder', 'begin_dict',
'deserialize_cell_data',
'parse_boc_header',
'string_to_cell', 'cell_to_string', 'read_string_tail', 'write_string_tail'
]
54 changes: 54 additions & 0 deletions tonsdk/utils/_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# https://github.com/ton-blockchain/TEPs/blob/master/text/0064-token-data-standard.md#data-serialization
from ctypes import Union

from tonsdk.boc import begin_cell, Builder, Slice


class SnakeData:
Copy link
Contributor

@sasha1618 sasha1618 Jun 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may write

class SnakeData:
    """
    Implementation of the TON data standard: https://github.com/ton-blockchain/TEPs/blob/master/text/0064-token-data-standard.md#data-serialization
    """

prefix = 0x00
prefix_len = 8

@classmethod
def write(cls, builder: Builder, data: Union[bytes, bytearray], prefixed=False):
if prefixed:
builder.store_uint(cls.prefix, cls.prefix_len)

# todo: implement data serialization logic to a given builder (now data is a bytes sequence)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we delete this?

# implementation example
builders = []

while len(data) > 0:
max_bytes = builder.builder_rembits >> 3
bits, data = data[:max_bytes], data[max_bytes:]

builder.store_bytes(bits)
builders.append(builder)

builder = begin_cell()

if len(builders) > 1:
last_builder = builders[-1]

for builder in reversed(builders[:-1]):
builder.store_ref(last_builder.end_cell())
last_builder = builder

return builders[0]

@classmethod
def read(cls, cs: Slice, prefixed=False):
data = bytearray()

if prefixed:
assert cs.preload_uint(cls.prefix_len) == cls.prefix

while True:
data.extend(cs.load_bytes(cs.slice_bits >> 3))

if cs.slice_refs > 0:
cs = cs.load_ref().begin_parse()
continue

break

return data