Some checks failed
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
Build & Push SDK Proto Builder / build (push) Has been cancelled
140 lines
3.8 KiB
Go
140 lines
3.8 KiB
Go
package keyring
|
|
|
|
import (
|
|
"github.com/cockroachdb/errors"
|
|
|
|
errorsmod "cosmossdk.io/errors"
|
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
|
"github.com/cosmos/cosmos-sdk/types"
|
|
)
|
|
|
|
var (
|
|
// ErrPrivKeyExtr is used to output an error if extraction of a private key from Local item fails.
|
|
ErrPrivKeyExtr = errors.New("private key extraction works only for Local")
|
|
// ErrPrivKeyNotAvailable is used when a Record_Local.PrivKey is nil.
|
|
ErrPrivKeyNotAvailable = errors.New("private key is not available")
|
|
// ErrCastAny is used to output an error if cast from types.Any fails.
|
|
ErrCastAny = errors.New("unable to cast to cryptotypes")
|
|
)
|
|
|
|
func newRecord(name string, pk cryptotypes.PubKey, item isRecord_Item) (*Record, error) {
|
|
any, err := codectypes.NewAnyWithValue(pk)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Record{name, any, item}, nil
|
|
}
|
|
|
|
// NewLocalRecord creates a new Record with local key item
|
|
func NewLocalRecord(name string, priv cryptotypes.PrivKey, pk cryptotypes.PubKey) (*Record, error) {
|
|
any, err := codectypes.NewAnyWithValue(priv)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
recordLocal := &Record_Local{any}
|
|
recordLocalItem := &Record_Local_{recordLocal}
|
|
|
|
return newRecord(name, pk, recordLocalItem)
|
|
}
|
|
|
|
// NewLedgerRecord creates a new Record with ledger item
|
|
func NewLedgerRecord(name string, pk cryptotypes.PubKey, path *hd.BIP44Params) (*Record, error) {
|
|
recordLedger := &Record_Ledger{path}
|
|
recordLedgerItem := &Record_Ledger_{recordLedger}
|
|
return newRecord(name, pk, recordLedgerItem)
|
|
}
|
|
|
|
func (rl *Record_Ledger) GetPath() *hd.BIP44Params {
|
|
return rl.Path
|
|
}
|
|
|
|
// NewOfflineRecord creates a new Record with offline item
|
|
func NewOfflineRecord(name string, pk cryptotypes.PubKey) (*Record, error) {
|
|
recordOffline := &Record_Offline{}
|
|
recordOfflineItem := &Record_Offline_{recordOffline}
|
|
return newRecord(name, pk, recordOfflineItem)
|
|
}
|
|
|
|
// NewMultiRecord creates a new Record with multi item
|
|
func NewMultiRecord(name string, pk cryptotypes.PubKey) (*Record, error) {
|
|
recordMulti := &Record_Multi{}
|
|
recordMultiItem := &Record_Multi_{recordMulti}
|
|
return newRecord(name, pk, recordMultiItem)
|
|
}
|
|
|
|
// GetPubKey fetches a public key of the record
|
|
func (k *Record) GetPubKey() (cryptotypes.PubKey, error) {
|
|
pk, ok := k.PubKey.GetCachedValue().(cryptotypes.PubKey)
|
|
if !ok {
|
|
return nil, errorsmod.Wrap(ErrCastAny, "PubKey")
|
|
}
|
|
|
|
return pk, nil
|
|
}
|
|
|
|
// GetAddress fetches an address of the record
|
|
func (k Record) GetAddress() (types.AccAddress, error) {
|
|
pk, err := k.GetPubKey()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return pk.Address().Bytes(), nil
|
|
}
|
|
|
|
// GetType fetches type of the record
|
|
func (k Record) GetType() KeyType {
|
|
switch {
|
|
case k.GetLocal() != nil:
|
|
return TypeLocal
|
|
case k.GetLedger() != nil:
|
|
return TypeLedger
|
|
case k.GetMulti() != nil:
|
|
return TypeMulti
|
|
case k.GetOffline() != nil:
|
|
return TypeOffline
|
|
default:
|
|
panic("unrecognized record type")
|
|
}
|
|
}
|
|
|
|
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
|
|
func (k *Record) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
|
|
var pk cryptotypes.PubKey
|
|
if err := unpacker.UnpackAny(k.PubKey, &pk); err != nil {
|
|
return err
|
|
}
|
|
|
|
if l := k.GetLocal(); l != nil {
|
|
var priv cryptotypes.PrivKey
|
|
return unpacker.UnpackAny(l.PrivKey, &priv)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func extractPrivKeyFromRecord(k *Record) (cryptotypes.PrivKey, error) {
|
|
rl := k.GetLocal()
|
|
if rl == nil {
|
|
return nil, ErrPrivKeyExtr
|
|
}
|
|
|
|
return extractPrivKeyFromLocal(rl)
|
|
}
|
|
|
|
func extractPrivKeyFromLocal(rl *Record_Local) (cryptotypes.PrivKey, error) {
|
|
if rl.PrivKey == nil {
|
|
return nil, ErrPrivKeyNotAvailable
|
|
}
|
|
|
|
priv, ok := rl.PrivKey.GetCachedValue().(cryptotypes.PrivKey)
|
|
if !ok {
|
|
return nil, errorsmod.Wrap(ErrCastAny, "PrivKey")
|
|
}
|
|
|
|
return priv, nil
|
|
}
|