Skip to content

Commit 4952956

Browse files
author
Woosang Son
authored
fix: treat addresses as strings in sdk internals (#298)
* fix: treat addresses as strings * fix: compile errors, test failures * fix: lint errors * fix: fix test failures, add message validation * docs: modify changelog * fix: test failure * chore: empty * fix: apply comment * fix: apply comment; use Empty() function
1 parent 7214594 commit 4952956

File tree

289 files changed

+1947
-2412
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

289 files changed

+1947
-2412
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
* (proto) [\#106](https://github.com/line/lfb-sdk/pull/106) Rename package of proto files
6767
* (api) [\#130](https://github.com/line/lfb-sdk/pull/130) Rename rest apis
6868
* (auth) [\#265](https://github.com/line/lfb-sdk/pull/265) Introduce sig block height for the new replay protection
69+
* (global) [\#298](https://github.com/line/lfb-sdk/pull/298) Treat addresses as strings
6970

7071
### Build, CI
7172
* (ci) [\#234](https://github.com/line/lfb-sdk/pull/234) Fix branch name in ci script

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ proto-all: proto-format proto-lint proto-gen
394394

395395
proto-gen:
396396
@echo "Generating Protobuf files"
397-
$(DOCKER) run --rm -e -v $(CURDIR):/workspace --workdir /workspace tendermintdev/sdk-proto-gen sh ./scripts/protocgen.sh
397+
$(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace tendermintdev/sdk-proto-gen sh ./scripts/protocgen.sh
398398

399399
proto-format:
400400
@echo "Formatting Protobuf files"

baseapp/accountwgs_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func newTestPrivKeys(num int) []*secp256k1.PrivKey {
9292
func getAddrs(privs []*secp256k1.PrivKey) []sdk.AccAddress {
9393
addrs := make([]sdk.AccAddress, 0, len(privs))
9494
for _, priv := range privs {
95-
addrs = append(addrs, sdk.AccAddress(priv.PubKey().Address()))
95+
addrs = append(addrs, sdk.BytesToAccAddress(priv.PubKey().Address()))
9696
}
9797
return addrs
9898
}

baseapp/msg_service_router_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ func TestMsgService(t *testing.T) {
103103

104104
// Second round: all signer infos are set, so each signer can sign.
105105
signerData := authsigning.SignerData{
106-
ChainID: "test",
107-
Sequence: 0,
106+
ChainID: "test",
107+
Sequence: 0,
108108
}
109109
sigV2, err = tx.SignWithPrivKey(
110110
encCfg.TxConfig.SignModeHandler().DefaultMode(), signerData,

client/context.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -287,28 +287,28 @@ func (ctx Context) printOutput(out []byte) error {
287287
// address is returned.
288288
func GetFromFields(kr keyring.Keyring, from string, genOnly bool) (sdk.AccAddress, string, keyring.KeyType, error) {
289289
if from == "" {
290-
return nil, "", 0, nil
290+
return "", "", 0, nil
291291
}
292292

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

299-
return addr, "", 0, nil
299+
return sdk.AccAddress(from), "", 0, nil
300300
}
301301

302302
var info keyring.Info
303-
if addr, err := sdk.AccAddressFromBech32(from); err == nil {
304-
info, err = kr.KeyByAddress(addr)
303+
if err := sdk.ValidateAccAddress(from); err == nil {
304+
info, err = kr.KeyByAddress(sdk.AccAddress(from))
305305
if err != nil {
306-
return nil, "", 0, err
306+
return sdk.AccAddress(from), "", 0, err
307307
}
308308
} else {
309309
info, err = kr.Key(from)
310310
if err != nil {
311-
return nil, "", 0, err
311+
return "", "", 0, err
312312
}
313313
}
314314

client/debug/main.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func PubkeyCmd() *cobra.Command {
7575
7676
Example:
7777
$ %s debug pubkey TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
78-
$ %s debug pubkey cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
78+
$ %s debug pubkey linkpub1cqmsrdepq28g8wmpa2hmq06y770jcjr48ntw932wc8l0cl7fz5xr4sahy3s3v0r3mez
7979
`, version.AppName, version.AppName),
8080
Args: cobra.ExactArgs(1),
8181
RunE: func(cmd *cobra.Command, args []string) error {
@@ -127,23 +127,23 @@ func AddrCmd() *cobra.Command {
127127
Long: fmt.Sprintf(`Convert an address between hex encoding and bech32.
128128
129129
Example:
130-
$ %s debug addr cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
130+
$ %s debug addr link19wgf6ymq2ur6r59st95e04e49m69z4al4fc982
131131
`, version.AppName),
132132
Args: cobra.ExactArgs(1),
133133
RunE: func(cmd *cobra.Command, args []string) error {
134134

135135
addrString := args[0]
136-
var addr []byte
136+
var addrBytes []byte
137137

138138
// try hex, then bech32
139139
var err error
140-
addr, err = hex.DecodeString(addrString)
140+
addrBytes, err = hex.DecodeString(addrString)
141141
if err != nil {
142142
var err2 error
143-
addr, err2 = sdk.AccAddressFromBech32(addrString)
143+
addrBytes, err2 = sdk.AccAddressToBytes(addrString)
144144
if err2 != nil {
145145
var err3 error
146-
addr, err3 = sdk.ValAddressFromBech32(addrString)
146+
addrBytes, err3 = sdk.ValAddressToBytes(addrString)
147147

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

155-
accAddr := sdk.AccAddress(addr)
156-
valAddr := sdk.ValAddress(addr)
155+
accAddr := sdk.BytesToAccAddress(addrBytes)
156+
valAddr := sdk.BytesToValAddress(addrBytes)
157157

158-
cmd.Println("Address:", addr)
159-
cmd.Printf("Address (hex): %X\n", addr)
158+
cmd.Println("Address Bytes:", addrBytes)
159+
cmd.Printf("Address (hex): %X\n", addrBytes)
160160
cmd.Printf("Bech32 Acc: %s\n", accAddr)
161161
cmd.Printf("Bech32 Val: %s\n", valAddr)
162162
return nil

client/keys/show.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,12 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
146146
func fetchKey(kb keyring.Keyring, keyref string) (keyring.Info, error) {
147147
info, err := kb.Key(keyref)
148148
if err != nil {
149-
accAddr, err := sdk.AccAddressFromBech32(keyref)
149+
err := sdk.ValidateAccAddress(keyref)
150150
if err != nil {
151151
return info, err
152152
}
153153

154-
info, err = kb.KeyByAddress(accAddr)
154+
info, err = kb.KeyByAddress(sdk.AccAddress(keyref))
155155
if err != nil {
156156
return info, errors.New("key not found")
157157
}

client/keys/show_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ func Test_multiSigKey_Properties(t *testing.T) {
2929
require.Equal(t, "myMultisig", tmp.GetName())
3030
require.Equal(t, keyring.TypeMulti, tmp.GetType())
3131
require.Equal(t, "BDF0C827D34CA39919C7688EB5A95383C60B3471", tmp.GetPubKey().Address().String())
32-
require.Equal(t, "link1hhcvsf7nfj3ejxw8dz8tt22ns0rqkdr3rrh7xy", sdk.MustBech32ifyAddressBytes("link", tmp.GetAddress()))
32+
acc := tmp.GetAddress()
33+
addrBytes, _ := sdk.AccAddressToBytes(acc.String())
34+
require.Equal(t, "link1hhcvsf7nfj3ejxw8dz8tt22ns0rqkdr3rrh7xy", sdk.MustBech32ifyAddressBytes("link", addrBytes))
3335
}
3436

3537
func Test_showKeysCmd(t *testing.T) {

client/rpc/validators.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func validatorOutput(validator *osttypes.Validator) (ValidatorOutput, error) {
109109
}
110110

111111
return ValidatorOutput{
112-
Address: sdk.ConsAddress(validator.Address),
112+
Address: sdk.BytesToConsAddress(validator.Address),
113113
PubKey: pk,
114114
ProposerPriority: validator.ProposerPriority,
115115
VotingPower: validator.VotingPower,

client/tx/tx.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,9 @@ func PrepareFactory(clientCtx client.Context, txf Factory) (Factory, error) {
316316
if err != nil {
317317
return txf, err
318318
}
319-
sigBlockHeight = height
319+
// `ctx.Height` of checkTx may be later by 1 block than consensus block height.
320+
// Some cli integrated test fails because of this(sigBlockHeight = height).
321+
sigBlockHeight = height - 1
320322
}
321323
}
322324

crypto/keyring/info.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (i localInfo) GetPubKey() cryptotypes.PubKey {
6969

7070
// GetType implements Info interface
7171
func (i localInfo) GetAddress() types.AccAddress {
72-
return i.PubKey.Address().Bytes()
72+
return types.BytesToAccAddress(i.PubKey.Address())
7373
}
7474

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

118118
// GetAddress implements Info interface
119119
func (i ledgerInfo) GetAddress() types.AccAddress {
120-
return i.PubKey.Address().Bytes()
120+
return types.BytesToAccAddress(i.PubKey.Address())
121121
}
122122

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

170170
// GetAddress implements Info interface
171171
func (i offlineInfo) GetAddress() types.AccAddress {
172-
return i.PubKey.Address().Bytes()
172+
return types.BytesToAccAddress(i.PubKey.Address())
173173
}
174174

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

226226
// GetAddress implements Info interface
227227
func (i multiInfo) GetAddress() types.AccAddress {
228-
return i.PubKey.Address().Bytes()
228+
return types.BytesToAccAddress(i.PubKey.Address())
229229
}
230230

231231
// GetPath implements Info interface

crypto/keyring/keyring_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ func TestKeyManagementKeyRing(t *testing.T) {
7979
require.NotNil(t, err)
8080
_, err = kb.KeyByAddress(accAddr(i2))
8181
require.NoError(t, err)
82-
addr, err := sdk.AccAddressFromBech32("link1yxfu3fldlgux939t0gwaqs82l4x77v2kasa7jf")
82+
addr := sdk.AccAddress("link1yxfu3fldlgux939t0gwaqs82l4x77v2kasa7jf")
83+
err = sdk.ValidateAccAddress(addr.String())
8384
require.NoError(t, err)
8485
_, err = kb.KeyByAddress(addr)
8586
require.NotNil(t, err)
@@ -432,7 +433,8 @@ func TestInMemoryKeyManagement(t *testing.T) {
432433
require.NotNil(t, err)
433434
_, err = cstore.KeyByAddress(accAddr(i2))
434435
require.NoError(t, err)
435-
addr, err := sdk.AccAddressFromBech32("link1yxfu3fldlgux939t0gwaqs82l4x77v2kasa7jf")
436+
addr := sdk.AccAddress("link1yxfu3fldlgux939t0gwaqs82l4x77v2kasa7jf")
437+
err = sdk.ValidateAccAddress(addr.String())
436438
require.NoError(t, err)
437439
_, err = cstore.KeyByAddress(addr)
438440
require.NotNil(t, err)

crypto/keyring/output.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func Bech32KeysOutput(infos []Info) ([]KeyOutput, error) {
5050

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

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

6363
// Bech32ValKeyOutput create a KeyOutput in with "val" Bech32 prefixes.
6464
func Bech32ValKeyOutput(keyInfo Info) (KeyOutput, error) {
65-
valAddr := sdk.ValAddress(keyInfo.GetPubKey().Address().Bytes())
65+
valAddr := sdk.BytesToValAddress(keyInfo.GetPubKey().Address())
6666

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

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

9393
bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk.PubKey)
9494
if err != nil {

crypto/keyring/output_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414
func TestBech32KeysOutput(t *testing.T) {
1515
tmpKey := secp256k1.GenPrivKey().PubKey()
1616
bechTmpKey := sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, tmpKey)
17-
tmpAddr := sdk.AccAddress(tmpKey.Address().Bytes())
17+
tmpAddr := sdk.BytesToAccAddress(tmpKey.Address())
1818

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

2424
expectedOutput := NewKeyOutput(multiInfo.GetName(), multiInfo.GetType().String(), accAddr.String(), bechPubKey)

crypto/keys/multisig/multisig_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestAddress(t *testing.T) {
2020
pubKeys, _ := generatePubKeysAndSignatures(5, msg)
2121
multisigKey := kmultisig.NewLegacyAminoPubKey(2, pubKeys)
2222

23-
require.Len(t, multisigKey.Address().Bytes(), 20)
23+
require.Len(t, multisigKey.Address(), 20)
2424
}
2525

2626
func TestEquals(t *testing.T) {

crypto/ledger/ledger_mock.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (mock LedgerSECP256K1Mock) GetAddressPubKeySECP256K1(derivationPath []uint3
8282

8383
// Generate the bech32 addr using existing ostcrypto/etc.
8484
pub := &csecp256k1.PubKey{Key: compressedPublicKey}
85-
addr := sdk.AccAddress(pub.Address()).String()
85+
addr := sdk.BytesToAccAddress(pub.Address()).String()
8686
return pk, addr, err
8787
}
8888

crypto/ledger/ledger_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestPublicKeyUnsafe(t *testing.T) {
3636
require.Equal(t, "linkpub1cqmsrdepq27djm9tzq3sftqsayx95refxk8r5jn0kyshhql9mdjhjx829zlvzygzwr2",
3737
pubKeyAddr, "Is your device using test mnemonic: %s ?", testutil.TestMnemonic)
3838

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

120-
addr2 := sdk.AccAddress(priv.PubKey().Address()).String()
120+
addr2 := sdk.BytesToAccAddress(priv.PubKey().Address()).String()
121121
require.Equal(t, addr, addr2)
122122
}
123123

@@ -162,7 +162,7 @@ func TestPublicKeyHDPath(t *testing.T) {
162162
require.NotNil(t, addr)
163163
require.NotNil(t, priv)
164164

165-
addr2 := sdk.AccAddress(priv.PubKey().Address()).String()
165+
addr2 := sdk.BytesToAccAddress(priv.PubKey().Address()).String()
166166
require.Equal(t, addr2, addr)
167167
require.Equal(t,
168168
expectedAddrs[i], addr,

docs/core/proto-docs.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9525,7 +9525,7 @@ TxBody is the body of a transaction that all signers sign over.
95259525
| Field | Type | Label | Description |
95269526
| ----- | ---- | ----- | ----------- |
95279527
| `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. |
9528-
| `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. |
9528+
| `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. |
95299529
| `memo` | [string](#string) | | memo is any arbitrary memo to be added to the transaction |
95309530
| `timeout_height` | [uint64](#uint64) | | timeout is the block height after which this transaction will not be processed by the chain |
95319531
| `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 |

server/init.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func GenerateCoinKey(algo keyring.SignatureAlgo) (sdk.AccAddress, string, error)
1616
if err != nil {
1717
return sdk.AccAddress([]byte{}), "", err
1818
}
19-
return sdk.AccAddress(info.GetPubKey().Address()), secret, nil
19+
return sdk.BytesToAccAddress(info.GetPubKey().Address()), secret, nil
2020
}
2121

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

51-
return sdk.AccAddress(info.GetPubKey().Address()), secret, nil
51+
return sdk.BytesToAccAddress(info.GetPubKey().Address()), secret, nil
5252
}

server/tm_cmds.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func ShowAddressCmd() *cobra.Command {
8787
cfg := serverCtx.Config
8888

8989
privValidator := pvm.LoadFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
90-
valConsAddr := (sdk.ConsAddress)(privValidator.GetAddress())
90+
valConsAddr := sdk.BytesToConsAddress(privValidator.GetAddress())
9191

9292
output, _ := cmd.Flags().GetString(cli.OutputFlag)
9393
if strings.ToLower(output) == "json" {

0 commit comments

Comments
 (0)