Skip to content

Commit

Permalink
Linting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-kearney committed Oct 24, 2024
1 parent 70c9a54 commit cb38ffc
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 32 deletions.
14 changes: 12 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type OptionFunc func(c *config) error
func WithClientVersion(clientVersion string) OptionFunc {
return func(c *config) error {
c.clientVersion = clientVersion

return nil
}
}
Expand All @@ -38,6 +39,7 @@ func WithClientVersion(clientVersion string) OptionFunc {
func WithRegistry(registry strfmt.Registry) OptionFunc {
return func(c *config) error {
c.registry = registry

return nil
}
}
Expand All @@ -46,6 +48,7 @@ func WithRegistry(registry strfmt.Registry) OptionFunc {
func WithTransportConfig(transportConfig client.TransportConfig) OptionFunc {
return func(c *config) error {
c.transportConfig = &transportConfig

return nil
}
}
Expand All @@ -56,6 +59,7 @@ func WithTransportConfig(transportConfig client.TransportConfig) OptionFunc {
func WithAPIKey(apiKey *apikey.Key) OptionFunc {
return func(c *config) error {
c.apiKey = apiKey

return nil
}
}
Expand All @@ -68,7 +72,9 @@ func WithAPIKeyName(keyname string) OptionFunc {
if err != nil {
return errors.Wrap(err, "failed to load API key")
}

c.apiKey = apiKey

return nil
}
}
Expand All @@ -81,7 +87,10 @@ func New(options ...OptionFunc) (*Client, error) {
}

for _, o := range options {
o(c)
err := o(c)
if err != nil {
return nil, err
}
}

// Create transport and client
Expand Down Expand Up @@ -115,12 +124,13 @@ type addClientVersion struct {

func (acv *addClientVersion) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("X-Client-Version", acv.Version)

return acv.inner.RoundTrip(r)
}

// NewHTTPClient returns a new base HTTP API client.
// Most users will call New() instead.
// Deprecated: Use New(WithRegistry(formats)) instead
// Deprecated: Use New(WithRegistry(formats)) instead.
func NewHTTPClient(formats strfmt.Registry) *client.TurnkeyAPI {
return client.NewHTTPClient(formats)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/signing/sign_transaction/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func main() {
// NB: make sure to create and register an API key,first.
// NB: make sure to create and register an API key, first.
client, err := sdk.New(sdk.WithAPIKeyName("default"))
if err != nil {
log.Fatal("failed to create new SDK client:", err)
Expand Down
2 changes: 1 addition & 1 deletion examples/wallets/create_wallet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func main() {
// NB: make sure to create WithAPIKeyand register an API key, first.
// NB: make sure to create and register an API key, first.
client, err := sdk.New(sdk.WithAPIKeyName("default"))
if err != nil {
log.Fatal("failed to create new SDK client:", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/apikey/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ type APIStamp struct {
// New generates a new Turnkey API key.
func New(organizationID string, opts ...optionFunc) (*Key, error) {
if organizationID == "" {
return nil, fmt.Errorf("please supply a valid Organization UUID")
return nil, errors.New("please supply a valid Organization UUID")
}

if _, err := uuid.Parse(organizationID); err != nil {
return nil, fmt.Errorf("failed to parse organization ID")
return nil, errors.New("failed to parse organization ID")
}

apiKey := &Key{
Expand Down
8 changes: 6 additions & 2 deletions pkg/apikey/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func (k *ecdsaKey) sign(msg []byte) (string, error) {
return hex.EncodeToString(sigBytes), nil
}

// TurnkeyECDSAPublicKeyBytes is the expected number of bytes for a public ECDSA
// key.
const TurnkeyECDSAPublicKeyBytes = 33

// EncodePrivateECDSAKey encodes an ECDSA private key into the Turnkey format.
// For now, "Turnkey format" = raw DER form.
func EncodePrivateECDSAKey(privateKey *ecdsa.PrivateKey) string {
Expand Down Expand Up @@ -81,7 +85,7 @@ func DecodeTurnkeyPublicECDSAKey(encodedPublicKey string, scheme signatureScheme
return nil, err
}

if len(bytes) != 33 {
if len(bytes) != TurnkeyECDSAPublicKeyBytes {
return nil, fmt.Errorf("expected a 33-bytes-long public key (compressed). Got %d bytes", len(bytes))
}

Expand All @@ -99,7 +103,7 @@ func DecodeTurnkeyPublicECDSAKey(encodedPublicKey string, scheme signatureScheme

pubkey, err := dcrec.ParsePubKey(bytes)
if err != nil {
return nil, fmt.Errorf("cannot parse bytes into secp256k1 public key")
return nil, errors.New("cannot parse bytes into secp256k1 public key")
}

x = pubkey.X()
Expand Down
9 changes: 4 additions & 5 deletions pkg/encryptionkey/encryptionkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package encryptionkey
import (
"encoding/hex"
"encoding/json"
"fmt"
"os"

"github.com/cloudflare/circl/hpke"
Expand Down Expand Up @@ -39,19 +38,19 @@ type Key struct {
// New generates a new Turnkey encryption key.
func New(userID string, organizationID string) (*Key, error) {
if userID == "" {
return nil, fmt.Errorf("please supply a valid User UUID")
return nil, errors.New("please supply a valid User UUID")
}

if _, err := uuid.Parse(userID); err != nil {
return nil, fmt.Errorf("failed to parse user ID")
return nil, errors.New("failed to parse user ID")
}

if organizationID == "" {
return nil, fmt.Errorf("please supply a valid Organization UUID")
return nil, errors.New("please supply a valid Organization UUID")
}

if _, err := uuid.Parse(organizationID); err != nil {
return nil, fmt.Errorf("failed to parse organization ID")
return nil, errors.New("failed to parse organization ID")
}

_, privateKey, err := KemID.Scheme().GenerateKeyPair()
Expand Down
8 changes: 5 additions & 3 deletions pkg/store/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
publicKeyExtension = "public"
privateKeyExtension = "private"
metadataExtension = "meta"
fileOwnerRWGroupRAllR = 0o0644
fileOwnerRW = 0o0600
)

// Store defines an api key Store using the local filesystem.
Expand Down Expand Up @@ -210,7 +212,7 @@ func (s *Store[T, M]) Store(name string, keypair common.IKey[M]) error {
return errors.Errorf("a keypair named %q already exists; exiting", name)
}

if err = createKeyFile(s.PublicKeyFile(name), keypair.GetPublicKey(), 0o0644); err != nil {
if err = createKeyFile(s.PublicKeyFile(name), keypair.GetPublicKey(), fileOwnerRWGroupRAllR); err != nil {
return errors.Wrap(err, "failed to store public key to file")
}

Expand All @@ -219,11 +221,11 @@ func (s *Store[T, M]) Store(name string, keypair common.IKey[M]) error {
privateKeyData = fmt.Sprintf("%s:%s", privateKeyData, curve)
}

if err = createKeyFile(s.PrivateKeyFile(name), privateKeyData, 0o0600); err != nil {
if err = createKeyFile(s.PrivateKeyFile(name), privateKeyData, fileOwnerRW); err != nil {
return errors.Wrap(err, "failed to store private key to file")
}

if err = s.createMetadataFile(s.MetadataFile(name), keypair.GetMetadata(), 0o0600); err != nil {
if err = s.createMetadataFile(s.MetadataFile(name), keypair.GetMetadata(), fileOwnerRW); err != nil {
return errors.Wrap(err, "failed to store key metadata")
}

Expand Down
19 changes: 3 additions & 16 deletions pkg/store/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import (

// MacOSX has $HOME set by default.
func TestGetKeyDirPathMacOSX(t *testing.T) {
require.NoError(t, os.Setenv("HOME", "/home/dir"))

defer func() {
require.NoError(t, os.Unsetenv("HOME"))
}()
t.Setenv("HOME", "/home/dir")

// Need to unset this explicitly: the test runner has this set by default!
originalValue := os.Getenv("XDG_CONFIG_HOME")
Expand All @@ -36,17 +32,8 @@ func TestGetKeyDirPathMacOSX(t *testing.T) {
// On UNIX, we expect XDG_CONFIG_HOME to be set.
// If it's not set, we're back to a MacOSX-like system.
func TestGetKeyDirPathUnix(t *testing.T) {
require.NoError(t, os.Setenv("XDG_CONFIG_HOME", "/special/dir"))

defer func() {
require.NoError(t, os.Unsetenv("XDG_CONFIG_HOME"))
}()

require.NoError(t, os.Setenv("HOME", "/home/dir"))

defer func() {
require.NoError(t, os.Unsetenv("HOME"))
}()
t.Setenv("XDG_CONFIG_HOME", "/special/dir")
t.Setenv("HOME", "/home/dir")

assert.Equal(t, "/special/dir/turnkey/keys", local.DefaultAPIKeysDir())
assert.Equal(t, "/special/dir/turnkey/encryption-keys", local.DefaultEncryptionKeysDir())
Expand Down

0 comments on commit cb38ffc

Please sign in to comment.