github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/types/config.go (about)

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