github.com/Finschia/finschia-sdk@v0.48.1/crypto/keyring/output.go (about)

     1  package keyring
     2  
     3  import (
     4  	"github.com/Finschia/finschia-sdk/codec"
     5  	codectypes "github.com/Finschia/finschia-sdk/codec/types"
     6  	cryptotypes "github.com/Finschia/finschia-sdk/crypto/types"
     7  	sdk "github.com/Finschia/finschia-sdk/types"
     8  )
     9  
    10  // TODO: Move this file to client/keys
    11  // Use protobuf interface marshaler rather then generic JSON
    12  
    13  // KeyOutput defines a structure wrapping around an Info object used for output
    14  // functionality.
    15  type KeyOutput struct {
    16  	Name     string `json:"name" yaml:"name"`
    17  	Type     string `json:"type" yaml:"type"`
    18  	Address  string `json:"address" yaml:"address"`
    19  	PubKey   string `json:"pubkey" yaml:"pubkey"`
    20  	Mnemonic string `json:"mnemonic,omitempty" yaml:"mnemonic"`
    21  }
    22  
    23  // NewKeyOutput creates a default KeyOutput instance without Mnemonic, Threshold and PubKeys
    24  func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.PubKey) (KeyOutput, error) { //nolint:interfacer
    25  	apk, err := codectypes.NewAnyWithValue(pk)
    26  	if err != nil {
    27  		return KeyOutput{}, err
    28  	}
    29  	bz, err := codec.ProtoMarshalJSON(apk, nil)
    30  	if err != nil {
    31  		return KeyOutput{}, err
    32  	}
    33  	return KeyOutput{
    34  		Name:    name,
    35  		Type:    keyType.String(),
    36  		Address: a.String(),
    37  		PubKey:  string(bz),
    38  	}, nil
    39  }
    40  
    41  // MkConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes.
    42  func MkConsKeyOutput(keyInfo Info) (KeyOutput, error) {
    43  	pk := keyInfo.GetPubKey()
    44  	addr := sdk.ConsAddress(pk.Address())
    45  	return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk)
    46  }
    47  
    48  // MkValKeyOutput create a KeyOutput in with "val" Bech32 prefixes.
    49  func MkValKeyOutput(keyInfo Info) (KeyOutput, error) {
    50  	pk := keyInfo.GetPubKey()
    51  	addr := sdk.ValAddress(pk.Address())
    52  	return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk)
    53  }
    54  
    55  // MkAccKeyOutput create a KeyOutput in with "acc" Bech32 prefixes. If the
    56  // public key is a multisig public key, then the threshold and constituent
    57  // public keys will be added.
    58  func MkAccKeyOutput(keyInfo Info) (KeyOutput, error) {
    59  	pk := keyInfo.GetPubKey()
    60  	addr := sdk.AccAddress(pk.Address())
    61  	return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk)
    62  }
    63  
    64  // MkAccKeysOutput returns a slice of KeyOutput objects, each with the "acc"
    65  // Bech32 prefixes, given a slice of Info objects. It returns an error if any
    66  // call to MkKeyOutput fails.
    67  func MkAccKeysOutput(infos []Info) ([]KeyOutput, error) {
    68  	kos := make([]KeyOutput, len(infos))
    69  	var err error
    70  	for i, info := range infos {
    71  		kos[i], err = MkAccKeyOutput(info)
    72  		if err != nil {
    73  			return nil, err
    74  		}
    75  	}
    76  
    77  	return kos, nil
    78  }