Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Julien Robert <julien@rbrt.fr> |
||
|---|---|---|
| .. | ||
| accountstd | ||
| cli | ||
| defaults | ||
| interfaces/account_abstraction/v1 | ||
| internal | ||
| proto | ||
| testing | ||
| v1 | ||
| account_test.go | ||
| CHANGELOG.md | ||
| coin_transfer.go | ||
| depinject.go | ||
| genesis_test.go | ||
| genesis.go | ||
| go.mod | ||
| go.sum | ||
| keeper_account_abstraction.go | ||
| keeper_test.go | ||
| keeper.go | ||
| module.go | ||
| msg_server_test.go | ||
| msg_server.go | ||
| query_server_test.go | ||
| query_server.go | ||
| README.md | ||
| sonar-project.properties | ||
| utils_test.go | ||
x/accounts
The x/accounts module provides module and facilities for writing smart cosmos-sdk accounts.
Supporting Custom Accounts in the x/auth gRPC Server
Overview
The x/auth module provides a mechanism for custom account types to be exposed via its Account and AccountInfo gRPC
queries. This feature is particularly useful for ensuring compatibility with existing wallets that have not yet integrated
with x/accounts but still need to parse account information post-migration.
Implementation
To support this feature, your custom account type needs to implement the auth.QueryLegacyAccount handler. Here are some important points to consider:
- Selective Implementation: This implementation is not required for every account type. It's only necessary for accounts you want to expose through the x/auth gRPC
AccountandAccountInfomethods. - Flexible Response: The
infofield in theQueryLegacyAccountResponseis optional. If your custom account cannot be represented as aBaseAccount, you can leave this field empty.
Example Implementation
A concrete example of implementation can be found in defaults/base/account.go. Here's a simplified version:
func (a Account) AuthRetroCompatibility(ctx context.Context, _ *authtypes.QueryLegacyAccount) (*authtypes.QueryLegacyAccountResponse, error) {
seq := a.GetSequence()
num := a.GetNumber()
address := a.GetAddress()
pubKey := a.GetPubKey()
baseAccount := &authtypes.BaseAccount{
AccountNumber: num,
Sequence: seq,
Address: address,
}
// Convert pubKey to Any type
pubKeyAny, err := gogotypes.NewAnyWithValue(pubKey)
if err != nil {
return nil, err
}
baseAccount.PubKey = pubKeyAny
// Convert the entire baseAccount to Any type
accountAny, err := gogotypes.NewAnyWithValue(baseAccount)
if err != nil {
return nil, err
}
return &authtypes.QueryLegacyAccountResponse{
Account: accountAny,
Info: baseAccount,
}, nil
}
Usage Notes
- Implement this handler only for account types you want to expose via x/auth gRPC methods.
- The
infofield in the response can be nil if your account doesn't fit theBaseAccountstructure.
Genesis
Creating accounts on genesis
In order to create accounts at genesis, the x/accounts module allows developers to provide
a list of genesis MsgInit messages that will be executed in the x/accounts genesis flow.
The init messages are generated offline. You can also use the following CLI command to generate the
json messages: simd accounts tx init [account type] [msg] --from me --genesis. This will generate
a jsonified init message wrapped in an x/accounts MsgInit.
This follows the same initialization flow and rules that would happen if the chain is running. The only concrete difference is that this is happening at the genesis block.
For example, given the following genesis.json file:
{
"app_state": {
"accounts": {
"init_account_msgs": [
{
"sender": "account_creator_address",
"account_type": "lockup",
"message": {
"@type": "cosmos.accounts.defaults.lockup.MsgInitLockupAccount",
"owner": "some_owner",
"end_time": "..",
"start_time": ".."
},
"funds": [
{
"denom": "stake",
"amount": "1000"
}
]
}
]
}
}
}
The accounts module will run the lockup account initialization message.