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 ledger custom coin type support bug #4716

Merged
merged 11 commits into from
Jul 29, 2019
1 change: 1 addition & 0 deletions .pending/bugfixes/keys/Fix-ledger-custom-co
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ledger custom coin type support bug
54 changes: 54 additions & 0 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,60 @@ import (
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) {
cmd := addKeyCommand()
assert.NotNil(t, cmd)
Expand Down
3 changes: 2 additions & 1 deletion crypto/ledger_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func (mock LedgerSECP256K1Mock) GetPublicKeySECP256K1(derivationPath []uint32) (
if derivationPath[0] != 44 {
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")
}

Expand Down
3 changes: 2 additions & 1 deletion crypto/ledger_secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

pubKey2, _, err := getPubKeyAddrSafe(device, path, sdk.Bech32PrefixAccAddr)
config := sdk.GetConfig()
pubKey2, _, err := getPubKeyAddrSafe(device, path, config.GetBech32AccountAddrPrefix())
if err != nil {
return err
}
Expand Down