Skip to content

Commit

Permalink
Allowing writes for deterministic stream types
Browse files Browse the repository at this point in the history
  • Loading branch information
mzkrasner committed Nov 1, 2024
1 parent 54e331f commit 98500bb
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
14 changes: 12 additions & 2 deletions ceramicsdk/ceramic_python/model_instance_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,21 +273,31 @@ def _make_raw_genesis(
validate_content_length(content, ModelInstanceDocument.MAX_DOCUMENT_SIZE)

controller = metadata_args.controller or signer.as_controller()

# Encode model ID as base64 string
model_bytes = bytes(bytearray(list(base36_decode_with_prefix(metadata_args.model))))
model_b64 = b64encode(model_bytes).decode('utf-8')
header = {
"controllers": [controller], # Remove the extra list encapsulation
"sep": "model",
"model": model_b64,
} if metadata_args.deterministic else {
"controllers": [controller],
"sep": "model",
"model": bytes(bytearray(list(base36_decode_with_prefix(metadata_args.model)))),
}

if metadata_args.deterministic:
if unique:
header["unique"] = "|".join(unique).encode("utf-8")
header["unique"] = "|".join(unique)
else:
random_bytes = os.urandom(12)
header["unique"] = b64encode(random_bytes).decode('utf-8')

if metadata_args.context:
header["context"] = bytes(bytearray(list(base36_decode_with_prefix(metadata_args.context))))
context_bytes = bytes(bytearray(list(base36_decode_with_prefix(metadata_args.context))))
context_b64 = b64encode(context_bytes).decode('utf-8')
header["context"] = context_b64 if metadata_args.deterministic else bytes(bytearray(list(base36_decode_with_prefix(metadata_args.context))))


return {"data": content, "header": header}
Expand Down
20 changes: 18 additions & 2 deletions ceramicsdk/orbis_python/orbis_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,29 @@ def add_row(self, entry_data):
if not self.controller:
raise ValueError("Read-only database. OrbisDB controller has not being specified. Cannot write to the database.")

# Check if model requires deterministic (set or single relation)
model_type = self.ceramic_client.load_stream(self.table_stream, opts={"sync": 0})["state"]["content"]["accountRelation"]["type"]
is_set_or_single = model_type in ["set", "single"]

metadata_args = ModelInstanceDocumentMetadataArgs(
controller=self.controller.public_key,
model=self.table_stream,
context=self.context_stream
context=self.context_stream,
deterministic=False
) if not is_set_or_single else ModelInstanceDocumentMetadataArgs(
controller=self.controller.public_key,
model=self.table_stream,
context=self.context_stream,
deterministic=True
)

doc = ModelInstanceDocument.create(self.ceramic_client, entry_data, metadata_args)
doc = ModelInstanceDocument.create(self.ceramic_client, entry_data, metadata_args) if not is_set_or_single else ModelInstanceDocument.create(
ceramic_client=self.ceramic_client,
content=None, # Must be None for deterministic creation
metadata_args=metadata_args
)
if is_set_or_single:
doc.replace(entry_data)
return doc.stream_id


Expand Down
2 changes: 1 addition & 1 deletion ceramicsdk/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="ceramicsdk",
version="0.1.2",
version="0.1.3",
author='Ceramic Ecosystem Developers',
description="This Ceramic client implements the payload building, encoding, and signing needed to interact with the Ceramic Network. It currently supports ModelInstanceDocument and OrbisDB.",
long_description=long_description,
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
python-dotenv==1.0.1
ceramicsdk==0.1.2
ceramicsdk==0.1.3
flask

0 comments on commit 98500bb

Please sign in to comment.