Skip to content

Commit

Permalink
feat: support inline public keys in messages
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Sep 26, 2019
2 parents 65fe4a5 + fbb1803 commit 55624b5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
include:
- stage: check
script:
- npx aegir commitlint --travis
- npx aegir dep-check
- npm run lint

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"libp2p-websocket-star-rendezvous": "~0.3.0",
"lodash": "^4.17.11",
"multiaddr": "^6.0.6",
"peer-id": "~0.12.2",
"peer-id": "~0.12.5",
"peer-info": "~0.15.1"
},
"dependencies": {
Expand Down
9 changes: 7 additions & 2 deletions src/message/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@ function messagePublicKey (message, callback) {
callback(new Error('Public Key does not match the originator'))
})
return
} else {
// should be available in the from property of the message (peer id)
const from = PeerId.createFromBytes(message.from)
if (from.pubKey) {
return callback(null, from.pubKey)
}
}
// TODO: Once js libp2p supports inlining public keys with the peer id
// attempt to unmarshal the public key here.

callback(new Error('Could not get the public key from the originator id'))
}

Expand Down
36 changes: 36 additions & 0 deletions test/sign.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,42 @@ describe('message signing', () => {
})
})

it('should be able to extract the public key from an inlined key', (done) => {
const testSecp256k1 = (peerId) => {
const message = {
from: peerId.id,
data: 'hello',
seqno: randomSeqno(),
topicIDs: ['test-topic']
}

const bytesToSign = Buffer.concat([SignPrefix, Message.encode(message)])
peerId.privKey.sign(bytesToSign, (err, expectedSignature) => {
if (err) return done(err)

signMessage(peerId, message, (err, signedMessage) => {
if (err) return done(err)

// Check the signature and public key
expect(signedMessage.signature).to.eql(expectedSignature)
signedMessage.key = undefined

// Verify the signature
verifySignature(signedMessage, (err, verified) => {
expect(err).to.not.exist()
expect(verified).to.eql(true)
done(err)
})
})
})
}

PeerId.create({ keyType: 'secp256k1', bits: 256 }, (err, peerId) => {
expect(err).to.not.exist()
testSecp256k1(peerId)
})
})

it('should be able to extract the public key from the message', (done) => {
const message = {
from: peerId.id,
Expand Down

0 comments on commit 55624b5

Please sign in to comment.