feat: nextTick instead of setImmediate, and fix sync in async (#136)

* fix: avoid sync callback in async function

* chore: fix linting

* chore: remove non jenkins ci

* refactor: use nextTick over setImmediate

* refactor: async/nextTick for better browser support
This commit is contained in:
Jacob Heun
2019-01-03 08:13:07 -08:00
committed by David Dias
parent 857d2bd902
commit c54ea206f0
15 changed files with 38 additions and 110 deletions

View File

@ -1,7 +1,7 @@
'use strict'
const crypto = require('crypto')
const setImmediate = require('async/setImmediate')
const nextTick = require('async/nextTick')
const curves = {
'P-256': 'prime256v1',
@ -16,7 +16,7 @@ exports.generateEphmeralKeyPair = function (curve, callback) {
const ecdh = crypto.createECDH(curves[curve])
ecdh.generateKeys()
setImmediate(() => callback(null, {
nextTick(() => callback(null, {
key: ecdh.getPublicKey(),
genSharedKey (theirPub, forcePrivate, cb) {
if (typeof forcePrivate === 'function') {
@ -35,7 +35,7 @@ exports.generateEphmeralKeyPair = function (curve, callback) {
return cb(err)
}
setImmediate(() => cb(null, secret))
nextTick(() => cb(null, secret))
}
}))
}

View File

@ -1,13 +1,13 @@
'use strict'
const nacl = require('tweetnacl')
const setImmediate = require('async/setImmediate')
const nextTick = require('async/nextTick')
exports.publicKeyLength = nacl.sign.publicKeyLength
exports.privateKeyLength = nacl.sign.secretKeyLength
exports.generateKey = function (callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
result = nacl.sign.keyPair()
@ -20,7 +20,7 @@ exports.generateKey = function (callback) {
// seed should be a 32 byte uint8array
exports.generateKeyFromSeed = function (seed, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
result = nacl.sign.keyPair.fromSeed(seed)
@ -32,13 +32,13 @@ exports.generateKeyFromSeed = function (seed, callback) {
}
exports.hashAndSign = function (key, msg, callback) {
setImmediate(() => {
nextTick(() => {
callback(null, Buffer.from(nacl.sign.detached(msg, key)))
})
}
exports.hashAndVerify = function (key, sig, msg, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
result = nacl.sign.detached.verify(msg, sig, key)

View File

@ -12,7 +12,7 @@ exports.generateKey = function (bits, callback) {
name: 'RSASSA-PKCS1-v1_5',
modulusLength: bits,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
true,
['sign', 'verify']
@ -31,7 +31,7 @@ exports.unmarshalPrivateKey = function (key, callback) {
key,
{
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
true,
['sign']
@ -59,13 +59,13 @@ exports.hashAndSign = function (key, msg, callback) {
key,
{
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
false,
['sign']
).then((privateKey) => {
return webcrypto.subtle.sign(
{name: 'RSASSA-PKCS1-v1_5'},
{ name: 'RSASSA-PKCS1-v1_5' },
privateKey,
Uint8Array.from(msg)
)
@ -78,13 +78,13 @@ exports.hashAndVerify = function (key, sig, msg, callback) {
key,
{
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
false,
['verify']
).then((publicKey) => {
return webcrypto.subtle.verify(
{name: 'RSASSA-PKCS1-v1_5'},
{ name: 'RSASSA-PKCS1-v1_5' },
publicKey,
sig,
msg
@ -109,7 +109,7 @@ function derivePublicFromPrivate (jwKey) {
},
{
name: 'RSASSA-PKCS1-v1_5',
hash: {name: 'SHA-256'}
hash: { name: 'SHA-256' }
},
true,
['verify']

View File

@ -3,11 +3,11 @@
const multihashing = require('multihashing-async')
const protobuf = require('protons')
const bs58 = require('bs58')
const nextTick = require('async/nextTick')
const crypto = require('./rsa')
const pbm = protobuf(require('./keys.proto'))
const forge = require('node-forge')
const setImmediate = require('async/setImmediate')
class RsaPublicKey {
constructor (key) {
@ -129,7 +129,7 @@ class RsaPrivateKey {
ensure(callback)
setImmediate(() => {
nextTick(() => {
let err = null
let pem = null
try {

View File

@ -1,6 +1,8 @@
'use strict'
const crypto = require('crypto')
const nextTick = require('async/nextTick')
let keypair
try {
if (process.env.LP2P_FORCE_CRYPTO_LIB === 'keypair') {
@ -8,7 +10,7 @@ try {
}
const ursa = require('ursa-optional') // throws if not compiled
keypair = ({bits}) => {
keypair = ({ bits }) => {
const key = ursa.generatePrivateKey(bits)
return {
private: key.toPrivatePem(),
@ -22,14 +24,13 @@ try {
keypair = require('keypair')
}
const setImmediate = require('async/setImmediate')
const pemToJwk = require('pem-jwk').pem2jwk
const jwkToPem = require('pem-jwk').jwk2pem
exports.utils = require('./rsa-utils')
exports.generateKey = function (bits, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
const key = keypair({ bits: bits })
@ -47,7 +48,7 @@ exports.generateKey = function (bits, callback) {
// Takes a jwk key
exports.unmarshalPrivateKey = function (key, callback) {
setImmediate(() => {
nextTick(() => {
if (!key) {
return callback(new Error('Key is invalid'))
}
@ -67,7 +68,7 @@ exports.getRandomValues = function (arr) {
}
exports.hashAndSign = function (key, msg, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
const sign = crypto.createSign('RSA-SHA256')
@ -83,7 +84,7 @@ exports.hashAndSign = function (key, msg, callback) {
}
exports.hashAndVerify = function (key, sig, msg, callback) {
setImmediate(() => {
nextTick(() => {
let result
try {
const verify = crypto.createVerify('RSA-SHA256')