Merge PR #4716: Fix ledger custom coin type support bug

This commit is contained in:
yys 2019-07-30 03:01:53 +09:00 committed by Alexander Bezobchuk
parent bd44492803
commit adf6ddd4a8
4 changed files with 59 additions and 2 deletions

View File

@ -0,0 +1 @@
Fix ledger custom coin type support bug

View File

@ -16,6 +16,60 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
) )
func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
config := sdk.GetConfig()
bech32PrefixAccAddr := "terra"
bech32PrefixAccPub := "terrapub"
bech32PrefixValAddr := "terravaloper"
bech32PrefixValPub := "terravaloperpub"
bech32PrefixConsAddr := "terravalcons"
bech32PrefixConsPub := "terravalconspub"
config.SetCoinType(330)
config.SetFullFundraiserPath("44'/330'/0'/0/0")
config.SetBech32PrefixForAccount(bech32PrefixAccAddr, bech32PrefixAccPub)
config.SetBech32PrefixForValidator(bech32PrefixValAddr, bech32PrefixValPub)
config.SetBech32PrefixForConsensusNode(bech32PrefixConsAddr, bech32PrefixConsPub)
cmd := addKeyCommand()
assert.NotNil(t, cmd)
// Prepare a keybase
kbHome, kbCleanUp := tests.NewTestCaseDir(t)
assert.NotNil(t, kbHome)
defer kbCleanUp()
viper.Set(flags.FlagHome, kbHome)
viper.Set(flags.FlagUseLedger, true)
/// Test Text
viper.Set(cli.OutputFlag, OutputFormatText)
// Now enter password
mockIn, _, _ := tests.ApplyMockIO(cmd)
mockIn.Reset("test1234\ntest1234\n")
assert.NoError(t, runAddCmd(cmd, []string{"keyname1"}))
// Now check that it has been stored properly
kb, err := NewKeyBaseFromHomeFlag()
assert.NoError(t, err)
assert.NotNil(t, kb)
key1, err := kb.Get("keyname1")
assert.NoError(t, err)
assert.NotNil(t, key1)
assert.Equal(t, "keyname1", key1.GetName())
assert.Equal(t, keys.TypeLedger, key1.GetType())
assert.Equal(t,
"terrapub1addwnpepqvpg7r26nl2pvqqern00m6s9uaax3hauu2rzg8qpjzq9hy6xve7sw0d84m6",
sdk.MustBech32ifyAccPub(key1.GetPubKey()))
config.SetCoinType(118)
config.SetFullFundraiserPath("44'/118'/0'/0/0")
config.SetBech32PrefixForAccount(sdk.Bech32PrefixAccAddr, sdk.Bech32PrefixAccPub)
config.SetBech32PrefixForValidator(sdk.Bech32PrefixValAddr, sdk.Bech32PrefixValPub)
config.SetBech32PrefixForConsensusNode(sdk.Bech32PrefixConsAddr, sdk.Bech32PrefixConsPub)
}
func Test_runAddCmdLedger(t *testing.T) { func Test_runAddCmdLedger(t *testing.T) {
cmd := addKeyCommand() cmd := addKeyCommand()
assert.NotNil(t, cmd) assert.NotNil(t, cmd)

View File

@ -41,7 +41,8 @@ func (mock LedgerSECP256K1Mock) GetPublicKeySECP256K1(derivationPath []uint32) (
if derivationPath[0] != 44 { if derivationPath[0] != 44 {
return nil, errors.New("Invalid derivation path") return nil, errors.New("Invalid derivation path")
} }
if derivationPath[1] != sdk.CoinType {
if derivationPath[1] != sdk.GetConfig().GetCoinType() {
return nil, errors.New("Invalid derivation path") return nil, errors.New("Invalid derivation path")
} }

View File

@ -119,7 +119,8 @@ func LedgerShowAddress(path hd.BIP44Params, expectedPubKey tmcrypto.PubKey) erro
return fmt.Errorf("the key's pubkey does not match with the one retrieved from Ledger. Check that the HD path and device are the correct ones") return fmt.Errorf("the key's pubkey does not match with the one retrieved from Ledger. Check that the HD path and device are the correct ones")
} }
pubKey2, _, err := getPubKeyAddrSafe(device, path, sdk.Bech32PrefixAccAddr) config := sdk.GetConfig()
pubKey2, _, err := getPubKeyAddrSafe(device, path, config.GetBech32AccountAddrPrefix())
if err != nil { if err != nil {
return err return err
} }