-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
account.go
106 lines (85 loc) · 2.45 KB
/
account.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package auth
import (
"errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
crypto "github.com/tendermint/go-crypto"
)
// Account is a standard account using a sequence number for replay protection
// and a pubkey for authentication.
type Account interface {
GetAddress() sdk.Address
SetAddress(sdk.Address) error // errors if already set.
GetPubKey() crypto.PubKey // can return nil.
SetPubKey(crypto.PubKey) error
GetSequence() int64
SetSequence(int64) error
GetCoins() sdk.Coins
SetCoins(sdk.Coins) error
}
// AccountDecoder unmarshals account bytes
type AccountDecoder func(accountBytes []byte) (Account, error)
//-----------------------------------------------------------
// BaseAccount
var _ Account = (*BaseAccount)(nil)
// BaseAccount - base account structure.
// Extend this by embedding this in your AppAccount.
// See the examples/basecoin/types/account.go for an example.
type BaseAccount struct {
Address sdk.Address `json:"address"`
Coins sdk.Coins `json:"coins"`
PubKey crypto.PubKey `json:"public_key"`
Sequence int64 `json:"sequence"`
}
func NewBaseAccountWithAddress(addr sdk.Address) BaseAccount {
return BaseAccount{
Address: addr,
}
}
// Implements sdk.Account.
func (acc BaseAccount) GetAddress() sdk.Address {
return acc.Address
}
// Implements sdk.Account.
func (acc *BaseAccount) SetAddress(addr sdk.Address) error {
if len(acc.Address) != 0 {
return errors.New("cannot override BaseAccount address")
}
acc.Address = addr
return nil
}
// Implements sdk.Account.
func (acc BaseAccount) GetPubKey() crypto.PubKey {
return acc.PubKey
}
// Implements sdk.Account.
func (acc *BaseAccount) SetPubKey(pubKey crypto.PubKey) error {
acc.PubKey = pubKey
return nil
}
// Implements sdk.Account.
func (acc *BaseAccount) GetCoins() sdk.Coins {
return acc.Coins
}
// Implements sdk.Account.
func (acc *BaseAccount) SetCoins(coins sdk.Coins) error {
acc.Coins = coins
return nil
}
// Implements sdk.Account.
func (acc *BaseAccount) GetSequence() int64 {
return acc.Sequence
}
// Implements sdk.Account.
func (acc *BaseAccount) SetSequence(seq int64) error {
acc.Sequence = seq
return nil
}
//----------------------------------------
// Wire
// Most users shouldn't use this, but this comes handy for tests.
func RegisterBaseAccount(cdc *wire.Codec) {
cdc.RegisterInterface((*Account)(nil), nil)
cdc.RegisterConcrete(&BaseAccount{}, "cosmos-sdk/BaseAccount", nil)
wire.RegisterCrypto(cdc)
}