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: apply types/address.go from [email protected] #603

Merged
merged 16 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Breaking Changes
* (proto) [\#564](https://github.com/line/lbm-sdk/pull/564) change gRPC path to original cosmos path
* (global) [\#603](https://github.com/line/lbm-sdk/pull/603) apply types/address.go from [email protected]

### Build, CI

Expand Down
2 changes: 1 addition & 1 deletion baseapp/accountwgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,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.BytesToAccAddress(priv.PubKey().Address()))
addrs = append(addrs, sdk.AccAddress(priv.PubKey().Address()))
}
return addrs
}
Expand Down
2 changes: 1 addition & 1 deletion baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestBaseApp_BlockGas(t *testing.T) {
require.Equal(t, []byte("ok"), okValue)
}
// check block gas is always consumed
baseGas := uint64(40043) // baseGas is the gas consumed before tx msg
baseGas := uint64(37352) // baseGas is the gas consumed before tx msg
expGasConsumed := addUint64Saturating(tc.gasToConsume, baseGas)
if expGasConsumed > txtypes.MaxGasWanted {
// capped by gasLimit
Expand Down
7 changes: 4 additions & 3 deletions client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,16 @@ func readTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err
clientCtx = clientCtx.WithSignModeStr(signModeStr)
}

if clientCtx.FeeGranter == "" || flagSet.Changed(flags.FlagFeeAccount) {
if clientCtx.FeeGranter == nil || flagSet.Changed(flags.FlagFeeAccount) {
granter, _ := flagSet.GetString(flags.FlagFeeAccount)

if granter != "" {
err := sdk.ValidateAccAddress(granter)
granterAcc, err := sdk.AccAddressFromBech32(granter)
if err != nil {
return clientCtx, err
}
clientCtx = clientCtx.WithFeeGranterAddress(sdk.AccAddress(granter))

clientCtx = clientCtx.WithFeeGranterAddress(granterAcc)
}
}

Expand Down
16 changes: 8 additions & 8 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,28 +335,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 "", "", 0, nil
return nil, "", 0, nil
}

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

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

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

Expand Down
19 changes: 8 additions & 11 deletions client/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ $ %s debug addr link19wgf6ymq2ur6r59st95e04e49m69z4al4fc982
RunE: func(cmd *cobra.Command, args []string) error {

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

// try hex, then bech32
var err error
addrBytes, err = hex.DecodeString(addrString)
addr, err = hex.DecodeString(addrString)
if err != nil {
var err2 error
addrBytes, err2 = sdk.AccAddressToBytes(addrString)
addr, err2 = sdk.AccAddressFromBech32(addrString)
if err2 != nil {
var err3 error
addrBytes, err3 = sdk.ValAddressToBytes(addrString)
addr, err3 = sdk.ValAddressFromBech32(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 @@ -91,13 +91,10 @@ $ %s debug addr link19wgf6ymq2ur6r59st95e04e49m69z4al4fc982
}
}

accAddr := sdk.BytesToAccAddress(addrBytes)
valAddr := sdk.BytesToValAddress(addrBytes)

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)
cmd.Println("Address:", addr)
cmd.Printf("Address (hex): %X\n", addr)
cmd.Printf("Bech32 Acc: %s\n", sdk.AccAddress(addr))
cmd.Printf("Bech32 Val: %s\n", sdk.ValAddress(addr))
return nil
},
}
Expand Down
3 changes: 1 addition & 2 deletions client/keys/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,10 @@ func fetchKey(kb keyring.Keyring, keyref string) (keyring.Info, error) {
if err == nil || !sdkerr.IsOf(err, sdkerr.ErrIO, sdkerr.ErrKeyNotFound) {
return info, err
}
err = sdk.ValidateAccAddress(keyref)
accAddr, err := sdk.AccAddressFromBech32(keyref)
if err != nil {
return info, err
}
accAddr := sdk.AccAddress(keyref)

info, err = kb.KeyByAddress(accAddr)
return info, sdkerr.Wrap(err, "Invalid key")
Expand Down
4 changes: 1 addition & 3 deletions client/keys/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ 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())
acc := tmp.GetAddress()
addrBytes, _ := sdk.AccAddressToBytes(acc.String())
require.Equal(t, "link1hhcvsf7nfj3ejxw8dz8tt22ns0rqkdr3rrh7xy", sdk.MustBech32ifyAddressBytes("link", addrBytes))
require.Equal(t, "link1hhcvsf7nfj3ejxw8dz8tt22ns0rqkdr3rrh7xy", sdk.MustBech32ifyAddressBytes("link", tmp.GetAddress()))
}

func Test_showKeysCmd(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion client/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *IntegrationTestSuite) TestQueryABCIHeight() {
req := abci.RequestQuery{
Path: fmt.Sprintf("store/%s/key", banktypes.StoreKey),
Height: tc.reqHeight,
Data: banktypes.CreateAccountBalancesPrefix(val.Address.Bytes()),
Data: banktypes.CreateAccountBalancesPrefix(val.Address),
Prove: true,
}

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

return ValidatorOutput{
Address: sdk.BytesToConsAddress(validator.Address),
Address: sdk.ConsAddress(validator.Address),
PubKey: pk,
ProposerPriority: validator.ProposerPriority,
VotingPower: validator.VotingPower,
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 types.BytesToAccAddress(i.PubKey.Address())
return i.PubKey.Address().Bytes()
}

// 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 types.BytesToAccAddress(i.PubKey.Address())
return i.PubKey.Address().Bytes()
}

// 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 types.BytesToAccAddress(i.PubKey.Address())
return i.PubKey.Address().Bytes()
}

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

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

// GetPath implements Info interface
Expand Down
2 changes: 1 addition & 1 deletion crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func (ks keystore) NewAccount(name string, mnemonic string, bip39Passphrase stri

// check if the a key already exists with the same address and return an error
// if found
address := sdk.BytesToAccAddress(privKey.PubKey().Address())
address := sdk.AccAddress(privKey.PubKey().Address())
if _, err := ks.KeyByAddress(address); err == nil {
return nil, fmt.Errorf("account with address %s already exists in keyring, delete the key first if you want to recreate it", address)
}
Expand Down
6 changes: 2 additions & 4 deletions crypto/keyring/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ func TestKeyManagementKeyRing(t *testing.T) {
require.NotNil(t, err)
_, err = kb.KeyByAddress(accAddr(i2))
require.NoError(t, err)
addr := sdk.AccAddress("link1yxfu3fldlgux939t0gwaqs82l4x77v2kasa7jf")
err = sdk.ValidateAccAddress(addr.String())
addr, err := sdk.AccAddressFromBech32("link1yxfu3fldlgux939t0gwaqs82l4x77v2kasa7jf")
require.NoError(t, err)
_, err = kb.KeyByAddress(addr)
require.NotNil(t, err)
Expand Down Expand Up @@ -433,8 +432,7 @@ func TestInMemoryKeyManagement(t *testing.T) {
require.NotNil(t, err)
_, err = cstore.KeyByAddress(accAddr(i2))
require.NoError(t, err)
addr := sdk.AccAddress("link1yxfu3fldlgux939t0gwaqs82l4x77v2kasa7jf")
err = sdk.ValidateAccAddress(addr.String())
addr, err := sdk.AccAddressFromBech32("link1yxfu3fldlgux939t0gwaqs82l4x77v2kasa7jf")
require.NoError(t, err)
_, err = cstore.KeyByAddress(addr)
require.NotNil(t, err)
Expand Down
6 changes: 3 additions & 3 deletions crypto/keyring/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.Pu
// MkConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes.
func MkConsKeyOutput(keyInfo Info) (KeyOutput, error) {
pk := keyInfo.GetPubKey()
addr := sdk.BytesToConsAddress(pk.Address())
addr := sdk.ConsAddress(pk.Address())
return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk)
}

// MkValKeyOutput create a KeyOutput in with "val" Bech32 prefixes.
func MkValKeyOutput(keyInfo Info) (KeyOutput, error) {
pk := keyInfo.GetPubKey()
addr := sdk.BytesToValAddress(pk.Address())
addr := sdk.ValAddress(pk.Address())
return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk)
}

Expand All @@ -57,7 +57,7 @@ func MkValKeyOutput(keyInfo Info) (KeyOutput, error) {
// public keys will be added.
func MkAccKeyOutput(keyInfo Info) (KeyOutput, error) {
pk := keyInfo.GetPubKey()
addr := sdk.BytesToAccAddress(pk.Address())
addr := sdk.AccAddress(pk.Address())
return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk)
}

Expand Down
2 changes: 1 addition & 1 deletion crypto/keyring/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestBech32KeysOutput(t *testing.T) {

info, err := NewMultiInfo("multisig", multisigPk)
require.NoError(t, err)
accAddr := sdk.BytesToAccAddress(info.GetPubKey().Address())
accAddr := sdk.AccAddress(info.GetPubKey().Address())
expectedOutput, err := NewKeyOutput(info.GetName(), info.GetType(), accAddr, multisigPk)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion crypto/keys/multisig/multisig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,6 @@ func TestProtoMarshalJSON(t *testing.T) {
require.NoError(err)
ko, err := keyring.MkAccKeyOutput(info)
require.NoError(err)
require.Equal(ko.Address, sdk.BytesToAccAddress(pk2.Address()).String())
require.Equal(ko.Address, sdk.AccAddress(pk2.Address()).String())
require.Equal(ko.PubKey, string(bz))
}
4 changes: 2 additions & 2 deletions crypto/ledger/ledger_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/cosmos/go-bip39"
secp256k1 "github.com/tendermint/btcd/btcec"

"github.com/line/ostracon/crypto"

"github.com/line/lbm-sdk/crypto/hd"
Expand Down Expand Up @@ -83,7 +83,7 @@ func (mock LedgerSECP256K1Mock) GetAddressPubKeySECP256K1(derivationPath []uint3

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

Expand Down
8 changes: 4 additions & 4 deletions crypto/ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func checkDefaultPubKey(t *testing.T, priv types.LedgerPrivKey) {
fmt.Sprintf("%x", cdc.Amino.MustMarshalBinaryBare(priv.PubKey())),
"Is your device using test mnemonic: %s ?", testdata.TestMnemonic)
require.Equal(t, expectedPkStr, priv.PubKey().String())
addr := sdk.BytesToAccAddress(priv.PubKey().Address())
addr := sdk.AccAddress(priv.PubKey().Address()).String()
require.Equal(t, "link1tdl7n2acgmec0y5nng0q2fahl9khyct3cgsktn",
addr.String(), "Is your device using test mnemonic: %s ?", testdata.TestMnemonic)
addr, "Is your device using test mnemonic: %s ?", testdata.TestMnemonic)
}

func TestPublicKeyUnsafeHDPath(t *testing.T) {
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestPublicKeySafe(t *testing.T) {
require.Nil(t, ShowAddress(path, priv.PubKey(), sdk.GetConfig().GetBech32AccountAddrPrefix()))
checkDefaultPubKey(t, priv)

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

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

addr2 := sdk.BytesToAccAddress(priv.PubKey().Address()).String()
addr2 := sdk.AccAddress(priv.PubKey().Address()).String()
require.Equal(t, addr2, addr)
require.Equal(t,
expectedAddrs[i], addr,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d
github.com/hdevalence/ed25519consensus v0.0.0-20220222234857-c00d1f31bab3
github.com/improbable-eng/grpc-web v0.15.0
github.com/jhump/protoreflect v1.10.3
Expand Down
1 change: 0 additions & 1 deletion server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ func DefaultConfig() *Config {
InterBlockCache: true,
InterBlockCacheSize: cache.DefaultCommitKVStoreCacheSize,
IAVLCacheSize: iavl.DefaultIAVLCacheSize,
Bech32CacheSize: sdk.DefaultBech32CacheSize,
Pruning: storetypes.PruningOptionDefault,
PruningKeepRecent: "0",
PruningKeepEvery: "0",
Expand Down
13 changes: 7 additions & 6 deletions server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ func GenerateCoinKey(algo keyring.SignatureAlgo) (sdk.AccAddress, string, error)
algo,
)
if err != nil {
return sdk.AccAddress(""), "", err
return sdk.AccAddress{}, "", err
}
return sdk.BytesToAccAddress(info.GetPubKey().Address()), secret, nil

return sdk.AccAddress(info.GetPubKey().Address()), secret, nil
}

// Deprecated: GenerateSaveCoinKey generates a new key mnemonic with its addrress.
Expand All @@ -43,20 +44,20 @@ func GenerateSaveCoinKey(

// ensure no overwrite
if !overwrite && exists {
return sdk.AccAddress(""), "", fmt.Errorf("key already exists, overwrite is disabled")
return sdk.AccAddress{}, "", fmt.Errorf("key already exists, overwrite is disabled")
}

// remove the old key by name if it exists
if exists {
if err := keybase.Delete(keyName); err != nil {
return sdk.AccAddress(""), "", fmt.Errorf("failed to overwrite key")
return sdk.AccAddress{}, "", fmt.Errorf("failed to overwrite key")
}
}

k, mnemonic, err := keybase.NewMnemonic(keyName, keyring.English, sdk.GetConfig().GetFullBIP44Path(), keyring.DefaultBIP39Passphrase, algo)
if err != nil {
return sdk.AccAddress(""), "", err
return sdk.AccAddress{}, "", err
}

return sdk.BytesToAccAddress(k.GetPubKey().Address()), mnemonic, nil
return k.GetAddress(), mnemonic, nil
}
Loading