mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-04-25 06:22:24 +00:00
feat: create b58 string on creation and throw on id mutation
This commit is contained in:
parent
bebb0a7eae
commit
78d96d0b14
10
src/index.js
10
src/index.js
@ -18,7 +18,7 @@ class PeerId {
|
||||
}
|
||||
|
||||
this._id = id
|
||||
this._idB58String = ''
|
||||
this._idB58String = mh.toB58String(this.id)
|
||||
this._privKey = privKey
|
||||
this._pubKey = pubKey
|
||||
}
|
||||
@ -27,6 +27,10 @@ class PeerId {
|
||||
return this._id
|
||||
}
|
||||
|
||||
set id (val) {
|
||||
throw new Error('Id is immutable')
|
||||
}
|
||||
|
||||
get privKey () {
|
||||
return this._privKey
|
||||
}
|
||||
@ -82,10 +86,6 @@ class PeerId {
|
||||
}
|
||||
|
||||
toB58String () {
|
||||
if (!this._idB58String) {
|
||||
this._idB58String = mh.toB58String(this.id)
|
||||
}
|
||||
|
||||
return this._idB58String
|
||||
}
|
||||
}
|
||||
|
@ -29,6 +29,17 @@ describe('PeerId', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('throws on changing the id', (done) => {
|
||||
PeerId.create((err, id) => {
|
||||
expect(err).to.not.exist
|
||||
expect(id.toB58String().length).to.equal(46)
|
||||
expect(() => {
|
||||
id.id = new Buffer('hello')
|
||||
}).to.throw(/immutable/)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('recreate an Id from Hex string', () => {
|
||||
const id = PeerId.createFromHexString(testIdHex)
|
||||
expect(testIdBytes).to.deep.equal(id.id)
|
||||
|
Loading…
x
Reference in New Issue
Block a user