22
33const {
44 ObjectKeys,
5- Promise,
65 SafeSet,
76} = primordials ;
87
@@ -42,10 +41,11 @@ const {
4241
4342const {
4443 lazyDOMException,
44+ promisify,
4545} = require ( 'internal/util' ) ;
4646
4747const {
48- generateKeyPair,
48+ generateKeyPair : _generateKeyPair ,
4949} = require ( 'internal/crypto/keygen' ) ;
5050
5151const {
@@ -56,6 +56,8 @@ const {
5656 createPublicKey,
5757} = require ( 'internal/crypto/keys' ) ;
5858
59+ const generateKeyPair = promisify ( _generateKeyPair ) ;
60+
5961function verifyAcceptableEcKeyUse ( name , type , usages ) {
6062let checkSet ;
6163switch ( name ) {
@@ -111,46 +113,44 @@ async function ecGenerateKey(algorithm, extractable, keyUsages) {
111113}
112114// Fall through
113115}
114- return new Promise ( ( resolve , reject ) => {
115- generateKeyPair ( 'ec' , { namedCurve } , ( err , pubKey , privKey ) => {
116- if ( err ) {
117- return reject ( lazyDOMException (
118- 'The operation failed for an operation-specific reason' ,
119- 'OperationError' ) ) ;
120- }
121116
122- const algorithm = { name, namedCurve } ;
117+ const keypair = await generateKeyPair ( 'ec' , { namedCurve } ) . catch ( ( err ) => {
118+ // TODO(@panva): add err as cause to DOMException
119+ throw lazyDOMException (
120+ 'The operation failed for an operation-specific reason' ,
121+ 'OperationError' ) ;
122+ } ) ;
123123
124- let publicUsages ;
125- let privateUsages ;
126- switch ( name ) {
127- case 'ECDSA' :
128- publicUsages = getUsagesUnion ( usageSet , 'verify' ) ;
129- privateUsages = getUsagesUnion ( usageSet , 'sign' ) ;
130- break ;
131- case 'ECDH' :
132- publicUsages = [ ] ;
133- privateUsages = getUsagesUnion ( usageSet , 'deriveKey' , 'deriveBits' ) ;
134- break ;
135- }
124+ let publicUsages ;
125+ let privateUsages ;
126+ switch ( name ) {
127+ case 'ECDSA' :
128+ publicUsages = getUsagesUnion ( usageSet , 'verify' ) ;
129+ privateUsages = getUsagesUnion ( usageSet , 'sign' ) ;
130+ break ;
131+ case 'ECDH' :
132+ publicUsages = [ ] ;
133+ privateUsages = getUsagesUnion ( usageSet , 'deriveKey' , 'deriveBits' ) ;
134+ break ;
135+ }
136136
137- const publicKey =
138- new InternalCryptoKey (
139- pubKey ,
140- algorithm ,
141- publicUsages ,
142- true ) ;
143-
144- const privateKey =
145- new InternalCryptoKey (
146- privKey ,
147- algorithm ,
148- privateUsages ,
149- extractable ) ;
150-
151- resolve ( { publicKey , privateKey } ) ;
152- } ) ;
153- } ) ;
137+ const keyAlgorithm = { name , namedCurve } ;
138+
139+ const publicKey =
140+ new InternalCryptoKey (
141+ keypair . publicKey ,
142+ keyAlgorithm ,
143+ publicUsages ,
144+ true ) ;
145+
146+ const privateKey =
147+ new InternalCryptoKey (
148+ keypair . privateKey ,
149+ keyAlgorithm ,
150+ privateUsages ,
151+ extractable ) ;
152+
153+ return { publicKey , privateKey } ;
154154}
155155
156156function ecExportKey ( key , format ) {
0 commit comments