/
helper.go
57 lines (49 loc) · 1.37 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package coin
import (
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire/data"
sdk "github.com/cosmos/cosmos-sdk"
"github.com/cosmos/cosmos-sdk/modules/auth"
)
// AccountWithKey is a helper for tests, that includes and account
// along with the private key to access it.
type AccountWithKey struct {
Key crypto.PrivKey
Sequence uint32
Account
}
// NewAccountWithKey creates an account with the given balance
// and a random private key
func NewAccountWithKey(coins Coins) *AccountWithKey {
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() sdk.Actor {
return auth.SigPerm(a.Key.PubKey().Address())
}
// NextSequence returns the next sequence to sign with
func (a *AccountWithKey) NextSequence() uint32 {
a.Sequence++
return a.Sequence
}
// MakeOption returns a string to use with InitState 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,
}
js, err := data.ToJSON(info)
if err != nil {
panic(err)
}
return string(js)
}