-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR #2614: Configurable Bech32 prefix for SDK users
- Loading branch information
1 parent
fcf5b77
commit 9cf53f2
Showing
9 changed files
with
213 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package types | ||
|
||
import ( | ||
"sync" | ||
) | ||
|
||
// 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 { | ||
mtx sync.RWMutex | ||
sealed bool | ||
bech32AddressPrefix map[string]string | ||
} | ||
|
||
var ( | ||
// Initializing an instance of Config | ||
sdkConfig = &Config{ | ||
sealed: false, | ||
bech32AddressPrefix: map[string]string{ | ||
"account_addr": Bech32PrefixAccAddr, | ||
"validator_addr": Bech32PrefixValAddr, | ||
"consensus_addr": Bech32PrefixConsAddr, | ||
"account_pub": Bech32PrefixAccPub, | ||
"validator_pub": Bech32PrefixValPub, | ||
"consensus_pub": Bech32PrefixConsPub, | ||
}, | ||
} | ||
) | ||
|
||
// GetConfig returns the config instance for the SDK. | ||
func GetConfig() *Config { | ||
return sdkConfig | ||
} | ||
|
||
func (config *Config) assertNotSealed() { | ||
config.mtx.Lock() | ||
defer config.mtx.Unlock() | ||
|
||
if config.sealed { | ||
panic("Config is sealed") | ||
} | ||
} | ||
|
||
// SetBech32PrefixForAccount builds the Config with Bech32 addressPrefix and publKeyPrefix for accounts | ||
// and returns the config instance | ||
func (config *Config) SetBech32PrefixForAccount(addressPrefix, pubKeyPrefix string) { | ||
config.assertNotSealed() | ||
config.bech32AddressPrefix["account_addr"] = addressPrefix | ||
config.bech32AddressPrefix["account_pub"] = pubKeyPrefix | ||
} | ||
|
||
// SetBech32PrefixForValidator builds the Config with Bech32 addressPrefix and publKeyPrefix for validators | ||
// and returns the config instance | ||
func (config *Config) SetBech32PrefixForValidator(addressPrefix, pubKeyPrefix string) { | ||
config.assertNotSealed() | ||
config.bech32AddressPrefix["validator_addr"] = addressPrefix | ||
config.bech32AddressPrefix["validator_pub"] = pubKeyPrefix | ||
} | ||
|
||
// SetBech32PrefixForConsensusNode builds the Config with Bech32 addressPrefix and publKeyPrefix for consensus nodes | ||
// and returns the config instance | ||
func (config *Config) SetBech32PrefixForConsensusNode(addressPrefix, pubKeyPrefix string) { | ||
config.assertNotSealed() | ||
config.bech32AddressPrefix["consensus_addr"] = addressPrefix | ||
config.bech32AddressPrefix["consensus_pub"] = pubKeyPrefix | ||
} | ||
|
||
// Seal seals the config such that the config state could not be modified further | ||
func (config *Config) Seal() *Config { | ||
config.mtx.Lock() | ||
defer config.mtx.Unlock() | ||
|
||
config.sealed = true | ||
return config | ||
} | ||
|
||
// GetBech32AccountAddrPrefix returns the Bech32 prefix for account address | ||
func (config *Config) GetBech32AccountAddrPrefix() string { | ||
return config.bech32AddressPrefix["account_addr"] | ||
} | ||
|
||
// GetBech32ValidatorAddrPrefix returns the Bech32 prefix for validator address | ||
func (config *Config) GetBech32ValidatorAddrPrefix() string { | ||
return config.bech32AddressPrefix["validator_addr"] | ||
} | ||
|
||
// GetBech32ConsensusAddrPrefix returns the Bech32 prefix for consensus node address | ||
func (config *Config) GetBech32ConsensusAddrPrefix() string { | ||
return config.bech32AddressPrefix["consensus_addr"] | ||
} | ||
|
||
// GetBech32AccountPubPrefix returns the Bech32 prefix for account public key | ||
func (config *Config) GetBech32AccountPubPrefix() string { | ||
return config.bech32AddressPrefix["account_pub"] | ||
} | ||
|
||
// GetBech32ValidatorPubPrefix returns the Bech32 prefix for validator public key | ||
func (config *Config) GetBech32ValidatorPubPrefix() string { | ||
return config.bech32AddressPrefix["validator_pub"] | ||
} | ||
|
||
// GetBech32ConsensusPubPrefix returns the Bech32 prefix for consensus node public key | ||
func (config *Config) GetBech32ConsensusPubPrefix() string { | ||
return config.bech32AddressPrefix["consensus_pub"] | ||
} |