Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add compact protobuf format #76

Merged
merged 4 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ Returns `Promise<PeerId>`.

Returns `Promise<PeerId>`.

### `createFromProtobuf(buf)`

`buf` is a protocol-buffers encoded PeerId (see `marshal()`)

## Export

### `toHexString()`
Expand Down Expand Up @@ -209,6 +213,10 @@ Returns an `obj` of the form
- `obj.pubKey: String` - The public key in protobuf format, encoded in 'base64'
- `obj.privKey: String` - The private key in protobuf format, encoded in 'base 64'

### `marshal(excludePrivatKey)`

Returns a protocol-buffers encoded version of the id, public key and, if `excludePrivateKey` is not set, the private key.

### `toPrint()`

Returns the Peer ID as a printable string without the `Qm` prefix.
Expand Down
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.

12 changes: 11 additions & 1 deletion test/peer-id.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
mkg20001 marked this conversation as resolved.
Show resolved Hide resolved

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 +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) {
Expand Down