Skip to content

Commit

Permalink
Add multibase encode and decode (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshalX authored Jan 10, 2024
1 parent faaabfd commit a99bc9b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libipld"
version = "1.0.1"
version = "1.1.0"
edition = "2021"
license = "MIT"
description = "Python binding to the Rust IPLD library"
Expand All @@ -19,3 +19,4 @@ anyhow = "1.0.75"
futures = "0.3"
libipld = { version = "0.16.0", features = ["dag-cbor", "dag-json", "dag-pb", "derive"] }
iroh-car = "0.4.0"
multibase = "0.9"
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
> This project aims to speed up [The AT Protocol SDK](https://github.com/MarshalX/atproto) by using Rust for the heavy lifting. Only atproto related parts are implemented first.
Code snippet:

```python
import libipld

Expand All @@ -13,20 +14,30 @@ print(libipld.decode_cid('bafyreig7jbijxpn4lfhvnvyuwf5u5jyhd7begxwyiqe7ingwxycjd
# Decode a DAG CBOR
print(libipld.decode_dag_cbor(b'\xa2aa\x0cabfhello!\x82\x00\x01'))
# Output: {'a': 12, 'b': 'hello!'}

# multibase
print(libipld.decode_multibase('ueWVzIG1hbmkgIQ'))
# Output: ('u', b'yes mani !')
print(libipld.encode_multibase('u', b'yes mani !'))
# Output: ueWVzIG1hbmkgIQ
```

### Features

- Decode DAG CBOR (`decode_cid(str) -> dict`)
- Decode CID (`decode_dag_cbor(bytes) -> dict`, `decode_dag_cbor_multi(bytes) -> list[dict]`)
- Decode CAR (`decode_car(bytes) -> tuple[dict, dict[str, dict]]`). Returns a header and blocks mapped by CID.
- Decode CAR (`decode_car(bytes) -> tuple[dict, dict[str, dict]]`). Returns a header and blocks mapped by CID.
- Decode Multibase (`decode_multibase(str) -> tuple[str, bytes]`). Returns base and data.
- Encode Multibase (`encode_multibase(str, bytes) -> str`). Accepts base and data.

Note: stub file will be provided in the future.

## Installing

You can install or upgrade `libipld` via

```bash
pip3 install -U libipld
pip install -U libipld
```

### Contributing
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::io::{BufReader, Cursor, Read, Seek};
use pyo3::prelude::*;
use pyo3::conversion::ToPyObject;
use pyo3::{PyObject, Python};
use pyo3::types::{PyBytes};
use anyhow::Result;
use iroh_car::{CarHeader, CarReader};
use futures::{executor, stream::StreamExt};
Expand Down Expand Up @@ -200,11 +201,26 @@ fn decode_cid(data: String) -> PyResult<HashMapItem> {
Ok(cid_to_hashmap(&cid))
}

#[pyfunction]
fn decode_multibase(py: Python, data: String) -> (char, PyObject) {
let (base, data) = multibase::decode(data).unwrap();
(base.code(), PyBytes::new(py, &data).into())
}

#[pyfunction]
fn encode_multibase(code: char, data: Vec<u8>) -> String {
let base = multibase::Base::from_code(code).unwrap();
let encoded = multibase::encode(base, data);
encoded
}

#[pymodule]
fn libipld(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(decode_cid, m)?)?;
m.add_function(wrap_pyfunction!(decode_car, m)?)?;
m.add_function(wrap_pyfunction!(decode_dag_cbor, m)?)?;
m.add_function(wrap_pyfunction!(decode_dag_cbor_multi, m)?)?;
m.add_function(wrap_pyfunction!(decode_multibase, m)?)?;
m.add_function(wrap_pyfunction!(encode_multibase, m)?)?;
Ok(())
}

0 comments on commit a99bc9b

Please sign in to comment.