Skip to content

Instantly share code, notes, and snippets.

@ProgramCrafter
Created March 3, 2023 20:43
Show Gist options
  • Save ProgramCrafter/704886244271f004319951a5e8eaa241 to your computer and use it in GitHub Desktop.
Save ProgramCrafter/704886244271f004319951a5e8eaa241 to your computer and use it in GitHub Desktop.
Minter of fully onchain NFTs
from tonsdk.boc import Builder, Cell, begin_dict
from tonsdk.utils import Address
import threading
import traceback
import hashlib
import base64
import sys
def read_file(path: str) -> bytes:
with open(path, 'rb') as f:
return f.read()
# return base64.b64encode(f.read())
def serialize_short(t: bytes) -> Cell:
c = Builder()
c.store_uint(0, 8)
c.store_bytes(t)
return c.end_cell()
def bytes_to_chunked(t: bytes) -> Cell:
CHUNK_BYTES = 126
b = Builder()
b.store_uint(1, 8)
d = begin_dict(32)
for i in range(0, len(t), CHUNK_BYTES):
part = t[i:i+CHUNK_BYTES]
c = Builder()
c.store_bytes(part)
d.store_ref(i // CHUNK_BYTES, c.end_cell())
b.store_maybe_ref(d.end_dict())
return b.end_cell()
def serialize_metadata(name: str, desc: str, image: bytes) -> Cell:
d = begin_dict(256)
d.store_ref(
hashlib.sha256(b'name').digest(),
serialize_short(name.encode('utf-8'))
)
d.store_ref(
hashlib.sha256(b'description').digest(),
serialize_short(desc.encode('utf-8'))
)
d.store_ref(
hashlib.sha256(b'image_data').digest(),
bytes_to_chunked(image)
)
return d.end_dict()
def serialize_content(name: str, desc: str, image: bytes) -> Cell:
b = Builder()
b.store_uint(0, 8)
b.store_maybe_ref(serialize_metadata(name, desc, image))
return b.end_cell()
def serialize_data(name: str, desc: str, image: bytes) -> Cell:
b = Builder()
b.store_address(Address('EQCyoez1VF4HbNNq5Rbqfr3zKuoAjKorhK-YZr7LIIiVrSD7'))
b.store_address(Address('EQCyoez1VF4HbNNq5Rbqfr3zKuoAjKorhK-YZr7LIIiVrSD7'))
b.store_ref(serialize_content(name, desc, image))
royalty = Builder()
royalty.store_uint(500, 16)
royalty.store_uint(10000, 16)
royalty.store_address(Address('EQCyoez1VF4HbNNq5Rbqfr3zKuoAjKorhK-YZr7LIIiVrSD7'))
b.store_ref(royalty.end_cell())
return b.end_cell()
def main():
sys.setrecursionlimit(100000)
# pfx = b'data:image/webp;base64,'
f1 = read_file(r"C:\Users\T\Downloads\downscaled--nft--frozen--kFYBsRgwbAP8b6IkZ7BF--9--inuoo.webp")
# f2 = pfx + read_file(r"C:\Users\T\Downloads\downscaled--nft--frozen--3qnDzat7CI3o2Xzgi2Oj--8--6hxdm.webp")
print('Loaded images')
# c1 = bytes_to_cells(f1)
# c2 = bytes_to_cells(f2)
# c3 = bytes_to_cells(f3)
# print('Converted bytes to cells')
nft_data = serialize_data(
'Test Frozen NFT',
'Minted as test or to wallets frozen in February, 2022',
f1
)
print()
print(base64.b64encode(nft_data.to_boc(False)).decode('ascii'))
try:
if __name__ == '__main__':
threading.stack_size(0x2000000)
t = threading.Thread(target=main)
t.start()
t.join()
except:
traceback.print_exc()
finally:
input('...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment