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

fix: treat addresses as strings in sdk internals #298

Merged
merged 10 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
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 @@ -66,6 +66,7 @@
* (proto) [\#106](https://github.com/line/lfb-sdk/pull/106) Rename package of proto files
* (api) [\#130](https://github.com/line/lfb-sdk/pull/130) Rename rest apis
* (auth) [\#265](https://github.com/line/lfb-sdk/pull/265) Introduce sig block height for the new replay protection
* (global) [\#298](https://github.com/line/lfb-sdk/pull/298) Treat addresses as strings

### Build, CI
* (ci) [\#234](https://github.com/line/lfb-sdk/pull/234) Fix branch name in ci script
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ proto-all: proto-format proto-lint proto-gen

proto-gen:
@echo "Generating Protobuf files"
$(DOCKER) run --rm -e -v $(CURDIR):/workspace --workdir /workspace tendermintdev/sdk-proto-gen sh ./scripts/protocgen.sh
$(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace tendermintdev/sdk-proto-gen sh ./scripts/protocgen.sh
whylee259 marked this conversation as resolved.
Show resolved Hide resolved

proto-format:
@echo "Formatting Protobuf files"
Expand Down
2 changes: 1 addition & 1 deletion baseapp/accountwgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func newTestPrivKeys(num int) []*secp256k1.PrivKey {
func getAddrs(privs []*secp256k1.PrivKey) []sdk.AccAddress {
addrs := make([]sdk.AccAddress, 0, len(privs))
for _, priv := range privs {
addrs = append(addrs, sdk.AccAddress(priv.PubKey().Address()))
addrs = append(addrs, sdk.BytesToAccAddress(priv.PubKey().Address()))
}
return addrs
}
Expand Down
4 changes: 2 additions & 2 deletions baseapp/msg_service_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func TestMsgService(t *testing.T) {

// Second round: all signer infos are set, so each signer can sign.
signerData := authsigning.SignerData{
ChainID: "test",
Sequence: 0,
ChainID: "test",
Sequence: 0,
}
sigV2, err = tx.SignWithPrivKey(
encCfg.TxConfig.SignModeHandler().DefaultMode(), signerData,
Expand Down
16 changes: 8 additions & 8 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,28 +287,28 @@ func (ctx Context) printOutput(out []byte) error {
// address is returned.
func GetFromFields(kr keyring.Keyring, from string, genOnly bool) (sdk.AccAddress, string, keyring.KeyType, error) {
if from == "" {
return nil, "", 0, nil
return "", "", 0, nil
}

if genOnly {
addr, err := sdk.AccAddressFromBech32(from)
err := sdk.ValidateAccAddress(from)
if err != nil {
return nil, "", 0, errors.Wrap(err, "must provide a valid Bech32 address in generate-only mode")
return sdk.AccAddress(from), "", 0, errors.Wrap(err, "must provide a valid Bech32 address in generate-only mode")
}

return addr, "", 0, nil
return sdk.AccAddress(from), "", 0, nil
}

var info keyring.Info
if addr, err := sdk.AccAddressFromBech32(from); err == nil {
info, err = kr.KeyByAddress(addr)
if err := sdk.ValidateAccAddress(from); err == nil {
info, err = kr.KeyByAddress(sdk.AccAddress(from))
if err != nil {
return nil, "", 0, err
return sdk.AccAddress(from), "", 0, err
}
} else {
info, err = kr.Key(from)
if err != nil {
return nil, "", 0, err
return "", "", 0, err
}
}

Expand Down
20 changes: 10 additions & 10 deletions client/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func PubkeyCmd() *cobra.Command {

Example:
$ %s debug pubkey TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
$ %s debug pubkey cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
$ %s debug pubkey linkpub1cqmsrdepq28g8wmpa2hmq06y770jcjr48ntw932wc8l0cl7fz5xr4sahy3s3v0r3mez
`, version.AppName, version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -127,23 +127,23 @@ func AddrCmd() *cobra.Command {
Long: fmt.Sprintf(`Convert an address between hex encoding and bech32.

Example:
$ %s debug addr cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
$ %s debug addr link19wgf6ymq2ur6r59st95e04e49m69z4al4fc982
`, version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {

addrString := args[0]
var addr []byte
var addrBytes []byte

// try hex, then bech32
var err error
addr, err = hex.DecodeString(addrString)
addrBytes, err = hex.DecodeString(addrString)
if err != nil {
var err2 error
addr, err2 = sdk.AccAddressFromBech32(addrString)
addrBytes, err2 = sdk.AccAddressToBytes(addrString)
if err2 != nil {
var err3 error
addr, err3 = sdk.ValAddressFromBech32(addrString)
addrBytes, err3 = sdk.ValAddressToBytes(addrString)

if err3 != nil {
return fmt.Errorf("expected hex or bech32. Got errors: hex: %v, bech32 acc: %v, bech32 val: %v", err, err2, err3)
Expand All @@ -152,11 +152,11 @@ $ %s debug addr cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
}
}

accAddr := sdk.AccAddress(addr)
valAddr := sdk.ValAddress(addr)
accAddr := sdk.BytesToAccAddress(addrBytes)
valAddr := sdk.BytesToValAddress(addrBytes)

cmd.Println("Address:", addr)
cmd.Printf("Address (hex): %X\n", addr)
cmd.Println("Address Bytes:", addrBytes)
cmd.Printf("Address (hex): %X\n", addrBytes)
cmd.Printf("Bech32 Acc: %s\n", accAddr)
cmd.Printf("Bech32 Val: %s\n", valAddr)
return nil
Expand Down
4 changes: 2 additions & 2 deletions client/keys/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
func fetchKey(kb keyring.Keyring, keyref string) (keyring.Info, error) {
info, err := kb.Key(keyref)
if err != nil {
accAddr, err := sdk.AccAddressFromBech32(keyref)
err := sdk.ValidateAccAddress(keyref)
if err != nil {
return info, err
}

info, err = kb.KeyByAddress(accAddr)
info, err = kb.KeyByAddress(sdk.AccAddress(keyref))
if err != nil {
return info, errors.New("key not found")
}
Expand Down
4 changes: 3 additions & 1 deletion client/keys/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ func Test_multiSigKey_Properties(t *testing.T) {
require.Equal(t, "myMultisig", tmp.GetName())
require.Equal(t, keyring.TypeMulti, tmp.GetType())
require.Equal(t, "BDF0C827D34CA39919C7688EB5A95383C60B3471", tmp.GetPubKey().Address().String())
require.Equal(t, "link1hhcvsf7nfj3ejxw8dz8tt22ns0rqkdr3rrh7xy", sdk.MustBech32ifyAddressBytes("link", tmp.GetAddress()))
acc := tmp.GetAddress()
addrBytes, _ := sdk.AccAddressToBytes(acc.String())
require.Equal(t, "link1hhcvsf7nfj3ejxw8dz8tt22ns0rqkdr3rrh7xy", sdk.MustBech32ifyAddressBytes("link", addrBytes))
whylee259 marked this conversation as resolved.
Show resolved Hide resolved
}

func Test_showKeysCmd(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion client/rpc/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func validatorOutput(validator *osttypes.Validator) (ValidatorOutput, error) {
}

return ValidatorOutput{
Address: sdk.ConsAddress(validator.Address),
Address: sdk.BytesToConsAddress(validator.Address),
PubKey: pk,
ProposerPriority: validator.ProposerPriority,
VotingPower: validator.VotingPower,
Expand Down
4 changes: 3 additions & 1 deletion client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ func PrepareFactory(clientCtx client.Context, txf Factory) (Factory, error) {
if err != nil {
return txf, err
}
sigBlockHeight = height
// `ctx.Height` of checkTx may be later by 1 block than consensus block height.
// Some cli integrated test fails because of this(sigBlockHeight = height).
sigBlockHeight = height - 1
}
}

Expand Down
8 changes: 4 additions & 4 deletions crypto/keyring/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (i localInfo) GetPubKey() cryptotypes.PubKey {

// GetType implements Info interface
func (i localInfo) GetAddress() types.AccAddress {
return i.PubKey.Address().Bytes()
return types.BytesToAccAddress(i.PubKey.Address())
}

// GetType implements Info interface
Expand Down Expand Up @@ -117,7 +117,7 @@ func (i ledgerInfo) GetPubKey() cryptotypes.PubKey {

// GetAddress implements Info interface
func (i ledgerInfo) GetAddress() types.AccAddress {
return i.PubKey.Address().Bytes()
return types.BytesToAccAddress(i.PubKey.Address())
}

// GetPath implements Info interface
Expand Down Expand Up @@ -169,7 +169,7 @@ func (i offlineInfo) GetAlgo() hd.PubKeyType {

// GetAddress implements Info interface
func (i offlineInfo) GetAddress() types.AccAddress {
return i.PubKey.Address().Bytes()
return types.BytesToAccAddress(i.PubKey.Address())
}

// GetPath implements Info interface
Expand Down Expand Up @@ -225,7 +225,7 @@ func (i multiInfo) GetPubKey() cryptotypes.PubKey {

// GetAddress implements Info interface
func (i multiInfo) GetAddress() types.AccAddress {
return i.PubKey.Address().Bytes()
return types.BytesToAccAddress(i.PubKey.Address())
}

// GetPath implements Info interface
Expand Down
6 changes: 4 additions & 2 deletions crypto/keyring/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func TestKeyManagementKeyRing(t *testing.T) {
require.NotNil(t, err)
_, err = kb.KeyByAddress(accAddr(i2))
require.NoError(t, err)
addr, err := sdk.AccAddressFromBech32("link1yxfu3fldlgux939t0gwaqs82l4x77v2kasa7jf")
addr := sdk.AccAddress("link1yxfu3fldlgux939t0gwaqs82l4x77v2kasa7jf")
err = sdk.ValidateAccAddress(addr.String())
require.NoError(t, err)
_, err = kb.KeyByAddress(addr)
require.NotNil(t, err)
Expand Down Expand Up @@ -432,7 +433,8 @@ func TestInMemoryKeyManagement(t *testing.T) {
require.NotNil(t, err)
_, err = cstore.KeyByAddress(accAddr(i2))
require.NoError(t, err)
addr, err := sdk.AccAddressFromBech32("link1yxfu3fldlgux939t0gwaqs82l4x77v2kasa7jf")
addr := sdk.AccAddress("link1yxfu3fldlgux939t0gwaqs82l4x77v2kasa7jf")
err = sdk.ValidateAccAddress(addr.String())
require.NoError(t, err)
_, err = cstore.KeyByAddress(addr)
require.NotNil(t, err)
Expand Down
8 changes: 4 additions & 4 deletions crypto/keyring/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Bech32KeysOutput(infos []Info) ([]KeyOutput, error) {

// Bech32ConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes.
func Bech32ConsKeyOutput(keyInfo Info) (KeyOutput, error) {
consAddr := sdk.ConsAddress(keyInfo.GetPubKey().Address().Bytes())
consAddr := sdk.BytesToConsAddress(keyInfo.GetPubKey().Address())

bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, keyInfo.GetPubKey())
if err != nil {
Expand All @@ -62,7 +62,7 @@ func Bech32ConsKeyOutput(keyInfo Info) (KeyOutput, error) {

// Bech32ValKeyOutput create a KeyOutput in with "val" Bech32 prefixes.
func Bech32ValKeyOutput(keyInfo Info) (KeyOutput, error) {
valAddr := sdk.ValAddress(keyInfo.GetPubKey().Address().Bytes())
valAddr := sdk.BytesToValAddress(keyInfo.GetPubKey().Address().Bytes())
whylee259 marked this conversation as resolved.
Show resolved Hide resolved

bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeValPub, keyInfo.GetPubKey())
if err != nil {
Expand All @@ -76,7 +76,7 @@ func Bech32ValKeyOutput(keyInfo Info) (KeyOutput, error) {
// public key is a multisig public key, then the threshold and constituent
// public keys will be added.
func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) {
accAddr := sdk.AccAddress(keyInfo.GetPubKey().Address().Bytes())
accAddr := sdk.BytesToAccAddress(keyInfo.GetPubKey().Address())
bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, keyInfo.GetPubKey())
if err != nil {
return KeyOutput{}, err
Expand All @@ -88,7 +88,7 @@ func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) {
pubKeys := make([]multisigPubKeyOutput, len(mInfo.PubKeys))

for i, pk := range mInfo.PubKeys {
accAddr := sdk.AccAddress(pk.PubKey.Address().Bytes())
accAddr := sdk.BytesToAccAddress(pk.PubKey.Address())

bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk.PubKey)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions crypto/keyring/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
func TestBech32KeysOutput(t *testing.T) {
tmpKey := secp256k1.GenPrivKey().PubKey()
bechTmpKey := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, tmpKey)
tmpAddr := sdk.AccAddress(tmpKey.Address().Bytes())
tmpAddr := sdk.BytesToAccAddress(tmpKey.Address())

multisigPks := kmultisig.NewLegacyAminoPubKey(1, []types.PubKey{tmpKey})
multiInfo := NewMultiInfo("multisig", multisigPks)
accAddr := sdk.AccAddress(multiInfo.GetPubKey().Address().Bytes())
accAddr := sdk.BytesToAccAddress(multiInfo.GetPubKey().Address())
bechPubKey := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, multiInfo.GetPubKey())

expectedOutput := NewKeyOutput(multiInfo.GetName(), multiInfo.GetType().String(), accAddr.String(), bechPubKey)
Expand Down
2 changes: 1 addition & 1 deletion crypto/ledger/ledger_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (mock LedgerSECP256K1Mock) GetAddressPubKeySECP256K1(derivationPath []uint3

// Generate the bech32 addr using existing ostcrypto/etc.
pub := &csecp256k1.PubKey{Key: compressedPublicKey}
addr := sdk.AccAddress(pub.Address()).String()
addr := sdk.BytesToAccAddress(pub.Address()).String()
return pk, addr, err
}

Expand Down
6 changes: 3 additions & 3 deletions crypto/ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestPublicKeyUnsafe(t *testing.T) {
require.Equal(t, "linkpub1cqmsrdepq27djm9tzq3sftqsayx95refxk8r5jn0kyshhql9mdjhjx829zlvzygzwr2",
pubKeyAddr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic)

addr := sdk.AccAddress(priv.PubKey().Address()).String()
addr := sdk.BytesToAccAddress(priv.PubKey().Address()).String()
require.Equal(t, "link1tdl7n2acgmec0y5nng0q2fahl9khyct3cgsktn",
addr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic)
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestPublicKeySafe(t *testing.T) {
require.Equal(t, "link1tdl7n2acgmec0y5nng0q2fahl9khyct3cgsktn",
addr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic)

addr2 := sdk.AccAddress(priv.PubKey().Address()).String()
addr2 := sdk.BytesToAccAddress(priv.PubKey().Address()).String()
require.Equal(t, addr, addr2)
}

Expand Down Expand Up @@ -162,7 +162,7 @@ func TestPublicKeyHDPath(t *testing.T) {
require.NotNil(t, addr)
require.NotNil(t, priv)

addr2 := sdk.AccAddress(priv.PubKey().Address()).String()
addr2 := sdk.BytesToAccAddress(priv.PubKey().Address()).String()
require.Equal(t, addr2, addr)
require.Equal(t,
expectedAddrs[i], addr,
Expand Down
2 changes: 1 addition & 1 deletion docs/core/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -9525,7 +9525,7 @@ TxBody is the body of a transaction that all signers sign over.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `messages` | [google.protobuf.Any](#google.protobuf.Any) | repeated | messages is a list of messages to be executed. The required signers of those messages define the number and order of elements in AuthInfo's signer_infos and Tx's signatures. Each required signer address is added to the list only the first time it occurs. By convention, the first required signer (usually from the first message) is referred to as the primary signer and pays the fee for the whole transaction. |
| `sig_block_height` | [uint64](#uint64) | | sig block height is available between current block height and current block height - `VALID_SIG_BLOCK_DURATION` this is used for distinguish signatures instead of account number. this is mandatory. |
| `sig_block_height` | [uint64](#uint64) | | sig block height is available between current block height and current block height - `VALID_SIG_BLOCK_PERIOD` this is used for distinguish signatures instead of account number. this is mandatory. |
| `memo` | [string](#string) | | memo is any arbitrary memo to be added to the transaction |
| `timeout_height` | [uint64](#uint64) | | timeout is the block height after which this transaction will not be processed by the chain |
| `extension_options` | [google.protobuf.Any](#google.protobuf.Any) | repeated | extension_options are arbitrary options that can be added by chains when the default options are not sufficient. If any of these are present and can't be handled, the transaction will be rejected |
Expand Down
4 changes: 2 additions & 2 deletions server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func GenerateCoinKey(algo keyring.SignatureAlgo) (sdk.AccAddress, string, error)
if err != nil {
return sdk.AccAddress([]byte{}), "", err
}
return sdk.AccAddress(info.GetPubKey().Address()), secret, nil
return sdk.BytesToAccAddress(info.GetPubKey().Address()), secret, nil
}

// GenerateSaveCoinKey returns the address of a public key, along with the secret
Expand Down Expand Up @@ -48,5 +48,5 @@ func GenerateSaveCoinKey(keybase keyring.Keyring, keyName string, overwrite bool
return sdk.AccAddress([]byte{}), "", err
}

return sdk.AccAddress(info.GetPubKey().Address()), secret, nil
return sdk.BytesToAccAddress(info.GetPubKey().Address()), secret, nil
}
2 changes: 1 addition & 1 deletion server/tm_cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func ShowAddressCmd() *cobra.Command {
cfg := serverCtx.Config

privValidator := pvm.LoadFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
valConsAddr := (sdk.ConsAddress)(privValidator.GetAddress())
valConsAddr := sdk.BytesToConsAddress(privValidator.GetAddress())

output, _ := cmd.Flags().GetString(cli.OutputFlag)
if strings.ToLower(output) == "json" {
Expand Down
22 changes: 5 additions & 17 deletions simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []
allowedAddrsMap := make(map[string]bool)

for _, addr := range jailAllowedAddrs {
_, err := sdk.ValAddressFromBech32(addr)
err := sdk.ValidateValAddress(addr)
if err != nil {
log.Fatal(err)
}
Expand All @@ -79,15 +79,9 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []
// withdraw all delegator rewards
dels := app.StakingKeeper.GetAllDelegations(ctx)
for _, delegation := range dels {
valAddr, err := sdk.ValAddressFromBech32(delegation.ValidatorAddress)
if err != nil {
panic(err)
}
valAddr := sdk.ValAddress(delegation.ValidatorAddress)

delAddr, err := sdk.AccAddressFromBech32(delegation.DelegatorAddress)
if err != nil {
panic(err)
}
delAddr := sdk.AccAddress(delegation.DelegatorAddress)
_, _ = app.DistrKeeper.WithdrawDelegationRewards(ctx, delAddr, valAddr)
}

Expand Down Expand Up @@ -115,14 +109,8 @@ func (app *SimApp) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []

// reinitialize all delegations
for _, del := range dels {
valAddr, err := sdk.ValAddressFromBech32(del.ValidatorAddress)
if err != nil {
panic(err)
}
delAddr, err := sdk.AccAddressFromBech32(del.DelegatorAddress)
if err != nil {
panic(err)
}
valAddr := sdk.ValAddress(del.ValidatorAddress)
delAddr := sdk.AccAddress(del.DelegatorAddress)
app.DistrKeeper.Hooks().BeforeDelegationCreated(ctx, delAddr, valAddr)
app.DistrKeeper.Hooks().AfterDelegationModified(ctx, delAddr, valAddr)
}
Expand Down
Loading