From e6f66b9039d5b31aee492773b2c187765cdbd862 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Wed, 1 Oct 2014 08:40:26 +0200 Subject: [PATCH] Cleanup promise error handling --- src/crypto/public_key/rsa.js | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/crypto/public_key/rsa.js b/src/crypto/public_key/rsa.js index e306e5e5..0ffb8b4d 100644 --- a/src/crypto/public_key/rsa.js +++ b/src/crypto/public_key/rsa.js @@ -152,26 +152,19 @@ function RSA() { } }; - var extractable = true; // make generated key extractable - - window.crypto.subtle.generateKey(keyGenOpt, extractable, ['sign', 'verify']) - .then(onGenerated, onError) - .then(onExported, onError); + var gen = window.crypto.subtle.generateKey(keyGenOpt, true, ['sign', 'verify']); + gen.then(exportKey).then(decodeKey).catch(onError); return; } - function onError() { - callback(new Error('Generating key failed!')); - } - - function onGenerated(key) { + function exportKey(key) { // export the generated keys as JsonWebKey (JWK) // https://tools.ietf.org/html/draft-ietf-jose-json-web-key-33 return window.crypto.subtle.exportKey('jwk', key.privateKey); } - function onExported(jwk) { + function decodeKey(jwk) { // map JWK parameters to local BigInteger type system var key = new keyObject(); key.n = toBigInteger(jwk.n); @@ -190,6 +183,10 @@ function RSA() { callback(null, key); } + function onError() { + callback(new Error('Generating key failed!')); + } + // // JS code //