-
Notifications
You must be signed in to change notification settings - Fork 565
/
account.go
128 lines (105 loc) · 3.88 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
package types
import (
"encoding/json"
"strings"
crypto "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkaddress "github.com/cosmos/cosmos-sdk/types/address"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
yaml "gopkg.in/yaml.v2"
)
var (
_ authtypes.GenesisAccount = (*InterchainAccount)(nil)
_ InterchainAccountI = (*InterchainAccount)(nil)
)
// InterchainAccountI wraps the authtypes.AccountI interface
type InterchainAccountI interface {
authtypes.AccountI
}
// interchainAccountPretty defines an unexported struct used for encoding the InterchainAccount details
type interchainAccountPretty struct {
Address sdk.AccAddress `json:"address" yaml:"address"`
PubKey string `json:"public_key" yaml:"public_key"`
AccountNumber uint64 `json:"account_number" yaml:"account_number"`
Sequence uint64 `json:"sequence" yaml:"sequence"`
AccountOwner string `json:"account_owner" yaml:"account_owner"`
}
// GenerateAddress returns an sdk.AccAddress derived using the provided module account address and port identifier.
// The sdk.AccAddress returned is a sub-address of the module account, using the controller chain's port identifier as the derivation key
func GenerateAddress(moduleAccAddr sdk.AccAddress, portID string) sdk.AccAddress {
return sdk.AccAddress(sdkaddress.Derive(moduleAccAddr, []byte(portID)))
}
// NewInterchainAccount creates and returns a new InterchainAccount type
func NewInterchainAccount(ba *authtypes.BaseAccount, accountOwner string) *InterchainAccount {
return &InterchainAccount{
BaseAccount: ba,
AccountOwner: accountOwner,
}
}
// SetPubKey implements the authtypes.AccountI interface
func (ia InterchainAccount) SetPubKey(pubKey crypto.PubKey) error {
return sdkerrors.Wrap(ErrUnsupported, "cannot set public key for interchain account")
}
// SetSequence implements the authtypes.AccountI interface
func (ia InterchainAccount) SetSequence(seq uint64) error {
return sdkerrors.Wrap(ErrUnsupported, "cannot set sequence number for interchain account")
}
// Validate implements basic validation of the InterchainAccount
func (ia InterchainAccount) Validate() error {
if strings.TrimSpace(ia.AccountOwner) == "" {
return sdkerrors.Wrap(ErrInvalidAccountAddress, "AccountOwner cannot be empty")
}
return ia.BaseAccount.Validate()
}
// String returns a string representation of the InterchainAccount
func (ia InterchainAccount) String() string {
out, _ := ia.MarshalYAML()
return string(out)
}
// MarshalYAML returns the YAML representation of the InterchainAccount
func (ia InterchainAccount) MarshalYAML() ([]byte, error) {
accAddr, err := sdk.AccAddressFromBech32(ia.Address)
if err != nil {
return nil, err
}
bz, err := yaml.Marshal(interchainAccountPretty{
Address: accAddr,
PubKey: "",
AccountNumber: ia.AccountNumber,
Sequence: ia.Sequence,
AccountOwner: ia.AccountOwner,
})
if err != nil {
return nil, err
}
return bz, nil
}
// MarshalJSON returns the JSON representation of the InterchainAccount
func (ia InterchainAccount) MarshalJSON() ([]byte, error) {
accAddr, err := sdk.AccAddressFromBech32(ia.Address)
if err != nil {
return nil, err
}
bz, err := json.Marshal(interchainAccountPretty{
Address: accAddr,
PubKey: "",
AccountNumber: ia.AccountNumber,
Sequence: ia.Sequence,
AccountOwner: ia.AccountOwner,
})
if err != nil {
return nil, err
}
return bz, nil
}
// UnmarshalJSON unmarshals raw JSON bytes into the InterchainAccount
func (ia *InterchainAccount) UnmarshalJSON(bz []byte) error {
var alias interchainAccountPretty
if err := json.Unmarshal(bz, &alias); err != nil {
return err
}
ia.BaseAccount = authtypes.NewBaseAccount(alias.Address, nil, alias.AccountNumber, alias.Sequence)
ia.AccountOwner = alias.AccountOwner
return nil
}