Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(crypto): update docs #22247

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crypto/codec/pubkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// PubKeyToProto converts a JSON public key (in `cryptokeys.JSONPubkey` format) to its corresponding protobuf public key type.
//
// Parameters:
// - pk: A `cryptokeys.JSONPubkey` containing the public key and its type.
//
// Returns:
// - cryptotypes.PubKey: The protobuf public key corresponding to the provided JSON public key.
// - error: An error if the key type is invalid or unsupported.
func PubKeyToProto(pk cryptokeys.JSONPubkey) (cryptotypes.PubKey, error) {
switch pk.KeyType {
case ed25519.PubKeyName:
Expand All @@ -30,6 +38,14 @@ func PubKeyToProto(pk cryptokeys.JSONPubkey) (cryptotypes.PubKey, error) {
}
}

// PubKeyFromProto converts a protobuf public key (in `cryptotypes.PubKey` format) to a JSON public key format (`cryptokeys.JSONPubkey`).
//
// Parameters:
// - pk: A `cryptotypes.PubKey` which is the protobuf representation of a public key.
//
// Returns:
// - cryptokeys.JSONPubkey: The JSON-formatted public key corresponding to the provided protobuf public key.
// - error: An error if the key type is invalid or unsupported.
func PubKeyFromProto(pk cryptotypes.PubKey) (cryptokeys.JSONPubkey, error) {
switch pk := pk.(type) {
case *ed25519.PubKey:
Expand Down
10 changes: 5 additions & 5 deletions crypto/hd/hdpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,11 @@ func derivePrivateKey(privKeyBytes, chainCode [32]byte, index uint32, harden boo
pubkeyBytes := ecPub.SerializeCompressed()
data = pubkeyBytes

/* By using btcec, we can remove the dependency on tendermint/crypto/secp256k1
pubkey := secp256k1.PrivKeySecp256k1(privKeyBytes).PubKey()
public := pubkey.(secp256k1.PubKeySecp256k1)
data = public[:]
*/
// By using btcec, we can remove the dependency on tendermint/crypto/secp256k1
// pubkey := secp256k1.PrivKeySecp256k1(privKeyBytes).PubKey()
// public := pubkey.(secp256k1.PubKeySecp256k1)
// data = public[:]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Inconsistency Between Comment and Implementation

The comment mentions using btcec to remove the dependency on tendermint/crypto/secp256k1. However, the actual implementation still utilizes secp for public key derivation:

  • ecPub := secp.PrivKeyFromBytes(privKeyBytes[:]).PubKey()
  • pubkeyBytes := ecPub.SerializeCompressed()

Please update the implementation to use btcec as intended or revise the comment to accurately describe the current usage of secp.

🔗 Analysis chain

Clarify btcec usage and improve comment

The comment indicates that btcec is now being used to remove the dependency on tendermint/crypto/secp256k1, which is a good practice for reducing external dependencies. However, the actual implementation using btcec is not visible in this diff.

  1. Please clarify how btcec is being used in the current implementation.
  2. Consider improving the comment to explain the specific btcec functions or methods being used for public key derivation.
  3. Ensure that the comment accurately reflects the current implementation.

To verify the actual implementation, please run the following script:

This will help ensure that the comment accurately reflects the code and that btcec is being used as intended.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check the actual implementation of public key derivation using btcec

# Search for btcec usage in the file
echo "Searching for btcec usage in crypto/hd/hdpath.go:"
rg --type go -n 'btcec' crypto/hd/hdpath.go

# Search for public key derivation implementation
echo "\nSearching for public key derivation implementation:"
rg --type go -n 'PubKey|SerializeCompressed' crypto/hd/hdpath.go

Length of output: 656

}

data = append(data, uint32ToBytes(index)...)
Expand Down
44 changes: 44 additions & 0 deletions crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,17 @@ func (ks keystore) ImportPubKey(uid, armor string) error {
return nil
}

// Sign signs a message using the private key associated with the provided UID.
//
// Parameters:
// - uid: The unique identifier of the account/key to use for signing.
// - msg: The message or data to be signed.
// - signMode: The signing mode that specifies how the message should be signed.
//
// Returns:
// - []byte: The generated signature.
// - types.PubKey: The public key corresponding to the private key used for signing.
// - error: Any error encountered during the signing process.
func (ks keystore) Sign(uid string, msg []byte, signMode signing.SignMode) ([]byte, types.PubKey, error) {
k, err := ks.Key(uid)
if err != nil {
Expand Down Expand Up @@ -536,6 +547,19 @@ func (ks keystore) List() ([]*Record, error) {
return ks.MigrateAll()
}

// NewMnemonic generates a new mnemonic and derives a new account from it.
//
// Parameters:
// - uid: A unique identifier for the account.
// - language: The language for the mnemonic (only English is supported).
// - hdPath: The hierarchical deterministic (HD) path for key derivation.
// - bip39Passphrase: The passphrase used in conjunction with the mnemonic for BIP-39.
// - algo: The signature algorithm used for signing keys.
//
// Returns:
// - *Record: A new key record that contains the private and public key information.
// - string: The generated mnemonic phrase.
// - error: Any error encountered during the process.
func (ks keystore) NewMnemonic(uid string, language Language, hdPath, bip39Passphrase string, algo SignatureAlgo) (*Record, string, error) {
if language != English {
return nil, "", ErrUnsupportedLanguage
Expand Down Expand Up @@ -707,6 +731,15 @@ func newFileBackendKeyringConfig(name, dir string, buf io.Reader) keyring.Config
}
}

// newRealPrompt creates a password prompt function to retrieve or create a passphrase
// for the keyring system.
//
// Parameters:
// - dir: The directory where the keyhash file is stored.
// - buf: An io.Reader input, typically used for reading user input (e.g., the passphrase).
//
// Returns:
// - A function that accepts a prompt string and returns the passphrase or an error.
func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) {
return func(prompt string) (string, error) {
keyhashStored := false
Expand Down Expand Up @@ -896,6 +929,7 @@ func (ks keystore) writeMultisigKey(name string, pk types.PubKey) (*Record, erro
return k, ks.writeRecord(k)
}

// MigrateAll migrates all legacy key information stored in the keystore to the new Record format.
func (ks keystore) MigrateAll() ([]*Record, error) {
keys, err := ks.db.Keys()
if err != nil {
Expand Down Expand Up @@ -1008,6 +1042,16 @@ func (ks keystore) SetItem(item keyring.Item) error {
return ks.db.Set(item)
}

// convertFromLegacyInfo converts a legacy account info (LegacyInfo) into a new Record format.
// It handles different types of legacy info and creates the corresponding Record based on the type.
//
// Parameters:
// - info: The legacy account information (LegacyInfo) that needs to be converted.
// It provides the name, public key, and other data depending on the type of account.

// Returns:
// - *Record: A pointer to the newly created Record that corresponds to the legacy account info.
// - error: An error if the conversion fails due to invalid info or an unsupported account type.
func (ks keystore) convertFromLegacyInfo(info LegacyInfo) (*Record, error) {
if info == nil {
return nil, errorsmod.Wrap(ErrLegacyToRecord, "info is nil")
Expand Down
7 changes: 7 additions & 0 deletions crypto/keyring/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ func extractPrivKeyFromRecord(k *Record) (cryptotypes.PrivKey, error) {
return extractPrivKeyFromLocal(rl)
}

// extractPrivKeyFromLocal extracts the private key from a local record.
// It checks if the private key is available in the provided Record_Local instance.
// Parameters:
// - rl: A pointer to a Record_Local instance from which the private key will be extracted.
// Returns:
// - priv: The extracted cryptotypes.PrivKey if successful.
// - error: An error if the private key is not available or if the casting fails.
func extractPrivKeyFromLocal(rl *Record_Local) (cryptotypes.PrivKey, error) {
if rl.PrivKey == nil {
return nil, ErrPrivKeyNotAvailable
Expand Down
Loading