Skip to content

Commit

Permalink
feat: add compact protobuf format
Browse files Browse the repository at this point in the history
  • Loading branch information
mkg20001 committed Jul 12, 2019
1 parent f50b2ac commit 5322a8e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"dependencies": {
"class-is": "^1.1.0",
"libp2p-crypto": "~0.17.0",
"multihashes": "~0.4.15"
"multihashes": "~0.4.15",
"protons": "^1.0.1"
},
"repository": {
"type": "git",
Expand Down
53 changes: 53 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const mh = require('multihashes')
const cryptoKeys = require('libp2p-crypto/src/keys')
const assert = require('assert')
const withIs = require('class-is')
const {PeerIdProto} = require('./proto')

class PeerId {
constructor (id, privKey, pubKey) {
Expand Down Expand Up @@ -67,6 +68,15 @@ class PeerId {
}
}

// Return the protobuf version of the peer-id
marshal (excludePriv) {
return PeerIdProto.encode({
id: this.toBytes(),
pubKey: this.marshalPubKey(),
privKey: excludePriv ? null : this.marshalPrivKey()
})
}

toPrint () {
let pid = this.toB58String()
// All sha256 nodes start with Qm
Expand Down Expand Up @@ -232,6 +242,49 @@ exports.createFromJSON = async (obj) => {
return new PeerIdWithIs(id, privKey, pub)
}

exports.createFromProtobuf = async (buf) => {
if (typeof buf === 'string') {
buf = Buffer.from(buf, 'hex')
}

let {id, privKey, pubKey} = PeerIdProto.decode(buf)

privKey = privKey ? await cryptoKeys.unmarshalPrivateKey(privKey) : false
pubKey = pubKey ? await cryptoKeys.unmarshalPublicKey(pubKey) : false

let pubDigest
let privDigest

if (privKey) {
privDigest = await computeDigest(privKey.public)
}

if (pubKey) {
pubDigest = await computeDigest(pubKey)
}

if (privKey) {
if (pubKey) {
if (!privDigest.equals(pubDigest)) {
throw new Error('Public and private key do not match')
}
}
return new PeerIdWithIs(privDigest, privKey, privKey.public)
}

// TODO: val id and pubDigest

if (pubKey) {
return new PeerIdWithIs(pubDigest, null, pubKey)
}

if (id) {
return new PeerIdWithIs(id)
}

throw new Error('Protobuf did not contain any usable key material')
}

exports.isPeerId = (peerId) => {
return Boolean(typeof peerId === 'object' &&
peerId._id &&
Expand Down
12 changes: 12 additions & 0 deletions src/proto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const protons = require('protons')
module.exports = protons(`
message PeerIdProto {
required bytes id = 1;
bytes pubKey = 2;
bytes privKey = 3;
}
`)
3 changes: 2 additions & 1 deletion test/fixtures/sample-id.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion test/peer-id.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ 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())
})

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)
Expand Down Expand Up @@ -200,7 +209,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) {
Expand Down

0 comments on commit 5322a8e

Please sign in to comment.