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

Deprecate SetFullFundraiserPath in favor of SetPurpose and SetCoinType #8629

Merged
merged 13 commits into from
Feb 22, 2021
Merged
4 changes: 2 additions & 2 deletions client/keys/add_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
bech32PrefixConsAddr := "terravalcons"
bech32PrefixConsPub := "terravalconspub"

config.SetPurpose(44)
config.SetCoinType(330)
config.SetFullFundraiserPath("44'/330'/0'/0/0")
config.SetBech32PrefixForAccount(bech32PrefixAccAddr, bech32PrefixAccPub)
config.SetBech32PrefixForValidator(bech32PrefixValAddr, bech32PrefixValPub)
config.SetBech32PrefixForConsensusNode(bech32PrefixConsAddr, bech32PrefixConsPub)
Expand Down Expand Up @@ -77,8 +77,8 @@ func Test_runAddCmdLedgerWithCustomCoinType(t *testing.T) {
"terrapub1addwnpepqvpg7r26nl2pvqqern00m6s9uaax3hauu2rzg8qpjzq9hy6xve7sw0d84m6",
sdk.MustBech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, key1.GetPubKey()))

config.SetPurpose(44)
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)
Expand Down
2 changes: 1 addition & 1 deletion client/keys/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Test_runDeleteCmd(t *testing.T) {
fakeKeyName1 := "runDeleteCmd_Key1"
fakeKeyName2 := "runDeleteCmd_Key2"

path := sdk.GetConfig().GetFullFundraiserPath()
path := sdk.GetConfig().GetFullBip44Path()
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved

kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion client/keys/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Test_runExportCmd(t *testing.T) {
kb.Delete("keyname1") // nolint:errcheck
})

path := sdk.GetConfig().GetFullFundraiserPath()
path := sdk.GetConfig().GetFullBip44Path()
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
_, err = kb.NewAccount("keyname1", testutil.TestMnemonic, "", path, hd.Secp256k1)
require.NoError(t, err)

Expand Down
2 changes: 1 addition & 1 deletion client/keys/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Test_runListCmd(t *testing.T) {
clientCtx := client.Context{}.WithKeyring(kb)
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)

path := "" //sdk.GetConfig().GetFullFundraiserPath()
path := "" //sdk.GetConfig().GetFullBip44Path()
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
_, err = kb.NewAccount("something", testutil.TestMnemonic, "", path, hd.Secp256k1)
require.NoError(t, err)

Expand Down
5 changes: 4 additions & 1 deletion types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ const (
// config.SetBech32PrefixForAccount(yourBech32PrefixAccAddr, yourBech32PrefixAccPub)
// config.SetBech32PrefixForValidator(yourBech32PrefixValAddr, yourBech32PrefixValPub)
// config.SetBech32PrefixForConsensusNode(yourBech32PrefixConsAddr, yourBech32PrefixConsPub)
// config.SetPurpose(yourPurpose)
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
// config.SetCoinType(yourCoinType)
// config.SetFullFundraiserPath(yourFullFundraiserPath)
// config.Seal()

// Bech32MainPrefix defines the main SDK Bech32 prefix of an account's address
Bech32MainPrefix = "cosmos"

// Purpose is the ATOM purpose as defined in SLIP44 (https://github.com/satoshilabs/slips/blob/master/slip-0044.md)
Purpose = 44

// CoinType is the ATOM coin type as defined in SLIP44 (https://github.com/satoshilabs/slips/blob/master/slip-0044.md)
CoinType = 118

Expand Down
42 changes: 26 additions & 16 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"context"
"fmt"
"sync"

"github.com/cosmos/cosmos-sdk/version"
Expand All @@ -13,14 +14,17 @@ const DefaultKeyringServiceName = "cosmos"
// Config is the structure that holds the SDK configuration parameters.
// This could be used to initialize certain configuration parameters for the SDK.
type Config struct {
fullFundraiserPath string
bech32AddressPrefix map[string]string
txEncoder TxEncoder
addressVerifier func([]byte) error
mtx sync.RWMutex
coinType uint32
sealed bool
sealedch chan struct{}

// SLIP-44 related
purpose uint32
coinType uint32

sealed bool
sealedch chan struct{}
}

// cosmos-sdk wide global singleton
Expand All @@ -41,9 +45,10 @@ func NewConfig() *Config {
"validator_pub": Bech32PrefixValPub,
"consensus_pub": Bech32PrefixConsPub,
},
coinType: CoinType,
fullFundraiserPath: FullFundraiserPath,
txEncoder: nil,

purpose: Purpose,
coinType: CoinType,
txEncoder: nil,
}
}

Expand Down Expand Up @@ -112,16 +117,16 @@ func (config *Config) SetAddressVerifier(addressVerifier func([]byte) error) {
config.addressVerifier = addressVerifier
}

// Set the BIP-0044 CoinType code on the config
func (config *Config) SetCoinType(coinType uint32) {
// Set the BIP-0044 Purpose code on the config
func (config *Config) SetPurpose(purpose uint32) {
config.assertNotSealed()
config.coinType = coinType
config.purpose = purpose
}

// Set the FullFundraiserPath (BIP44Prefix) on the config
func (config *Config) SetFullFundraiserPath(fullFundraiserPath string) {
// Set the BIP-0044 CoinType code on the config
func (config *Config) SetCoinType(coinType uint32) {
config.assertNotSealed()
config.fullFundraiserPath = fullFundraiserPath
config.coinType = coinType
}

// Seal seals the config such that the config state could not be modified further
Expand Down Expand Up @@ -181,14 +186,19 @@ func (config *Config) GetAddressVerifier() func([]byte) error {
return config.addressVerifier
}

// GetPurpose returns the BIP-0044 Purpose code on the config.
func (config *Config) GetPurpose() uint32 {
return config.purpose
}

// GetCoinType returns the BIP-0044 CoinType code on the config.
func (config *Config) GetCoinType() uint32 {
return config.coinType
}

// GetFullFundraiserPath returns the BIP44Prefix.
func (config *Config) GetFullFundraiserPath() string {
return config.fullFundraiserPath
// GetFullBip44Path returns the BIP44Prefix.
func (config *Config) GetFullBip44Path() string {
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf("m/%d'/%d'/0'/0/0", config.purpose, config.coinType)
}

func KeyringServiceName() string {
Expand Down
24 changes: 12 additions & 12 deletions types/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ func TestConfigTestSuite(t *testing.T) {
suite.Run(t, new(configTestSuite))
}

func (s *contextTestSuite) TestConfig_SetPurpose() {
config := sdk.NewConfig()
config.SetPurpose(44)
s.Require().Equal(uint32(44), config.GetPurpose())

config.SetPurpose(0)
s.Require().Equal(uint32(0), config.GetPurpose())

config.Seal()
s.Require().Panics(func() { config.SetPurpose(10) })
}

func (s *configTestSuite) TestConfig_SetCoinType() {
config := sdk.NewConfig()
config.SetCoinType(1)
Expand All @@ -41,18 +53,6 @@ func (s *configTestSuite) TestConfig_SetTxEncoder() {
s.Require().Panics(func() { config.SetTxEncoder(encFunc) })
}

func (s *configTestSuite) TestConfig_SetFullFundraiserPath() {
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
config := sdk.NewConfig()
config.SetFullFundraiserPath("test/path")
s.Require().Equal("test/path", config.GetFullFundraiserPath())

config.SetFullFundraiserPath("test/poth")
s.Require().Equal("test/poth", config.GetFullFundraiserPath())

config.Seal()
s.Require().Panics(func() { config.SetFullFundraiserPath("x/test/path") })
}

func (s *configTestSuite) TestKeyringServiceName() {
s.Require().Equal(sdk.DefaultKeyringServiceName, sdk.KeyringServiceName())
}