File tree 2 files changed +16
-5
lines changed
2 files changed +16
-5
lines changed Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ class PeerId {
18
18
}
19
19
20
20
this . _id = id
21
- this . _idB58String = ''
21
+ this . _idB58String = mh . toB58String ( this . id )
22
22
this . _privKey = privKey
23
23
this . _pubKey = pubKey
24
24
}
@@ -27,6 +27,10 @@ class PeerId {
27
27
return this . _id
28
28
}
29
29
30
+ set id ( val ) {
31
+ throw new Error ( 'Id is immutable' )
32
+ }
33
+
30
34
get privKey ( ) {
31
35
return this . _privKey
32
36
}
@@ -82,10 +86,6 @@ class PeerId {
82
86
}
83
87
84
88
toB58String ( ) {
85
- if ( ! this . _idB58String ) {
86
- this . _idB58String = mh . toB58String ( this . id )
87
- }
88
-
89
89
return this . _idB58String
90
90
}
91
91
}
Original file line number Diff line number Diff line change @@ -29,6 +29,17 @@ describe('PeerId', () => {
29
29
} )
30
30
} )
31
31
32
+ it ( 'throws on changing the id' , ( done ) => {
33
+ PeerId . create ( ( err , id ) => {
34
+ expect ( err ) . to . not . exist
35
+ expect ( id . toB58String ( ) . length ) . to . equal ( 46 )
36
+ expect ( ( ) => {
37
+ id . id = new Buffer ( 'hello' )
38
+ } ) . to . throw ( / i m m u t a b l e / )
39
+ done ( )
40
+ } )
41
+ } )
42
+
32
43
it ( 'recreate an Id from Hex string' , ( ) => {
33
44
const id = PeerId . createFromHexString ( testIdHex )
34
45
expect ( testIdBytes ) . to . deep . equal ( id . id )
You can’t perform that action at this time.
0 commit comments