Skip to content

Storing a File in a Golem Base Entity

TODO: This worked with TypeScript, but when trying to retrieve the file we get an exception error in the Python SDK. Need to investigate.

In this example, we'll show you how to store a binary file in a new entity on a running Golem-base Op-Geth node.

Tip: We'll be using a live, test node!

Here we're assuming you:

  1. Have a private.key file that contains an Ethereum address. If not, read here on how to create one.

  2. Add funds. If you need to do so but aren't sure how, check out this page.

Now, at the command prompt, add in the golem-base-sdk, along with two helper packages:

pip install golem_base_sdk asyncio anyio

Next, create a file called main.py and add the following code to it; adjust the /path/to to point to wherever you have your private.key stored.

import asyncio
import anyio
from golem_base_sdk import ( Annotation, GolemBaseClient, GolemBaseCreate )

async def main() -> None:
    with open('/path/to/private.key', 'rb') as private_key_file:
        key_bytes = private_key_file.read(32)

    client = await GolemBaseClient.create_rw_client(
        rpc_url='https://kaolin.holesky.golem-base.io/rpc',
        ws_url='wss://kaolin.holesky.golem-base.io/rpc/ws',
        private_key=key_bytes,
    )

    with open("golem.png", "rb") as f:
        binary_data = f.read()

    create_receipt = await client.create_entities(
        [GolemBaseCreate(binary_data, 60, [Annotation("name", "golem.png")], [])]
    )

    print(f"Entity hash: {create_receipt[0].entity_key.as_hex_string()}")
    print(f"Expires at: {create_receipt[0].expiration_block}")

    await client.disconnect()

if __name__ == "__main__":
    asyncio.run(main())

And then here's code to retrieve it; when you run this code, paste in the hash string printed out in the code above (including the initial 0x).


import asyncio
import anyio
from golem_base_sdk import ( Annotation, GolemBaseClient, GolemBaseCreate, GenericBytes)

async def main() -> None:
    with open('./private.key', 'rb') as private_key_file:
        key_bytes = private_key_file.read(32)

    client = await GolemBaseClient.create_rw_client(rpc_url='https://rpc.kaolin.holesky.golem-base.io',ws_url='wss://ws.rpc.kaolin.holesky.golem-base.io',private_key=key_bytes)

    hash = input('Please paste in your entity hash: ')

    value = await client.get_storage_value(GenericBytes.from_hex_string(hash))
    with open("restored_golem.png", "wb") as f:
        f.write(value)

    await client.disconnect()

if __name__ == "__main__":
    asyncio.run(main())

Tip: We suggest you query for your entity based on the filename.

Code coming soon!