Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Add bech32 prefix to authkeeper #9759

Merged
merged 73 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from 68 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
030fc2c
first draft
cyberbono3 Jul 20, 2021
556810d
tests pass
cyberbono3 Jul 23, 2021
8ec7872
add DefaultBech32Prefix
cyberbono3 Jul 23, 2021
52ec23d
add comments to auth/query.proto
cyberbono3 Jul 23, 2021
e636781
set sdk.Bech32MainPrefix argument in NewAccountKeeeper()
cyberbono3 Jul 26, 2021
9788e2c
update x/auth/atlas/atlas-v0.39.1.md
cyberbono3 Jul 26, 2021
3fcd6c3
fix TestBech32Prefix
cyberbono3 Jul 26, 2021
64f031f
fix tests
cyberbono3 Jul 26, 2021
befc823
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into cy…
cyberbono3 Jul 26, 2021
413cf7a
gofmt
cyberbono3 Jul 26, 2021
d9be317
update atlas-v0.39.1.md
cyberbono3 Jul 26, 2021
1dd726b
Merge branch 'master' into cyberbono3/add-bech32Prefix-to-authkeeper
cyberbono3 Jul 26, 2021
6a42f00
draft
cyberbono3 Jul 29, 2021
2861b30
Merge branch 'cyberbono3/add-bech32Prefix-to-authkeeper' of https://g…
cyberbono3 Jul 29, 2021
704b006
WIP applying aaron suggestions
cyberbono3 Jul 29, 2021
2459aa0
tests pass
cyberbono3 Aug 2, 2021
54945f1
apply aaron suggestions
cyberbono3 Aug 2, 2021
5992482
update proto comments
cyberbono3 Aug 2, 2021
1734485
update AddressBytesToString signature
cyberbono3 Aug 2, 2021
0cae2b4
add changelog entry for NewAccountKeeper API breaking change
cyberbono3 Aug 3, 2021
743b73c
introduce ConvertAddressStringToBytes and ConvertAddressBytesToString…
cyberbono3 Aug 4, 2021
d602c76
merge master into my branch and resolve conflicts
cyberbono3 Aug 4, 2021
0b5d411
revert unnecessary changes
cyberbono3 Aug 4, 2021
1a552d7
remove unnecessary code
cyberbono3 Aug 4, 2021
366e589
update codec comments
cyberbono3 Aug 4, 2021
ac607ee
address lint issues
cyberbono3 Aug 4, 2021
6cde687
Update CHANGELOG.md
cyberbono3 Aug 5, 2021
4f84700
Update proto/cosmos/auth/v1beta1/query.proto
cyberbono3 Aug 5, 2021
8d223e1
Update proto/cosmos/auth/v1beta1/query.proto
cyberbono3 Aug 5, 2021
9696a52
Update proto/cosmos/auth/v1beta1/query.proto
cyberbono3 Aug 5, 2021
888cf80
Update proto/cosmos/auth/v1beta1/query.proto
cyberbono3 Aug 5, 2021
afb95d5
Update types/address/codec.go
cyberbono3 Aug 5, 2021
21d3090
Update proto/cosmos/auth/v1beta1/query.proto
cyberbono3 Aug 5, 2021
404b31f
Update x/auth/keeper/keeper.go
cyberbono3 Aug 5, 2021
9c5941d
proto-gen
cyberbono3 Aug 5, 2021
7b77ced
apply reviewer comments
cyberbono3 Aug 5, 2021
acf5525
minor fixes
cyberbono3 Aug 5, 2021
c544184
implement VerifyFormat
cyberbono3 Aug 5, 2021
903cd03
Update CHANGELOG.md
cyberbono3 Aug 6, 2021
3afebd7
Update types/address/codec.go
cyberbono3 Aug 6, 2021
9289089
fix grpc query tests
cyberbono3 Aug 9, 2021
ecc5c3e
let VerifyAddressFormat call VerifyFormat
cyberbono3 Aug 9, 2021
570332c
gofmt
cyberbono3 Aug 9, 2021
aea81bc
rename address.Codec methods
cyberbono3 Aug 10, 2021
3d48438
proto gen
cyberbono3 Aug 10, 2021
e6ed444
remove TestCustomAddressVerifier
cyberbono3 Aug 11, 2021
e45fb18
Merge branch 'master' into cyberbono3/add-bech32Prefix-to-authkeeper
cyberbono3 Aug 12, 2021
76f9c8a
revert global GetConfig and TestCustomAddressVerifier
cyberbono3 Aug 15, 2021
35600c5
use sdkerrors in keeper.go
cyberbono3 Aug 17, 2021
e31297b
Update CHANGELOG.md
cyberbono3 Aug 18, 2021
9efcf4a
Update types/address.go
cyberbono3 Aug 18, 2021
60cf6e7
rename address codec interface methods
cyberbono3 Aug 18, 2021
293bdf1
move bech32_codec logic into separate file
cyberbono3 Aug 18, 2021
31bac6d
remove VerifyFormat
cyberbono3 Aug 19, 2021
12106d3
Update types/address/codec.go
cyberbono3 Aug 20, 2021
74cdb60
Update types/address/codec.go
cyberbono3 Aug 20, 2021
49f1858
add ref to VerifyAddressFormat TODO
cyberbono3 Aug 20, 2021
f12c44b
Merge branch 'master' of https://github.com/cosmos/cosmos-sdk into cy…
cyberbono3 Aug 20, 2021
3678bce
Update types/address/codec.go
cyberbono3 Aug 24, 2021
53844bb
Update x/auth/keeper/keeper.go
cyberbono3 Aug 24, 2021
b3fb3bd
Fix comments
amaury1093 Aug 24, 2021
131de71
Update x/auth/keeper/keeper.go
cyberbono3 Aug 24, 2021
b32ce72
Update x/auth/keeper/grpc_query_test.go
cyberbono3 Aug 24, 2021
acc6675
Update x/auth/keeper/keeper.go
cyberbono3 Aug 24, 2021
0b27c66
Update x/auth/keeper/keeper.go
cyberbono3 Aug 24, 2021
d4267bf
Update x/auth/keeper/grpc_query_test.go
cyberbono3 Aug 24, 2021
9aae756
Update x/auth/keeper/grpc_query_test.go
cyberbono3 Aug 24, 2021
3d2d5b0
address reviewer comments
cyberbono3 Aug 24, 2021
5d0ba44
Update x/auth/keeper/keeper.go
cyberbono3 Aug 25, 2021
a351a89
Update x/auth/keeper/grpc_query_test.go
cyberbono3 Aug 25, 2021
aa76cbc
Update x/auth/keeper/grpc_query_test.go
cyberbono3 Aug 25, 2021
fb9e5a3
apply reviewer comments
cyberbono3 Aug 25, 2021
4d2a866
Merge branch 'master' into cyberbono3/add-bech32Prefix-to-authkeeper
amaury1093 Aug 25, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### API Breaking Changes

* [\#9759](https://github.com/cosmos/cosmos-sdk/pull/9759) `NewAccountKeeeper` in `x/auth` now takes an additional `bech32Prefix` argument that represents `sdk.Bech32MainPrefix`.
* [\#9628](https://github.com/cosmos/cosmos-sdk/pull/9628) Rename `x/{mod}/legacy` to `x/{mod}/migrations`.
* [\#9571](https://github.com/cosmos/cosmos-sdk/pull/9571) Implemented error handling for staking hooks, which now return an error on failure.
* [\#9427](https://github.com/cosmos/cosmos-sdk/pull/9427) Move simapp `FundAccount` and `FundModuleAccount` to `x/bank/testutil`
Expand Down
6 changes: 2 additions & 4 deletions client/docs/statik/statik.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/grpc/tmservice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/network"
qtypes "github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/testutil/rest"
qtypes "github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/version"
)

Expand Down
94 changes: 94 additions & 0 deletions docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
- [PageResponse](#cosmos.base.query.v1beta1.PageResponse)

- [cosmos/auth/v1beta1/query.proto](#cosmos/auth/v1beta1/query.proto)
- [AddressBytesToStringRequest](#cosmos.auth.v1beta1.AddressBytesToStringRequest)
- [AddressBytesToStringResponse](#cosmos.auth.v1beta1.AddressBytesToStringResponse)
- [AddressStringToBytesRequest](#cosmos.auth.v1beta1.AddressStringToBytesRequest)
- [AddressStringToBytesResponse](#cosmos.auth.v1beta1.AddressStringToBytesResponse)
- [Bech32PrefixRequest](#cosmos.auth.v1beta1.Bech32PrefixRequest)
- [Bech32PrefixResponse](#cosmos.auth.v1beta1.Bech32PrefixResponse)
- [QueryAccountRequest](#cosmos.auth.v1beta1.QueryAccountRequest)
- [QueryAccountResponse](#cosmos.auth.v1beta1.QueryAccountResponse)
- [QueryAccountsRequest](#cosmos.auth.v1beta1.QueryAccountsRequest)
Expand Down Expand Up @@ -844,6 +850,91 @@ corresponding request message has used PageRequest.



<a name="cosmos.auth.v1beta1.AddressBytesToStringRequest"></a>

### AddressBytesToStringRequest
AddressBytesToStringRequest is the request type for AddressString rpc method


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `address_bytes` | [bytes](#bytes) | | |






<a name="cosmos.auth.v1beta1.AddressBytesToStringResponse"></a>

### AddressBytesToStringResponse
AddressBytesToStringResponse is the response type for AddressString rpc method


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `address_string` | [string](#string) | | |






<a name="cosmos.auth.v1beta1.AddressStringToBytesRequest"></a>

### AddressStringToBytesRequest
AddressStringToBytesRequest is the request type for AccountBytes rpc method


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `address_string` | [string](#string) | | |






<a name="cosmos.auth.v1beta1.AddressStringToBytesResponse"></a>

### AddressStringToBytesResponse
AddressStringToBytesResponse is the response type for AddressBytes rpc method


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `address_bytes` | [bytes](#bytes) | | |






<a name="cosmos.auth.v1beta1.Bech32PrefixRequest"></a>

### Bech32PrefixRequest
Bech32PrefixRequest is the request type for Bech32Prefix rpc method






<a name="cosmos.auth.v1beta1.Bech32PrefixResponse"></a>

### Bech32PrefixResponse
Bech32PrefixResponse is the response type for Bech32Prefix rpc method


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `bech32_prefix` | [string](#string) | | |






<a name="cosmos.auth.v1beta1.QueryAccountRequest"></a>

### QueryAccountRequest
Expand Down Expand Up @@ -972,6 +1063,9 @@ Query defines the gRPC querier service.
| `Account` | [QueryAccountRequest](#cosmos.auth.v1beta1.QueryAccountRequest) | [QueryAccountResponse](#cosmos.auth.v1beta1.QueryAccountResponse) | Account returns account details based on address. | GET|/cosmos/auth/v1beta1/accounts/{address}|
| `Params` | [QueryParamsRequest](#cosmos.auth.v1beta1.QueryParamsRequest) | [QueryParamsResponse](#cosmos.auth.v1beta1.QueryParamsResponse) | Params queries all parameters. | GET|/cosmos/auth/v1beta1/params|
| `ModuleAccounts` | [QueryModuleAccountsRequest](#cosmos.auth.v1beta1.QueryModuleAccountsRequest) | [QueryModuleAccountsResponse](#cosmos.auth.v1beta1.QueryModuleAccountsResponse) | ModuleAccounts returns all the existing module accounts. | GET|/cosmos/auth/v1beta1/module_accounts|
| `Bech32Prefix` | [Bech32PrefixRequest](#cosmos.auth.v1beta1.Bech32PrefixRequest) | [Bech32PrefixResponse](#cosmos.auth.v1beta1.Bech32PrefixResponse) | Bech32 queries bech32Prefix | GET|/cosmos/auth/v1beta1/bech32|
| `AddressBytesToString` | [AddressBytesToStringRequest](#cosmos.auth.v1beta1.AddressBytesToStringRequest) | [AddressBytesToStringResponse](#cosmos.auth.v1beta1.AddressBytesToStringResponse) | AddressBytesToString converts Account Address bytes to string | GET|/cosmos/auth/v1beta1/bech32/{address_bytes}|
| `AddressStringToBytes` | [AddressStringToBytesRequest](#cosmos.auth.v1beta1.AddressStringToBytesRequest) | [AddressStringToBytesResponse](#cosmos.auth.v1beta1.AddressStringToBytesResponse) | AddressStringToBytes converts Address string to bytes | GET|/cosmos/auth/v1beta1/bech32/{address_string}|

<!-- end services -->

Expand Down
63 changes: 53 additions & 10 deletions proto/cosmos/auth/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ service Query {
rpc ModuleAccounts(QueryModuleAccountsRequest) returns (QueryModuleAccountsResponse) {
option (google.api.http).get = "/cosmos/auth/v1beta1/module_accounts";
}

// Bech32 queries bech32Prefix
rpc Bech32Prefix(Bech32PrefixRequest) returns (Bech32PrefixResponse) {
option (google.api.http).get = "/cosmos/auth/v1beta1/bech32";
}

// AddressBytesToString converts Account Address bytes to string
rpc AddressBytesToString(AddressBytesToStringRequest) returns (AddressBytesToStringResponse) {
option (google.api.http).get = "/cosmos/auth/v1beta1/bech32/{address_bytes}";
}

// AddressStringToBytes converts Address string to bytes
rpc AddressStringToBytes(AddressStringToBytesRequest) returns (AddressStringToBytesResponse) {
option (google.api.http).get = "/cosmos/auth/v1beta1/bech32/{address_string}";
}
}

// QueryAccountsRequest is the request type for the Query/Accounts RPC method.
Expand All @@ -57,6 +72,15 @@ message QueryAccountRequest {
string address = 1;
}

// QueryModuleAccountsRequest is the request type for the Query/ModuleAccounts RPC method.
message QueryModuleAccountsRequest {}

// QueryParamsResponse is the response type for the Query/Params RPC method.
message QueryParamsResponse {
// params defines the parameters of the module.
Params params = 1 [(gogoproto.nullable) = false];
}

// QueryAccountResponse is the response type for the Query/Account RPC method.
message QueryAccountResponse {
// account defines the account of the corresponding address.
Expand All @@ -66,16 +90,35 @@ message QueryAccountResponse {
// QueryParamsRequest is the request type for the Query/Params RPC method.
message QueryParamsRequest {}

// QueryParamsResponse is the response type for the Query/Params RPC method.
message QueryParamsResponse {
// params defines the parameters of the module.
Params params = 1 [(gogoproto.nullable) = false];
}

// QueryModuleAccountsRequest is the request type for the Query/ModuleAccounts RPC method.
message QueryModuleAccountsRequest {}

// QueryModuleAccountsResponse is the response type for the Query/ModuleAccounts RPC method.
message QueryModuleAccountsResponse {
repeated google.protobuf.Any accounts = 1 [(cosmos_proto.accepts_interface) = "ModuleAccountI"];
}
}

// Bech32PrefixRequest is the request type for Bech32Prefix rpc method
message Bech32PrefixRequest {}

// Bech32PrefixResponse is the response type for Bech32Prefix rpc method
message Bech32PrefixResponse {
string bech32_prefix = 1;
}

// AddressBytesToStringRequest is the request type for AddressString rpc method
message AddressBytesToStringRequest {
bytes address_bytes = 1;
}

// AddressBytesToStringResponse is the response type for AddressString rpc method
message AddressBytesToStringResponse {
string address_string = 1;
}

// AddressStringToBytesRequest is the request type for AccountBytes rpc method
message AddressStringToBytesRequest {
string address_string = 1;
}

// AddressStringToBytesResponse is the response type for AddressBytes rpc method
message AddressStringToBytesResponse {
bytes address_bytes = 1;
}
2 changes: 1 addition & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func NewSimApp(

// add keepers
app.AccountKeeper = authkeeper.NewAccountKeeper(
appCodec, keys[authtypes.StoreKey], app.GetSubspace(authtypes.ModuleName), authtypes.ProtoBaseAccount, maccPerms,
appCodec, keys[authtypes.StoreKey], app.GetSubspace(authtypes.ModuleName), authtypes.ProtoBaseAccount, maccPerms, sdk.Bech32MainPrefix,
)
app.BankKeeper = bankkeeper.NewBaseKeeper(
appCodec, keys[banktypes.StoreKey], app.AccountKeeper, app.GetSubspace(banktypes.ModuleName), app.ModuleAccountAddrs(),
Expand Down
4 changes: 3 additions & 1 deletion types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ func AccAddressFromHex(address string) (addr AccAddress, err error) {

// VerifyAddressFormat verifies that the provided bytes form a valid address
// according to the default address rules or a custom address verifier set by
// GetConfig().SetAddressVerifier()
// GetConfig().SetAddressVerifier().
// TODO make an issue to get rid of global Config
// ref: https://github.com/cosmos/cosmos-sdk/issues/9690
func VerifyAddressFormat(bz []byte) error {
verifier := GetConfig().GetAddressVerifier()
if verifier != nil {
Expand Down
9 changes: 9 additions & 0 deletions types/address/codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package address

// Codec defines an interface to convert addresses from and to string/bytes.
type Codec interface {
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved
// StringToBytes decodes text to bytes
StringToBytes(text string) ([]byte, error)
// BytesToString encodes bytes to text
BytesToString(bz []byte) (string, error)
}
46 changes: 46 additions & 0 deletions x/auth/keeper/bech32_codec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/types/bech32"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

type bech32Codec struct {
cyberbono3 marked this conversation as resolved.
Show resolved Hide resolved
bech32Prefix string
}

var _ address.Codec = &bech32Codec{}

func newBech32Codec(prefix string) bech32Codec {
return bech32Codec{prefix}
}

// StringToBytes encodes text to bytes
func (bc bech32Codec) StringToBytes(text string) ([]byte, error) {
hrp, bz, err := bech32.DecodeAndConvert(text)
if err != nil {
return nil, err
}

if hrp != bc.bech32Prefix {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "hrp does not match bech32Prefix")
}

if err := sdk.VerifyAddressFormat(bz); err != nil {
return nil, err
}

return bz, nil
}

// BytesToString decodes bytes to text
func (bc bech32Codec) BytesToString(bz []byte) (string, error) {
text, err := bech32.ConvertAndEncode(bc.bech32Prefix, bz)
if err != nil {
return "", err
}

return text, nil
}
45 changes: 45 additions & 0 deletions x/auth/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package keeper

import (
"context"
"errors"
"strings"

"github.com/cosmos/cosmos-sdk/store/prefix"
"github.com/cosmos/cosmos-sdk/types/query"
Expand Down Expand Up @@ -108,3 +110,46 @@ func (ak AccountKeeper) ModuleAccounts(c context.Context, req *types.QueryModule

return &types.QueryModuleAccountsResponse{Accounts: modAccounts}, nil
}

func (ak AccountKeeper) Bech32Prefix(ctx context.Context, req *types.Bech32PrefixRequest) (*types.Bech32PrefixResponse, error) {
bech32Prefix, err := ak.getBech32Prefix()
if err != nil {
return nil, err
}

return &types.Bech32PrefixResponse{Bech32Prefix: bech32Prefix}, nil
}

func (ak AccountKeeper) AddressBytesToString(ctx context.Context, req *types.AddressBytesToStringRequest) (*types.AddressBytesToStringResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if len(req.AddressBytes) == 0 {
return nil, errors.New("empty address bytes is not allowed")
}

text, err := ak.addressCdC.BytesToString(req.AddressBytes)
if err != nil {
return nil, err
}

return &types.AddressBytesToStringResponse{AddressString: text}, nil
}

func (ak AccountKeeper) AddressStringToBytes(ctx context.Context, req *types.AddressStringToBytesRequest) (*types.AddressStringToBytesResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if len(strings.TrimSpace(req.AddressString)) == 0 {
return nil, errors.New("empty address string is not allowed")
}

bz, err := ak.addressCdC.StringToBytes(req.AddressString)
if err != nil {
return nil, err
}

return &types.AddressStringToBytesResponse{AddressBytes: bz}, nil
}
Loading