github.com/cosmos/cosmos-sdk@v0.50.10/types/config.go (about) 1 package types 2 3 import ( 4 "context" 5 "fmt" 6 "sync" 7 8 "github.com/cosmos/cosmos-sdk/version" 9 ) 10 11 // DefaultKeyringServiceName defines a default service name for the keyring. 12 const DefaultKeyringServiceName = "cosmos" 13 14 // Config is the structure that holds the SDK configuration parameters. 15 // This could be used to initialize certain configuration parameters for the SDK. 16 type Config struct { 17 fullFundraiserPath string 18 bech32AddressPrefix map[string]string 19 txEncoder TxEncoder 20 addressVerifier func([]byte) error 21 mtx sync.RWMutex 22 23 // SLIP-44 related 24 purpose uint32 25 coinType uint32 26 27 sealed bool 28 sealedch chan struct{} 29 } 30 31 // cosmos-sdk wide global singleton 32 var ( 33 sdkConfig *Config 34 initConfig sync.Once 35 ) 36 37 // New returns a new Config with default values. 38 func NewConfig() *Config { 39 return &Config{ 40 sealedch: make(chan struct{}), 41 bech32AddressPrefix: map[string]string{ 42 "account_addr": Bech32PrefixAccAddr, 43 "validator_addr": Bech32PrefixValAddr, 44 "consensus_addr": Bech32PrefixConsAddr, 45 "account_pub": Bech32PrefixAccPub, 46 "validator_pub": Bech32PrefixValPub, 47 "consensus_pub": Bech32PrefixConsPub, 48 }, 49 fullFundraiserPath: FullFundraiserPath, 50 51 purpose: Purpose, 52 coinType: CoinType, 53 txEncoder: nil, 54 } 55 } 56 57 // GetConfig returns the config instance for the SDK. 58 func GetConfig() *Config { 59 initConfig.Do(func() { 60 sdkConfig = NewConfig() 61 }) 62 return sdkConfig 63 } 64 65 // GetSealedConfig returns the config instance for the SDK if/once it is sealed. 66 func GetSealedConfig(ctx context.Context) (*Config, error) { 67 config := GetConfig() 68 select { 69 case <-config.sealedch: 70 return config, nil 71 case <-ctx.Done(): 72 return nil, ctx.Err() 73 } 74 } 75 76 func (config *Config) assertNotSealed() { 77 config.mtx.RLock() 78 defer config.mtx.RUnlock() 79 80 if config.sealed { 81 panic("Config is sealed") 82 } 83 } 84 85 // SetBech32PrefixForAccount builds the Config with Bech32 addressPrefix and publKeyPrefix for accounts 86 // and returns the config instance 87 func (config *Config) SetBech32PrefixForAccount(addressPrefix, pubKeyPrefix string) { 88 config.assertNotSealed() 89 config.bech32AddressPrefix["account_addr"] = addressPrefix 90 config.bech32AddressPrefix["account_pub"] = pubKeyPrefix 91 } 92 93 // SetBech32PrefixForValidator builds the Config with Bech32 addressPrefix and publKeyPrefix for validators 94 // 95 // and returns the config instance 96 func (config *Config) SetBech32PrefixForValidator(addressPrefix, pubKeyPrefix string) { 97 config.assertNotSealed() 98 config.bech32AddressPrefix["validator_addr"] = addressPrefix 99 config.bech32AddressPrefix["validator_pub"] = pubKeyPrefix 100 } 101 102 // SetBech32PrefixForConsensusNode builds the Config with Bech32 addressPrefix and publKeyPrefix for consensus nodes 103 // and returns the config instance 104 func (config *Config) SetBech32PrefixForConsensusNode(addressPrefix, pubKeyPrefix string) { 105 config.assertNotSealed() 106 config.bech32AddressPrefix["consensus_addr"] = addressPrefix 107 config.bech32AddressPrefix["consensus_pub"] = pubKeyPrefix 108 } 109 110 // SetTxEncoder builds the Config with TxEncoder used to marshal StdTx to bytes 111 func (config *Config) SetTxEncoder(encoder TxEncoder) { 112 config.assertNotSealed() 113 config.txEncoder = encoder 114 } 115 116 // SetAddressVerifier builds the Config with the provided function for verifying that addresses 117 // have the correct format 118 func (config *Config) SetAddressVerifier(addressVerifier func([]byte) error) { 119 config.assertNotSealed() 120 config.addressVerifier = addressVerifier 121 } 122 123 // Set the FullFundraiserPath (BIP44Prefix) on the config. 124 // 125 // Deprecated: This method is supported for backward compatibility only and will be removed in a future release. Use SetPurpose and SetCoinType instead. 126 func (config *Config) SetFullFundraiserPath(fullFundraiserPath string) { 127 config.assertNotSealed() 128 config.fullFundraiserPath = fullFundraiserPath 129 } 130 131 // Set the BIP-0044 Purpose code on the config 132 func (config *Config) SetPurpose(purpose uint32) { 133 config.assertNotSealed() 134 config.purpose = purpose 135 } 136 137 // Set the BIP-0044 CoinType code on the config 138 func (config *Config) SetCoinType(coinType uint32) { 139 config.assertNotSealed() 140 config.coinType = coinType 141 } 142 143 // Seal seals the config such that the config state could not be modified further 144 func (config *Config) Seal() *Config { 145 config.mtx.Lock() 146 147 if config.sealed { 148 config.mtx.Unlock() 149 return config 150 } 151 152 // signal sealed after state exposed/unlocked 153 config.sealed = true 154 config.mtx.Unlock() 155 close(config.sealedch) 156 157 return config 158 } 159 160 // GetBech32AccountAddrPrefix returns the Bech32 prefix for account address 161 func (config *Config) GetBech32AccountAddrPrefix() string { 162 return config.bech32AddressPrefix["account_addr"] 163 } 164 165 // GetBech32ValidatorAddrPrefix returns the Bech32 prefix for validator address 166 func (config *Config) GetBech32ValidatorAddrPrefix() string { 167 return config.bech32AddressPrefix["validator_addr"] 168 } 169 170 // GetBech32ConsensusAddrPrefix returns the Bech32 prefix for consensus node address 171 func (config *Config) GetBech32ConsensusAddrPrefix() string { 172 return config.bech32AddressPrefix["consensus_addr"] 173 } 174 175 // GetBech32AccountPubPrefix returns the Bech32 prefix for account public key 176 func (config *Config) GetBech32AccountPubPrefix() string { 177 return config.bech32AddressPrefix["account_pub"] 178 } 179 180 // GetBech32ValidatorPubPrefix returns the Bech32 prefix for validator public key 181 func (config *Config) GetBech32ValidatorPubPrefix() string { 182 return config.bech32AddressPrefix["validator_pub"] 183 } 184 185 // GetBech32ConsensusPubPrefix returns the Bech32 prefix for consensus node public key 186 func (config *Config) GetBech32ConsensusPubPrefix() string { 187 return config.bech32AddressPrefix["consensus_pub"] 188 } 189 190 // GetTxEncoder return function to encode transactions 191 func (config *Config) GetTxEncoder() TxEncoder { 192 return config.txEncoder 193 } 194 195 // GetAddressVerifier returns the function to verify that addresses have the correct format 196 func (config *Config) GetAddressVerifier() func([]byte) error { 197 return config.addressVerifier 198 } 199 200 // GetPurpose returns the BIP-0044 Purpose code on the config. 201 func (config *Config) GetPurpose() uint32 { 202 return config.purpose 203 } 204 205 // GetCoinType returns the BIP-0044 CoinType code on the config. 206 func (config *Config) GetCoinType() uint32 { 207 return config.coinType 208 } 209 210 // GetFullFundraiserPath returns the BIP44Prefix. 211 // 212 // Deprecated: This method is supported for backward compatibility only and will be removed in a future release. Use GetFullBIP44Path instead. 213 func (config *Config) GetFullFundraiserPath() string { 214 return config.fullFundraiserPath 215 } 216 217 // GetFullBIP44Path returns the BIP44Prefix. 218 func (config *Config) GetFullBIP44Path() string { 219 return fmt.Sprintf("m/%d'/%d'/0'/0/0", config.purpose, config.coinType) 220 } 221 222 func KeyringServiceName() string { 223 if len(version.Name) == 0 { 224 return DefaultKeyringServiceName 225 } 226 return version.Name 227 }