From a99bc9b81ae5aca2fe83306b28cbf97c664d9975 Mon Sep 17 00:00:00 2001 From: Ilya Siamionau Date: Wed, 10 Jan 2024 22:54:31 +0100 Subject: [PATCH] Add multibase encode and decode (#3) --- Cargo.toml | 3 ++- README.md | 15 +++++++++++++-- src/lib.rs | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9a98b1e..096b53e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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" diff --git a/README.md b/README.md index 4b549c0..1ed1b83 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 142cb97..cd026ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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}; @@ -200,11 +201,26 @@ fn decode_cid(data: String) -> PyResult { 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) -> 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(()) }