Skip to content

Commit a99bc9b

Browse files
authored
Add multibase encode and decode (#3)
1 parent faaabfd commit a99bc9b

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "libipld"
3-
version = "1.0.1"
3+
version = "1.1.0"
44
edition = "2021"
55
license = "MIT"
66
description = "Python binding to the Rust IPLD library"
@@ -19,3 +19,4 @@ anyhow = "1.0.75"
1919
futures = "0.3"
2020
libipld = { version = "0.16.0", features = ["dag-cbor", "dag-json", "dag-pb", "derive"] }
2121
iroh-car = "0.4.0"
22+
multibase = "0.9"

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
> 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.
44
55
Code snippet:
6+
67
```python
78
import libipld
89

@@ -13,20 +14,30 @@ print(libipld.decode_cid('bafyreig7jbijxpn4lfhvnvyuwf5u5jyhd7begxwyiqe7ingwxycjd
1314
# Decode a DAG CBOR
1415
print(libipld.decode_dag_cbor(b'\xa2aa\x0cabfhello!\x82\x00\x01'))
1516
# Output: {'a': 12, 'b': 'hello!'}
17+
18+
# multibase
19+
print(libipld.decode_multibase('ueWVzIG1hbmkgIQ'))
20+
# Output: ('u', b'yes mani !')
21+
print(libipld.encode_multibase('u', b'yes mani !'))
22+
# Output: ueWVzIG1hbmkgIQ
1623
```
1724

1825
### Features
1926

2027
- Decode DAG CBOR (`decode_cid(str) -> dict`)
2128
- Decode CID (`decode_dag_cbor(bytes) -> dict`, `decode_dag_cbor_multi(bytes) -> list[dict]`)
22-
- Decode CAR (`decode_car(bytes) -> tuple[dict, dict[str, dict]]`). Returns a header and blocks mapped by CID.
29+
- Decode CAR (`decode_car(bytes) -> tuple[dict, dict[str, dict]]`). Returns a header and blocks mapped by CID.
30+
- Decode Multibase (`decode_multibase(str) -> tuple[str, bytes]`). Returns base and data.
31+
- Encode Multibase (`encode_multibase(str, bytes) -> str`). Accepts base and data.
32+
33+
Note: stub file will be provided in the future.
2334

2435
## Installing
2536

2637
You can install or upgrade `libipld` via
2738

2839
```bash
29-
pip3 install -U libipld
40+
pip install -U libipld
3041
```
3142

3243
### Contributing

src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::io::{BufReader, Cursor, Read, Seek};
44
use pyo3::prelude::*;
55
use pyo3::conversion::ToPyObject;
66
use pyo3::{PyObject, Python};
7+
use pyo3::types::{PyBytes};
78
use anyhow::Result;
89
use iroh_car::{CarHeader, CarReader};
910
use futures::{executor, stream::StreamExt};
@@ -200,11 +201,26 @@ fn decode_cid(data: String) -> PyResult<HashMapItem> {
200201
Ok(cid_to_hashmap(&cid))
201202
}
202203

204+
#[pyfunction]
205+
fn decode_multibase(py: Python, data: String) -> (char, PyObject) {
206+
let (base, data) = multibase::decode(data).unwrap();
207+
(base.code(), PyBytes::new(py, &data).into())
208+
}
209+
210+
#[pyfunction]
211+
fn encode_multibase(code: char, data: Vec<u8>) -> String {
212+
let base = multibase::Base::from_code(code).unwrap();
213+
let encoded = multibase::encode(base, data);
214+
encoded
215+
}
216+
203217
#[pymodule]
204218
fn libipld(_py: Python, m: &PyModule) -> PyResult<()> {
205219
m.add_function(wrap_pyfunction!(decode_cid, m)?)?;
206220
m.add_function(wrap_pyfunction!(decode_car, m)?)?;
207221
m.add_function(wrap_pyfunction!(decode_dag_cbor, m)?)?;
208222
m.add_function(wrap_pyfunction!(decode_dag_cbor_multi, m)?)?;
223+
m.add_function(wrap_pyfunction!(decode_multibase, m)?)?;
224+
m.add_function(wrap_pyfunction!(encode_multibase, m)?)?;
209225
Ok(())
210226
}

0 commit comments

Comments
 (0)