Skip to content

Commit

Permalink
feat: Add custom cli cmd and fix error in payments (#498)
Browse files Browse the repository at this point in the history
* add flag

* add cli command for signing ApiTokens

* fix error in paymentProcessor distribution logic

Co-authored-by: aljo242 <[email protected]>
  • Loading branch information
giunatale and aljo242 authored Nov 9, 2021
1 parent f313a92 commit 974855b
Show file tree
Hide file tree
Showing 7 changed files with 387 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ genesis/beta
genesis/gamma
genesis/pylonsd

artifacts

1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ VERSION := $(shell echo $(shell git describe --tags 2> /dev/null || echo "dev-$(
COMMIT := $(shell git log -1 --format='%H')
PROJECT_NAME = $(shell git remote get-url origin | xargs basename -s .git)
ARTIFACT_DIR := ./artifacts
LEDGER_ENABLED ?= true

export GO111MODULE = on

Expand Down
7 changes: 7 additions & 0 deletions proto/pylons/redeem_info.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ message RedeemInfo {
string amount = 4 [(gogoproto.nullable) = false, (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int"];;
string signature = 5;
}

message CreatePaymentAccount {
string address = 1;
string token = 2;
string signature = 3;
}

77 changes: 77 additions & 0 deletions x/pylons/client/cli/gen_signed_api_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cli

import (
"encoding/base64"
"errors"
"strconv"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"

"github.com/Pylons-tech/pylons/x/pylons/types"
)

var _ = strconv.Itoa(0)

func CmdGenerateSignedAPIToken() *cobra.Command {
cmd := &cobra.Command{
Use: "sign-api-token [api-token]",
Short: "generate signature for provided API token (used for external identification)",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
apiToken := args[0]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

if clientCtx.GetFromAddress().Empty() {
return errors.New("must use an identity to sign (use '--from' flag)")
}

txf := tx.NewFactoryCLI(clientCtx, cmd.Flags())
sigBytes, err := SignAPIToken(txf, clientCtx.GetFromName(), apiToken)
if err != nil {
return err
}

sigBase64 := base64.StdEncoding.EncodeToString(sigBytes)

return printOutput(clientCtx, apiToken, sigBase64)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}

func SignAPIToken(txf tx.Factory, name, apiToken string) ([]byte, error) {
if txf.Keybase() == nil {
return nil, errors.New("keybase must be set prior to signing a transaction")
}

bytesToSign := []byte(apiToken)

// Sign those bytes
sigBytes, _, err := txf.Keybase().Sign(name, bytesToSign)
if err != nil {
return nil, err
}

return sigBytes, nil
}

func printOutput(ctx client.Context, token, sigBase64 string) error {
msg := types.CreatePaymentAccount{
Address: ctx.GetFromAddress().String(),
Token: token,
Signature: sigBase64,
}

return ctx.PrintProto(&msg)
}
2 changes: 2 additions & 0 deletions x/pylons/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ func GetTxCmd() *cobra.Command {
cmd.AddCommand(CmdCreateCookbook())
cmd.AddCommand(CmdUpdateCookbook())

cmd.AddCommand(CmdGenerateSignedAPIToken())

return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion x/pylons/keeper/coins.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (k Keeper) MintCreditToAddr(ctx sdk.Context, addr sdk.AccAddress, amounts,
}

// send fees to the fee collector
err = k.bankKeeper.SendCoinsFromModuleToModule(ctx, ppMacc, feesMacc, actualAmt)
err = k.bankKeeper.SendCoinsFromModuleToModule(ctx, ppMacc, feesMacc, fees)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 974855b

Please sign in to comment.