An implementation of the DAG-PB spec for Python.
Project description
ipld-dag-pb
An implementation of the DAG-PB spec for Python.
Install
pip install ipld-dag-pb
Usage
from ipld_dag_pb import PBNode, encode, decode, code
from multiformats import multihash, CID
node = PBNode("Some data as a string".encode("utf-8"))
encoded_bytes = encode(node)
# Alternatively:
# from ipld_dag_pb import prepare
# encoded_bytes = encode(prepare("Some data as a string"))
digest = multihash.digest(encoded_bytes, "sha2-256")
cid = CID("base32", 1, code, digest)
print(str(cid) + " => " + encoded_bytes.hex())
# bafybeibweq2akodv3uezac2g225qvjynh7lv3xv4wp5c4uxfppogtwt3fe => 0a15536f6d652064617461206173206120737472696e67
decoded_bytes = decode(encoded_bytes)
print(decoded_bytes)
print("decoded 'Data': " + bytes(decoded_bytes.data).decode("utf-8"))
# decoded 'Data': Some data as a string
prepare()
The DAG-PB encoding is very strict about the Data Model forms that are passed in. The objects must exactly resemble what they would if they were to undergo a round-trip of encode & decode. Therefore, extraneous or mistyped properties are not acceptable and will be rejected. See the DAG-PB spec for full details of the acceptable schema and additional constraints.
Due to this strictness, a prepare() function is made available which simplifies construction and allows for more flexible input forms. Prior to encoding objects, call prepare() to receive a new object that strictly conforms to the schema.
from ipld_dag_pb import prepare
from multiformats import CID
print(vars(prepare("Some data as a string")))
# -> {'data': b'Some data as a string', 'links': []}
print(vars(prepare({"links": [CID.decode("bafkqabiaaebagba")]})))
# -> {'data': None, 'links': [<ipld_dag_pb.node.PBLink object at 0x102c1b0e0>]}
Contributing
All welcome! storacha.network is open-source.
License
Dual-licensed under Apache-2.0 OR MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ipld_dag_pb-0.0.1.tar.gz.
File metadata
- Download URL: ipld_dag_pb-0.0.1.tar.gz
- Upload date:
- Size: 21.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e205f613d6a0f4b276bfc2d40f0c07db173a0fd2f938862f867ce05f2eddd417
|
|
| MD5 |
0cc1fbb1c401ffd28adaaffd38330566
|
|
| BLAKE2b-256 |
73426a6cbb0cab9bd644d561b7f1023b20d8b84cabee739b35f2c473a53e5606
|
File details
Details for the file ipld_dag_pb-0.0.1-py3-none-any.whl.
File metadata
- Download URL: ipld_dag_pb-0.0.1-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc5fee4bff126571446a4ae5ac8342c05221759f995e7d4ded22ad9df33c8c89
|
|
| MD5 |
ca092ea8b08179c2f40c502bcb28c350
|
|
| BLAKE2b-256 |
c89c7a042e1b5e54ea807300fd96123767e7251bcd2bd59312ba2e33376bad5f
|