File tree Expand file tree Collapse file tree 1 file changed +11
-9
lines changed Expand file tree Collapse file tree 1 file changed +11
-9
lines changed Original file line number Diff line number Diff line change 3
3
import base58
4
4
import multihash
5
5
6
+ from functools import (
7
+ cached_property ,
8
+ )
9
+
6
10
from libp2p .crypto .keys import (
7
11
PublicKey ,
8
12
)
@@ -36,25 +40,23 @@ def digest(self) -> bytes:
36
40
37
41
class ID :
38
42
_bytes : bytes
39
- _xor_id : int | None = None
40
- _b58_str : str | None = None
41
43
42
44
def __init__ (self , peer_id_bytes : bytes ) -> None :
43
45
self ._bytes = peer_id_bytes
44
46
45
- @property
47
+ @cached_property
46
48
def xor_id (self ) -> int :
47
- if not self ._xor_id :
48
- self ._xor_id = int (sha256_digest (self ._bytes ).hex (), 16 )
49
- return self ._xor_id
49
+ return int (sha256_digest (self ._bytes ).hex (), 16 )
50
+
51
+ @cached_property
52
+ def base58 (self ) -> str :
53
+ return base58 .b58encode (self ._bytes ).decode ()
50
54
51
55
def to_bytes (self ) -> bytes :
52
56
return self ._bytes
53
57
54
58
def to_base58 (self ) -> str :
55
- if not self ._b58_str :
56
- self ._b58_str = base58 .b58encode (self ._bytes ).decode ()
57
- return self ._b58_str
59
+ return self .base58
58
60
59
61
def __repr__ (self ) -> str :
60
62
return f"<libp2p.peer.id.ID ({ self !s} )>"
You can’t perform that action at this time.
0 commit comments