Skip to content

Commit

Permalink
refactor: DeriveBits moving variables to isolate state and check for …
Browse files Browse the repository at this point in the history
…the nil
  • Loading branch information
olegbespalov committed Apr 22, 2024
1 parent 97787be commit d283eeb
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions webcrypto/subtle_crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"hash"

"github.com/dop251/goja"
"go.k6.io/k6/js/common"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/js/promises"
)
Expand Down Expand Up @@ -634,8 +635,10 @@ func (sc *SubtleCrypto) DeriveKey(
func (sc *SubtleCrypto) DeriveBits(algorithm goja.Value, baseKey goja.Value, length int) *goja.Promise {
rt := sc.vu.Runtime()

var publicKey, privateKey CryptoKey
var algName string
var (
publicKey, privateKey CryptoKey
deriver bitsDeriver
)

err := func() error {
if err := rt.ExportTo(baseKey, &privateKey); err != nil {
Expand All @@ -647,18 +650,25 @@ func (sc *SubtleCrypto) DeriveBits(algorithm goja.Value, baseKey goja.Value, len
}

alg := algorithm.ToObject(rt)
if common.IsNullish(alg) {
return NewError(InvalidAccessError, "algorithm is not an object")
}

pcValue := alg.Get("public")
if err := rt.ExportTo(pcValue, &publicKey); err != nil {
return NewError(InvalidAccessError, "algorithm's public is not a valid CryptoKey")
}

algName = alg.Get("name").String()

if publicKey.Type != PublicCryptoKeyType {
return NewError(InvalidAccessError, "algorithm's public key is not a public key")
}

var err error
deriver, err = newBitsDeriver(alg.Get("name").String())
if err != nil {
return err
}

return nil
}()

Expand All @@ -671,11 +681,6 @@ func (sc *SubtleCrypto) DeriveBits(algorithm goja.Value, baseKey goja.Value, len
callback := sc.vu.RegisterCallback()
go func() {
result, err := func() ([]byte, error) {
deriver, err := newBitsDeriver(algName)
if err != nil {
return nil, err
}

b, err := deriver(privateKey, publicKey)
if err != nil {
return nil, NewError(OperationError, err.Error())
Expand Down

0 comments on commit d283eeb

Please sign in to comment.