-
Notifications
You must be signed in to change notification settings - Fork 8
/
mod.rs
740 lines (678 loc) · 21.9 KB
/
mod.rs
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
//! This module contains commonly-used constants.
pub mod exceptions;
#[cfg(test)]
pub mod test_cases;
pub mod utils;
use crate::constants::CryptoAlgorithm;
use crate::core::addresscodec::exceptions::XRPLAddressCodecException;
use crate::core::addresscodec::utils::*;
use crate::skip_err;
use alloc::string::String;
use alloc::vec::Vec;
use core::convert::TryInto;
use strum::IntoEnumIterator;
/// Map the algorithm to the prefix.
fn _algorithm_to_prefix<'a>(algo: &CryptoAlgorithm) -> &'a [u8] {
match algo {
CryptoAlgorithm::ED25519 => &ED25519_SEED_PREFIX,
CryptoAlgorithm::SECP256K1 => &FAMILY_SEED_PREFIX,
}
}
/// Returns whether a decoded X-Address is a test address.
fn _is_test_address(prefix: &[u8]) -> Result<bool, XRPLAddressCodecException> {
if ADDRESS_PREFIX_BYTES_MAIN == prefix {
Ok(false)
} else if ADDRESS_PREFIX_BYTES_TEST == prefix {
Ok(true)
} else {
Err(XRPLAddressCodecException::InvalidXAddressPrefix)
}
}
/// Returns the destination tag extracted from the suffix
/// of the X-Address.
fn _get_tag_from_buffer(buffer: &[u8]) -> Result<Option<u64>, XRPLAddressCodecException> {
let flag = &buffer[0];
if flag >= &2 {
Err(XRPLAddressCodecException::UnsupportedXAddress)
} else if flag == &1 {
// Little-endian to big-endian
Ok(Some(
buffer[1] as u64
+ buffer[2] as u64 * 0x100
+ buffer[3] as u64 * 0x10000
+ buffer[4] as u64 * 0x1000000,
))
// inverse of what happens in encode
} else if flag != &0 {
Err(XRPLAddressCodecException::InvalidXAddressZeroNoTag)
} else if hex::decode("0000000000000000")? != buffer[1..9] {
Err(XRPLAddressCodecException::InvalidXAddressZeroRemain)
} else {
Ok(None)
}
}
/// Returns an encoded seed.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use xrpl::core::addresscodec::encode_seed;
/// use xrpl::core::addresscodec::exceptions::XRPLAddressCodecException;
/// use xrpl::constants::CryptoAlgorithm;
/// use xrpl::core::addresscodec::utils::SEED_LENGTH;
///
/// let entropy: [u8; SEED_LENGTH] = [
/// 207, 45, 227, 120, 251, 221, 126, 46, 232,
/// 125, 72, 109, 251, 90, 123, 255
/// ];
/// let encoding_type: CryptoAlgorithm = CryptoAlgorithm::SECP256K1;
/// let seed: String = "sn259rEFXrQrWyx3Q7XneWcwV6dfL".into();
///
/// let encoding: Option<String> = match encode_seed(
/// entropy,
/// encoding_type,
/// ) {
/// Ok(seed) => Some(seed),
/// Err(e) => match e {
/// XRPLAddressCodecException::UnknownSeedEncoding => None,
/// _ => None,
/// }
/// };
///
/// assert_eq!(Some(seed), encoding);
/// ```
pub fn encode_seed(
entropy: [u8; SEED_LENGTH],
encoding_type: CryptoAlgorithm,
) -> Result<String, XRPLAddressCodecException> {
encode_base58(
&entropy,
_algorithm_to_prefix(&encoding_type),
Some(SEED_LENGTH),
)
}
/// Returns an encoded seed.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use xrpl::core::addresscodec::decode_seed;
/// use xrpl::core::addresscodec::exceptions::XRPLAddressCodecException;
/// use xrpl::core::addresscodec::utils::SEED_LENGTH;
/// use xrpl::constants::CryptoAlgorithm;
/// extern crate alloc;
/// use alloc::vec;
///
/// let seed: &str = "sn259rEFXrQrWyx3Q7XneWcwV6dfL";
/// let tuple: ([u8; SEED_LENGTH], CryptoAlgorithm) = (
/// [207, 45, 227, 120, 251, 221, 126, 46, 232, 125, 72, 109, 251, 90, 123, 255],
/// CryptoAlgorithm::SECP256K1,
/// );
///
/// let decoding: Option<([u8; SEED_LENGTH], CryptoAlgorithm)> = match decode_seed(seed) {
/// Ok((bytes, algorithm)) => Some((bytes, algorithm)),
/// Err(e) => match e {
/// XRPLAddressCodecException::UnknownSeedEncoding => None,
/// _ => None,
/// }
/// };
///
/// assert_eq!(Some(tuple), decoding);
/// ```
pub fn decode_seed(
seed: &str,
) -> Result<([u8; SEED_LENGTH], CryptoAlgorithm), XRPLAddressCodecException> {
let mut result: Option<Result<Vec<u8>, XRPLAddressCodecException>> = None;
let mut algo: Option<CryptoAlgorithm> = None;
for a in CryptoAlgorithm::iter() {
let decode = decode_base58(seed, _algorithm_to_prefix(&a));
result = Some(skip_err!(decode));
algo = Some(a);
}
match result {
Some(Ok(val)) => {
let decoded: [u8; SEED_LENGTH] = val.try_into()?;
Ok((decoded, algo.expect("decode_seed")))
}
Some(Err(_)) | None => Err(XRPLAddressCodecException::UnknownSeedEncoding),
}
}
/// Returns the X-Address representation of the data.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use xrpl::core::addresscodec::classic_address_to_xaddress;
/// use xrpl::core::addresscodec::exceptions::XRPLAddressCodecException;
///
/// let classic_address: &str = "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59".into();
/// let tag: Option<u64> = None;
/// let is_test_network: bool = false;
/// let xaddress: String = "X7AcgcsBL6XDcUb289X4mJ8djcdyKaB5hJDWMArnXr61cqZ".into();
///
/// let conversion: Option<String> = match classic_address_to_xaddress(
/// classic_address,
/// tag,
/// is_test_network
/// ) {
/// Ok(address) => Some(address),
/// Err(e) => match e {
/// XRPLAddressCodecException::InvalidXAddressPrefix => None,
/// XRPLAddressCodecException::UnsupportedXAddress => None,
/// XRPLAddressCodecException::InvalidXAddressZeroNoTag => None,
/// XRPLAddressCodecException::InvalidXAddressZeroRemain => None,
/// XRPLAddressCodecException::UnexpectedPayloadLength {
/// expected: _,
/// found: _,
/// } => None,
/// _ => None,
/// }
/// };
///
/// assert_eq!(Some(xaddress), conversion);
/// ```
pub fn classic_address_to_xaddress(
classic_address: &str,
tag: Option<u64>,
is_test_network: bool,
) -> Result<String, XRPLAddressCodecException> {
let classic_address_bytes = decode_classic_address(classic_address)?;
let flag: bool = tag.is_some();
let tag_val: u64;
if classic_address_bytes.len() != CLASSIC_ADDRESS_ID_LENGTH {
Err(XRPLAddressCodecException::InvalidCAddressIdLength {
length: CLASSIC_ADDRESS_ID_LENGTH,
})
} else if tag.is_some() && tag > Some(u32::max_value().into()) {
Err(XRPLAddressCodecException::InvalidCAddressTag)
} else {
if let Some(tval) = tag {
tag_val = tval;
} else {
tag_val = 0;
}
let mut bytestring = match is_test_network {
true => ADDRESS_PREFIX_BYTES_TEST,
false => ADDRESS_PREFIX_BYTES_MAIN,
}
.to_vec();
bytestring.extend_from_slice(&classic_address_bytes);
let encoded_tag = [
flag as u8,
(tag_val & 0xFF) as u8,
(tag_val >> 8 & 0xFF) as u8,
(tag_val >> 16 & 0xFF) as u8,
(tag_val >> 24 & 0xFF) as u8,
0,
0,
0,
0,
];
bytestring.extend_from_slice(&encoded_tag);
Ok(bs58::encode(bytestring)
.with_alphabet(&XRPL_ALPHABET)
.with_check()
.into_string())
}
}
/// Returns a tuple containing the classic address, tag,
/// and whether the address is on a test network for an
/// X-Address.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use xrpl::core::addresscodec::xaddress_to_classic_address;
/// use xrpl::core::addresscodec::exceptions::XRPLAddressCodecException;
///
/// let xaddress: &str = "X7AcgcsBL6XDcUb289X4mJ8djcdyKaB5hJDWMArnXr61cqZ";
/// let classic: (String, Option<u64>, bool) = (
/// "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59".into(),
/// None,
/// false,
/// );
///
/// let conversion: Option<(String, Option<u64>, bool)> = match xaddress_to_classic_address(xaddress) {
/// Ok((address, tag, is_test_network)) => Some((address, tag, is_test_network)),
/// Err(e) => match e {
/// XRPLAddressCodecException::InvalidXAddressPrefix => None,
/// XRPLAddressCodecException::UnsupportedXAddress => None,
/// XRPLAddressCodecException::InvalidXAddressZeroNoTag => None,
/// XRPLAddressCodecException::InvalidXAddressZeroRemain => None,
/// XRPLAddressCodecException::UnexpectedPayloadLength {
/// expected: _,
/// found: _,
/// } => None,
/// _ => None,
/// }
/// };
///
/// assert_eq!(Some(classic), conversion);
/// ```
pub fn xaddress_to_classic_address(
xaddress: &str,
) -> Result<(String, Option<u64>, bool), XRPLAddressCodecException> {
// Convert b58 to bytes
let decoded = bs58::decode(xaddress)
.with_alphabet(&XRPL_ALPHABET)
.with_check(None)
.into_vec()?;
let is_test_network = _is_test_address(&decoded[..2])?;
let classic_address_bytes = &decoded[2..22];
// extracts the destination tag
let tag = _get_tag_from_buffer(&decoded[22..])?;
let classic_address = encode_classic_address(classic_address_bytes)?;
Ok((classic_address, tag, is_test_network))
}
/// Returns the classic address encoding of these bytes
/// as a base58 string.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use xrpl::core::addresscodec::encode_classic_address;
/// use xrpl::core::addresscodec::exceptions::XRPLAddressCodecException;
///
/// let bytes: &[u8] = &[
/// 94, 123, 17, 37, 35, 246, 141, 47, 94, 135, 157, 180,
/// 234, 197, 28, 102, 152, 166, 147, 4
/// ];
/// let address: String = "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59".into();
///
/// let encoding: Option<String> = match encode_classic_address(bytes) {
/// Ok(address) => Some(address),
/// Err(e) => match e {
/// XRPLAddressCodecException::UnexpectedPayloadLength {
/// expected: _,
/// found: _,
/// } => None,
/// _ => None,
/// }
/// };
///
/// assert_eq!(Some(address), encoding);
/// ```
pub fn encode_classic_address(bytestring: &[u8]) -> Result<String, XRPLAddressCodecException> {
encode_base58(
bytestring,
&CLASSIC_ADDRESS_PREFIX,
Some(CLASSIC_ADDRESS_LENGTH.into()),
)
}
/// Returns the decoded bytes of the classic address.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use xrpl::core::addresscodec::decode_classic_address;
/// use xrpl::core::addresscodec::exceptions::XRPLAddressCodecException;
/// extern crate alloc;
/// use alloc::vec;
///
/// let key: &str = "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59";
/// let bytes: Vec<u8> = vec![
/// 94, 123, 17, 37, 35, 246, 141, 47, 94, 135, 157, 180,
/// 234, 197, 28, 102, 152, 166, 147, 4
/// ];
///
/// let decoding: Option<Vec<u8>> = match decode_classic_address(key) {
/// Ok(bytes) => Some(bytes),
/// Err(e) => match e {
/// XRPLAddressCodecException::InvalidEncodingPrefixLength => None,
/// _ => None,
/// }
/// };
///
/// assert_eq!(Some(bytes), decoding);
/// ```
pub fn decode_classic_address(classic_address: &str) -> Result<Vec<u8>, XRPLAddressCodecException> {
decode_base58(classic_address, &CLASSIC_ADDRESS_PREFIX)
}
/// Returns the node public key encoding of these bytes
/// as a base58 string.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use xrpl::core::addresscodec::encode_node_public_key;
/// use xrpl::core::addresscodec::exceptions::XRPLAddressCodecException;
///
/// let bytes: &[u8] = &[
/// 3, 136, 229, 186, 135, 160, 0, 203, 128, 114, 64, 223,
/// 140, 132, 142, 176, 181, 255, 165, 200, 229, 165, 33,
/// 188, 142, 16, 92, 15, 10, 68, 33, 120, 40
/// ];
/// let key: String = "n9MXXueo837zYH36DvMc13BwHcqtfAWNJY5czWVbp7uYTj7x17TH".into();
///
/// let encoding: Option<String> = match encode_node_public_key(bytes) {
/// Ok(key) => Some(key),
/// Err(e) => match e {
/// XRPLAddressCodecException::UnexpectedPayloadLength {
/// expected: _,
/// found: _,
/// } => None,
/// _ => None,
/// }
/// };
///
/// assert_eq!(Some(key), encoding);
/// ```
pub fn encode_node_public_key(bytestring: &[u8]) -> Result<String, XRPLAddressCodecException> {
encode_base58(
bytestring,
&NODE_PUBLIC_KEY_PREFIX,
Some(NODE_PUBLIC_KEY_LENGTH.into()),
)
}
/// Returns the decoded bytes of the node public key.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use xrpl::core::addresscodec::decode_node_public_key;
/// use xrpl::core::addresscodec::exceptions::XRPLAddressCodecException;
/// extern crate alloc;
/// use alloc::vec;
///
/// let key: &str = "n9MXXueo837zYH36DvMc13BwHcqtfAWNJY5czWVbp7uYTj7x17TH";
/// let bytes: Vec<u8> = vec![
/// 3, 136, 229, 186, 135, 160, 0, 203, 128, 114, 64, 223,
/// 140, 132, 142, 176, 181, 255, 165, 200, 229, 165, 33,
/// 188, 142, 16, 92, 15, 10, 68, 33, 120, 40
/// ];
///
/// let decoding: Option<Vec<u8>> = match decode_node_public_key(key) {
/// Ok(bytes) => Some(bytes),
/// Err(e) => match e {
/// XRPLAddressCodecException::InvalidEncodingPrefixLength => None,
/// _ => None,
/// }
/// };
///
/// assert_eq!(Some(bytes), decoding);
/// ```
pub fn decode_node_public_key(node_public_key: &str) -> Result<Vec<u8>, XRPLAddressCodecException> {
decode_base58(node_public_key, &NODE_PUBLIC_KEY_PREFIX)
}
/// Returns the account public key encoding of these
/// bytes as a base58 string.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use xrpl::core::addresscodec::encode_account_public_key;
/// use xrpl::core::addresscodec::exceptions::XRPLAddressCodecException;
///
/// let bytes: &[u8] = &[
/// 2, 54, 147, 241, 89, 103, 174, 53, 125, 3, 39, 151,
/// 74, 212, 111, 227, 193, 39, 17, 59, 17, 16, 214, 4,
/// 79, 212, 30, 114, 54, 137, 248, 28, 198
/// ];
/// let key: String = "aB44YfzW24VDEJQ2UuLPV2PvqcPCSoLnL7y5M1EzhdW4LnK5xMS3".into();
///
/// let encoding: Option<String> = match encode_account_public_key(bytes) {
/// Ok(key) => Some(key),
/// Err(e) => match e {
/// XRPLAddressCodecException::UnexpectedPayloadLength {
/// expected: _,
/// found: _,
/// } => None,
/// _ => None,
/// }
/// };
///
/// assert_eq!(Some(key), encoding);
/// ```
pub fn encode_account_public_key(bytestring: &[u8]) -> Result<String, XRPLAddressCodecException> {
encode_base58(
bytestring,
&ACCOUNT_PUBLIC_KEY_PREFIX,
Some(ACCOUNT_PUBLIC_KEY_LENGTH.into()),
)
}
/// Returns the decoded bytes of the node public key.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use xrpl::core::addresscodec::decode_account_public_key;
/// use xrpl::core::addresscodec::exceptions::XRPLAddressCodecException;
/// extern crate alloc;
/// use alloc::vec;
///
/// let key: &str = "aB44YfzW24VDEJQ2UuLPV2PvqcPCSoLnL7y5M1EzhdW4LnK5xMS3";
/// let bytes: Vec<u8> = vec![
/// 2, 54, 147, 241, 89, 103, 174, 53, 125, 3, 39, 151,
/// 74, 212, 111, 227, 193, 39, 17, 59, 17, 16, 214, 4,
/// 79, 212, 30, 114, 54, 137, 248, 28, 198
/// ];
///
/// let decoding: Option<Vec<u8>> = match decode_account_public_key(key) {
/// Ok(bytes) => Some(bytes),
/// Err(e) => match e {
/// XRPLAddressCodecException::InvalidEncodingPrefixLength => None,
/// _ => None,
/// }
/// };
///
/// assert_eq!(Some(bytes), decoding);
/// ```
pub fn decode_account_public_key(
account_public_key: &str,
) -> Result<Vec<u8>, XRPLAddressCodecException> {
decode_base58(account_public_key, &ACCOUNT_PUBLIC_KEY_PREFIX)
}
/// Returns whether `classic_address` is a valid classic address.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use xrpl::core::addresscodec::is_valid_classic_address;
///
/// let address: &str = "rpGaCyHRYbgKhErgFih3RdjJqXDsYBouz3";
///
/// assert!(is_valid_classic_address(address));
/// ```
pub fn is_valid_classic_address(classic_address: &str) -> bool {
decode_base58(classic_address, &CLASSIC_ADDRESS_PREFIX).is_ok()
}
/// Returns whether ``xaddress`` is a valid X-Address.
///
/// # Examples
///
/// ## Basic usage
///
/// ```
/// use xrpl::core::addresscodec::is_valid_xaddress;
///
/// let address: &str = "X7AcgcsBL6XDcUb289X4mJ8djcdyKaB5hJDWMArnXr61cqZ";
///
/// assert!(is_valid_xaddress(address));
/// ```
pub fn is_valid_xaddress(xaddress: &str) -> bool {
xaddress_to_classic_address(xaddress).is_ok()
}
#[cfg(test)]
mod test {
use super::*;
use crate::alloc::string::ToString;
use crate::core::addresscodec::test_cases::*;
#[test]
fn test_algorithm_to_prefix() {
assert_eq!(
&FAMILY_SEED_PREFIX,
_algorithm_to_prefix(&CryptoAlgorithm::SECP256K1)
);
assert_eq!(
&ED25519_SEED_PREFIX,
_algorithm_to_prefix(&CryptoAlgorithm::ED25519)
);
}
#[test]
fn test_encode_seed() {
let bytes: [u8; 16] = [
207, 45, 227, 120, 251, 221, 126, 46, 232, 125, 72, 109, 251, 90, 123, 255,
];
assert_eq!(
Ok(SECP256K1_ENCODED_SEED_TEST.to_string()),
encode_seed(bytes, CryptoAlgorithm::SECP256K1)
);
let bytes: [u8; 16] = [
76, 58, 29, 33, 63, 189, 251, 20, 199, 194, 141, 96, 148, 105, 179, 65,
];
assert_eq!(
Ok(ED25519_ENCODED_SEED_TEST.to_string()),
encode_seed(bytes, CryptoAlgorithm::ED25519)
);
}
#[test]
fn test_decode_seed() {
let hex_bytes = hex::decode(SECP256K1_HEX_TEST).expect("");
let (decode_result, encoding_type) = decode_seed(SECP256K1_ENCODED_SEED_TEST).unwrap();
assert_eq!(hex_bytes, decode_result);
assert_eq!(CryptoAlgorithm::SECP256K1, encoding_type);
let hex_bytes = hex::decode(ED25519_HEX_TEST).expect("");
let (decode_result, encoding_type) = decode_seed(ED25519_ENCODED_SEED_TEST).unwrap();
assert_eq!(hex_bytes, decode_result);
assert_eq!(CryptoAlgorithm::ED25519, encoding_type);
}
#[test]
fn test_classic_address_to_xaddress() {
for case in ADDRESS_TEST_CASES {
assert_eq!(
classic_address_to_xaddress(case.address, case.tag, true),
Ok(case.test_xaddress.to_string()),
);
assert_eq!(
classic_address_to_xaddress(case.address, case.tag, false),
Ok(case.main_xaddress.to_string()),
);
}
}
#[test]
fn test_xaddress_to_classic_address() {
for case in ADDRESS_TEST_CASES {
let (classic_address, tag, is_test) =
xaddress_to_classic_address(case.test_xaddress).unwrap();
assert_eq!(*case.address, classic_address);
assert_eq!(case.tag, tag);
assert!(is_test);
let (classic_address, tag, is_test) =
xaddress_to_classic_address(case.main_xaddress).unwrap();
assert_eq!(*case.address, classic_address);
assert_eq!(case.tag, tag);
assert!(!is_test);
}
}
#[test]
fn test_encode_node_public_key() {
let bytes = hex::decode(NODE_PUBLIC_KEY_HEX_TEST).expect("");
assert_eq!(
encode_node_public_key(&bytes),
Ok(NODE_PUBLIC_KEY_TEST.to_string()),
);
}
#[test]
fn test_decode_node_public_key() {
assert_eq!(
decode_node_public_key(NODE_PUBLIC_KEY_TEST),
Ok(hex::decode(NODE_PUBLIC_KEY_HEX_TEST).expect("")),
);
}
#[test]
fn test_encode_account_public_key() {
assert_eq!(
encode_account_public_key(&hex::decode(ACCOUNT_PUBLIC_KEY_HEX_TEST).expect("")),
Ok(ACCOUNT_PUBLIC_KEY_TEST.to_string()),
);
}
#[test]
fn test_decode_account_public_key() {
assert_eq!(
decode_account_public_key(ACCOUNT_PUBLIC_KEY_TEST),
Ok(hex::decode(ACCOUNT_PUBLIC_KEY_HEX_TEST).expect("")),
);
}
#[test]
fn test_is_valid_classic_address() {
for case in ADDRESS_TEST_CASES {
assert!(is_valid_classic_address(case.address))
}
}
#[test]
fn test_is_valid_xaddress() {
for case in ADDRESS_TEST_CASES {
assert!(is_valid_xaddress(case.test_xaddress))
}
}
#[test]
fn accept_seed_encode_decode_secp256k1_low() {
let encoded_string = "sp6JS7f14BuwFY8Mw6bTtLKWauoUs";
let bytes: [u8; 16] = Default::default();
let (decode_result, encoding_type) = decode_seed(encoded_string).unwrap();
assert_eq!(
encode_seed(bytes, CryptoAlgorithm::SECP256K1),
Ok(encoded_string.to_string()),
);
assert_eq!(decode_result, bytes);
assert_eq!(encoding_type, CryptoAlgorithm::SECP256K1);
}
#[test]
fn accept_seed_encode_decode_secp256k1_high() {
let bytes: [u8; 16] = [255; 16];
let encoded_string = "saGwBRReqUNKuWNLpUAq8i8NkXEPN";
let (decode_result, encoding_type) = decode_seed(encoded_string).unwrap();
assert_eq!(
encode_seed(bytes, CryptoAlgorithm::SECP256K1),
Ok(encoded_string.to_string()),
);
assert_eq!(decode_result, bytes);
assert_eq!(encoding_type, CryptoAlgorithm::SECP256K1);
}
#[test]
fn accept_seed_encode_decode_ed25519_low() {
let encoded_string = "sEdSJHS4oiAdz7w2X2ni1gFiqtbJHqE";
let bytes: [u8; 16] = Default::default();
let (decode_result, encoding_type) = decode_seed(encoded_string).unwrap();
assert_eq!(
encode_seed(bytes, CryptoAlgorithm::ED25519),
Ok(encoded_string.to_string()),
);
assert_eq!(decode_result, bytes);
assert_eq!(encoding_type, CryptoAlgorithm::ED25519);
}
#[test]
fn accept_seed_encode_decode_ed25519_high() {
let bytes: [u8; 16] = [255; 16];
let encoded_string = "sEdV19BLfeQeKdEXyYA4NhjPJe6XBfG";
let (decode_result, encoding_type) = decode_seed(encoded_string).unwrap();
assert_eq!(
encode_seed(bytes, CryptoAlgorithm::ED25519),
Ok(encoded_string.to_string()),
);
assert_eq!(decode_result, bytes);
assert_eq!(encoding_type, CryptoAlgorithm::ED25519);
}
}