mirror of
https://github.com/fluencelabs/js-peer-id
synced 2025-04-25 12:32:27 +00:00
commit
f0d72b7bfe
15
src/index.js
15
src/index.js
@ -17,11 +17,20 @@ class PeerId {
|
|||||||
assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments')
|
assert(privKey.public.bytes.equals(pubKey.bytes), 'inconsistent arguments')
|
||||||
}
|
}
|
||||||
|
|
||||||
this.id = id
|
this._id = id
|
||||||
|
this._idB58String = mh.toB58String(this.id)
|
||||||
this._privKey = privKey
|
this._privKey = privKey
|
||||||
this._pubKey = pubKey
|
this._pubKey = pubKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get id () {
|
||||||
|
return this._id
|
||||||
|
}
|
||||||
|
|
||||||
|
set id (val) {
|
||||||
|
throw new Error('Id is immutable')
|
||||||
|
}
|
||||||
|
|
||||||
get privKey () {
|
get privKey () {
|
||||||
return this._privKey
|
return this._privKey
|
||||||
}
|
}
|
||||||
@ -61,7 +70,7 @@ class PeerId {
|
|||||||
// of go-ipfs for its config file
|
// of go-ipfs for its config file
|
||||||
toJSON () {
|
toJSON () {
|
||||||
return {
|
return {
|
||||||
id: mh.toB58String(this.id),
|
id: this.toB58String(),
|
||||||
privKey: toB64Opt(this.marshalPrivKey()),
|
privKey: toB64Opt(this.marshalPrivKey()),
|
||||||
pubKey: toB64Opt(this.marshalPubKey())
|
pubKey: toB64Opt(this.marshalPubKey())
|
||||||
}
|
}
|
||||||
@ -77,7 +86,7 @@ class PeerId {
|
|||||||
}
|
}
|
||||||
|
|
||||||
toB58String () {
|
toB58String () {
|
||||||
return 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', () => {
|
it('recreate an Id from Hex string', () => {
|
||||||
const id = PeerId.createFromHexString(testIdHex)
|
const id = PeerId.createFromHexString(testIdHex)
|
||||||
expect(testIdBytes).to.deep.equal(id.id)
|
expect(testIdBytes).to.deep.equal(id.id)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user