cosmos-sdk/modules/coin/helper.go

58 lines
1.4 KiB
Go
Raw Normal View History

2017-07-06 04:40:02 -07:00
package coin
import (
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire/data"
2017-07-06 05:59:45 -07:00
"github.com/tendermint/basecoin"
2017-07-19 01:51:36 -07:00
"github.com/tendermint/basecoin/modules/auth"
2017-07-06 04:40:02 -07:00
)
// AccountWithKey is a helper for tests, that includes and account
// along with the private key to access it.
type AccountWithKey struct {
2017-07-25 07:06:07 -07:00
Key crypto.PrivKey
Sequence uint32
2017-07-06 04:40:02 -07:00
Account
}
// NewAccountWithKey creates an account with the given balance
// and a random private key
2017-07-06 05:59:45 -07:00
func NewAccountWithKey(coins Coins) *AccountWithKey {
2017-07-06 04:40:02 -07:00
return &AccountWithKey{
Key: crypto.GenPrivKeyEd25519().Wrap(),
Account: Account{Coins: coins},
}
}
// Address returns the public key address for this account
func (a *AccountWithKey) Address() []byte {
return a.Key.PubKey().Address()
}
// Actor returns the basecoin actor associated with this account
func (a *AccountWithKey) Actor() basecoin.Actor {
return auth.SigPerm(a.Key.PubKey().Address())
2017-07-06 04:40:02 -07:00
}
2017-07-25 07:06:07 -07:00
// NextSequence returns the next sequence to sign with
func (a *AccountWithKey) NextSequence() uint32 {
a.Sequence++
return a.Sequence
}
2017-07-06 04:40:02 -07:00
// MakeOption returns a string to use with SetOption to initialize this account
//
// This is intended for use in test cases
func (a *AccountWithKey) MakeOption() string {
info := GenesisAccount{
Address: a.Address(),
Balance: a.Coins,
2017-07-06 04:40:02 -07:00
}
js, err := data.ToJSON(info)
if err != nil {
panic(err)
}
return string(js)
}