2020-03-25 08:20:36 -07:00
|
|
|
package keyring
|
2019-03-01 13:29:33 -08:00
|
|
|
|
|
|
|
import (
|
2021-03-25 07:53:22 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
2019-03-01 13:29:33 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2021-03-25 07:53:22 -07:00
|
|
|
// TODO: Move this file to client/keys
|
|
|
|
// Use protobuf interface marshaler rather then generic JSON
|
|
|
|
|
2019-03-01 13:29:33 -08:00
|
|
|
// KeyOutput defines a structure wrapping around an Info object used for output
|
|
|
|
// functionality.
|
|
|
|
type KeyOutput struct {
|
2021-03-25 07:53:22 -07:00
|
|
|
Name string `json:"name" yaml:"name"`
|
|
|
|
Type string `json:"type" yaml:"type"`
|
|
|
|
Address string `json:"address" yaml:"address"`
|
|
|
|
PubKey string `json:"pubkey" yaml:"pubkey"`
|
|
|
|
Mnemonic string `json:"mnemonic,omitempty" yaml:"mnemonic"`
|
2019-07-31 08:47:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewKeyOutput creates a default KeyOutput instance without Mnemonic, Threshold and PubKeys
|
2021-03-25 07:53:22 -07:00
|
|
|
func NewKeyOutput(name string, keyType KeyType, a sdk.Address, pk cryptotypes.PubKey) (KeyOutput, error) { // nolint:interfacer
|
|
|
|
apk, err := codectypes.NewAnyWithValue(pk)
|
2019-03-01 13:29:33 -08:00
|
|
|
if err != nil {
|
|
|
|
return KeyOutput{}, err
|
|
|
|
}
|
2021-03-25 07:53:22 -07:00
|
|
|
bz, err := codec.ProtoMarshalJSON(apk, nil)
|
2019-03-01 13:29:33 -08:00
|
|
|
if err != nil {
|
|
|
|
return KeyOutput{}, err
|
|
|
|
}
|
2021-03-25 07:53:22 -07:00
|
|
|
return KeyOutput{
|
|
|
|
Name: name,
|
|
|
|
Type: keyType.String(),
|
|
|
|
Address: a.String(),
|
|
|
|
PubKey: string(bz),
|
|
|
|
}, nil
|
|
|
|
}
|
2019-03-01 13:29:33 -08:00
|
|
|
|
2021-03-25 07:53:22 -07:00
|
|
|
// MkConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes.
|
|
|
|
func MkConsKeyOutput(keyInfo Info) (KeyOutput, error) {
|
|
|
|
pk := keyInfo.GetPubKey()
|
|
|
|
addr := sdk.ConsAddress(pk.Address())
|
|
|
|
return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk)
|
2019-03-01 13:29:33 -08:00
|
|
|
}
|
|
|
|
|
2021-03-25 07:53:22 -07:00
|
|
|
// MkValKeyOutput create a KeyOutput in with "val" Bech32 prefixes.
|
|
|
|
func MkValKeyOutput(keyInfo Info) (KeyOutput, error) {
|
|
|
|
pk := keyInfo.GetPubKey()
|
|
|
|
addr := sdk.ValAddress(pk.Address())
|
|
|
|
return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MkAccKeyOutput create a KeyOutput in with "acc" Bech32 prefixes. If the
|
2019-03-01 13:29:33 -08:00
|
|
|
// public key is a multisig public key, then the threshold and constituent
|
|
|
|
// public keys will be added.
|
2021-03-25 07:53:22 -07:00
|
|
|
func MkAccKeyOutput(keyInfo Info) (KeyOutput, error) {
|
|
|
|
pk := keyInfo.GetPubKey()
|
|
|
|
addr := sdk.AccAddress(pk.Address())
|
|
|
|
return NewKeyOutput(keyInfo.GetName(), keyInfo.GetType(), addr, pk)
|
|
|
|
}
|
2019-03-01 13:29:33 -08:00
|
|
|
|
2021-03-25 07:53:22 -07:00
|
|
|
// MkAccKeysOutput returns a slice of KeyOutput objects, each with the "acc"
|
|
|
|
// Bech32 prefixes, given a slice of Info objects. It returns an error if any
|
|
|
|
// call to MkKeyOutput fails.
|
|
|
|
func MkAccKeysOutput(infos []Info) ([]KeyOutput, error) {
|
|
|
|
kos := make([]KeyOutput, len(infos))
|
|
|
|
var err error
|
|
|
|
for i, info := range infos {
|
|
|
|
kos[i], err = MkAccKeyOutput(info)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-03-01 13:29:33 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 07:53:22 -07:00
|
|
|
return kos, nil
|
2019-03-01 13:29:33 -08:00
|
|
|
}
|