basecli sendtx handles chain/addr format

This commit is contained in:
Ethan Frey
2017-06-16 15:42:18 +02:00
parent cba5523ca5
commit 33d4f930da
3 changed files with 33 additions and 3 deletions
+29 -2
View File
@@ -2,6 +2,7 @@ package commands
import (
"encoding/hex"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@@ -78,9 +79,9 @@ func doSendTx(cmd *cobra.Command, args []string) error {
func readSendTxFlags(tx *btypes.SendTx) error {
// parse to address
to, err := ParseHexFlag(FlagTo)
to, err := parseChainAddress(viper.GetString(FlagTo))
if err != nil {
return errors.Errorf("To address is invalid hex: %v\n", err)
return err
}
//parse the fee and amounts into coin types
@@ -109,6 +110,32 @@ func readSendTxFlags(tx *btypes.SendTx) error {
return nil
}
func parseChainAddress(toFlag string) ([]byte, error) {
var toHex string
var chainPrefix string
spl := strings.Split(toFlag, "/")
switch len(spl) {
case 1:
toHex = spl[0]
case 2:
chainPrefix = spl[0]
toHex = spl[1]
default:
return nil, errors.Errorf("To address has too many slashes")
}
// convert destination address to bytes
to, err := hex.DecodeString(cmn.StripHex(toHex))
if err != nil {
return nil, errors.Errorf("To address is invalid hex: %v\n", err)
}
if chainPrefix != "" {
to = []byte(chainPrefix + "/" + string(to))
}
return to, nil
}
/******** AppTx *********/
// BroadcastAppTx wraps, signs, and executes an app tx basecoin transaction
+2 -1
View File
@@ -97,7 +97,8 @@ func (s *SendTx) ValidateBasic() error {
}
}
for _, out := range s.Tx.Outputs {
if len(out.Address) != 20 {
// we now allow chain/addr, so it can be more than 20 bytes
if len(out.Address) < 20 {
return errors.Errorf("Invalid output address length: %d", len(out.Address))
}
if !out.Coins.IsValid() {