Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Commit

Permalink
fix: use latest multihashing-async as it is all promises now
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Jul 5, 2019
1 parent b6fbe7e commit 162c230
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 50 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ node_modules

dist
package-lock.json
yarn.lock
yarn.lock
.vscode
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
"license": "MIT",
"dependencies": {
"bs58": "^4.0.1",
"multihashing-async": "~0.6.0",
"multihashing-async": "^0.7.0",
"nodeify": "^1.0.1",
"safe-buffer": "^5.1.2",
"secp256k1": "^3.6.2"
},
"devDependencies": {
"aegir": "^18.2.2",
"aegir": "^19.0.5",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"dirty-chai": "^2.0.1",
Expand All @@ -61,4 +61,4 @@
"Yusef Napora <[email protected]>",
"dignifiedquire <[email protected]>"
]
}
}
16 changes: 3 additions & 13 deletions src/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const HASH_ALGORITHM = 'sha2-256'
module.exports = (randomBytes) => {
const privateKeyLength = 32

async function generateKey () {
function generateKey () {
let privateKey
do {
privateKey = randomBytes(32)
Expand All @@ -17,23 +17,13 @@ module.exports = (randomBytes) => {
}

async function hashAndSign (key, msg) {
const digest = await new Promise((resolve, reject) => {
multihashing.digest(msg, HASH_ALGORITHM, (err, digest) => {
if (err) return reject(err)
resolve(digest)
})
})
const digest = await multihashing.digest(msg, HASH_ALGORITHM)
const sig = secp256k1.sign(digest, key)
return secp256k1.signatureExport(sig.signature)
}

async function hashAndVerify (key, sig, msg) {
const digest = await new Promise((resolve, reject) => {
multihashing.digest(msg, HASH_ALGORITHM, (err, digest) => {
if (err) return reject(err)
resolve(digest)
})
})
const digest = await multihashing.digest(msg, HASH_ALGORITHM)
sig = secp256k1.signatureImport(sig)
return secp256k1.verify(digest, sig, key)
}
Expand Down
35 changes: 11 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = (keysProtobuf, randomBytes, crypto) => {
this._key = key
}

async verify (data, sig) {
verify (data, sig) {
return crypto.hashAndVerify(this._key, sig, data)
}

Expand All @@ -31,13 +31,8 @@ module.exports = (keysProtobuf, randomBytes, crypto) => {
return this.bytes.equals(key.bytes)
}

async hash () {
return new Promise((resolve, reject) => {
multihashing(this.bytes, 'sha2-256', (err, res) => {
if (err) return reject(err)
resolve(res)
})
})
hash () {
return multihashing(this.bytes, 'sha2-256')
}
}

Expand All @@ -49,7 +44,7 @@ module.exports = (keysProtobuf, randomBytes, crypto) => {
crypto.validatePublicKey(this._publicKey)
}

async sign (message) {
sign (message) {
return crypto.hashAndSign(this._key, message)
}

Expand All @@ -72,13 +67,8 @@ module.exports = (keysProtobuf, randomBytes, crypto) => {
return this.bytes.equals(key.bytes)
}

async hash () {
return new Promise((resolve, reject) => {
multihashing(this.bytes, 'sha2-256', (err, res) => {
if (err) return reject(err)
resolve(res)
})
})
hash () {
return multihashing(this.bytes, 'sha2-256')
}

/**
Expand All @@ -91,17 +81,14 @@ module.exports = (keysProtobuf, randomBytes, crypto) => {
* @param {function(Error, id)} callback
* @returns {undefined}
*/
id (callback) {
this.public.hash((err, hash) => {
if (err) {
return callback(err)
}
callback(null, bs58.encode(hash))
})
async id () {
const hash = await this.public.hash()

return bs58.encode(hash)
}
}

async function unmarshalSecp256k1PrivateKey (bytes) {
function unmarshalSecp256k1PrivateKey (bytes) {
return new Secp256k1PrivateKey(bytes)
}

Expand Down
15 changes: 6 additions & 9 deletions test/secp256k1.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,10 @@ describe('secp256k1 keys', () => {
expect(pkMarshal).to.eql(pkMarshal2)
})

it('key id', (done) => {
key.id((err, id) => {
expect(err).to.not.exist()
expect(id).to.exist()
expect(id).to.be.a('string')
done()
})
it('key id', async () => {
const id = await key.id()
expect(id).to.exist()
expect(id).to.be.a('string')
})

describe('key equals', () => {
Expand Down Expand Up @@ -103,7 +100,7 @@ describe('key generation error', () => {

before(() => {
generateKey = crypto.generateKey
crypto.generateKey = async () => { throw new Error('Error generating key') }
crypto.generateKey = () => { throw new Error('Error generating key') }
secp256k1 = require('../src')(keysPBM, randomBytes, crypto)
})

Expand All @@ -127,7 +124,7 @@ describe('handles generation of invalid key', () => {

before(() => {
generateKey = crypto.generateKey
crypto.generateKey = async () => Buffer.from('not a real key')
crypto.generateKey = () => Buffer.from('not a real key')
secp256k1 = require('../src')(keysPBM, randomBytes, crypto)
})

Expand Down

0 comments on commit 162c230

Please sign in to comment.