-
Notifications
You must be signed in to change notification settings - Fork 25
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 public key support #4
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d88884c
feat: add public key support
vasco-santos f76b391
fix: code review
vasco-santos 7812af8
fix: code review
vasco-santos bc86534
Merge branch 'feat/add-public-key-support' of github.com:ipfs/js-ipns…
vasco-santos 1c023a6
fix: code review
vasco-santos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,10 @@ | |
"debug": "^3.1.0", | ||
"interface-datastore": "^0.4.2", | ||
"left-pad": "^1.3.0", | ||
"libp2p-crypto": "^0.13.0", | ||
"multihashes": "^0.4.14", | ||
"nano-date": "^2.1.0", | ||
"peer-id": "^0.11.0", | ||
"protons": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
|
@@ -48,9 +51,7 @@ | |
"chai-string": "^1.4.0", | ||
"dirty-chai": "^2.0.1", | ||
"ipfs": "^0.29.3", | ||
"ipfsd-ctl": "^0.36.0", | ||
"libp2p-crypto": "^0.13.0", | ||
"multihashes": "^0.4.13" | ||
"ipfsd-ctl": "^0.36.0" | ||
}, | ||
"contributors": [ | ||
"Vasco Santos <[email protected]>" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
@@ -44,7 +48,7 @@ const create = (privateKey, value, seq, lifetime, callback) => { | |
|
||
const entry = { | ||
value: value, | ||
signature: signature, // TODO confirm format compliance with go-ipfs | ||
signature: signature, | ||
validityType: validityType, | ||
validity: isoValidity, | ||
sequence: seq | ||
|
@@ -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,56 @@ 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) => { | ||
callback(new Error('not implemented yet')) | ||
if (!publicKey || !publicKey.bytes || !entry) { | ||
const error = 'one or more of the provided parameters are not defined' | ||
|
||
log.error(error) | ||
return callback(Object.assign(new Error(error), { code: ERRORS.ERR_UNDEFINED_PARAMETER })) | ||
} | ||
|
||
// 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) | ||
} | ||
|
||
// If we failed to extract the public key from the peer ID, embed it in the record. | ||
try { | ||
entry.pubKey = crypto.keys.marshalPublicKey(publicKey) | ||
} catch (err) { | ||
log.error(err) | ||
return callback(err) | ||
} | ||
callback(null, entry) | ||
}) | ||
} | ||
|
||
/** | ||
|
@@ -120,7 +165,24 @@ 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 || !peerId) { | ||
const error = 'one or more of the provided parameters are not defined' | ||
|
||
log.error(error) | ||
return callback(Object.assign(new Error(error), { code: ERRORS.ERR_UNDEFINED_PARAMETER })) | ||
} | ||
|
||
if (entry.pubKey) { | ||
let pubKey | ||
try { | ||
pubKey = crypto.keys.unmarshalPublicKey(entry.pubKey) | ||
} catch (err) { | ||
log.error(err) | ||
return callback(err) | ||
} | ||
return callback(null, pubKey) | ||
} | ||
callback(null, peerId.pubKey) | ||
} | ||
|
||
// rawStdEncoding with RFC4648 | ||
|
@@ -139,16 +201,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 +226,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 = { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Check
publicKey
,publicKey.bytes
andentry
exist?