/
types.go
146 lines (121 loc) · 3.41 KB
/
types.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package auth
import (
"encoding/json"
"errors"
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/tendermint/tendermint/crypto"
commoncodec "github.com/irisnet/core-sdk-go/common/codec"
codectypes "github.com/irisnet/core-sdk-go/common/codec/types"
sdk "github.com/irisnet/core-sdk-go/types"
)
// Account is an interface used to store coins at a given address within state.
// It presumes a notion of sequence numbers for replay protection,
// a notion of account numbers for replay protection for previously pruned accounts,
// and a pubkey for authentication purposes.
//
// Many complex conditions can be used in the concrete struct which implements Account.
type Account interface {
GetAddress() sdk.AccAddress
SetAddress(sdk.AccAddress) error // errors if already set.
GetPubKey() crypto.PubKey // can return nil.
SetPubKey(crypto.PubKey) error
GetAccountNumber() uint64
SetAccountNumber(uint64) error
GetSequence() uint64
SetSequence(uint64) error
}
//BaseAccount Have they all been implemented
var _ Account = (*BaseAccount)(nil)
// GetAddress - Implements sdk.AccountI.
func (acc BaseAccount) GetAddress() sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(acc.Address)
return addr
}
// SetAddress - Implements sdk.AccountI.
func (acc *BaseAccount) SetAddress(addr sdk.AccAddress) error {
if len(acc.Address) != 0 {
return errors.New("cannot override BaseAccount address")
}
acc.Address = addr.String()
return nil
}
// GetPubKey - Implements sdk.AccountI.
func (acc BaseAccount) GetPubKey() (pk crypto.PubKey) {
if acc.PubKey == nil {
return nil
}
content, ok := acc.PubKey.GetCachedValue().(crypto.PubKey)
if !ok {
return nil
}
return content
}
// SetPubKey - Implements sdk.AccountI.
func (acc *BaseAccount) SetPubKey(pubKey crypto.PubKey) error {
if pubKey == nil {
acc.PubKey = nil
} else {
protoMsg, ok := pubKey.(proto.Message)
if !ok {
return sdk.Wrap(fmt.Errorf("err invalid key, can't proto encode %T", protoMsg))
}
any, err := codectypes.NewAnyWithValue(protoMsg)
if err != nil {
return err
}
acc.PubKey = any
}
return nil
}
// GetAccountNumber - Implements AccountI
func (acc BaseAccount) GetAccountNumber() uint64 {
return acc.AccountNumber
}
// SetAccountNumber - Implements AccountI
func (acc *BaseAccount) SetAccountNumber(accNumber uint64) error {
acc.AccountNumber = accNumber
return nil
}
// GetSequence - Implements sdk.AccountI.
func (acc BaseAccount) GetSequence() uint64 {
return acc.Sequence
}
// SetSequence - Implements sdk.AccountI.
func (acc *BaseAccount) SetSequence(seq uint64) error {
acc.Sequence = seq
return nil
}
//json.Marshal BaseAccount
func (acc BaseAccount) String() string {
out, _ := json.Marshal(acc)
return string(out)
}
// Convert return a sdk.BaseAccount
func (acc *BaseAccount) Convert() interface{} {
// error don't use it
return nil
}
// Convert return a sdk.BaseAccount
// in order to unpack pubKey so not use Convert()
func (acc *BaseAccount) ConvertAccount(cdc commoncodec.Marshaler) interface{} {
account := sdk.BaseAccount{
Address: acc.Address,
AccountNumber: acc.AccountNumber,
Sequence: acc.Sequence,
}
var pkStr string
if acc.PubKey == nil {
return account
}
var pk crypto.PubKey
if err := cdc.UnpackAny(acc.PubKey, &pk); err != nil {
return sdk.BaseAccount{}
}
pkStr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk)
if err != nil {
panic(err)
}
account.PubKey = pkStr
return account
}