19
19
// DEALINGS IN THE SOFTWARE.
20
20
21
21
use crate :: PublicKey ;
22
- use bytes:: Bytes ;
23
22
use thiserror:: Error ;
24
23
use multihash:: { Code , Multihash , MultihashDigest } ;
25
24
use rand:: Rng ;
26
25
use std:: { convert:: TryFrom , borrow:: Borrow , fmt, hash, str:: FromStr , cmp} ;
26
+ use std:: sync:: Arc ;
27
27
28
28
/// Public keys with byte-lengths smaller than `MAX_INLINE_KEY_LENGTH` will be
29
29
/// automatically used as the peer id using an identity multihash.
@@ -35,7 +35,7 @@ const MAX_INLINE_KEY_LENGTH: usize = 42;
35
35
// TODO: maybe keep things in decoded version?
36
36
#[ derive( Clone , Eq ) ]
37
37
pub struct PeerId {
38
- multihash : Bytes ,
38
+ multihash : Arc < Vec < u8 > > ,
39
39
}
40
40
41
41
impl fmt:: Debug for PeerId {
@@ -78,9 +78,9 @@ impl PeerId {
78
78
Code :: Sha2_256
79
79
} ;
80
80
81
- let multihash = hash_algorithm. digest ( & key_enc) . to_bytes ( ) . into ( ) ;
81
+ let multihash = hash_algorithm. digest ( & key_enc) . to_bytes ( ) ;
82
82
83
- PeerId { multihash }
83
+ PeerId { multihash : Arc :: new ( multihash ) }
84
84
}
85
85
86
86
/// Checks whether `data` is a valid `PeerId`. If so, returns the `PeerId`. If not, returns
@@ -99,9 +99,9 @@ impl PeerId {
99
99
/// peer ID, it is returned as an `Err`.
100
100
pub fn from_multihash ( multihash : Multihash ) -> Result < PeerId , Multihash > {
101
101
match Code :: try_from ( multihash. code ( ) ) {
102
- Ok ( Code :: Sha2_256 ) => Ok ( PeerId { multihash : multihash. to_bytes ( ) . into ( ) } ) ,
102
+ Ok ( Code :: Sha2_256 ) => Ok ( PeerId { multihash : Arc :: new ( multihash. to_bytes ( ) ) } ) ,
103
103
Ok ( Code :: Identity ) if multihash. digest ( ) . len ( ) <= MAX_INLINE_KEY_LENGTH
104
- => Ok ( PeerId { multihash : multihash. to_bytes ( ) . into ( ) } ) ,
104
+ => Ok ( PeerId { multihash : Arc :: new ( multihash. to_bytes ( ) ) } ) ,
105
105
_ => Err ( multihash)
106
106
}
107
107
}
@@ -112,8 +112,8 @@ impl PeerId {
112
112
pub fn random ( ) -> PeerId {
113
113
let peer_id = rand:: thread_rng ( ) . gen :: < [ u8 ; 32 ] > ( ) ;
114
114
PeerId {
115
- multihash : Multihash :: wrap ( Code :: Identity . into ( ) , & peer_id)
116
- . expect ( "The digest size is never too large" ) . to_bytes ( ) . into ( )
115
+ multihash : Arc :: new ( Multihash :: wrap ( Code :: Identity . into ( ) , & peer_id)
116
+ . expect ( "The digest size is never too large" ) . to_bytes ( ) )
117
117
}
118
118
}
119
119
@@ -123,7 +123,7 @@ impl PeerId {
123
123
/// equality of peer IDs. That is, two peer IDs may be considered equal
124
124
/// while having a different byte representation as per `into_bytes`.
125
125
pub fn into_bytes ( self ) -> Vec < u8 > {
126
- self . multihash . to_vec ( )
126
+ self . multihash . as_ref ( ) . clone ( )
127
127
}
128
128
129
129
/// Returns a raw bytes representation of this `PeerId`.
0 commit comments