Skip to content

Commit

Permalink
Make staker command to use global parameters library (#170)
Browse files Browse the repository at this point in the history
* Make staker commands to use global parameters library
  • Loading branch information
KonradStaniec authored Jun 20, 2024
1 parent d88515b commit c853313
Show file tree
Hide file tree
Showing 11 changed files with 568 additions and 477 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
resource_class: large
steps:
- go/install:
version: "1.21.4"
version: "1.22.3"
- checkout
- run:
name: Print Go environment
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21 as builder
FROM golang:1.22.3 as builder

# Install cli tools for building and final image
RUN apt-get update && apt-get install -y make git bash gcc curl jq
Expand Down
2 changes: 0 additions & 2 deletions cmd/stakercli/helpers/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ package helpers
const (
StakingAmountFlag = "staking-amount"
StakingTimeBlocksFlag = "staking-time"
UnbondingFee = "unbonding-fee"
UnbondingTimeFlag = "unbonding-time"
)
99 changes: 99 additions & 0 deletions cmd/stakercli/transaction/parsers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package transaction

import (
"encoding/hex"
"fmt"
"math"

"github.com/babylonchain/babylon/btcstaking"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil"
"github.com/urfave/cli"
)

func parseSchnorPubKeyFromCliCtx(ctx *cli.Context, flagName string) (*btcec.PublicKey, error) {
pkHex := ctx.String(flagName)
return parseSchnorPubKeyFromHex(pkHex)
}

func parseSchnorPubKeyFromHex(pkHex string) (*btcec.PublicKey, error) {
pkBytes, err := hex.DecodeString(pkHex)
if err != nil {
return nil, err
}

pk, err := schnorr.ParsePubKey(pkBytes)
if err != nil {
return nil, err
}

return pk, nil
}

func parseAmountFromCliCtx(ctx *cli.Context, flagName string) (btcutil.Amount, error) {
amt := ctx.Int64(flagName)

if amt <= 0 {
return 0, fmt.Errorf("staking amount should be greater than 0")
}

return btcutil.Amount(amt), nil
}

func parseLockTimeBlocksFromCliCtx(ctx *cli.Context, flagName string) (uint16, error) {
timeBlocks := ctx.Int64(flagName)

if timeBlocks <= 0 {
return 0, fmt.Errorf("staking time blocks should be greater than 0")
}

if timeBlocks > math.MaxUint16 {
return 0, fmt.Errorf("staking time blocks should be less or equal to %d", math.MaxUint16)
}

return uint16(timeBlocks), nil
}

func parseCovenantKeysFromCliCtx(ctx *cli.Context) ([]*btcec.PublicKey, error) {
covenantMembersPks := ctx.StringSlice(covenantMembersPksFlag)
return parseCovenantKeysFromSlice(covenantMembersPks)
}

func parseCovenantKeysFromSlice(covenantMembersPks []string) ([]*btcec.PublicKey, error) {
covenantPubKeys := make([]*btcec.PublicKey, len(covenantMembersPks))

for i, fpPk := range covenantMembersPks {
fpPkBytes, err := hex.DecodeString(fpPk)
if err != nil {
return nil, err
}

fpSchnorrKey, err := schnorr.ParsePubKey(fpPkBytes)
if err != nil {
return nil, err
}

covenantPubKeys[i] = fpSchnorrKey
}

return covenantPubKeys, nil
}

func parseMagicBytesFromCliCtx(ctx *cli.Context) ([]byte, error) {
magicBytesHex := ctx.String(magicBytesFlag)
return parseMagicBytesFromHex(magicBytesHex)
}

func parseMagicBytesFromHex(magicBytesHex string) ([]byte, error) {
magicBytes, err := hex.DecodeString(magicBytesHex)
if err != nil {
return nil, err
}

if len(magicBytes) != btcstaking.MagicBytesLen {
return nil, fmt.Errorf("magic bytes should be of length %d", btcstaking.MagicBytesLen)
}

return magicBytes, nil
}
69 changes: 0 additions & 69 deletions cmd/stakercli/transaction/staking.go

This file was deleted.

Loading

0 comments on commit c853313

Please sign in to comment.