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

     1  package keys
     2  
     3  import (
     4  	ethcmn "github.com/ethereum/go-ethereum/common"
     5  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     6  )
     7  
     8  // KeyOutput defines a structure wrapping around an Info object used for output
     9  // functionality.
    10  type KeyOutput struct {
    11  	Name        string                 `json:"name" yaml:"name"`
    12  	Type        string                 `json:"type" yaml:"type"`
    13  	Address     string                 `json:"address" yaml:"address"`
    14  	EthAddress  string                 `json:"eth_address" yaml:"eth_address"`
    15  	OperAddress string                 `json:"oper_address" yaml:"oper_address"`
    16  	PubKey      string                 `json:"pubkey" yaml:"pubkey"`
    17  	Mnemonic    string                 `json:"mnemonic,omitempty" yaml:"mnemonic"`
    18  	Threshold   uint                   `json:"threshold,omitempty" yaml:"threshold"`
    19  	PubKeys     []multisigPubKeyOutput `json:"pubkeys,omitempty" yaml:"pubkeys"`
    20  }
    21  
    22  // NewKeyOutput creates a default KeyOutput instance without Mnemonic, Threshold and PubKeys
    23  func NewKeyOutput(name, keyType, address, pubkey string) KeyOutput {
    24  	return KeyOutput{
    25  		Name:    name,
    26  		Type:    keyType,
    27  		Address: address,
    28  		PubKey:  pubkey,
    29  	}
    30  }
    31  
    32  type multisigPubKeyOutput struct {
    33  	Address string `json:"address" yaml:"address"`
    34  	PubKey  string `json:"pubkey" yaml:"pubkey"`
    35  	Weight  uint   `json:"weight" yaml:"weight"`
    36  }
    37  
    38  // Bech32KeysOutput returns a slice of KeyOutput objects, each with the "acc"
    39  // Bech32 prefixes, given a slice of Info objects. It returns an error if any
    40  // call to Bech32KeyOutput fails.
    41  func Bech32KeysOutput(infos []Info) ([]KeyOutput, error) {
    42  	kos := make([]KeyOutput, len(infos))
    43  	for i, info := range infos {
    44  		ko, err := Bech32KeyOutput(info)
    45  		if err != nil {
    46  			return nil, err
    47  		}
    48  		kos[i] = ko
    49  	}
    50  
    51  	return kos, nil
    52  }
    53  
    54  // Bech32ConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes.
    55  func Bech32ConsKeyOutput(keyInfo Info) (KeyOutput, error) {
    56  	consAddr := sdk.ConsAddress(keyInfo.GetPubKey().Address().Bytes())
    57  
    58  	bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, keyInfo.GetPubKey())
    59  	if err != nil {
    60  		return KeyOutput{}, err
    61  	}
    62  
    63  	return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType().String(), consAddr.String(), bechPubKey), nil
    64  }
    65  
    66  // Bech32ValKeyOutput create a KeyOutput in with "val" Bech32 prefixes.
    67  func Bech32ValKeyOutput(keyInfo Info) (KeyOutput, error) {
    68  	valAddr := sdk.ValAddress(keyInfo.GetPubKey().Address().Bytes())
    69  
    70  	bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeValPub, keyInfo.GetPubKey())
    71  	if err != nil {
    72  		return KeyOutput{}, err
    73  	}
    74  
    75  	return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType().String(), valAddr.String(), bechPubKey), nil
    76  }
    77  
    78  // Bech32KeyOutput create a KeyOutput in with "acc" Bech32 prefixes. If the
    79  // public key is a multisig public key, then the threshold and constituent
    80  // public keys will be added.
    81  func Bech32KeyOutput(keyInfo Info) (KeyOutput, error) {
    82  	accAddr := sdk.AccAddress(keyInfo.GetPubKey().Address().Bytes())
    83  	bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, keyInfo.GetPubKey())
    84  	if err != nil {
    85  		return KeyOutput{}, err
    86  	}
    87  
    88  	ko := NewKeyOutput(keyInfo.GetName(), keyInfo.GetType().String(), accAddr.String(), bechPubKey)
    89  
    90  	if mInfo, ok := keyInfo.(*multiInfo); ok {
    91  		pubKeys := make([]multisigPubKeyOutput, len(mInfo.PubKeys))
    92  
    93  		for i, pk := range mInfo.PubKeys {
    94  			accAddr := sdk.AccAddress(pk.PubKey.Address().Bytes())
    95  
    96  			bechPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk.PubKey)
    97  			if err != nil {
    98  				return KeyOutput{}, err
    99  			}
   100  
   101  			pubKeys[i] = multisigPubKeyOutput{accAddr.String(), bechPubKey, pk.Weight}
   102  		}
   103  
   104  		ko.Threshold = mInfo.Threshold
   105  		ko.PubKeys = pubKeys
   106  	}
   107  	ko.EthAddress = ethcmn.BytesToAddress(accAddr.Bytes()).String()
   108  	ko.OperAddress = sdk.ValAddress(accAddr.Bytes()).String()
   109  
   110  	return ko, nil
   111  }