From 670566b7ebcd92f949ef67a8d0624cdfc57bcb62 Mon Sep 17 00:00:00 2001 From: Matt C Date: Thu, 21 Dec 2017 14:23:31 +0000 Subject: [PATCH] Promisified generation of key pair --- package-lock.json | 141 +++++++++++++++++++++++++++-- package.json | 1 + src/core/config/OperationConfig.js | 1 + src/core/operations/PGP.js | 49 +++------- 4 files changed, 152 insertions(+), 40 deletions(-) diff --git a/package-lock.json b/package-lock.json index 43bbb206..23c35c41 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1065,6 +1065,11 @@ "integrity": "sha1-eRQg1/VR7qKJdFOop3ZT+WYG1nw=", "dev": true }, + "bn": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/bn/-/bn-1.0.1.tgz", + "integrity": "sha1-oVOCXmsessLbdyYUmwR6B84KO7M=" + }, "bn.js": { "version": "4.11.8", "resolved": "https://registry.npmjs.org/bn.js/-/bn.js-4.11.8.tgz", @@ -1284,6 +1289,11 @@ "integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg=", "dev": true }, + "bzip-deflate": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/bzip-deflate/-/bzip-deflate-1.0.0.tgz", + "integrity": "sha1-sC2wB+83vrzCk4Skssb08PTHlsk=" + }, "caller-path": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/caller-path/-/caller-path-0.1.0.tgz", @@ -2041,8 +2051,7 @@ "deep-equal": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", - "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=", - "dev": true + "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=" }, "deep-extend": { "version": "0.4.2", @@ -2437,8 +2446,15 @@ "es6-promise": { "version": "4.0.5", "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.0.5.tgz", - "integrity": "sha1-eILzCt3lskDM+n99eMVIMwlRrkI=", - "dev": true + "integrity": "sha1-eILzCt3lskDM+n99eMVIMwlRrkI=" + }, + "es6-promisify": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/es6-promisify/-/es6-promisify-5.0.0.tgz", + "integrity": "sha1-UQnWLz5W6pZ8S2NQWu8IKRyKUgM=", + "requires": { + "es6-promise": "4.0.5" + } }, "es6-set": { "version": "0.1.5", @@ -4061,6 +4077,24 @@ "integrity": "sha1-P5E2XKvmC3ftDruiS0VOPgnZWoI=", "dev": true }, + "iced-error": { + "version": "0.0.12", + "resolved": "https://registry.npmjs.org/iced-error/-/iced-error-0.0.12.tgz", + "integrity": "sha1-4KhhRigXzwzpdLE/ymEtOg1dEL4=" + }, + "iced-lock": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/iced-lock/-/iced-lock-1.1.0.tgz", + "integrity": "sha1-YRbvHKs6zW5rEIk7snumIv0/3nI=", + "requires": { + "iced-runtime": "1.0.3" + } + }, + "iced-runtime": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/iced-runtime/-/iced-runtime-1.0.3.tgz", + "integrity": "sha1-LU9PuZmreqVDCxk8d6f85BGDGc4=" + }, "iconv-lite": { "version": "0.4.19", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.19.tgz", @@ -4877,12 +4911,69 @@ "resolved": "https://registry.npmjs.org/jsrsasign/-/jsrsasign-8.0.4.tgz", "integrity": "sha1-P3uCOIRPEmtJanVW7J9LUR+V+GE=" }, + "kbpgp": { + "version": "2.0.76", + "resolved": "https://registry.npmjs.org/kbpgp/-/kbpgp-2.0.76.tgz", + "integrity": "sha1-qKtufM8279812BNdfJb/bpSLMAI=", + "requires": { + "bn": "1.0.1", + "bzip-deflate": "1.0.0", + "deep-equal": "1.0.1", + "iced-error": "0.0.12", + "iced-lock": "1.1.0", + "iced-runtime": "1.0.3", + "keybase-ecurve": "1.0.0", + "keybase-nacl": "1.0.10", + "minimist": "1.2.0", + "pgp-utils": "0.0.34", + "purepack": "1.0.4", + "triplesec": "3.0.26", + "tweetnacl": "0.13.3" + }, + "dependencies": { + "minimist": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", + "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=" + }, + "tweetnacl": { + "version": "0.13.3", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.13.3.tgz", + "integrity": "sha1-1ii1bzvMPVrnS6nUwacE3vWrS1Y=" + } + } + }, "kew": { "version": "0.7.0", "resolved": "https://registry.npmjs.org/kew/-/kew-0.7.0.tgz", "integrity": "sha1-edk9LTM2PW/dKXCzNdkUGtWR15s=", "dev": true }, + "keybase-ecurve": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/keybase-ecurve/-/keybase-ecurve-1.0.0.tgz", + "integrity": "sha1-xrxyrdpGA/0xhP7n6ZaU7Y/WmtI=", + "requires": { + "bn": "1.0.1" + } + }, + "keybase-nacl": { + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/keybase-nacl/-/keybase-nacl-1.0.10.tgz", + "integrity": "sha1-OGWDHpSBUWSI33y9mJRn6VDYeos=", + "requires": { + "iced-runtime": "1.0.3", + "tweetnacl": "0.13.3", + "uint64be": "1.0.1" + }, + "dependencies": { + "tweetnacl": { + "version": "0.13.3", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-0.13.3.tgz", + "integrity": "sha1-1ii1bzvMPVrnS6nUwacE3vWrS1Y=" + } + } + }, "kind-of": { "version": "3.2.2", "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz", @@ -5312,6 +5403,14 @@ "moment": "2.18.1" } }, + "more-entropy": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/more-entropy/-/more-entropy-0.0.7.tgz", + "integrity": "sha1-Z7/G96hvJvvDeqyD/UbYjGHRCbU=", + "requires": { + "iced-runtime": "1.0.3" + } + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -5840,6 +5939,15 @@ "integrity": "sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=", "dev": true }, + "pgp-utils": { + "version": "0.0.34", + "resolved": "https://registry.npmjs.org/pgp-utils/-/pgp-utils-0.0.34.tgz", + "integrity": "sha1-2E9J98GTteC5QV9cxcKmle15DCM=", + "requires": { + "iced-error": "0.0.12", + "iced-runtime": "1.0.3" + } + }, "phantomjs-prebuilt": { "version": "2.1.15", "resolved": "https://registry.npmjs.org/phantomjs-prebuilt/-/phantomjs-prebuilt-2.1.15.tgz", @@ -6870,8 +6978,7 @@ "progress": { "version": "1.1.8", "resolved": "https://registry.npmjs.org/progress/-/progress-1.1.8.tgz", - "integrity": "sha1-4mDHj2Fhzdmw5WzD4Khd4Xx6V74=", - "dev": true + "integrity": "sha1-4mDHj2Fhzdmw5WzD4Khd4Xx6V74=" }, "promise": { "version": "7.3.1", @@ -6945,6 +7052,11 @@ "integrity": "sha1-wNWmOycYgArY4esPpSachN1BhF4=", "dev": true }, + "purepack": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/purepack/-/purepack-1.0.4.tgz", + "integrity": "sha1-CGKC/ZOShfWGZLqam7oxzbFlzNI=" + }, "q": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/q/-/q-1.5.0.tgz", @@ -8245,6 +8357,18 @@ "integrity": "sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM=", "dev": true }, + "triplesec": { + "version": "3.0.26", + "resolved": "https://registry.npmjs.org/triplesec/-/triplesec-3.0.26.tgz", + "integrity": "sha1-3/K7R1ikIzcuc5o5fYmR8Fl9CsE=", + "requires": { + "iced-error": "0.0.12", + "iced-lock": "1.1.0", + "iced-runtime": "1.0.3", + "more-entropy": "0.0.7", + "progress": "1.1.8" + } + }, "tryit": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/tryit/-/tryit-1.0.3.tgz", @@ -8356,6 +8480,11 @@ } } }, + "uint64be": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/uint64be/-/uint64be-1.0.1.tgz", + "integrity": "sha1-H3FUIC8qG4rzU4cd2mUb80zpPpU=" + }, "underscore": { "version": "1.7.0", "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.7.0.tgz", diff --git a/package.json b/package.json index fa55d305..27713acd 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ "crypto-api": "^0.7.5", "crypto-js": "^3.1.9-1", "diff": "^3.3.1", + "es6-promisify": "^5.0.0", "escodegen": "^1.9.0", "esmangle": "^1.0.1", "esprima": "^4.0.0", diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js index 020a315e..9ea937e6 100755 --- a/src/core/config/OperationConfig.js +++ b/src/core/config/OperationConfig.js @@ -3848,6 +3848,7 @@ const OperationConfig = { }, "Generate PGP Key Pair": { module: "PGP", + manualBake: true, description: "", inputType: "string", outputType: "string", diff --git a/src/core/operations/PGP.js b/src/core/operations/PGP.js index 0f488260..f375d3fb 100755 --- a/src/core/operations/PGP.js +++ b/src/core/operations/PGP.js @@ -1,4 +1,5 @@ import * as kbpgp from "kbpgp"; +import promisify from "es6-promisify"; const ECC_SIZES = ["256", "384"]; const RSA_SIZES = ["1024", "2048", "4096"]; @@ -116,41 +117,21 @@ const PGP = { expire_in: 86400 * 365 * 2 // eslint-disable-line camelcase }], }; - - return new Promise((resolve, reject) => { - kbpgp.KeyManager.generate(keyGenerationOptions, (genErr, unsignedKey) => { - if (genErr) { - return reject(`Error from kbpgp whilst generating key: ${genErr}`); - } - - unsignedKey.sign({}, signErr => { - let signedKey = unsignedKey; - if (signErr) { - return reject(`Error from kbpgp whilst signing the generated key: ${signErr}`); - } - - let privateKeyExportOptions = {}; - if (password) privateKeyExportOptions.passphrase = password; - - signedKey.export_pgp_private(privateKeyExportOptions, (privateExportErr, privateKey) => { - if (privateExportErr) { - return reject(`Error from kbpgp whilst exporting the private part of the signed key: ${privateExportErr}`); - } - - signedKey.export_pgp_public({}, (publicExportErr, publicKey) => { - if (publicExportErr) { - return reject(`Error from kbpgp whilst exporting the public part of the signed key: ${publicExportErr}`); - } - - return resolve(privateKey + "\n" + publicKey); - }); - }); - - }); - }); + return new Promise(async (resolve, reject) => { + try { + const unsignedKey = await promisify(kbpgp.KeyManager.generate)(keyGenerationOptions); + await promisify(unsignedKey.sign, unsignedKey)({}); + let signedKey = unsignedKey; + let privateKeyExportOptions = {}; + if (password) privateKeyExportOptions.passphrase = password; + const privateKey = await promisify(signedKey.export_pgp_private, signedKey)(privateKeyExportOptions); + const publicKey = await promisify(signedKey.export_pgp_public, signedKey)({}); + resolve(privateKey + "\n" + publicKey); + } catch (err) { + reject(`Error from kbpgp whilst generating key pair: ${err}`); + } }); - }, - + } }; export default PGP;