feat(peerid): support creating from secp256k1; harmonize algo with Go (#95)

This commit is contained in:
Arve Knudsen
2019-07-11 21:31:39 +02:00
committed by Jacob Heun
parent 989b413a96
commit 17440a3f9a
4 changed files with 44 additions and 14 deletions

View File

@ -36,6 +36,12 @@ describe('PeerId', () => {
expect(id.toB58String().length).to.equal(46)
})
it('can be created for a Secp256k1 key', async () => {
const id = await PeerId.create({ keyType: 'secp256k1', bits: 256 })
const expB58 = mh.toB58String(mh.encode(id.pubKey.bytes, 'identity'))
expect(id.toB58String()).to.equal(expB58)
})
it('isPeerId', async () => {
const id = await PeerId.create(testOpts)
expect(PeerId.isPeerId(id)).to.equal(true)
@ -80,6 +86,20 @@ describe('PeerId', () => {
expect(id.marshalPubKey()).to.deep.equal(id2.marshalPubKey())
})
it('can be created from a Secp256k1 public key', async () => {
const privKey = await crypto.keys.generateKeyPair('secp256k1', 256)
const id = await PeerId.createFromPubKey(privKey.public.bytes)
const expB58 = mh.toB58String(mh.encode(id.pubKey.bytes, 'identity'))
expect(id.toB58String()).to.equal(expB58)
})
it('can be created from a Secp256k1 private key', async () => {
const privKey = await crypto.keys.generateKeyPair('secp256k1', 256)
const id = await PeerId.createFromPrivKey(privKey.bytes)
const expB58 = mh.toB58String(mh.encode(id.pubKey.bytes, 'identity'))
expect(id.toB58String()).to.equal(expB58)
})
it('Compare generated ID with one created from PubKey', async () => {
const id1 = await PeerId.create(testOpts)
const id2 = await PeerId.createFromPubKey(id1.marshalPubKey())