secret_toolkit_notification/
cbor.rs

1use cosmwasm_std::{CanonicalAddr, StdError, StdResult};
2use minicbor::{data as cbor_data, encode as cbor_encode, Encoder};
3
4/// Length of encoding an arry header that holds less than 24 items
5pub const CBL_ARRAY_SHORT: usize = 1;
6
7/// Length of encoding an arry header that holds between 24 and 255 items
8pub const CBL_ARRAY_MEDIUM: usize = 2;
9
10/// Length of encoding an arry header that holds more than 255 items
11pub const CBL_ARRAY_LARGE: usize = 3;
12
13/// Length of encoding a u8 value that is less than 24
14pub const CBL_U8_LESS_THAN_24: usize = 1;
15
16/// Length of encoding a u8 value that is greater than or equal to 24
17pub const CBL_U8: usize = 1 + 1;
18
19/// Length of encoding a u16 value
20pub const CBL_U16: usize = 1 + 2;
21
22/// Length of encoding a u32 value
23pub const CBL_U32: usize = 1 + 4;
24
25/// Length of encoding a u53 value (the maximum safe integer size for javascript)
26pub const CBL_U53: usize = 1 + 8;
27
28/// Length of encoding a u64 value (with the bignum tag attached)
29pub const CBL_BIGNUM_U64: usize = 1 + 1 + 8;
30
31// Length of encoding a timestamp
32pub const CBL_TIMESTAMP: usize = 1 + 1 + 8;
33
34// Length of encoding a 20-byte canonical address
35pub const CBL_ADDRESS: usize = 1 + 20;
36
37/// Wraps the CBOR error to CosmWasm StdError
38pub fn cbor_to_std_error<T>(_e: cbor_encode::Error<T>) -> StdError {
39    StdError::generic_err("CBOR encoding error")
40}
41
42/// Extends the minicbor encoder with wrapper functions that handle CBOR errors
43pub trait EncoderExt {
44    fn ext_tag(&mut self, tag: cbor_data::IanaTag) -> StdResult<&mut Self>;
45
46    fn ext_u8(&mut self, value: u8) -> StdResult<&mut Self>;
47    fn ext_u32(&mut self, value: u32) -> StdResult<&mut Self>;
48    fn ext_u64_from_u128(&mut self, value: u128) -> StdResult<&mut Self>;
49    fn ext_address(&mut self, value: CanonicalAddr) -> StdResult<&mut Self>;
50    fn ext_bytes(&mut self, value: &[u8]) -> StdResult<&mut Self>;
51    fn ext_timestamp(&mut self, value: u64) -> StdResult<&mut Self>;
52}
53
54impl<T: cbor_encode::Write> EncoderExt for Encoder<T> {
55    #[inline]
56    fn ext_tag(&mut self, tag: cbor_data::IanaTag) -> StdResult<&mut Self> {
57        self.tag(cbor_data::Tag::from(tag))
58            .map_err(cbor_to_std_error)
59    }
60
61    #[inline]
62    fn ext_u8(&mut self, value: u8) -> StdResult<&mut Self> {
63        self.u8(value).map_err(cbor_to_std_error)
64    }
65
66    #[inline]
67    fn ext_u32(&mut self, value: u32) -> StdResult<&mut Self> {
68        self.u32(value).map_err(cbor_to_std_error)
69    }
70
71    #[inline]
72    fn ext_u64_from_u128(&mut self, value: u128) -> StdResult<&mut Self> {
73        self.ext_tag(cbor_data::IanaTag::PosBignum)?
74            .ext_bytes(&value.to_be_bytes()[8..])
75    }
76
77    #[inline]
78    fn ext_address(&mut self, value: CanonicalAddr) -> StdResult<&mut Self> {
79        self.ext_bytes(value.as_slice())
80    }
81
82    #[inline]
83    fn ext_bytes(&mut self, value: &[u8]) -> StdResult<&mut Self> {
84        self.bytes(value).map_err(cbor_to_std_error)
85    }
86
87    #[inline]
88    fn ext_timestamp(&mut self, value: u64) -> StdResult<&mut Self> {
89        self.ext_tag(cbor_data::IanaTag::Timestamp)?
90            .u64(value)
91            .map_err(cbor_to_std_error)
92    }
93}