Cleanup promise error handling

This commit is contained in:
Tankred Hase 2014-10-01 08:40:26 +02:00
parent 0af12b81a3
commit e6f66b9039

View File

@ -152,26 +152,19 @@ function RSA() {
} }
}; };
var extractable = true; // make generated key extractable var gen = window.crypto.subtle.generateKey(keyGenOpt, true, ['sign', 'verify']);
gen.then(exportKey).then(decodeKey).catch(onError);
window.crypto.subtle.generateKey(keyGenOpt, extractable, ['sign', 'verify'])
.then(onGenerated, onError)
.then(onExported, onError);
return; return;
} }
function onError() { function exportKey(key) {
callback(new Error('Generating key failed!'));
}
function onGenerated(key) {
// export the generated keys as JsonWebKey (JWK) // export the generated keys as JsonWebKey (JWK)
// https://tools.ietf.org/html/draft-ietf-jose-json-web-key-33 // https://tools.ietf.org/html/draft-ietf-jose-json-web-key-33
return window.crypto.subtle.exportKey('jwk', key.privateKey); return window.crypto.subtle.exportKey('jwk', key.privateKey);
} }
function onExported(jwk) { function decodeKey(jwk) {
// map JWK parameters to local BigInteger type system // map JWK parameters to local BigInteger type system
var key = new keyObject(); var key = new keyObject();
key.n = toBigInteger(jwk.n); key.n = toBigInteger(jwk.n);
@ -190,6 +183,10 @@ function RSA() {
callback(null, key); callback(null, key);
} }
function onError() {
callback(new Error('Generating key failed!'));
}
// //
// JS code // JS code
// //