-
Notifications
You must be signed in to change notification settings - Fork 27
feat: add public key support #4
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
Changes from 4 commits
d88884c
f76b391
7812af8
bc86534
1c023a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,9 @@ | |
"debug": "^3.1.0", | ||
"interface-datastore": "^0.4.2", | ||
"left-pad": "^1.3.0", | ||
"multihashes": "^0.4.14", | ||
"nano-date": "^2.1.0", | ||
"peer-id": "^0.11.0", | ||
"protons": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
|
@@ -49,8 +51,7 @@ | |
"dirty-chai": "^2.0.1", | ||
"ipfs": "^0.29.3", | ||
"ipfsd-ctl": "^0.36.0", | ||
"libp2p-crypto": "^0.13.0", | ||
"multihashes": "^0.4.13" | ||
"libp2p-crypto": "^0.13.0" | ||
}, | ||
"contributors": [ | ||
"Vasco Santos <[email protected]>" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ const base32Encode = require('base32-encode') | |
const Big = require('big.js') | ||
const NanoDate = require('nano-date').default | ||
const { Key } = require('interface-datastore') | ||
const crypto = require('libp2p-crypto') | ||
const PeerId = require('peer-id') | ||
const multihash = require('multihashes') | ||
|
||
const debug = require('debug') | ||
const log = debug('jsipns') | ||
|
@@ -13,6 +16,7 @@ const ipnsEntryProto = require('./pb/ipns.proto') | |
const { parseRFC3339 } = require('./utils') | ||
const ERRORS = require('./errors') | ||
|
||
const ID_MULTIHASH_CODE = multihash.names.id | ||
/** | ||
* Creates a new ipns entry and signs it with the given private key. | ||
* The ipns entry validity should follow the [RFC3339]{@link https://www.ietf.org/rfc/rfc3339.txt} with nanoseconds precision. | ||
|
@@ -68,7 +72,7 @@ const validate = (publicKey, entry, callback) => { | |
const dataForSignature = ipnsEntryDataForSig(value, validityType, validity) | ||
|
||
// Validate Signature | ||
publicKey.verify(dataForSignature, entry.signature, (err, result) => { | ||
publicKey.verify(dataForSignature, entry.signature, (err) => { | ||
if (err) { | ||
log.error('record signature verification failed') | ||
return callback(Object.assign(new Error('record signature verification failed'), { code: ERRORS.ERR_SIGNATURE_VERIFICATION })) | ||
|
@@ -100,15 +104,44 @@ const validate = (publicKey, entry, callback) => { | |
} | ||
|
||
/** | ||
* Validates the given ipns entry against the given public key. | ||
* Embed the given public key in the given entry. While not strictly required, | ||
* some nodes (eg. DHT servers) may reject IPNS entries that don't embed their | ||
* public keys as they may not be able to validate them efficiently. | ||
* As a consequence of nodes needing to validade a record upon receipt, they need | ||
* the public key associated with it. For olde RSA keys, it is easier if we just | ||
* send this as part of the record itself. For newer ed25519 keys, the public key | ||
* can be embedded in the peerId. | ||
* | ||
* @param {Object} publicKey public key for validating the record. | ||
* @param {Object} publicKey public key to embed. | ||
* @param {Object} entry ipns entry record. | ||
* @param {function(Error)} [callback] | ||
* @return {Void} | ||
*/ | ||
const embedPublicKey = (publicKey, entry, callback) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check |
||
callback(new Error('not implemented yet')) | ||
// Create a peer id from the public key. | ||
PeerId.createFromPubKey(publicKey.bytes, (err, peerId) => { | ||
if (err) { | ||
log.error(err) | ||
return callback(Object.assign(new Error(err), { code: ERRORS.ERR_PEER_ID_FROM_PUBLIC_KEY })) | ||
} | ||
|
||
// Try to extract the public key from the ID. If we can, no need to embed it | ||
let extractedPublicKey | ||
try { | ||
extractedPublicKey = extractPublicKeyFromId(peerId) | ||
} catch (err) { | ||
log.error(err) | ||
return callback(Object.assign(new Error(err), { code: ERRORS.ERR_PUBLIC_KEY_FROM_ID })) | ||
} | ||
|
||
if (extractedPublicKey) { | ||
return callback(null, null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needs a result as the second parameter as per the docs...and a test ;) |
||
} | ||
|
||
// If we failed to extract the public key from the peer ID, embed it in the record. | ||
entry.pubKey = crypto.keys.marshalPublicKey(publicKey) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try/catch around this? |
||
callback(null, entry) | ||
}) | ||
} | ||
|
||
/** | ||
|
@@ -120,7 +153,17 @@ const embedPublicKey = (publicKey, entry, callback) => { | |
* @return {Void} | ||
*/ | ||
const extractPublicKey = (peerId, entry, callback) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check |
||
callback(new Error('not implemented yet')) | ||
if (entry.pubKey) { | ||
try { | ||
const pubKey = crypto.keys.unmarshalPublicKey(entry.pubKey) | ||
|
||
return callback(null, pubKey) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid callback in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. true! |
||
} catch (err) { | ||
log.error(err) | ||
return callback(err) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor, use |
||
callback(null, peerId.pubKey) | ||
} | ||
|
||
// rawStdEncoding with RFC4648 | ||
|
@@ -139,16 +182,16 @@ const getLocalKey = (key) => new Key(`/ipns/${rawStdEncoding(key)}`) | |
* Get key for sharing the record in the routing mechanism. | ||
* Format: ${base32(/ipns/<HASH>)}, ${base32(/pk/<HASH>)} | ||
* | ||
* @param {Buffer} key peer identifier object. | ||
* @param {Buffer} pid peer identifier represented by the multihash of the public key as Buffer. | ||
* @returns {Object} containing the `nameKey` and the `ipnsKey`. | ||
*/ | ||
const getIdKeys = (key) => { | ||
const getIdKeys = (pid) => { | ||
const pkBuffer = Buffer.from('/pk/') | ||
const ipnsBuffer = Buffer.from('/ipns/') | ||
|
||
return { | ||
nameKey: rawStdEncoding(Buffer.concat([pkBuffer, key])), | ||
ipnsKey: rawStdEncoding(Buffer.concat([ipnsBuffer, key])) | ||
pkKey: new Key(rawStdEncoding(Buffer.concat([pkBuffer, pid]))), | ||
ipnsKey: new Key(rawStdEncoding(Buffer.concat([ipnsBuffer, pid]))) | ||
} | ||
} | ||
|
||
|
@@ -164,13 +207,35 @@ const sign = (privateKey, value, validityType, validity, callback) => { | |
}) | ||
} | ||
|
||
// Create record data for being signed | ||
const ipnsEntryDataForSig = (value, validityType, eol) => { | ||
// Utility for getting the validity type code name of a validity | ||
const getValidityType = (validityType) => { | ||
if (validityType.toString() === '0') { | ||
return 'EOL' | ||
} else { | ||
const error = `unrecognized validity type ${validityType.toString()}` | ||
log.error(error) | ||
throw Object.assign(new Error(error), { code: ERRORS.ERR_UNRECOGNIZED_VALIDITY }) | ||
} | ||
} | ||
|
||
// Utility for creating the record data for being signed | ||
const ipnsEntryDataForSig = (value, validityType, validity) => { | ||
const valueBuffer = Buffer.from(value) | ||
const validityTypeBuffer = Buffer.from(validityType.toString()) | ||
const eolBuffer = Buffer.from(eol) | ||
const validityTypeBuffer = Buffer.from(getValidityType(validityType)) | ||
const validityBuffer = Buffer.from(validity) | ||
|
||
return Buffer.concat([valueBuffer, validityBuffer, validityTypeBuffer]) | ||
} | ||
|
||
// Utility for extracting the public key from a peer-id | ||
const extractPublicKeyFromId = (peerId) => { | ||
const decodedId = multihash.decode(peerId.id) | ||
|
||
if (decodedId.code !== ID_MULTIHASH_CODE) { | ||
return null | ||
} | ||
|
||
return Buffer.concat([valueBuffer, validityTypeBuffer, eolBuffer]) | ||
return crypto.keys.unmarshalPublicKey(decodedId.digest) | ||
} | ||
|
||
module.exports = { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to move to
dependencies