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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! A `DefinitionFields` structure to represent the [`definitions.json`](https://github.com/KeystoneHQ/rippled_binary_codec/blob/main/src/fixtures/definitions.json) JSON data and methods to manipulate the fields.
use std::{cmp::Ordering, collections::BTreeMap, fmt::Debug};
use bytes::{BufMut, Bytes, BytesMut};
use serde::{Serialize, de::DeserializeOwned};
use serde_json::from_str;
use crate::types::{account::Account, amount::Amount, blob::Blob, definition::Definitions, hash::Hash, path_set::PathSet, starray::STArray, stobject::STObject};
/// A trait to be implemented by each field for serialization.
pub trait SerializeField {
fn to_bytes(&self) -> Option<Vec<u8>>;
}
/// A structure of ripple definitions.
pub struct DefinitionFields{
pub definitions: Option<Definitions>
}
impl DefinitionFields {
/// Init a DefinitionFields structure with the [`definitions.json`](https://github.com/KeystoneHQ/rippled_binary_codec/blob/main/src/fixtures/definitions.json) file.
///
/// This [`definitions.json`](https://github.com/KeystoneHQ/rippled_binary_codec/blob/main/src/fixtures/definitions.json) file should be in sync with the [`official definitions`](https://github.com/ripple/ripple-binary-codec/blob/master/src/enums/definitions.json).
///
pub fn new()-> Self{
let definitions_json: &str = include_str!("fixtures/definitions.json");
Self {
definitions: from_str::<Definitions>(definitions_json).ok()
}
}
///Return a tuple sort key for a given field name.
///
/// **tuple sort key**: (type_order, field_order)
///
/// Where `type_order` and `field_order` are parsed from [`definitions.json`](https://github.com/KeystoneHQ/rippled_binary_codec/blob/main/src/fixtures/definitions.json).
///
/// For example, in [`definitions.json`](https://github.com/KeystoneHQ/rippled_binary_codec/blob/main/src/fixtures/definitions.json), it defines:
///
///```json
/// {
/// "TYPES": {
/// ...
/// "AccountID": 8
/// ...
/// },
/// "FIELDS": {
/// ...
/// "Account": [
/// {
/// "nth": 1,
/// "isVLEncoded": true,
/// "isSerialized": true,
/// "isSigningField": true,
/// "type": "AccountID"
/// }
/// ]
/// ...
/// }
/// }
///```
/// then the [`get_field_sort_key`()] for `Account` field should return (8,1), where 8 is `TYPES["AccountID"]` , `1` is `FIELDS["Account"]["nth"]`.
///
/// [`get_field_sort_key`()]: https://docs.rs/rippled_binary_codec/0.0.2/rippled_binary_codec/definition_fields/struct.DefinitionFields.html#method.get_field_sort_key
///
/// # Example
///
///```
///use rippled_binary_codec::definition_fields::DefinitionFields;
///
///fn get_field_sort_key_example(){
/// let fields = DefinitionFields::new();
/// let account_sort_key = fields.get_field_sort_key("Account".to_string());
/// println!("account_sort_key: {:?}", account_sort_key); // (8,1)
///}
///```
///
/// # Errors
/// If it fails to get the `type_order` or `field_order`, `(-1,-1)` will be returned.
pub fn get_field_sort_key(&self, field_name: String)-> (i32, i32){
match self.definitions.clone() {
Some(definitions)=>{
if let Some(field_type_name) = definitions.fields.get(&field_name).and_then(|f| Some(f.to_owned().type_name)){
if let Some(type_sort_key) = definitions.types.get(&field_type_name).to_owned(){
if let Some(field_sort_key) = definitions.fields.get(&field_name).and_then(|f| Some(f.to_owned().nth)){
return (type_sort_key.to_owned(), field_sort_key)
}
}
}
return (-1,-1);
},
_=> {
return (-1,-1);
}
}
}
/// Ordering the input fields by it's sort key.
///
/// # Example
///
///```
///use rippled_binary_codec::definition_fields::DefinitionFields;
///
///fn ordering_fields_example() {
/// let fields = DefinitionFields::new();
/// let before_sort: Vec<String> = vec!["Account", "Expiration", "Fee", "Flags", "OfferSequence"].into_iter().map(String::from).collect();
/// let sorted: Vec<String> = fields.ordering_fields(before_sort);
/// println!("sorted field: {:?}", sorted); // ["Flags", "Expiration", "OfferSequence", "Fee", "Account"]
///}
///```
pub fn ordering_fields(&self, fields: Vec<String>)-> Vec<String>{
let mut sort_key: Vec<(i32, i32)> = Vec::new();
let mut keys = fields.to_owned();
for key in keys.clone() {
let field = self.get_field_sort_key(key);
sort_key.push(field);
}
keys.sort_by(|a, b| {
let a_sort_key = self.get_field_sort_key(a.to_string());
let b_sort_key = self.get_field_sort_key(b.to_string());
match a_sort_key.0.cmp(&b_sort_key.0) {
Ordering::Equal => a_sort_key.1.cmp(&b_sort_key.1),
other => other,
}
});
return keys
}
/// Get the value of field in data.
///
/// # Example
///
///```
///use serde_json::Value;
///use serde_json::json;
///use rippled_binary_codec::definition_fields::DefinitionFields;
///
///fn get_account_example(){
/// let fields = DefinitionFields::new();
/// let input= json!({
/// "Account": "rMBzp8CgpE441cp5PVyA9rpVV7oT8hP3ys",
/// "Expiration": 595640108
/// });
/// let output: Value = fields.get_field_by_name(input, "Account").unwrap();
/// println!("account: {:?}", output.as_str().unwrap()); // "rMBzp8CgpE441cp5PVyA9rpVV7oT8hP3ys"
///}
///```
///
/// # Errors
/// If the field is failed to get, `None` will be returned.
pub fn get_field_by_name<T, R>(&self, data: T, field: &str) -> Option<R>
where
T: Serialize + Debug,
R: DeserializeOwned,
{
let mut map = match serde_value::to_value(data) {
Ok(serde_value::Value::Map(map)) => map,
_ => {
return None;
},
};
let key = serde_value::Value::String(field.to_owned());
let value = map.remove(&key)?;
return R::deserialize(value).ok();
}
///
/// # Example
///
///```
///use rippled_binary_codec::definition_fields::DefinitionFields;
///
///fn get_definition_field_example(){
/// let fields = DefinitionFields::new();
/// let type_name: String = fields.get_definition_field("TransactionType".to_string(), "type").unwrap();
/// let is_signing_field: bool = fields.get_definition_field("TransactionType".to_string(), "isSigningField").unwrap();
/// println!("type_name: {}", type_name); // "UInt16"
/// println!("is_signing_field: {}", is_signing_field); // true
///}
///```
///
/// # Errors
/// If the `field_name` is not in [`definitions.json`] or `key` is not in the [`DefinitionField`][`crate::types::definition::DefinitionField`], `None` will be returned.
pub fn get_definition_field<R>(&self, field_name: String, key: &str) -> Option<R>
where
R: DeserializeOwned,
{
let definitions = self.definitions.as_ref()?;
let fields: BTreeMap<serde_value::Value,serde_value::Value> = self.get_field_by_name(definitions.to_owned(),"FIELDS")?;
let field: BTreeMap<serde_value::Value, serde_value::Value> = self.get_field_by_name(fields, field_name.as_str())?;
return self.get_field_by_name(field, key)?;
}
fn cal_field_id(&self, field_code: i32, type_code: i32) -> Bytes {
let mut buf = BytesMut::with_capacity(3);
if type_code < 16 && field_code < 16 {
let combined_code = (type_code << 4) | field_code;
buf.put_u8(combined_code.to_be_bytes()[3]);
} else if type_code >= 16 && field_code < 16 {
buf.put_u8(field_code.to_be_bytes()[3]);
buf.put_u8(type_code.to_be_bytes()[3]);
} else if type_code < 16 && field_code >= 16 {
let type_code = type_code << 4;
buf.put_u8(type_code.to_be_bytes()[3]);
buf.put_u8(field_code.to_be_bytes()[3]);
}else{
buf.put_u8(0x00);
buf.put_u8(type_code.to_be_bytes()[3]);
buf.put_u8(field_code.to_be_bytes()[3]);
}
return buf.freeze();
}
/// Return the unique field id for a given field name, this field id consists of the type code ant field code, in 1 to 3 bytes
/// depending on whether those values are "common"(<16) or "uncommon"<>=16>.
pub fn get_field_id(&self, field_name: String) -> Option<Bytes>{
let definitions = self.definitions.as_ref()?;
let field_type: String = self.get_definition_field(field_name.clone(), "type")?;
let field_code = self.get_definition_field(field_name, "nth")?;
let types: BTreeMap<serde_value::Value,serde_value::Value> = self.get_field_by_name(definitions.to_owned(), "TYPES")?;
let type_code: i32 = self.get_field_by_name(types, &field_type)?;
return Some(self.cal_field_id(field_code, type_code));
}
/// Return a bytes object containing the serialized version of a field,
/// including it's field id prefix. `id_prefix` is generated by [`get_field_id()`],
/// `fields` are serialized with specific logic:
///
/// [`get_field_id()`]: https://docs.rs/rippled_binary_codec/0.0.1/rippled_binary_codec/definition_fields/struct.DefinitionFields.html#method.get_field_id
/// - [`Account`][`crate::types::account::Account`] for serializing **AccountID** type of field.
/// - [`Amount`][`crate::types::amount::Amount`] for serializing **Amount** type of field.
/// - [`Blob`][`crate::types::blob::Blob`] for serializing **Blob** type of field.
/// - [`Hash`][`crate::types::hash::Hash`] for serializing **Hash128**,**Hash160**,**Hash256** type of field.
/// - [`PathSet`][`crate::types::path_set::PathSet`] for serializing **PathSet** type of field.
/// - [`STArray`][`crate::types::starray::STArray`] for serializing **STArray** type of field.
/// - [`STObject`][`crate::types::stobject::STObject`] for serializing **STObject** type of field.
/// - [`to_be_bytes()`] for serializing **UInt8**, **UInt16**, **UInt32** type of field and slice to specific length.
///
/// [`to_be_bytes()`]: https://doc.rust-lang.org/std/primitive.u64.html#method.to_be_bytes
///
/// # Example
///
///```
///use serde_json::Value;
///use rippled_binary_codec::definition_fields::DefinitionFields;
///
///fn field_to_bytes(){
/// let fields = DefinitionFields::new();
/// let bytes: Vec<u8> = fields.field_to_bytes("Expiration".to_string(),Value::from(595640108)).unwrap();
/// println!("Serialized expiration: {:?}", bytes); // [42, 35, 128, 191, 44]
///}
///
///```
/// # Errors
/// If the field is failed to serialize, `None` will be returned.
pub fn field_to_bytes(&self, field_name: String, field_val: serde_json::Value) -> Option<Vec<u8>> {
let field_type : String = self.get_definition_field(field_name.clone(), "type")?;
let id_prefix: Bytes = self.get_field_id(field_name.clone())?;
let mut buf = BytesMut::with_capacity(1024);
let definitions = self.definitions.as_ref()?;
if field_name == "TransactionType".to_string() {
buf.extend_from_slice(&id_prefix);
let types: BTreeMap<serde_value::Value,serde_value::Value> = self.get_field_by_name(definitions.to_owned(), "TRANSACTION_TYPES")?;
let field_val =field_val.as_str()?;
let type_unit: u16 = self.get_field_by_name::<BTreeMap<serde_value::Value,serde_value::Value> ,u16>(types, &field_val)?;
buf.put_u16(type_unit);
return Some(buf.to_vec());
}
let slice: Vec<u8> = match field_type.as_str() {
"AccountID" => {
Account{data: field_val}.to_bytes()
},
"Amount" =>{
Amount{data: field_val}.to_bytes()
},
"Blob" =>{
Blob{data: field_val}.to_bytes()
},
"Hash128"=>{
Hash{
data: field_val,
len: 16
}.to_bytes()
},
"Hash160"=>{
Hash{
data: field_val,
len: 20
}.to_bytes()
},
"Hash256"=>{
Hash{
data: field_val,
len: 32
}.to_bytes()
},
"PathSet"=>{
PathSet {data: field_val}.to_bytes()
},
"STArray"=>{
STArray {data: field_val}.to_bytes()
},
"STObject"=>{
STObject{data: field_val}.to_bytes()
},
"UInt8"=>{
let input: u64 = field_val.as_u64()?;
let len = input.to_be_bytes().len();
Some(input.to_be_bytes()[len-1..].to_vec())
},
"UInt16"=>{
let input: u64 = field_val.as_u64()?;
let len = input.to_be_bytes().len();
Some(input.to_be_bytes()[len-2..].to_vec())
},
"UInt32"=>{
let input: u64 = field_val.as_u64()?;
let len = input.to_be_bytes().len();
Some(input.to_be_bytes()[len-4..].to_vec())
}
_ => {
None
}
}?;
buf.extend_from_slice(&id_prefix);
buf.extend_from_slice(&slice);
return Some(buf.to_vec());
}
}
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use serde_json::{Value, json};
use crate::types::definition::DefinitionField;
use super::*;
#[test]
fn test_ordering_fields() {
let fields = DefinitionFields::new();
let before_sort: Vec<String> = vec!["Account", "Expiration", "Fee", "Flags", "OfferSequence", "Sequence", "SigningPubKey", "TakerGets", "TakerPays", "TransactionType", "TxnSignature", "hash"].into_iter().map(String::from).collect();
let after_sort: Vec<String> = fields.ordering_fields(before_sort);
let expected: Vec<String> = vec!["TransactionType", "Flags", "Sequence", "Expiration", "OfferSequence", "hash", "TakerPays", "TakerGets", "Fee", "SigningPubKey", "TxnSignature", "Account"].into_iter().map(String::from).collect();
assert_eq!(after_sort, expected);
}
#[test]
fn test_get_field_sort_key(){
let fields = DefinitionFields::new();
let account_sort_key = fields.get_field_sort_key("Account".to_string());
assert_eq!(account_sort_key,(8,1));
}
#[test]
fn test_field_to_bytes(){
let fields = DefinitionFields::new();
let expiration: Vec<u8> = fields.field_to_bytes("Expiration".to_string(),Value::from(595640108)).unwrap();
assert_eq!(expiration, [42, 35, 128, 191, 44]);
}
#[test]
fn test_get_field_by_name(){
let fields = DefinitionFields::new();
let input= json!({
"Account": "rMBzp8CgpE441cp5PVyA9rpVV7oT8hP3ys",
"Expiration": 595640108
});
let account: Value = fields.get_field_by_name(input.to_owned(), "Account").unwrap();
let expected = "rMBzp8CgpE441cp5PVyA9rpVV7oT8hP3ys";
assert_eq!(account.as_str().unwrap(),expected);
}
#[test]
fn test_load_def() {
let definitions = DefinitionFields::new().definitions.unwrap();
assert_eq!(definitions.types.len(),20);
assert_eq!(definitions.transaction_types.len(),31);
assert_eq!(definitions.transaction_results.len(),127);
let generic_field = DefinitionField {
nth: 0,
is_signing_field: false,
is_serialized: false,
is_vl_encoded: false,
type_name: String::from("Unknown")
};
assert_eq!(definitions.fields.get("Generic"),Some(&generic_field));
}
#[test]
fn test_get_definition_field(){
let fields = DefinitionFields::new();
let type_name: String = fields.get_definition_field("TransactionType".to_string(), "type").unwrap();
let is_vl_encoded: bool = fields.get_definition_field("TransactionType".to_string(), "isVLEncoded").unwrap();
let is_serialized: bool = fields.get_definition_field("TransactionType".to_string(), "isSerialized").unwrap();
let is_signing_field: bool = fields.get_definition_field("TransactionType".to_string(), "isSigningField").unwrap();
assert_eq!(type_name, "UInt16".to_string());
assert_eq!(is_vl_encoded, false);
assert_eq!(is_serialized, true);
assert_eq!(is_signing_field, true);
}
#[test]
fn test_get_field_id() {
let fields = DefinitionFields::new();
let keys: Vec<String> = vec!["TransactionType", "Flags", "Sequence", "Expiration", "OfferSequence", "hash", "TakerPays", "TakerGets", "Fee", "SigningPubKey", "TxnSignature", "Account"].into_iter().map(String::from).collect();
let mut result: HashMap<String, Bytes> = HashMap::new();
for key in keys {
let id_prefix= fields.get_field_id(key.clone());
result.insert(key, id_prefix.unwrap());
}
assert_eq!(result.get("TransactionType").unwrap().slice(..), b"\x12"[..]);
assert_eq!(result.get("Flags").unwrap().slice(..), b"\x22"[..]);
assert_eq!(result.get("Sequence").unwrap().slice(..), b"\x24"[..]);
assert_eq!(result.get("Expiration").unwrap().slice(..), b"\x2a"[..]);
assert_eq!(result.get("OfferSequence").unwrap().slice(..), b" \x19"[..]);
assert_eq!(result.get("TakerPays").unwrap().slice(..), b"\x64"[..]);
assert_eq!(result.get("TakerGets").unwrap().slice(..), b"\x65"[..]);
assert_eq!(result.get("Fee").unwrap().slice(..), b"\x68"[..]);
assert_eq!(result.get("SigningPubKey").unwrap().slice(..), b"\x73"[..]);
assert_eq!(result.get("TxnSignature").unwrap().slice(..), b"\x74"[..]);
assert_eq!(result.get("Account").unwrap().slice(..), b"\x81"[..]);
}
}