Skip to content

Commit

Permalink
Comments addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-turnkey committed Aug 13, 2024
1 parent dd96af4 commit ed45fe0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/apikey/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
10 changes: 2 additions & 8 deletions pkg/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions pkg/apikey/apikey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions pkg/apikey/schemes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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")
}
Expand Down

0 comments on commit ed45fe0

Please sign in to comment.