From 1e7c2bc07594143bc18e9f970dda704452a14fe4 Mon Sep 17 00:00:00 2001 From: Arnaud Mimart <33665250+amimart@users.noreply.github.com> Date: Tue, 28 Feb 2023 13:55:06 +0100 Subject: [PATCH] feat(storage): add cursor bs58 encoding helpers --- contracts/cw-storage/src/cursor.rs | 15 +++++++++++++++ contracts/cw-storage/src/lib.rs | 1 + 2 files changed, 16 insertions(+) create mode 100644 contracts/cw-storage/src/cursor.rs diff --git a/contracts/cw-storage/src/cursor.rs b/contracts/cw-storage/src/cursor.rs new file mode 100644 index 00000000..6764f1ad --- /dev/null +++ b/contracts/cw-storage/src/cursor.rs @@ -0,0 +1,15 @@ +use crate::msg::{Cursor, ObjectId}; +use crate::ContractError; +use cosmwasm_std::{StdError, StdResult}; + +pub fn encode(id: ObjectId) -> Cursor { + bs58::encode(id).into_string() +} + +pub fn decode(cursor: String) -> StdResult { + let raw = bs58::decode(cursor) + .into_vec() + .map_err(|err| StdError::parse_err("Cursor", err))?; + + String::from_utf8(raw).map_err(|err| StdError::parse_err("Cursor", err)) +} diff --git a/contracts/cw-storage/src/lib.rs b/contracts/cw-storage/src/lib.rs index d838ea40..6d87cde9 100644 --- a/contracts/cw-storage/src/lib.rs +++ b/contracts/cw-storage/src/lib.rs @@ -1,5 +1,6 @@ pub mod contract; pub mod crypto; +mod cursor; mod error; pub mod msg; pub mod state;