Cleaned up unneeded adapters

This commit is contained in:
Ethan Frey 2017-07-03 22:34:30 +02:00
parent 413ea2e23f
commit 9cd303d1fd
3 changed files with 0 additions and 265 deletions

View File

@ -1,82 +0,0 @@
package commands
import (
"github.com/pkg/errors"
crypto "github.com/tendermint/go-crypto"
keys "github.com/tendermint/go-crypto/keys"
wire "github.com/tendermint/go-wire"
bc "github.com/tendermint/basecoin/types"
)
// AppTx Application transaction structure for client
type AppTx struct {
chainID string
signers []crypto.PubKey
Tx *bc.AppTx
}
var _ keys.Signable = &AppTx{}
// SignBytes returned the unsigned bytes, needing a signature
func (s *AppTx) SignBytes() []byte {
return s.Tx.SignBytes(s.chainID)
}
// Sign will add a signature and pubkey.
//
// Depending on the Signable, one may be able to call this multiple times for multisig
// Returns error if called with invalid data or too many times
func (s *AppTx) Sign(pubkey crypto.PubKey, sig crypto.Signature) error {
if len(s.signers) > 0 {
return errors.New("AppTx already signed")
}
s.Tx.SetSignature(sig)
s.signers = []crypto.PubKey{pubkey}
return nil
}
// Signers will return the public key(s) that signed if the signature
// is valid, or an error if there is any issue with the signature,
// including if there are no signatures
func (s *AppTx) Signers() ([]crypto.PubKey, error) {
if len(s.signers) == 0 {
return nil, errors.New("No signatures on AppTx")
}
return s.signers, nil
}
// TxBytes returns the transaction data as well as all signatures
// It should return an error if Sign was never called
func (s *AppTx) TxBytes() ([]byte, error) {
// TODO: verify it is signed
// Code and comment from: basecoin/cmd/basecoin/commands/tx.go
// Don't you hate having to do this?
// How many times have I lost an hour over this trick?!
txBytes := wire.BinaryBytes(bc.TxS{s.Tx})
return txBytes, nil
}
// TODO: this should really be in the basecoin.types SendTx,
// but that code is too ugly now, needs refactor..
func (a *AppTx) ValidateBasic() error {
if a.chainID == "" {
return errors.New("No chain-id specified")
}
in := a.Tx.Input
if len(in.Address) != 20 {
return errors.Errorf("Invalid input address length: %d", len(in.Address))
}
if !in.Coins.IsValid() {
return errors.Errorf("Invalid input coins %v", in.Coins)
}
if in.Coins.IsZero() {
return errors.New("Input coins cannot be zero")
}
if in.Sequence <= 0 {
return errors.New("Sequence must be greater than 0")
}
return nil
}

View File

@ -6,13 +6,11 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/tendermint/basecoin" "github.com/tendermint/basecoin"
"github.com/tendermint/light-client/commands" "github.com/tendermint/light-client/commands"
txcmd "github.com/tendermint/light-client/commands/txs" txcmd "github.com/tendermint/light-client/commands/txs"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
cmn "github.com/tendermint/tmlibs/common" cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/basecoin/modules/coin" "github.com/tendermint/basecoin/modules/coin"
@ -145,74 +143,6 @@ func parseChainAddress(toFlag string) (string, []byte, error) {
return chainPrefix, to, nil return chainPrefix, to, nil
} }
//-------------------------
// AppTx
// BroadcastAppTx wraps, signs, and executes an app tx basecoin transaction
func BroadcastAppTx(tx *btypes.AppTx) (*ctypes.ResultBroadcastTxCommit, error) {
// Sign if needed and post to the node. This it the work-horse
return txcmd.SignAndPostTx(WrapAppTx(tx))
}
// AddAppTxFlags adds flags required by apptx
func AddAppTxFlags(fs *flag.FlagSet) {
fs.String(FlagAmount, "", "Coins to send in the format <amt><coin>,<amt><coin>...")
fs.String(FlagFee, "0mycoin", "Coins for the transaction fee of the format <amt><coin>")
fs.Int64(FlagGas, 0, "Amount of gas for this transaction")
fs.Int(FlagSequence, -1, "Sequence number for this transaction")
}
// ReadAppTxFlags reads in the standard flags
// your command should parse info to set tx.Name and tx.Data
func ReadAppTxFlags() (gas int64, fee btypes.Coin, txInput btypes.TxInput, err error) {
// Set the gas
gas = viper.GetInt64(FlagGas)
// Parse the fee and amounts into coin types
fee, err = btypes.ParseCoin(viper.GetString(FlagFee))
if err != nil {
return
}
// retrieve the amount
var amount btypes.Coins
amount, err = btypes.ParseCoins(viper.GetString(FlagAmount))
if err != nil {
return
}
// get the PubKey of the signer
pk := txcmd.GetSigner()
// get addr if available
var addr []byte
if !pk.Empty() {
addr = pk.Address()
}
// set the output
txInput = btypes.TxInput{
Coins: amount,
Sequence: viper.GetInt(FlagSequence),
Address: addr,
}
// set the pubkey if needed
if txInput.Sequence == 1 {
txInput.PubKey = pk
}
return
}
// WrapAppTx wraps the transaction with chain id
func WrapAppTx(tx *btypes.AppTx) *AppTx {
return &AppTx{
chainID: commands.GetChainID(),
Tx: tx,
}
}
/** TODO copied from basecoin cli - put in common somewhere? **/ /** TODO copied from basecoin cli - put in common somewhere? **/
// ParseHexFlag parses a flag string to byte array // ParseHexFlag parses a flag string to byte array

View File

@ -1,113 +0,0 @@
package commands
import (
"github.com/pkg/errors"
bc "github.com/tendermint/basecoin/types"
crypto "github.com/tendermint/go-crypto"
keys "github.com/tendermint/go-crypto/keys"
wire "github.com/tendermint/go-wire"
)
type SendTx struct {
chainID string
signers []crypto.PubKey
Tx *bc.SendTx
}
var _ keys.Signable = &SendTx{}
// SignBytes returned the unsigned bytes, needing a signature
func (s *SendTx) SignBytes() []byte {
return s.Tx.SignBytes(s.chainID)
}
// Sign will add a signature and pubkey.
//
// Depending on the Signable, one may be able to call this multiple times for multisig
// Returns error if called with invalid data or too many times
func (s *SendTx) Sign(pubkey crypto.PubKey, sig crypto.Signature) error {
addr := pubkey.Address()
set := s.Tx.SetSignature(addr, sig)
if !set {
return errors.Errorf("Cannot add signature for address %X", addr)
}
s.signers = append(s.signers, pubkey)
return nil
}
// Signers will return the public key(s) that signed if the signature
// is valid, or an error if there is any issue with the signature,
// including if there are no signatures
func (s *SendTx) Signers() ([]crypto.PubKey, error) {
if len(s.signers) == 0 {
return nil, errors.New("No signatures on SendTx")
}
return s.signers, nil
}
// TxBytes returns the transaction data as well as all signatures
// It should return an error if Sign was never called
func (s *SendTx) TxBytes() ([]byte, error) {
// TODO: verify it is signed
// Code and comment from: basecoin/cmd/basecoin/commands/tx.go
// Don't you hate having to do this?
// How many times have I lost an hour over this trick?!
txBytes := wire.BinaryBytes(struct {
bc.Tx `json:"unwrap"`
}{s.Tx})
return txBytes, nil
}
// AddSigner sets address and pubkey info on the tx based on the key that
// will be used for signing
func (s *SendTx) AddSigner(pk crypto.PubKey) {
// get addr if available
var addr []byte
if !pk.Empty() {
addr = pk.Address()
}
// set the send address, and pubkey if needed
in := s.Tx.Inputs
in[0].Address = addr
if in[0].Sequence == 1 {
in[0].PubKey = pk
}
}
// TODO: this should really be in the basecoin.types SendTx,
// but that code is too ugly now, needs refactor..
func (s *SendTx) ValidateBasic() error {
if s.chainID == "" {
return errors.New("No chain-id specified")
}
for _, in := range s.Tx.Inputs {
if len(in.Address) != 20 {
return errors.Errorf("Invalid input address length: %d", len(in.Address))
}
if !in.Coins.IsValid() {
return errors.Errorf("Invalid input coins %v", in.Coins)
}
if in.Coins.IsZero() {
return errors.New("Input coins cannot be zero")
}
if in.Sequence <= 0 {
return errors.New("Sequence must be greater than 0")
}
}
for _, out := range s.Tx.Outputs {
// 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() {
return errors.Errorf("Invalid output coins %v", out.Coins)
}
if out.Coins.IsZero() {
return errors.New("Output coins cannot be zero")
}
}
return nil
}