1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use mina_serialization_types::{json::*, *};
use mina_serialization_types_macros::AutoFrom;
use thiserror::Error;
#[derive(Clone, Default, Eq, PartialEq, Debug, derive_more::From, derive_more::Into, AutoFrom)]
#[auto_from(mina_serialization_types::staged_ledger_diff::SignedCommandMemo)]
#[auto_from(mina_serialization_types::staged_ledger_diff::SignedCommandMemoJson)]
pub struct SignedCommandMemo(pub Vec<u8>);
impl_strconv_via_json!(SignedCommandMemo, SignedCommandMemoJson);
impl SignedCommandMemo {
pub fn try_from_text(s: impl AsRef<[u8]>) -> Result<Self, SignedCommandMemoError> {
const DIGEST_LEN: usize = 32;
const MAX_INPUT_STRING_LENGTH: usize = DIGEST_LEN;
const MEMO_LEN: usize = DIGEST_LEN + 2;
const TAG_INDEX: usize = 0;
const LEN_INDEX: usize = 1;
const BYTES_TAG: u8 = 1;
let s = s.as_ref();
if s.len() > MAX_INPUT_STRING_LENGTH {
return Err(SignedCommandMemoError::StringTooLong);
}
let mut v = vec![0; MEMO_LEN];
v[TAG_INDEX] = BYTES_TAG;
v[LEN_INDEX] = s.len() as u8;
for (i, &b) in s.iter().enumerate() {
v[i + 2] = b;
}
Ok(Self(v))
}
pub fn to_text(&self) -> String {
let len = self.0[1] as usize;
String::from_utf8_lossy(&self.0[2..(2 + len)]).into()
}
}
#[derive(Debug, Error)]
pub enum SignedCommandMemoError {
#[error("Input string is too long")]
StringTooLong,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::*;
#[test]
fn memo_roundtrip() -> anyhow::Result<()> {
let bs58_encoded = "E4Yd7qwaRCHR6t7i6ToM98eSUy5eKKadQUPZX7Vpw4CWBvWyd8fzK";
let text_expected = "FPayment";
let memo_from_bs58 = SignedCommandMemo::from_str(bs58_encoded)?;
let memo_from_text = SignedCommandMemo::try_from_text(text_expected)?;
assert_eq!(memo_from_bs58, memo_from_text);
let memo = memo_from_bs58;
assert_eq!(&memo.to_string(), bs58_encoded);
assert_eq!(&memo.to_text(), text_expected);
Ok(())
}
}