feat: add compact protobuf format (#76)

* docs: document .createFromProtobuf and .marshal
* fix: lint
* test: verify .marshal() does it's job
This commit is contained in:
Maciej Krüger
2019-07-12 19:35:46 +02:00
committed by Jacob Heun
parent f50b2ac016
commit 76864184da
6 changed files with 88 additions and 3 deletions

View File

@ -86,6 +86,16 @@ describe('PeerId', () => {
expect(id.marshalPubKey()).to.deep.equal(id2.marshalPubKey())
})
it('Recreate from Protobuf', async () => {
const id = await PeerId.createFromProtobuf(testId.marshaled)
expect(testIdB58String).to.equal(id.toB58String())
const encoded = Buffer.from(testId.privKey, 'base64')
const id2 = await PeerId.createFromPrivKey(encoded)
expect(testIdB58String).to.equal(id2.toB58String())
expect(id.marshalPubKey()).to.deep.equal(id2.marshalPubKey())
expect(id.marshal().toString('hex')).to.deep.equal(testId.marshaled)
})
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)
@ -200,7 +210,7 @@ describe('PeerId', () => {
Buffer.from(''), 'aGVsbG93b3JsZA==', 'helloworld', ''
]
const fncs = ['createFromPubKey', 'createFromPrivKey', 'createFromJSON']
const fncs = ['createFromPubKey', 'createFromPrivKey', 'createFromJSON', 'createFromProtobuf']
for (const gb of garbage) {
for (const fn of fncs) {