/
account.go
147 lines (117 loc) · 3.28 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
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
147
package v1beta1
import (
"errors"
errorsmod "cosmossdk.io/errors"
smartaccounttypes "github.com/aura-nw/aura/x/smartaccount/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)
var (
_ authtypes.AccountI = (*SmartAccount)(nil)
_ authtypes.GenesisAccount = (*SmartAccount)(nil)
_ codectypes.UnpackInterfacesMessage = (*SmartAccount)(nil)
)
// ------------------------------ SmartAccount ------------------------------
func NewSmartAccount(address string, accountNum, seq uint64) *SmartAccount {
return &SmartAccount{
Address: address,
AccountNumber: accountNum,
Sequence: seq,
}
}
func NewSmartAccountFromAccount(acc authtypes.AccountI) *SmartAccount {
return NewSmartAccount(acc.GetAddress().String(), acc.GetAccountNumber(), acc.GetSequence())
}
func (acc *SmartAccount) GetAddress() sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(acc.Address)
return addr
}
func (acc *SmartAccount) SetAddress(addr sdk.AccAddress) error {
if len(acc.Address) != 0 {
return errors.New("cannot override SmartAccount address")
}
acc.Address = addr.String()
return nil
}
func (acc *SmartAccount) GetPubKey() cryptotypes.PubKey {
if acc.PubKey == nil {
return nil
}
content, ok := acc.PubKey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return nil
}
return content
}
func (acc *SmartAccount) SetPubKey(pubKey cryptotypes.PubKey) error {
if pubKey == nil {
acc.PubKey = nil
return nil
}
any, err := codectypes.NewAnyWithValue(pubKey)
if err == nil {
acc.PubKey = any
}
return err
}
func (acc *SmartAccount) GetAccountNumber() uint64 {
return acc.AccountNumber
}
func (acc *SmartAccount) SetAccountNumber(accNumber uint64) error {
acc.AccountNumber = accNumber
return nil
}
func (acc *SmartAccount) GetSequence() uint64 {
return acc.Sequence
}
func (acc *SmartAccount) SetSequence(seq uint64) error {
acc.Sequence = seq
return nil
}
func (acc *SmartAccount) Validate() error {
if acc.Address == "" || acc.PubKey == nil {
return nil
}
_, err := sdk.AccAddressFromBech32(acc.Address)
if err != nil {
return err
}
return nil
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (acc SmartAccount) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
if acc.PubKey == nil {
return nil
}
var pubKey cryptotypes.PubKey
return unpacker.UnpackAny(acc.PubKey, &pubKey)
}
// PubKeyDecode decode *Any to cryptotypes.PubKey
func PubKeyDecode(pubKey *codectypes.Any) (cryptotypes.PubKey, error) {
if pubKey == nil {
return nil, smartaccounttypes.ErrNilPubkey
}
pkAny := pubKey.GetCachedValue()
pk, ok := pkAny.(cryptotypes.PubKey)
if ok {
return pk, nil
} else {
return nil, errorsmod.Wrapf(smartaccounttypes.ErrInvalidPubKey, "expecting PubKey, got: %T", pkAny)
}
}
// PubKeyToAny convert pubkey string to *Any
func PubKeyToAny(cdc codec.Codec, raw []byte) (*codectypes.Any, error) {
var pubKey cryptotypes.PubKey
err := cdc.UnmarshalInterfaceJSON(raw, &pubKey)
if err != nil {
return nil, err
}
any, err := codectypes.NewAnyWithValue(pubKey)
if err != nil {
return nil, err
}
return any, nil
}