Skip to content

Commit

Permalink
refactor: simplify internals of EC keys exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Apr 22, 2024
1 parent a5032b6 commit ab3c87c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions webcrypto/elliptic_curve.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,18 +409,23 @@ func pickEllipticCurve(k string) (elliptic.Curve, error) {
}
}

func exportECKey(alg string, ck *CryptoKey, format KeyFormat) (interface{}, error) {
func exportECKey(ck *CryptoKey, format KeyFormat) (interface{}, error) {
if ck.handle == nil {
return nil, NewError(OperationError, "key data is not accessible")
}

alg, ok := ck.Algorithm.(EcKeyAlgorithm)
if !ok {
return nil, NewError(InvalidAccessError, "key algorithm is not a valid EC algorithm")
}

switch format {
case RawKeyFormat:
if ck.Type != PublicCryptoKeyType {
return nil, NewError(InvalidAccessError, "key is not a valid elliptic curve public key")
}

bytes, err := extractPublicKeyBytes(alg, ck.handle)
bytes, err := extractPublicKeyBytes(alg.Name, ck.handle)
if err != nil {
return nil, NewError(OperationError, "unable to extract public key data: "+err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion webcrypto/subtle_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ func (sc *SubtleCrypto) ExportKey(format KeyFormat, key goja.Value) *goja.Promis
return
}
case ECDH, ECDSA:
result, err = exportECKey(keyAlgorithmName, ck, format)
result, err = exportECKey(ck, format)
if err != nil {
reject(err)
return
Expand Down

0 comments on commit ab3c87c

Please sign in to comment.