From ed45fe0d5c271ae4998406cd1fe92e7909c43d4b Mon Sep 17 00:00:00 2001 From: Robin Arenson Date: Tue, 13 Aug 2024 16:30:11 -0400 Subject: [PATCH] Comments addressed --- examples/apikey/generate.go | 2 +- pkg/apikey/apikey.go | 10 ++-------- pkg/apikey/apikey_test.go | 6 +++--- pkg/apikey/schemes.go | 8 ++++---- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/examples/apikey/generate.go b/examples/apikey/generate.go index 0b2562a..64bf53d 100644 --- a/examples/apikey/generate.go +++ b/examples/apikey/generate.go @@ -26,7 +26,7 @@ func main() { log.Fatalln("organization ID must be set") } - key, err := apikey.New(organizationID, apikey.WithScheme(apikey.SchemeP256)) + key, err := apikey.New(organizationID) if err != nil { log.Fatalln("failed to generate API key:", err) } diff --git a/pkg/apikey/apikey.go b/pkg/apikey/apikey.go index 5e0fc11..83ca4ca 100644 --- a/pkg/apikey/apikey.go +++ b/pkg/apikey/apikey.go @@ -98,10 +98,6 @@ func New(organizationID string, opts ...optionFunc) (*Key, error) { // FromTurnkeyPrivateKey takes a Turnkey-encoded private key, derives a public key from it, and then returns the corresponding Turnkey API key. func FromTurnkeyPrivateKey(encodedPrivateKey string, scheme signatureScheme) (*Key, error) { - if scheme == SchemeED25519 { - return fromTurnkeyED25519Key(encodedPrivateKey) - } - switch scheme { case SchemeP256: return fromTurnkeyECDSAKey(encodedPrivateKey, scheme) @@ -110,9 +106,8 @@ func FromTurnkeyPrivateKey(encodedPrivateKey string, scheme signatureScheme) (*K case SchemeED25519: return fromTurnkeyED25519Key(encodedPrivateKey) default: + return nil, errors.New("unsupported signature scheme") } - - return nil, errors.New("unsupported signature scheme") } // Stamp generates a signing stamp for the given message with the given API key. @@ -163,9 +158,8 @@ func (k Key) GetCurve() string { case SchemeP256: return string(CurveP256) default: + return string(CurveP256) } - - return string(CurveP256) } // LoadMetadata loads a JSON metadata file. diff --git a/pkg/apikey/apikey_test.go b/pkg/apikey/apikey_test.go index d60486a..9db65a3 100644 --- a/pkg/apikey/apikey_test.go +++ b/pkg/apikey/apikey_test.go @@ -70,7 +70,7 @@ func Test_Sign_ECDSA(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(tt *testing.T) { - apiKey, err := apikey.FromTurnkeyPrivateKey(tc.tkPrivateKey, apikey.CurveToScheme(tc.curve)) + apiKey, err := apikey.FromTurnkeyPrivateKey(tc.tkPrivateKey, tc.curve.ToScheme()) require.NoError(tt, err) stampHeader, err := apikey.Stamp([]byte("hello"), apiKey) @@ -84,12 +84,12 @@ func Test_Sign_ECDSA(t *testing.T) { require.NoError(tt, json.Unmarshal(testStamp, &stamp)) assert.Equal(tt, tc.tkPublicKey, stamp.PublicKey) - assert.Equal(tt, apikey.CurveToScheme(tc.curve), stamp.Scheme) + assert.Equal(tt, tc.curve.ToScheme(), stamp.Scheme) sigBytes, err := hex.DecodeString(stamp.Signature) require.NoError(tt, err) - publicKey, err := apikey.DecodeTurnkeyPublicECDSAKey(stamp.PublicKey, apikey.CurveToScheme(tc.curve)) + publicKey, err := apikey.DecodeTurnkeyPublicECDSAKey(stamp.PublicKey, tc.curve.ToScheme()) require.NoError(tt, err) // Verify the soundness of the hash: diff --git a/pkg/apikey/schemes.go b/pkg/apikey/schemes.go index 67e770f..eb6ba14 100644 --- a/pkg/apikey/schemes.go +++ b/pkg/apikey/schemes.go @@ -33,15 +33,15 @@ const ( defaultSignatureScheme = SchemeP256 ) -// CurveToScheme takes a Curve and returns its associated signatureScheme. -func CurveToScheme(curve Curve) signatureScheme { +// ToScheme returns a Curve's associated signatureScheme. +func (c Curve) ToScheme() signatureScheme { symbolMap := map[Curve]signatureScheme{ CurveP256: SchemeP256, CurveSecp256k1: SchemeSECP256K1, CurveEd25519: SchemeED25519, } - scheme, ok := symbolMap[curve] + scheme, ok := symbolMap[c] if ok { return scheme } @@ -58,7 +58,7 @@ func ExtractSignatureSchemeFromSuffixedPrivateKey(data string) (string, signatur return pieces[0], SchemeP256, nil } - scheme := CurveToScheme(Curve(pieces[1])) + scheme := Curve(pieces[1]).ToScheme() if scheme == SchemeUnsupported { return "", SchemeUnsupported, errors.New("improperly formatted raw key string or unsupported scheme") }