Add more flags to help with multisig

This commit is contained in:
Ethan Frey 2017-07-18 20:45:48 +02:00
parent 7b0934bf9f
commit a060bde1c4
4 changed files with 59 additions and 4 deletions

View File

@ -73,6 +73,7 @@ func ValidateResult(res *ctypes.ResultBroadcastTxCommit) error {
// If app is not specified or "", then assume auth.NameSigs // If app is not specified or "", then assume auth.NameSigs
func ParseAddress(input string) (res basecoin.Actor, err error) { func ParseAddress(input string) (res basecoin.Actor, err error) {
chain, app := "", auth.NameSigs chain, app := "", auth.NameSigs
input = strings.TrimSpace(input)
spl := strings.SplitN(input, ":", 3) spl := strings.SplitN(input, ":", 3)
if len(spl) == 3 { if len(spl) == 3 {

View File

@ -23,12 +23,14 @@ var SendTxCmd = &cobra.Command{
const ( const (
FlagTo = "to" FlagTo = "to"
FlagAmount = "amount" FlagAmount = "amount"
FlagFrom = "from"
) )
func init() { func init() {
flags := SendTxCmd.Flags() flags := SendTxCmd.Flags()
flags.String(FlagTo, "", "Destination address for the bits") flags.String(FlagTo, "", "Destination address for the bits")
flags.String(FlagAmount, "", "Coins to send in the format <amt><coin>,<amt><coin>...") flags.String(FlagAmount, "", "Coins to send in the format <amt><coin>,<amt><coin>...")
flags.String(FlagFrom, "", "Address sending coins, if not first signer")
} }
// doSendTx is an example of how to make a tx // doSendTx is an example of how to make a tx
@ -70,6 +72,11 @@ func readSendTxFlags() (tx basecoin.Tx, err error) {
return tx, err return tx, err
} }
fromAddr, err := readFromAddr()
if err != nil {
return tx, err
}
amountCoins, err := coin.ParseCoins(viper.GetString(FlagAmount)) amountCoins, err := coin.ParseCoins(viper.GetString(FlagAmount))
if err != nil { if err != nil {
return tx, err return tx, err
@ -77,7 +84,7 @@ func readSendTxFlags() (tx basecoin.Tx, err error) {
// craft the inputs and outputs // craft the inputs and outputs
ins := []coin.TxInput{{ ins := []coin.TxInput{{
Address: bcmd.GetSignerAct(), Address: fromAddr,
Coins: amountCoins, Coins: amountCoins,
}} }}
outs := []coin.TxOutput{{ outs := []coin.TxOutput{{
@ -87,3 +94,11 @@ func readSendTxFlags() (tx basecoin.Tx, err error) {
return coin.NewSendTx(ins, outs), nil return coin.NewSendTx(ins, outs), nil
} }
func readFromAddr() (basecoin.Actor, error) {
from := viper.GetString(FlagFrom)
if from == "" {
return bcmd.GetSignerAct(), nil
}
return bcmd.ParseAddress(from)
}

View File

@ -12,7 +12,8 @@ import (
//nolint //nolint
const ( const (
FlagFee = "fee" FlagFee = "fee"
FlagPayer = "payer"
) )
// FeeWrapper wraps a tx with an optional fee payment // FeeWrapper wraps a tx with an optional fee payment
@ -32,11 +33,26 @@ func (FeeWrapper) Wrap(tx basecoin.Tx) (res basecoin.Tx, err error) {
if toll.IsZero() { if toll.IsZero() {
return tx, nil return tx, nil
} }
res = fee.NewFee(tx, toll, bcmd.GetSignerAct())
payer, err := readPayer()
if err != nil {
return res, err
}
res = fee.NewFee(tx, toll, payer)
return return
} }
// Register adds the sequence flags to the cli // Register adds the sequence flags to the cli
func (FeeWrapper) Register(fs *pflag.FlagSet) { func (FeeWrapper) Register(fs *pflag.FlagSet) {
fs.String(FlagFee, "0mycoin", "Coins for the transaction fee of the format <amt><coin>") fs.String(FlagFee, "0mycoin", "Coins for the transaction fee of the format <amt><coin>")
fs.String(FlagPayer, "", "Account to pay fee if not current signer (for multisig)")
}
func readPayer() (basecoin.Actor, error) {
payer := viper.GetString(FlagPayer)
if payer == "" {
return bcmd.GetSignerAct(), nil
}
return bcmd.ParseAddress(payer)
} }

View File

@ -2,6 +2,7 @@ package commands
import ( import (
"fmt" "fmt"
"strings"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -14,6 +15,7 @@ import (
// nolint // nolint
const ( const (
FlagSequence = "sequence" FlagSequence = "sequence"
FlagNonceKey = "nonce-key"
) )
// NonceWrapper wraps a tx with a nonce // NonceWrapper wraps a tx with a nonce
@ -30,7 +32,10 @@ func (NonceWrapper) Wrap(tx basecoin.Tx) (res basecoin.Tx, err error) {
if seq < 0 { if seq < 0 {
return res, fmt.Errorf("sequence must be greater than 0") return res, fmt.Errorf("sequence must be greater than 0")
} }
signers := []basecoin.Actor{bcmd.GetSignerAct()} signers, err := readNonceKey()
if err != nil {
return res, err
}
res = nonce.NewTx(uint32(seq), signers, tx) res = nonce.NewTx(uint32(seq), signers, tx)
return return
} }
@ -38,4 +43,22 @@ func (NonceWrapper) Wrap(tx basecoin.Tx) (res basecoin.Tx, err error) {
// Register adds the sequence flags to the cli // Register adds the sequence flags to the cli
func (NonceWrapper) Register(fs *pflag.FlagSet) { func (NonceWrapper) Register(fs *pflag.FlagSet) {
fs.Int(FlagSequence, -1, "Sequence number for this transaction") fs.Int(FlagSequence, -1, "Sequence number for this transaction")
fs.String(FlagNonceKey, "", "Set of comma-separated addresses for the nonce (for multisig)")
}
func readNonceKey() (signers []basecoin.Actor, err error) {
nonce := viper.GetString(FlagNonceKey)
if nonce == "" {
return []basecoin.Actor{bcmd.GetSignerAct()}, nil
}
var act basecoin.Actor
for _, k := range strings.Split(nonce, ",") {
act, err = bcmd.ParseAddress(k)
if err != nil {
return
}
signers = append(signers, act)
}
return
} }