2
2
// SPDX-License-Identifier: Apache-2.0, MIT
3
3
4
4
use std:: convert:: TryInto ;
5
- use std:: hash:: { Hash , Hasher } ;
6
- use std:: ops:: Deref ;
7
- use std:: { fmt, u64} ;
5
+ use std:: hash:: Hash ;
6
+ use std:: u64;
8
7
9
8
use super :: { from_leb_bytes, to_leb_bytes, Error , Protocol , BLS_PUB_LEN , PAYLOAD_HASH_LEN } ;
10
9
11
- /// Public key struct used as BLS Address data.
12
- /// This type is only needed to be able to implement traits on it due to limitations on
13
- /// arrays within Rust that are greater than 32 length. Can be dereferenced into `[u8; 48]`.
14
- #[ derive( Copy , Clone ) ]
15
- pub struct BLSPublicKey ( pub [ u8 ; BLS_PUB_LEN ] ) ;
16
-
17
- impl Hash for BLSPublicKey {
18
- fn hash < H : Hasher > ( & self , state : & mut H ) {
19
- state. write ( & self . 0 ) ;
20
- }
21
- }
22
-
23
- impl Eq for BLSPublicKey { }
24
- impl PartialEq for BLSPublicKey {
25
- fn eq ( & self , other : & Self ) -> bool {
26
- self . 0 [ ..] . eq ( & other. 0 [ ..] )
27
- }
28
- }
29
-
30
- impl fmt:: Debug for BLSPublicKey {
31
- fn fmt ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
32
- self . 0 [ ..] . fmt ( formatter)
33
- }
34
- }
35
-
36
- impl From < [ u8 ; BLS_PUB_LEN ] > for BLSPublicKey {
37
- fn from ( pk : [ u8 ; BLS_PUB_LEN ] ) -> Self {
38
- BLSPublicKey ( pk)
39
- }
40
- }
41
-
42
- impl Deref for BLSPublicKey {
43
- type Target = [ u8 ; BLS_PUB_LEN ] ;
44
- fn deref ( & self ) -> & Self :: Target {
45
- & self . 0
46
- }
47
- }
48
-
49
10
/// Payload is the data of the Address. Variants are the supported Address protocols.
50
11
#[ derive( Copy , Clone , Debug , Hash , PartialEq , Eq ) ]
51
12
pub enum Payload {
@@ -56,7 +17,7 @@ pub enum Payload {
56
17
/// Actor protocol address, 20 byte hash of actor data
57
18
Actor ( [ u8 ; PAYLOAD_HASH_LEN ] ) ,
58
19
/// BLS key address, full 48 byte public key
59
- BLS ( BLSPublicKey ) ,
20
+ BLS ( [ u8 ; BLS_PUB_LEN ] ) ,
60
21
}
61
22
62
23
impl Payload {
@@ -99,14 +60,11 @@ impl Payload {
99
60
. try_into ( )
100
61
. map_err ( |_| Error :: InvalidPayloadLength ( payload. len ( ) ) ) ?,
101
62
) ,
102
- Protocol :: BLS => {
103
- if payload. len ( ) != BLS_PUB_LEN {
104
- return Err ( Error :: InvalidBLSLength ( payload. len ( ) ) ) ;
105
- }
106
- let mut pk = [ 0u8 ; BLS_PUB_LEN ] ;
107
- pk. copy_from_slice ( payload) ;
108
- Self :: BLS ( pk. into ( ) )
109
- }
63
+ Protocol :: BLS => Self :: BLS (
64
+ payload
65
+ . try_into ( )
66
+ . map_err ( |_| Error :: InvalidPayloadLength ( payload. len ( ) ) ) ?,
67
+ ) ,
110
68
} ;
111
69
Ok ( payload)
112
70
}
0 commit comments