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

chore: downgrade peer-id to the same version used by libp2p #26

Merged
merged 1 commit into from
Sep 25, 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
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,25 @@
"dependencies": {
"base32-encode": "^1.1.0",
"debug": "^4.1.1",
"err-code": "^1.1.2",
"err-code": "^2.0.0",
"interface-datastore": "~0.7.0",
"left-pad": "^1.3.0",
"libp2p-crypto": "~0.17.0",
"libp2p-crypto": "^0.16.2",
"multihashes": "~0.4.14",
"peer-id": "~0.13.2",
"peer-id": "^0.12.2",
"promisify-es6": "^1.0.3",
"protons": "^1.0.1",
"timestamp-nano": "^1.0.0"
},
"devDependencies": {
"aegir": "^18.0.3",
"aegir": "^20.3.1",
"chai": "^4.2.0",
"chai-bytes": "~0.1.2",
"chai-string": "^1.5.0",
"dirty-chai": "^2.0.1",
"ipfs": "~0.33.1",
"ipfsd-ctl": "~0.40.2"
"ipfs": "^0.37.1",
"ipfs-http-client": "^33.1.1",
"ipfsd-ctl": "^0.47.2"
},
"contributors": [
"Diogo Silva <[email protected]>",
Expand Down
32 changes: 18 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const crypto = require('libp2p-crypto')
const PeerId = require('peer-id')
const multihash = require('multihashes')
const errCode = require('err-code')
const promisify = require('promisify-es6')

const debug = require('debug')
const log = debug('jsipns')
Expand Down Expand Up @@ -91,13 +92,15 @@ const validate = async (publicKey, entry) => {
// Validate Signature
let isValid
try {
isValid = await publicKey.verify(dataForSignature, entry.signature)
isValid = await promisify(publicKey.verify, {
context: publicKey
})(dataForSignature, entry.signature)
} catch (err) {
isValid = false
}
if (!isValid) {
log.error('record signature verification failed')
throw errCode('record signature verification failed', ERRORS.ERR_SIGNATURE_VERIFICATION)
throw errCode(new Error('record signature verification failed'), ERRORS.ERR_SIGNATURE_VERIFICATION)
}

// Validate according to the validity type
Expand All @@ -108,16 +111,16 @@ const validate = async (publicKey, entry) => {
validityDate = parseRFC3339(validity.toString())
} catch (e) {
log.error('unrecognized validity format (not an rfc3339 format)')
throw errCode('unrecognized validity format (not an rfc3339 format)', ERRORS.ERR_UNRECOGNIZED_FORMAT)
throw errCode(new Error('unrecognized validity format (not an rfc3339 format)'), ERRORS.ERR_UNRECOGNIZED_FORMAT)
}

if (validityDate < Date.now()) {
log.error('record has expired')
throw errCode('record has expired', ERRORS.ERR_IPNS_EXPIRED_RECORD)
throw errCode(new Error('record has expired'), ERRORS.ERR_IPNS_EXPIRED_RECORD)
}
} else if (validityType) {
log.error('unrecognized validity type')
throw errCode('unrecognized validity type', ERRORS.ERR_UNRECOGNIZED_VALIDITY)
throw errCode(new Error('unrecognized validity type'), ERRORS.ERR_UNRECOGNIZED_VALIDITY)
}

log(`ipns entry for ${value} is valid`)
Expand All @@ -138,15 +141,15 @@ const validate = async (publicKey, entry) => {
*/
const embedPublicKey = async (publicKey, entry) => {
if (!publicKey || !publicKey.bytes || !entry) {
const error = 'one or more of the provided parameters are not defined'
const error = new Error('one or more of the provided parameters are not defined')
log.error(error)
throw errCode(error, ERRORS.ERR_UNDEFINED_PARAMETER)
}

// Create a peer id from the public key.
let peerId
try {
peerId = await PeerId.createFromPubKey(publicKey.bytes)
peerId = await promisify(PeerId.createFromPubKey)(publicKey.bytes)
} catch (err) {
throw errCode(err, ERRORS.ERR_PEER_ID_FROM_PUBLIC_KEY)
}
Expand Down Expand Up @@ -183,7 +186,7 @@ const embedPublicKey = async (publicKey, entry) => {
*/
const extractPublicKey = (peerId, entry) => {
if (!entry || !peerId) {
const error = 'one or more of the provided parameters are not defined'
const error = new Error('one or more of the provided parameters are not defined')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do the new Error only on throw?

Same applies on another case below

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There won't be a stack trace in the logs if we just log the message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually prefer to have clean logs and with the ERR CODE find and replicate the issue. But, I understand your point as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's only one place in the entire codebase a certain error code is emitted, and it's also only invoked from one place in the entire codebase I'd agree, but that's rarely the case so more verbose logs can only help.


log.error(error)
throw errCode(error, ERRORS.ERR_UNDEFINED_PARAMETER)
Expand Down Expand Up @@ -241,10 +244,13 @@ const getIdKeys = (pid) => {
const sign = (privateKey, value, validityType, validity) => {
try {
const dataForSignature = ipnsEntryDataForSig(value, validityType, validity)
return privateKey.sign(dataForSignature)

return promisify(privateKey.sign, {
context: privateKey
})(dataForSignature)
} catch (error) {
log.error('record signature creation failed')
throw errCode('record signature creation failed: ' + error.message, ERRORS.ERR_SIGNATURE_CREATION)
throw errCode(new Error('record signature creation failed: ' + error.message), ERRORS.ERR_SIGNATURE_CREATION)
}
}

Expand All @@ -254,7 +260,7 @@ const getValidityType = (validityType) => {
return 'EOL'
}

const error = `unrecognized validity type ${validityType.toString()}`
const error = new Error(`unrecognized validity type ${validityType.toString()}`)
log.error(error)
throw errCode(error, ERRORS.ERR_UNRECOGNIZED_VALIDITY)
}
Expand Down Expand Up @@ -287,9 +293,7 @@ const validator = {
validate: async (marshalledData, key) => {
const receivedEntry = unmarshal(marshalledData)
const bufferId = key.slice('/ipns/'.length)
let peerId

peerId = PeerId.createFromBytes(bufferId)
const peerId = PeerId.createFromBytes(bufferId)

// extract public key
const pubKey = extractPublicKey(peerId, receivedEntry)
Expand Down
45 changes: 18 additions & 27 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ chai.use(chaiBytes)
chai.use(chaiString)

const ipfs = require('ipfs')
const ipfsHttpClient = require('ipfs-http-client')
const DaemonFactory = require('ipfsd-ctl')
const crypto = require('libp2p-crypto')
const { fromB58String } = require('multihashes')
const promisify = require('promisify-es6')

const ipns = require('../src')
const ERRORS = require('../src/errors')

const df = DaemonFactory.create({ type: 'proc', exec: ipfs })
const df = DaemonFactory.create({
type: 'proc',
exec: ipfs,
IpfsClient: ipfsHttpClient
})

describe('ipns', function () {
this.timeout(20 * 1000)
Expand All @@ -30,35 +36,18 @@ describe('ipns', function () {
let ipfsId = null
let rsa = null

const spawnDaemon = () => {
return new Promise((resolve, reject) => {
df.spawn({ initOptions: { bits: 512 } }, (err, _ipfsd) => {
expect(err).to.not.exist()
ipfsd = _ipfsd
ipfs = ipfsd.api

ipfs.id((err, id) => {
if (err) {
return reject(err)
}

ipfsId = id
resolve()
})
})
})
}

before(async () => {
rsa = await crypto.keys.generateKeyPair('RSA', 2048)
return spawnDaemon()
rsa = await promisify(crypto.keys.generateKeyPair, {
context: crypto.keys
})('RSA', 2048)
ipfsd = await df.spawn({ initOptions: { bits: 512 } })
ipfs = ipfsd.api
ipfsId = await ipfs.id()
})

after(function (done) {
after(async () => {
if (ipfsd) {
ipfsd.stop(() => done())
} else {
done()
await ipfsd.stop()
}
})

Expand Down Expand Up @@ -191,7 +180,9 @@ describe('ipns', function () {
const sequence = 0
const validity = 1000000

const ed25519 = await crypto.keys.generateKeyPair('ed25519', 2048)
const ed25519 = await promisify(crypto.keys.generateKeyPair, {
context: crypto.keys
})('ed25519', 2048)
const entry = await ipns.create(ed25519, cid, sequence, validity)
const entryWithKey = ipns.embedPublicKey(ed25519.public, entry)
expect(entryWithKey).to.not.exist() // Should be null
Expand Down