forked from cerc-io/laconicd-deprecated
cfac906f92
* WIP setting up Ethereum key CLI commands * Functional key gen and showing Ethereum address * Cleaned up changes * WIP setting up Ethereum key CLI commands * Functional key gen and showing Ethereum address * Cleaned up changes * Changed address to cosmos specific address * Remove default bech32 prefixes and add basic add command test * Changed Private key type to slice of bytes for compatibility and storability * switch back to using cosmos crypto Keybase interfaces * Changed key output to ethereum addressing instead of bitcoin and key generation to allow seeding from mnemonic and bip39 password * Updated show command and added test * Remove prefix requirement for showing keys and added existing keys commands to CLI temporarily * Removed unnecessary duplicate code * Readd prefixes for accounts temporarily * Fix linting issue * Remove TODO for setting PK to specific length of bytes (all functions use slice) * Cleaned up descriptions to remove multi-sigs
86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
package keys
|
|
|
|
import (
|
|
"encoding/hex"
|
|
|
|
cosmosKeys "github.com/cosmos/cosmos-sdk/crypto/keys"
|
|
)
|
|
|
|
// KeyOutput defines a structure wrapping around an Info object used for output
|
|
// functionality.
|
|
type KeyOutput struct {
|
|
Name string `json:"name"`
|
|
Type string `json:"type"`
|
|
Address string `json:"address"`
|
|
PubKey string `json:"pubkey"`
|
|
Mnemonic string `json:"mnemonic,omitempty"`
|
|
Threshold uint `json:"threshold,omitempty"`
|
|
}
|
|
|
|
// Bech32KeysOutput 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 Bech32KeyOutput fails.
|
|
func Bech32KeysOutput(infos []cosmosKeys.Info) ([]cosmosKeys.KeyOutput, error) {
|
|
kos := make([]cosmosKeys.KeyOutput, len(infos))
|
|
for i, info := range infos {
|
|
ko, err := Bech32KeyOutput(info)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
kos[i] = ko
|
|
}
|
|
|
|
return kos, nil
|
|
}
|
|
|
|
// Bech32ConsKeyOutput create a KeyOutput in with "cons" Bech32 prefixes.
|
|
func Bech32ConsKeyOutput(keyInfo cosmosKeys.Info) (cosmosKeys.KeyOutput, error) {
|
|
consAddr := keyInfo.GetPubKey().Address()
|
|
bytes := keyInfo.GetPubKey().Bytes()
|
|
|
|
// bechPubKey, err := sdk.Bech32ifyConsPub(keyInfo.GetPubKey())
|
|
// if err != nil {
|
|
// return KeyOutput{}, err
|
|
// }
|
|
|
|
return cosmosKeys.KeyOutput{
|
|
Name: keyInfo.GetName(),
|
|
Type: keyInfo.GetType().String(),
|
|
Address: consAddr.String(),
|
|
PubKey: hex.EncodeToString(bytes),
|
|
}, nil
|
|
}
|
|
|
|
// Bech32ValKeyOutput create a KeyOutput in with "val" Bech32 prefixes.
|
|
func Bech32ValKeyOutput(keyInfo cosmosKeys.Info) (cosmosKeys.KeyOutput, error) {
|
|
valAddr := keyInfo.GetPubKey().Address()
|
|
bytes := keyInfo.GetPubKey().Bytes()
|
|
|
|
// bechPubKey, err := sdk.Bech32ifyValPub(keyInfo.GetPubKey())
|
|
// if err != nil {
|
|
// return KeyOutput{}, err
|
|
// }
|
|
|
|
return cosmosKeys.KeyOutput{
|
|
Name: keyInfo.GetName(),
|
|
Type: keyInfo.GetType().String(),
|
|
Address: valAddr.String(),
|
|
PubKey: hex.EncodeToString(bytes),
|
|
}, nil
|
|
}
|
|
|
|
// Bech32KeyOutput create a KeyOutput in with "acc" Bech32 prefixes.
|
|
func Bech32KeyOutput(info cosmosKeys.Info) (cosmosKeys.KeyOutput, error) {
|
|
accAddr := info.GetPubKey().Address()
|
|
bytes := info.GetPubKey().Bytes()
|
|
|
|
ko := cosmosKeys.KeyOutput{
|
|
Name: info.GetName(),
|
|
Type: info.GetType().String(),
|
|
Address: accAddr.String(),
|
|
PubKey: hex.EncodeToString(bytes),
|
|
}
|
|
|
|
return ko, nil
|
|
}
|