1use std::str::FromStr;
2
3use super::error::{Error, Result};
4use bs58::Alphabet;
5use rust_decimal::{prelude::ToPrimitive, Decimal, MathematicalOps};
6use serde::{ser, Serialize};
7
8pub fn encode_variable_length(length: usize) -> Vec<u8> {
9 let mut len_bytes = [0u8; 3];
10 if length <= 192 {
11 return vec![length as u8];
12 } else if length <= 12480 {
13 let length_a = length - 193;
14 len_bytes[0] = 193 + (length_a >> 8usize) as u8;
15 len_bytes[1] = (length_a & 0xff) as u8;
16 return vec![len_bytes[0], len_bytes[1]];
17 } else if length <= 918744 {
18 let length_a = length - 12481;
19 len_bytes[0] = 241 + (length_a >> 16usize) as u8;
20 len_bytes[1] = ((length_a >> 8isize) & 0xff) as u8;
21 len_bytes[2] = (length_a & 0xff) as u8;
22 return vec![len_bytes[0], len_bytes[1], len_bytes[2]];
23 }
24 panic!("overflow error");
25}
26
27pub fn encode_field_id(type_code: u8, field_code: u8) -> Vec<u8> {
28 if type_code < 16 && field_code < 16 {
29 let field_id = type_code << 4 | field_code;
30 return vec![field_id as u8];
31 } else if type_code < 16 && field_code >= 16 {
32 let field_id = type_code << 4;
33 return vec![field_id as u8, field_code as u8];
34 } else if type_code >= 16 && field_code < 16 {
35 return vec![field_code as u8, type_code as u8];
36 }
37 vec![0u8, type_code, field_code]
38}
39
40pub const XRPL_ALPHABET: Alphabet = *bs58::Alphabet::RIPPLE;
41
42pub fn decode_base58(b58_string: &str, prefix: &[u8]) -> Result<Vec<u8>> {
43 let prefix_len = prefix.len();
44 let decoded = bs58::decode(b58_string)
45 .with_alphabet(&XRPL_ALPHABET)
46 .with_check(None)
47 .into_vec()
48 .map_err(|e| Error::InvalidAddress)?;
49 if &decoded[..prefix_len] != prefix {
50 Err(Error::InvalidAddress)
51 } else {
52 Ok(decoded[prefix_len..].to_vec())
53 }
54}
55
56pub fn encode_currency_code(currency_code: &str) -> Vec<u8> {
57 if currency_code.as_bytes().len() == 3 {
58 return [
59 [0u8; 12].to_vec(),
60 currency_code.as_bytes().to_vec(),
61 [0u8; 5].to_vec(),
62 ]
63 .concat();
64 }
65 if currency_code.as_bytes().len() == 40 {
66 return hex::decode(¤cy_code).unwrap()
67 }
68 panic!("invalid currency code with length: {}", currency_code.as_bytes().len())
69}
70
71pub fn encode_issued_currency_amount(
72 amount: &str,
73 currency: &str,
74 issuer: &str,
75) -> Result<Vec<u8>> {
76 let encoded_address = decode_base58(issuer, &[0x00])?;
77
78 let mut decimal_amount = Decimal::from_str(amount)
79 .map_err(|e| Error::InvalidIssuedCurrencyAmount(format!("{:?}", e)))?;
80
81 let mut encoded_amount;
82
83 if decimal_amount.is_zero() {
84 encoded_amount = [0u8; 8];
85 encoded_amount[0] |= 0x80;
86 } else {
87 let e = decimal_amount.log10().floor().to_u32().unwrap();
89 decimal_amount.rescale(15 - e);
90 encoded_amount = decimal_amount.mantissa().to_u64().unwrap().to_be_bytes();
91 encoded_amount[0] |= 0x80;
92 if decimal_amount.is_sign_positive() {
93 encoded_amount[0] |= 0x40;
94 }
95 let exponent = e as i32 - 15;
96 let exponent_bytes = (97 + exponent).to_u8().unwrap();
97 encoded_amount[0] |= exponent_bytes >> 2u8;
98 encoded_amount[1] |= (exponent_bytes & 0x03) << 6u8;
99 }
100
101 let encoded_currency = encode_currency_code(currency);
102
103 Ok([encoded_amount.to_vec(), encoded_currency, encoded_address]
104 .concat()
105 .to_vec())
106}
107
108#[derive(Default)]
109pub struct StringSerializer {
110 pub value: Option<String>,
111}
112
113impl<'a> ser::Serializer for &'a mut StringSerializer {
114 type Ok = ();
121
122 type Error = Error;
124
125 type SerializeSeq = Self;
130 type SerializeTuple = Self;
131 type SerializeTupleStruct = Self;
132 type SerializeTupleVariant = Self;
133 type SerializeMap = Self;
134 type SerializeStruct = Self;
135 type SerializeStructVariant = Self;
136
137 fn serialize_bool(self, v: bool) -> Result<()> {
141 unimplemented!()
142 }
143
144 fn serialize_i8(self, v: i8) -> Result<()> {
149 unimplemented!()
150 }
151
152 fn serialize_i16(self, v: i16) -> Result<()> {
153 unimplemented!()
154 }
155
156 fn serialize_i32(self, v: i32) -> Result<()> {
157 unimplemented!()
158 }
159
160 fn serialize_i64(self, v: i64) -> Result<()> {
163 unimplemented!()
164 }
165
166 fn serialize_u8(self, v: u8) -> Result<()> {
167 unimplemented!()
168 }
169
170 fn serialize_u16(self, v: u16) -> Result<()> {
171 unimplemented!()
172 }
173
174 fn serialize_u32(self, v: u32) -> Result<()> {
175 unimplemented!()
176 }
177
178 fn serialize_u64(self, v: u64) -> Result<()> {
179 unimplemented!()
180 }
181
182 fn serialize_f32(self, v: f32) -> Result<()> {
183 unimplemented!()
184 }
185
186 fn serialize_f64(self, v: f64) -> Result<()> {
187 unimplemented!()
188 }
189
190 fn serialize_char(self, v: char) -> Result<()> {
193 unimplemented!()
194 }
195
196 fn serialize_str(self, v: &str) -> Result<()> {
200 self.value = Some(v.to_owned());
201 Ok(())
202 }
203
204 fn serialize_bytes(self, v: &[u8]) -> Result<()> {
208 unimplemented!()
209 }
210
211 fn serialize_none(self) -> Result<()> {
213 unimplemented!()
214 }
215
216 fn serialize_some<T>(self, value: &T) -> Result<()>
222 where
223 T: ?Sized + Serialize,
224 {
225 unimplemented!()
226 }
227
228 fn serialize_unit(self) -> Result<()> {
231 unimplemented!()
232 }
233
234 fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
238 unimplemented!()
239 }
240
241 fn serialize_unit_variant(
246 self,
247 _name: &'static str,
248 _variant_index: u32,
249 variant: &'static str,
250 ) -> Result<()> {
251 unimplemented!()
252 }
253
254 fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<()>
257 where
258 T: ?Sized + Serialize,
259 {
260 unimplemented!()
261 }
262
263 fn serialize_newtype_variant<T>(
269 self,
270 _name: &'static str,
271 _variant_index: u32,
272 variant: &'static str,
273 value: &T,
274 ) -> Result<()>
275 where
276 T: ?Sized + Serialize,
277 {
278 unimplemented!()
279 }
280
281 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
292 unimplemented!()
293 }
294
295 fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
300 unimplemented!()
301 }
302
303 fn serialize_tuple_struct(
305 self,
306 _name: &'static str,
307 len: usize,
308 ) -> Result<Self::SerializeTupleStruct> {
309 unimplemented!()
310 }
311
312 fn serialize_tuple_variant(
315 self,
316 _name: &'static str,
317 _variant_index: u32,
318 variant: &'static str,
319 _len: usize,
320 ) -> Result<Self::SerializeTupleVariant> {
321 unimplemented!()
322 }
323
324 fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
326 unimplemented!()
327 }
328
329 fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
335 unimplemented!()
336 }
337
338 fn serialize_struct_variant(
341 self,
342 _name: &'static str,
343 _variant_index: u32,
344 variant: &'static str,
345 _len: usize,
346 ) -> Result<Self::SerializeStructVariant> {
347 unimplemented!()
348 }
349}
350
351impl<'a> ser::SerializeSeq for &'a mut StringSerializer {
359 type Ok = ();
361 type Error = Error;
363
364 fn serialize_element<T>(&mut self, value: &T) -> Result<()>
366 where
367 T: ?Sized + Serialize,
368 {
369 unimplemented!()
370 }
371
372 fn end(self) -> Result<()> {
374 unimplemented!()
375 }
376}
377
378impl<'a> ser::SerializeTuple for &'a mut StringSerializer {
380 type Ok = ();
381 type Error = Error;
382
383 fn serialize_element<T>(&mut self, value: &T) -> Result<()>
384 where
385 T: ?Sized + Serialize,
386 {
387 unimplemented!()
388 }
389
390 fn end(self) -> Result<()> {
391 unimplemented!()
392 }
393}
394
395impl<'a> ser::SerializeTupleStruct for &'a mut StringSerializer {
397 type Ok = ();
398 type Error = Error;
399
400 fn serialize_field<T>(&mut self, value: &T) -> Result<()>
401 where
402 T: ?Sized + Serialize,
403 {
404 unimplemented!()
405 }
406
407 fn end(self) -> Result<()> {
408 unimplemented!()
409 }
410}
411
412impl<'a> ser::SerializeTupleVariant for &'a mut StringSerializer {
413 type Ok = ();
414 type Error = Error;
415
416 fn serialize_field<T>(&mut self, value: &T) -> Result<()>
417 where
418 T: ?Sized + Serialize,
419 {
420 unimplemented!()
421 }
422
423 fn end(self) -> Result<()> {
424 unimplemented!()
425 }
426}
427
428impl<'a> ser::SerializeMap for &'a mut StringSerializer {
437 type Ok = ();
438 type Error = Error;
439
440 fn serialize_key<T>(&mut self, key: &T) -> Result<()>
449 where
450 T: ?Sized + Serialize,
451 {
452 unimplemented!()
453 }
454
455 fn serialize_value<T>(&mut self, value: &T) -> Result<()>
459 where
460 T: ?Sized + Serialize,
461 {
462 unimplemented!()
463 }
464
465 fn end(self) -> Result<()> {
466 unimplemented!()
467 }
468}
469
470impl<'a> ser::SerializeStruct for &'a mut StringSerializer {
473 type Ok = ();
474 type Error = Error;
475
476 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
477 where
478 T: ?Sized + Serialize,
479 {
480 unimplemented!()
481 }
482
483 fn end(self) -> Result<()> {
484 unimplemented!()
485 }
486}
487
488impl<'a> ser::SerializeStructVariant for &'a mut StringSerializer {
491 type Ok = ();
492 type Error = Error;
493
494 fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<()>
495 where
496 T: ?Sized + Serialize,
497 {
498 unimplemented!()
499 }
500
501 fn end(self) -> Result<()> {
502 unimplemented!()
503 }
504}