2018-10-31 12:13:13 -07:00
package types
import (
2020-05-08 01:30:55 -07:00
"context"
2021-02-22 07:14:09 -08:00
"fmt"
2018-10-31 12:13:13 -07:00
"sync"
2020-01-23 10:47:11 -08:00
"github.com/cosmos/cosmos-sdk/version"
2018-10-31 12:13:13 -07:00
)
2019-09-30 08:49:12 -07:00
// DefaultKeyringServiceName defines a default service name for the keyring.
const DefaultKeyringServiceName = "cosmos"
2018-10-31 12:13:13 -07:00
// 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 {
2019-05-30 05:46:38 -07:00
fullFundraiserPath string
2019-10-14 08:43:19 -07:00
bech32AddressPrefix map [ string ] string
2018-12-12 13:29:42 -08:00
txEncoder TxEncoder
2019-05-02 12:36:42 -07:00
addressVerifier func ( [ ] byte ) error
2019-10-14 08:43:19 -07:00
mtx sync . RWMutex
2021-02-22 07:14:09 -08:00
// SLIP-44 related
purpose uint32
coinType uint32
sealed bool
sealedch chan struct { }
2018-10-31 12:13:13 -07:00
}
2019-09-30 08:49:12 -07:00
// cosmos-sdk wide global singleton
2020-05-08 01:30:55 -07:00
var (
sdkConfig * Config
initConfig sync . Once
)
2020-01-23 10:47:11 -08:00
2020-05-08 01:30:55 -07:00
// New returns a new Config with default values.
func NewConfig ( ) * Config {
return & Config {
sealedch : make ( chan struct { } ) ,
2018-10-31 12:13:13 -07:00
bech32AddressPrefix : map [ string ] string {
"account_addr" : Bech32PrefixAccAddr ,
"validator_addr" : Bech32PrefixValAddr ,
"consensus_addr" : Bech32PrefixConsAddr ,
"account_pub" : Bech32PrefixAccPub ,
"validator_pub" : Bech32PrefixValPub ,
"consensus_pub" : Bech32PrefixConsPub ,
} ,
2019-05-30 05:46:38 -07:00
fullFundraiserPath : FullFundraiserPath ,
2021-02-22 07:14:09 -08:00
purpose : Purpose ,
coinType : CoinType ,
txEncoder : nil ,
2018-10-31 12:13:13 -07:00
}
2020-05-08 01:30:55 -07:00
}
// GetConfig returns the config instance for the SDK.
func GetConfig ( ) * Config {
initConfig . Do ( func ( ) {
sdkConfig = NewConfig ( )
} )
2018-10-31 12:13:13 -07:00
return sdkConfig
}
2020-05-08 01:30:55 -07:00
// GetSealedConfig returns the config instance for the SDK if/once it is sealed.
func GetSealedConfig ( ctx context . Context ) ( * Config , error ) {
config := GetConfig ( )
select {
case <- config . sealedch :
return config , nil
case <- ctx . Done ( ) :
return nil , ctx . Err ( )
}
}
2018-10-31 12:13:13 -07:00
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
}
2018-12-12 13:29:42 -08:00
// SetTxEncoder builds the Config with TxEncoder used to marshal StdTx to bytes
func ( config * Config ) SetTxEncoder ( encoder TxEncoder ) {
config . assertNotSealed ( )
config . txEncoder = encoder
}
2019-05-02 12:36:42 -07:00
// SetAddressVerifier builds the Config with the provided function for verifying that addresses
// have the correct format
func ( config * Config ) SetAddressVerifier ( addressVerifier func ( [ ] byte ) error ) {
config . assertNotSealed ( )
config . addressVerifier = addressVerifier
}
2021-02-22 07:14:09 -08:00
// Set the FullFundraiserPath (BIP44Prefix) on the config.
//
// Deprecated: This method is supported for backward compatibility only and will be removed in a future release. Use SetPurpose and SetCoinType instead.
func ( config * Config ) SetFullFundraiserPath ( fullFundraiserPath string ) {
2019-05-30 05:46:38 -07:00
config . assertNotSealed ( )
2021-02-22 07:14:09 -08:00
config . fullFundraiserPath = fullFundraiserPath
2019-05-30 05:46:38 -07:00
}
2021-02-22 07:14:09 -08:00
// Set the BIP-0044 Purpose code on the config
func ( config * Config ) SetPurpose ( purpose uint32 ) {
2019-05-30 05:46:38 -07:00
config . assertNotSealed ( )
2021-02-22 07:14:09 -08:00
config . purpose = purpose
}
// Set the BIP-0044 CoinType code on the config
func ( config * Config ) SetCoinType ( coinType uint32 ) {
config . assertNotSealed ( )
config . coinType = coinType
2019-05-30 05:46:38 -07:00
}
2018-10-31 12:13:13 -07:00
// Seal seals the config such that the config state could not be modified further
func ( config * Config ) Seal ( ) * Config {
config . mtx . Lock ( )
2020-05-08 01:30:55 -07:00
if config . sealed {
config . mtx . Unlock ( )
return config
}
// signal sealed after state exposed/unlocked
2018-10-31 12:13:13 -07:00
config . sealed = true
2020-05-08 01:30:55 -07:00
config . mtx . Unlock ( )
close ( config . sealedch )
2018-10-31 12:13:13 -07:00
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" ]
}
2018-12-12 13:29:42 -08:00
// GetTxEncoder return function to encode transactions
func ( config * Config ) GetTxEncoder ( ) TxEncoder {
return config . txEncoder
}
2019-05-02 12:36:42 -07:00
// GetAddressVerifier returns the function to verify that addresses have the correct format
func ( config * Config ) GetAddressVerifier ( ) func ( [ ] byte ) error {
return config . addressVerifier
}
2019-05-30 05:46:38 -07:00
2021-02-22 07:14:09 -08:00
// GetPurpose returns the BIP-0044 Purpose code on the config.
func ( config * Config ) GetPurpose ( ) uint32 {
return config . purpose
}
2019-09-30 08:49:12 -07:00
// GetCoinType returns the BIP-0044 CoinType code on the config.
2019-05-30 05:46:38 -07:00
func ( config * Config ) GetCoinType ( ) uint32 {
return config . coinType
}
2019-09-30 08:49:12 -07:00
// GetFullFundraiserPath returns the BIP44Prefix.
2021-02-22 07:14:09 -08:00
//
// Deprecated: This method is supported for backward compatibility only and will be removed in a future release. Use GetFullBIP44Path instead.
2019-05-30 05:46:38 -07:00
func ( config * Config ) GetFullFundraiserPath ( ) string {
return config . fullFundraiserPath
}
2019-09-30 08:49:12 -07:00
2021-02-22 07:14:09 -08:00
// GetFullBIP44Path returns the BIP44Prefix.
func ( config * Config ) GetFullBIP44Path ( ) string {
return fmt . Sprintf ( "m/%d'/%d'/0'/0/0" , config . purpose , config . coinType )
}
2020-01-23 10:47:11 -08:00
func KeyringServiceName ( ) string {
if len ( version . Name ) == 0 {
return DefaultKeyringServiceName
}
return version . Name
2019-09-30 08:49:12 -07:00
}