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
use borsh::{BorshDeserialize, BorshSerialize};
use constant_time_eq::constant_time_eq_n;
use core::{
fmt,
hash::{Hash, Hasher},
};
use zeroize::{Zeroize, ZeroizeOnDrop};
pub type ByteArray12 = [u8; 12];
pub type ByteArray16 = [u8; 16];
pub type ByteArray24 = [u8; 24];
pub type ByteArray32 = [u8; 32];
pub type ByteArray64 = [u8; 64];
pub type ByteArray128 = [u8; 128];
pub type ByteArray256 = [u8; 256];
pub type UnixTimestamp = u64;
pub type UnixTimestampSigned = i64;
#[derive(Clone, Copy, Default, PartialOrd, Ord, BorshDeserialize, BorshSerialize)]
pub struct Blake3Hash(pub ByteArray32);
impl Blake3Hash {
#[cfg(feature = "hex")]
pub fn to_hex(&self) -> String {
hex::encode(&self.0)
}
}
impl Zeroize for Blake3Hash {
fn zeroize(&mut self) {
self.0 = Blake3Hash::default().0;
}
}
impl PartialEq for Blake3Hash {
fn eq(&self, other: &Self) -> bool {
constant_time_eq_n(&self.0, &other.0)
}
}
impl Eq for Blake3Hash {}
impl Hash for Blake3Hash {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
#[cfg(feature = "hex")]
impl fmt::Debug for Blake3Hash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Blake3Hash").field(&self.to_hex()).finish()
}
}
#[derive(Clone, Copy, PartialOrd, Ord, BorshDeserialize, BorshSerialize)]
pub struct TaiTimestamp(pub ByteArray12);
impl TaiTimestamp {
#[cfg(feature = "tai64")]
pub fn to_datetime(&self) -> crate::UtilitiesResult<String> {
use crate::Utilities;
use monotonic_time::DateTime;
let timestamp = Utilities::bytes_to_tai64n(&self.0)?;
let duration = Utilities::tai64_get_secs(timestamp)?;
let mut datetime = DateTime::new();
datetime.to_datetime(duration);
Ok(datetime.to_string())
}
#[cfg(feature = "hex")]
pub fn to_hex(&self) -> String {
hex::encode(&self.0)
}
}
#[cfg(all(feature = "zeroize_timestamp", feature = "tai64"))]
impl Zeroize for TaiTimestamp {
fn zeroize(&mut self) {
self.0 = tai64::Tai64N::UNIX_EPOCH.to_bytes()
}
}
impl PartialEq for TaiTimestamp {
fn eq(&self, other: &Self) -> bool {
constant_time_eq_n(&self.0, &other.0)
}
}
impl Eq for TaiTimestamp {}
impl Hash for TaiTimestamp {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
#[cfg(feature = "tai64")]
impl TaiTimestamp {
pub fn now() -> Self {
TaiTimestamp(tai64::Tai64N::now().to_bytes())
}
pub fn new() -> Self {
TaiTimestamp(tai64::Tai64N::UNIX_EPOCH.to_bytes())
}
}
#[cfg(feature = "tai64")]
impl Default for TaiTimestamp {
fn default() -> Self {
TaiTimestamp::new()
}
}
#[cfg(feature = "tai64")]
impl fmt::Debug for TaiTimestamp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.to_datetime() {
Ok(timestamp) => write!(f, "{:?}", timestamp),
Err(error) => write!(f, "{:?}", error),
}
}
}
#[derive(Clone, Copy, Default, PartialOrd, Ord, BorshDeserialize, BorshSerialize)]
pub struct Ed25519Public(pub [u8; 32]);
impl Ed25519Public {
#[cfg(feature = "hex")]
pub fn to_hex(&self) -> String {
hex::encode(&self.0)
}
#[cfg(feature = "base58")]
pub fn to_base58(&self) -> String {
bs58::encode(&self.0).into_string()
}
}
#[cfg(feature = "zeroize_ed25519_public")]
impl Zeroize for Ed25519Public {
fn zeroize(&mut self) {
self.0 = Ed25519Public::default().0
}
}
impl PartialEq for Ed25519Public {
fn eq(&self, other: &Self) -> bool {
constant_time_eq_n(&self.0, &other.0)
}
}
impl Eq for Ed25519Public {}
impl Hash for Ed25519Public {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
#[cfg(feature = "base58")]
impl fmt::Debug for Ed25519Public {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Ed25519Public")
.field(&self.to_base58())
.finish()
}
}
#[derive(Clone, Copy, PartialOrd, Ord, BorshDeserialize, BorshSerialize)]
pub struct Ed25519Signature(pub [u8; 64]);
impl Ed25519Signature {
#[cfg(feature = "hex")]
pub fn to_hex(&self) -> String {
hex::encode(&self.0)
}
#[cfg(feature = "base58")]
pub fn to_base58(&self) -> String {
bs58::encode(&self.0).into_string()
}
}
#[cfg(feature = "zeroize_ed25519_signature")]
impl Zeroize for Ed25519Signature {
fn zeroize(&mut self) {
self.0 = Ed25519Signature::default().0
}
}
impl PartialEq for Ed25519Signature {
fn eq(&self, other: &Self) -> bool {
constant_time_eq_n(&self.0, &other.0)
}
}
impl Eq for Ed25519Signature {}
impl Hash for Ed25519Signature {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
impl Default for Ed25519Signature {
fn default() -> Self {
Ed25519Signature([0u8; 64])
}
}
#[cfg(feature = "base58")]
impl fmt::Debug for Ed25519Signature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Ed25519Signature")
.field(&self.to_base58())
.finish()
}
}
#[derive(Clone, Copy, Default, PartialOrd, Ord, BorshDeserialize, BorshSerialize)]
pub struct Sr25519Public(pub [u8; 32]);
impl Sr25519Public {
#[cfg(feature = "hex")]
pub fn to_hex(&self) -> String {
hex::encode(&self.0)
}
#[cfg(feature = "base58")]
pub fn to_base58(&self) -> String {
bs58::encode(&self.0).into_string()
}
}
#[cfg(feature = "zeroize_sr25519_public")]
impl Zeroize for Sr25519Public {
fn zeroize(&mut self) {
self.0 = Sr25519Public::default().0
}
}
impl PartialEq for Sr25519Public {
fn eq(&self, other: &Self) -> bool {
constant_time_eq_n(&self.0, &other.0)
}
}
impl Eq for Sr25519Public {}
impl Hash for Sr25519Public {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
#[cfg(feature = "base58")]
impl fmt::Debug for Sr25519Public {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Sr25519Public")
.field(&self.to_base58())
.finish()
}
}
#[derive(Clone, Copy, PartialOrd, Ord, BorshDeserialize, BorshSerialize)]
pub struct Sr25519Signature(pub [u8; 64]);
impl Sr25519Signature {
#[cfg(feature = "hex")]
pub fn to_hex(&self) -> String {
hex::encode(&self.0)
}
#[cfg(feature = "base58")]
pub fn to_base58(&self) -> String {
bs58::encode(&self.0).into_string()
}
}
#[cfg(feature = "zeroize_sr25519_signature")]
impl Zeroize for Sr25519Signature {
fn zeroize(&mut self) {
self.0 = Sr25519Signature::default().0
}
}
impl PartialEq for Sr25519Signature {
fn eq(&self, other: &Self) -> bool {
constant_time_eq_n(&self.0, &other.0)
}
}
impl Eq for Sr25519Signature {}
impl Hash for Sr25519Signature {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
impl Default for Sr25519Signature {
fn default() -> Self {
Sr25519Signature([0u8; 64])
}
}
#[cfg(feature = "base58")]
impl fmt::Debug for Sr25519Signature {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Sr25519Signature")
.field(&self.to_base58())
.finish()
}
}
#[derive(Clone, Copy, Default, PartialOrd, Ord, BorshDeserialize, BorshSerialize)]
pub struct X25519Public(pub [u8; 32]);
impl X25519Public {
#[cfg(feature = "hex")]
pub fn to_hex(&self) -> String {
hex::encode(&self.0)
}
#[cfg(feature = "base58")]
pub fn to_base58(&self) -> String {
bs58::encode(&self.0).into_string()
}
}
#[cfg(feature = "zeroize_x25519_public")]
impl Zeroize for X25519Public {
fn zeroize(&mut self) {
self.0 = X25519Public::default().0
}
}
impl PartialEq for X25519Public {
fn eq(&self, other: &Self) -> bool {
constant_time_eq_n(&self.0, &other.0)
}
}
impl Eq for X25519Public {}
impl Hash for X25519Public {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
#[cfg(feature = "hex")]
impl fmt::Debug for X25519Public {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("X25519Public").field(&self.to_hex()).finish()
}
}
#[derive(PartialOrd, Default, Ord, BorshDeserialize, BorshSerialize)]
pub struct Secret32Bytes(pub [u8; 32]);
impl PartialEq for Secret32Bytes {
fn eq(&self, other: &Self) -> bool {
constant_time_eq_n(&self.0, &other.0)
}
}
impl Eq for Secret32Bytes {}
impl Hash for Secret32Bytes {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
impl Zeroize for Secret32Bytes {
fn zeroize(&mut self) {
self.0 = Secret32Bytes::default().0;
}
}
impl ZeroizeOnDrop for Secret32Bytes {}
#[cfg(feature = "debug_secret")]
impl Secret32Bytes {
pub fn dangerous_debug(&self) -> String {
hex::encode(&self.0)
}
pub fn to_hex<'a>(&self) -> &'a str {
"[REDACTED]"
}
}
#[cfg(feature = "clonable_secret")]
impl Clone for Secret32Bytes {
fn clone(&self) -> Self {
Secret32Bytes(self.0)
}
}
impl fmt::Debug for Secret32Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Secret32Bytes").field(&"[REDACTED]").finish()
}
}
#[derive(Clone, Copy, Default, PartialOrd, Ord, BorshDeserialize, BorshSerialize)]
pub struct AeadNonce(pub ByteArray12);
#[cfg(feature = "zeroize_aead")]
impl Zeroize for AeadNonce {
fn zeroize(&mut self) {
self.0 = AeadNonce::default().0
}
}
impl PartialEq for AeadNonce {
fn eq(&self, other: &Self) -> bool {
constant_time_eq_n(&self.0, &other.0)
}
}
impl Eq for AeadNonce {}
impl Hash for AeadNonce {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
#[cfg(feature = "hex")]
impl fmt::Debug for AeadNonce {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("AeadNonce")
.field(&hex::encode(&self.0))
.finish()
}
}
#[derive(Clone, Copy, Default, PartialOrd, Ord, BorshDeserialize, BorshSerialize)]
pub struct AeadXNonce(pub ByteArray24);
#[cfg(feature = "zeroize_aead")]
impl Zeroize for AeadXNonce {
fn zeroize(&mut self) {
self.0 = AeadXNonce::default().0
}
}
impl PartialEq for AeadXNonce {
fn eq(&self, other: &Self) -> bool {
constant_time_eq_n(&self.0, &other.0)
}
}
impl Eq for AeadXNonce {}
impl Hash for AeadXNonce {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
#[cfg(feature = "hex")]
impl fmt::Debug for AeadXNonce {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("AeadXNonce")
.field(&hex::encode(&self.0))
.finish()
}
}
#[derive(Clone, Default, Copy, PartialOrd, Ord, BorshDeserialize, BorshSerialize)]
pub struct AeadTag(pub ByteArray16);
#[cfg(feature = "zeroize_aead")]
impl Zeroize for AeadTag {
fn zeroize(&mut self) {
self.0 = AeadTag::default().0
}
}
impl PartialEq for AeadTag {
fn eq(&self, other: &Self) -> bool {
constant_time_eq_n(&self.0, &other.0)
}
}
impl Eq for AeadTag {}
impl Hash for AeadTag {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
#[cfg(feature = "hex")]
impl fmt::Debug for AeadTag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("AeadTag")
.field(&hex::encode(&self.0))
.finish()
}
}
#[derive(PartialOrd, Default, Ord, BorshDeserialize, BorshSerialize)]
pub struct SecretVec(pub Vec<u8>);
impl PartialEq for SecretVec {
fn eq(&self, other: &Self) -> bool {
constant_time_eq::constant_time_eq(&self.0, &other.0)
}
}
impl Eq for SecretVec {}
impl Hash for SecretVec {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
impl Zeroize for SecretVec {
fn zeroize(&mut self) {
self.0.clear();
}
}
impl ZeroizeOnDrop for SecretVec {}
#[cfg(feature = "debug_secret")]
impl SecretVec {
pub fn dangerous_debug(&self) -> String {
hex::encode(&self.0)
}
pub fn to_hex<'a>(&self) -> &'a str {
"[REDACTED]"
}
}
#[cfg(feature = "clonable_secret")]
impl Clone for SecretVec {
fn clone(&self) -> Self {
SecretVec(self.0.clone())
}
}
impl fmt::Debug for SecretVec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("SecretVec").field(&"[REDACTED]").finish()
}
}
#[derive(
PartialOrd, Clone, PartialEq, Eq, Hash, Default, Ord, BorshDeserialize, BorshSerialize,
)]
pub struct HexVec(pub Vec<u8>);
#[cfg(feature = "hex")]
impl fmt::Debug for HexVec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("HexVec")
.field(&hex::encode(&self.0))
.finish()
}
}