diff --git a/package.json b/package.json index 4aa40f0..254e00d 100644 --- a/package.json +++ b/package.json @@ -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 ", diff --git a/src/index.js b/src/index.js index e6cc4f2..dd1738a 100644 --- a/src/index.js +++ b/src/index.js @@ -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') @@ -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 @@ -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`) @@ -138,7 +141,7 @@ 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) } @@ -146,7 +149,7 @@ const embedPublicKey = async (publicKey, entry) => { // 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) } @@ -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') log.error(error) throw errCode(error, ERRORS.ERR_UNDEFINED_PARAMETER) @@ -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) } } @@ -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) } @@ -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) diff --git a/test/index.spec.js b/test/index.spec.js index 595b24e..6f6151d 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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) @@ -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() } }) @@ -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