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 4485864
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 44 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]>"
]
}
}
14 changes: 2 additions & 12 deletions src/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 8 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 Down Expand Up @@ -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,13 +81,10 @@ 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)
}
}

Expand Down
11 changes: 4 additions & 7 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

0 comments on commit 4485864

Please sign in to comment.