Skip to content

Commit

Permalink
Merge pull-request #47
Browse files Browse the repository at this point in the history
  • Loading branch information
timurnkey committed Nov 2, 2023
2 parents a0d6fd2 + 741b668 commit 3f70e6f
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 127 deletions.
31 changes: 15 additions & 16 deletions src/cmd/turnkey/pkg/activities.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,46 @@ var activitiesListCmd = &cobra.Command{
Use: "list",
Short: "Return the set of activities for an organization",
Run: func(cmd *cobra.Command, args []string) {
activitiesFilter := make([]models.V1ActivityStatus, len(activitiesListStatus))
activitiesFilter := make([]models.ActivityStatus, len(activitiesListStatus))

for i, s := range activitiesListStatus {
if s == Help {
Output(models.ActivityStatuses())
Output(models.ActivityStatusEnum)

return
}

if s == "all" {
activitiesFilter = models.ActivityStatuses()

activitiesFilter = models.ActivityStatusEnum
break
}

switch s {
case "created":
activitiesFilter[i] = models.V1ActivityStatusACTIVITYSTATUSCREATED
activitiesFilter[i] = models.ActivityStatusCreated
case "pending":
activitiesFilter[i] = models.V1ActivityStatusACTIVITYSTATUSPENDING
activitiesFilter[i] = models.ActivityStatusPending
case "completed":
activitiesFilter[i] = models.V1ActivityStatusACTIVITYSTATUSCOMPLETED
activitiesFilter[i] = models.ActivityStatusCompleted
case "failed":
activitiesFilter[i] = models.V1ActivityStatusACTIVITYSTATUSFAILED
activitiesFilter[i] = models.ActivityStatusFailed
case "consensus":
activitiesFilter[i] = models.V1ActivityStatusACTIVITYSTATUSCONSENSUSNEEDED
activitiesFilter[i] = models.ActivityStatusConsensusNeeded
case "consensus_needed":
activitiesFilter[i] = models.V1ActivityStatusACTIVITYSTATUSCONSENSUSNEEDED
activitiesFilter[i] = models.ActivityStatusConsensusNeeded
case "rejected":
activitiesFilter[i] = models.V1ActivityStatusACTIVITYSTATUSREJECTED
activitiesFilter[i] = models.ActivityStatusRejected
default:
activitiesFilter[i] = models.V1ActivityStatus(s)
activitiesFilter[i] = models.ActivityStatus(s)
}
}

params := activities.NewPublicAPIServiceGetActivitiesParams().WithDefaults().WithBody(&models.V1GetActivitiesRequest{
params := activities.NewGetActivitiesParams().WithDefaults().WithBody(&models.GetActivitiesRequest{
FilterByStatus: activitiesFilter,
OrganizationID: &Organization,
})

res, err := APIClient.V0().Activities.PublicAPIServiceGetActivities(params, APIClient.Authenticator)
res, err := APIClient.V0().Activities.GetActivities(params, APIClient.Authenticator)
if err != nil {
OutputError(err)
}
Expand All @@ -88,12 +87,12 @@ var activitiesGetCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
id := args[0]

params := activities.NewPublicAPIServiceGetActivityParams().WithDefaults().WithBody(&models.V1GetActivityRequest{
params := activities.NewGetActivityParams().WithDefaults().WithBody(&models.GetActivityRequest{
ActivityID: &id,
OrganizationID: &Organization,
})

res, err := APIClient.V0().Activities.PublicAPIServiceGetActivity(params, APIClient.Authenticator)
res, err := APIClient.V0().Activities.GetActivity(params, APIClient.Authenticator)
if err != nil {
OutputError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/turnkey/pkg/address-formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ var addressFormatsListCmd = &cobra.Command{
Use: "list",
Short: "Return the available key formats",
Run: func(cmd *cobra.Command, args []string) {
Output(models.AddressFormats())
Output(models.AddressFormatEnum)
},
}
2 changes: 1 addition & 1 deletion src/cmd/turnkey/pkg/curves.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ var curvesListCmd = &cobra.Command{
Use: "list",
Short: "Return the available curve types",
Run: func(cmd *cobra.Command, args []string) {
Output(models.Curves())
Output(models.CurveEnum)
},
}
25 changes: 13 additions & 12 deletions src/cmd/turnkey/pkg/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import (
"github.com/rotisserie/eris"
"github.com/spf13/cobra"

"github.com/tkhq/go-sdk/pkg/api/client/private_keys"
"github.com/tkhq/go-sdk/pkg/api/client/signers"
"github.com/tkhq/go-sdk/pkg/api/models"
"github.com/tkhq/go-sdk/pkg/util"
)

var ethTxPayload string
var (
ethTxSigner string
ethTxPayload string
)

func init() {
ethCmd.PersistentFlags().StringVarP(&signingKeyID, "private-key", "s", "", "name or ID of the private signing key")

rootCmd.AddCommand(ethCmd)

ethTxCmd.Flags().StringVarP(&ethTxSigner, "signer", "s", "", "wallet account address, private key address, or private key ID")
ethTxCmd.Flags().StringVar(&ethTxPayload, "payload", "", "payload of the transaction")

ethCmd.AddCommand(ethTxCmd)
Expand All @@ -28,7 +30,6 @@ var ethCmd = &cobra.Command{
PersistentPreRun: func(cmd *cobra.Command, args []string) {
basicSetup(cmd)
LoadKeypair("")
LoadSigningKey("")
LoadClient()
},
}
Expand All @@ -38,8 +39,8 @@ var ethTxCmd = &cobra.Command{
Short: "Perform signing and other actions for a transaction",
Aliases: []string{"tx"},
Run: func(cmd *cobra.Command, args []string) {
transactionType := models.V1TransactionTypeTRANSACTIONTYPEETHEREUM
activityType := string(models.V1ActivityTypeACTIVITYTYPESIGNTRANSACTION)
transactionType := models.TransactionTypeEthereum
activityType := string(models.ActivityTypeSignTransaction)

payload, err := ParameterToString(ethTxPayload)
if err != nil {
Expand All @@ -52,11 +53,11 @@ var ethTxCmd = &cobra.Command{
OutputError(eris.New("payload cannot be empty"))
}

params := private_keys.NewPublicAPIServiceSignTransactionParams().WithBody(
&models.V1SignTransactionRequest{
params := signers.NewSignTransactionParams().WithBody(
&models.SignTransactionRequest{
OrganizationID: &Organization,
Parameters: &models.V1SignTransactionIntent{
PrivateKeyID: &signingKeyID,
Parameters: &models.SignTransactionIntentV2{
SignWith: &ethTxSigner,
Type: &transactionType,
UnsignedTransaction: &payload,
},
Expand All @@ -65,7 +66,7 @@ var ethTxCmd = &cobra.Command{
},
)

resp, err := APIClient.V0().PrivateKeys.PublicAPIServiceSignTransaction(params, APIClient.Authenticator)
resp, err := APIClient.V0().Signers.SignTransaction(params, APIClient.Authenticator)
if err != nil {
OutputError(eris.Wrap(err, "request failed"))
}
Expand Down
16 changes: 8 additions & 8 deletions src/cmd/turnkey/pkg/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ var organizationsCreateCmd = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
curve := models.Immutablecommonv1Curve(privateKeysCreateCurve)
curve := models.Curve(privateKeysCreateCurve)

addressFormats := make([]models.Immutablecommonv1AddressFormat, len(privateKeysCreateAddressFormats))
addressFormats := make([]models.AddressFormat, len(privateKeysCreateAddressFormats))

for n, f := range privateKeysCreateAddressFormats {
addressFormats[n] = models.Immutablecommonv1AddressFormat(f)
addressFormats[n] = models.AddressFormat(f)
}

params := private_keys.NewPublicAPIServiceCreatePrivateKeysParams()
params.SetBody(&models.V1CreatePrivateKeysRequest{
params := private_keys.NewCreatePrivateKeysParams()
params.SetBody(&models.CreatePrivateKeysRequest{
OrganizationID: &Organization,
Parameters: &models.V1CreatePrivateKeysIntentV2{
PrivateKeys: []*models.V1PrivateKeyParams{
Parameters: &models.CreatePrivateKeysIntentV2{
PrivateKeys: []*models.PrivateKeyParams{
{
AddressFormats: addressFormats,
Curve: &curve,
Expand All @@ -61,7 +61,7 @@ var organizationsCreateCmd = &cobra.Command{
},
})

resp, err := APIClient.V0().PrivateKeys.PublicAPIServiceCreatePrivateKeys(params, APIClient.Authenticator)
resp, err := APIClient.V0().PrivateKeys.CreatePrivateKeys(params, APIClient.Authenticator)
if err != nil {
OutputError(eris.Wrap(err, "request failed"))
}
Expand Down
80 changes: 14 additions & 66 deletions src/cmd/turnkey/pkg/private-keys.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pkg

import (
"github.com/google/uuid"
"github.com/rotisserie/eris"
"github.com/spf13/cobra"

Expand All @@ -11,8 +10,6 @@ import (
)

var (
signingKeyID string

privateKeysCreateAddressFormats []string
privateKeysCreateCurve string
privateKeysCreateName string
Expand Down Expand Up @@ -59,33 +56,31 @@ var privateKeysCreateCmd = &cobra.Command{
}
},
Run: func(cmd *cobra.Command, args []string) {
curve := models.Immutablecommonv1Curve(privateKeysCreateCurve)
curve := models.Curve(privateKeysCreateCurve)

if curve == Help {
Output(models.Curves())

Output(models.CurveEnum)
return
}

addressFormats := make([]models.Immutablecommonv1AddressFormat, len(privateKeysCreateAddressFormats))
addressFormats := make([]models.AddressFormat, len(privateKeysCreateAddressFormats))

for n, f := range privateKeysCreateAddressFormats {
if f == Help {
Output(models.AddressFormats())

Output(models.AddressFormatEnum)
return
}

addressFormats[n] = models.Immutablecommonv1AddressFormat(f)
addressFormats[n] = models.AddressFormat(f)
}

activity := string(models.V1ActivityTypeACTIVITYTYPECREATEPRIVATEKEYS)
activity := string(models.ActivityTypeCreatePrivateKeys)

params := private_keys.NewPublicAPIServiceCreatePrivateKeysParams()
params.SetBody(&models.V1CreatePrivateKeysRequest{
params := private_keys.NewCreatePrivateKeysParams()
params.SetBody(&models.CreatePrivateKeysRequest{
OrganizationID: &Organization,
Parameters: &models.V1CreatePrivateKeysIntentV2{
PrivateKeys: []*models.V1PrivateKeyParams{
Parameters: &models.CreatePrivateKeysIntentV2{
PrivateKeys: []*models.PrivateKeyParams{
{
AddressFormats: addressFormats,
Curve: &curve,
Expand All @@ -102,7 +97,7 @@ var privateKeysCreateCmd = &cobra.Command{
OutputError(eris.Wrap(err, "request validation failed"))
}

resp, err := APIClient.V0().PrivateKeys.PublicAPIServiceCreatePrivateKeys(params, APIClient.Authenticator)
resp, err := APIClient.V0().PrivateKeys.CreatePrivateKeys(params, APIClient.Authenticator)
if err != nil {
OutputError(eris.Wrap(err, "request failed"))
}
Expand All @@ -119,17 +114,17 @@ var privateKeysListCmd = &cobra.Command{
Use: "list",
Short: "Return private keys for the organization",
Run: func(cmd *cobra.Command, args []string) {
params := private_keys.NewPublicAPIServiceGetPrivateKeysParams()
params := private_keys.NewGetPrivateKeysParams()

params.SetBody(&models.V1GetPrivateKeysRequest{
params.SetBody(&models.GetPrivateKeysRequest{
OrganizationID: &Organization,
})

if err := params.Body.Validate(nil); err != nil {
OutputError(eris.Wrap(err, "request validation failed"))
}

resp, err := APIClient.V0().PrivateKeys.PublicAPIServiceGetPrivateKeys(params, APIClient.Authenticator)
resp, err := APIClient.V0().PrivateKeys.GetPrivateKeys(params, APIClient.Authenticator)
if err != nil {
OutputError(eris.Wrap(err, "request failed"))
}
Expand All @@ -141,50 +136,3 @@ var privateKeysListCmd = &cobra.Command{
Output(resp.Payload)
},
}

// LoadSigningKey require-loads a signing key.
func LoadSigningKey(name string) {
if name != "" {
signingKeyID = name
}

if signingKeyID == "" {
OutputError(eris.New("no private key provided"))
}

if _, err := uuid.Parse(signingKeyID); err != nil {
signingKeyID, err = lookupPrivateKeyByName(signingKeyID)
if err != nil {
OutputError(eris.Wrap(err, "provided private key was not a UUID and lookup by name failed"))
}
}
}

func lookupPrivateKeyByName(name string) (string, error) {
params := private_keys.NewPublicAPIServiceGetPrivateKeysParams()

params.SetBody(&models.V1GetPrivateKeysRequest{
OrganizationID: &Organization,
})

if err := params.Body.Validate(nil); err != nil {
return "", eris.Wrap(err, "formulation of a lookup by name request failed")
}

resp, err := APIClient.V0().PrivateKeys.PublicAPIServiceGetPrivateKeys(params, APIClient.Authenticator)
if err != nil {
return "", eris.Wrap(err, "lookup by name failed")
}

if !resp.IsSuccess() {
return "", eris.Errorf("lookup by name failed: %s", resp.Error())
}

for _, k := range resp.Payload.PrivateKeys {
if *k.PrivateKeyName == name {
return *k.PrivateKeyID, nil
}
}

return "", eris.Errorf("private key name %q not found in list of private keys", name)
}
Loading

0 comments on commit 3f70e6f

Please sign in to comment.