Skip to content

Commit

Permalink
Merge branch 'main' into marko/store_stateat
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Oct 16, 2024
2 parents fd3909c + abaccb4 commit 74de046
Show file tree
Hide file tree
Showing 125 changed files with 1,477 additions and 2,302 deletions.
4 changes: 2 additions & 2 deletions api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module cosmossdk.io/api
go 1.21

require (
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fedbb9acfd2f.1
github.com/cosmos/cosmos-proto v1.0.0-beta.5
github.com/cosmos/gogoproto v1.7.0
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142
Expand All @@ -12,7 +12,7 @@ require (
)

require (
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88ef6483f90f.2 // indirect
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions api/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2 h1:90/4O5QkHb8EZdA2SAhueRzYw6u5ZHCPKtReFqshnTY=
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2/go.mod h1:1+3gJj2NvZ1mTLAtHu+lMhOjGgQPiCKCeo+9MBww0Eo=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88ef6483f90f.2 h1:b7EEYTUHmWSBEyISHlHvXbJPqtKiHRuUignL1tsHnNQ=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88ef6483f90f.2/go.mod h1:HqcXMSa5qnNuakaMUo+hWhF51mKbcrZxGl9Vp5EeJXc=
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fedbb9acfd2f.1 h1:DIUSA9vcIz63uUotWfbXXlwv1iTL+C0O2kEMLsnIIbc=
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fedbb9acfd2f.1/go.mod h1:JTBMfyi+qAXUHumX+rcD2WIq9FNWmdcNh5MjBnSw0L0=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1 h1:F78ecjvMtgd1aZ1Aj9cvBjURxVGCYvRM+OOy5eR+pjw=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1/go.mod h1:zqi/LZjZhyvjCMTEVIwAf5VRlkLduuCfqmZxgoormq0=
github.com/cosmos/cosmos-proto v1.0.0-beta.5 h1:eNcayDLpip+zVLRLYafhzLvQlSmyab+RC5W7ZfmxJLA=
github.com/cosmos/cosmos-proto v1.0.0-beta.5/go.mod h1:hQGLpiIUloJBMdQMMWb/4wRApmI9hjHH05nefC0Ojec=
github.com/cosmos/gogoproto v1.7.0 h1:79USr0oyXAbxg3rspGh/m4SWNyoz/GLaAh0QlCe2fro=
Expand Down
24 changes: 22 additions & 2 deletions client/account_retriever.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ type Account interface {
GetSequence() uint64
}

type mockAccount struct {
addr []byte
}

func (m mockAccount) GetAddress() sdk.AccAddress {
return m.addr
}

func (m mockAccount) GetPubKey() cryptotypes.PubKey {
return nil
}

func (m mockAccount) GetAccountNumber() uint64 {
return 0
}

func (m mockAccount) GetSequence() uint64 {
return 0
}

// AccountRetriever defines the interfaces required by transactions to
// ensure an account exists and to be able to query for account fields necessary
// for signing.
Expand All @@ -32,8 +52,8 @@ type MockAccountRetriever struct {
ReturnAccNum, ReturnAccSeq uint64
}

func (mar MockAccountRetriever) GetAccount(_ Context, _ sdk.AccAddress) (Account, error) {
return nil, nil
func (mar MockAccountRetriever) GetAccount(_ Context, address sdk.AccAddress) (Account, error) {
return mockAccount{addr: address}, nil
}

func (mar MockAccountRetriever) GetAccountWithHeight(_ Context, _ sdk.AccAddress) (Account, int64, error) {
Expand Down
6 changes: 3 additions & 3 deletions client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ require (
)

require (
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2 // indirect
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88ef6483f90f.2 // indirect
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fedbb9acfd2f.1 // indirect
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1 // indirect
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29 // indirect
cosmossdk.io/errors v1.0.1 // indirect
Expand Down Expand Up @@ -125,7 +125,7 @@ require (
github.com/petermattis/goid v0.0.0-20240813172612-4fcff4a6cae7 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.20.4 // indirect
github.com/prometheus/client_golang v1.20.5 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.60.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
Expand Down
12 changes: 6 additions & 6 deletions client/v2/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2 h1:90/4O5QkHb8EZdA2SAhueRzYw6u5ZHCPKtReFqshnTY=
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.34.2-20240701160653-fedbb9acfd2f.2/go.mod h1:1+3gJj2NvZ1mTLAtHu+lMhOjGgQPiCKCeo+9MBww0Eo=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88ef6483f90f.2 h1:b7EEYTUHmWSBEyISHlHvXbJPqtKiHRuUignL1tsHnNQ=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88ef6483f90f.2/go.mod h1:HqcXMSa5qnNuakaMUo+hWhF51mKbcrZxGl9Vp5EeJXc=
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fedbb9acfd2f.1 h1:DIUSA9vcIz63uUotWfbXXlwv1iTL+C0O2kEMLsnIIbc=
buf.build/gen/go/cometbft/cometbft/protocolbuffers/go v1.35.1-20240701160653-fedbb9acfd2f.1/go.mod h1:JTBMfyi+qAXUHumX+rcD2WIq9FNWmdcNh5MjBnSw0L0=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1 h1:F78ecjvMtgd1aZ1Aj9cvBjURxVGCYvRM+OOy5eR+pjw=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.35.1-20240130113600-88ef6483f90f.1/go.mod h1:zqi/LZjZhyvjCMTEVIwAf5VRlkLduuCfqmZxgoormq0=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cosmossdk.io/collections v0.4.0 h1:PFmwj2W8szgpD5nOd8GWH6AbYNi1f2J6akWXJ7P5t9s=
Expand Down Expand Up @@ -414,8 +414,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo=
github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3OK1iX/F2sw+iXX5zU=
github.com/prometheus/client_golang v1.20.4 h1:Tgh3Yr67PaOv/uTqloMsCEdeuFTatm5zIq5+qNN23vI=
github.com/prometheus/client_golang v1.20.4/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand Down
3 changes: 0 additions & 3 deletions collections/quad.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ type quadKeyCodec[K1, K2, K3, K4 any] struct {

type jsonQuadKey [4]json.RawMessage


// EncodeJSON encodes Quads to json
func (t quadKeyCodec[K1, K2, K3, K4]) EncodeJSON(value Quad[K1, K2, K3, K4]) ([]byte, error) {
json1, err := t.keyCodec1.EncodeJSON(*value.k1)
Expand Down Expand Up @@ -210,7 +209,6 @@ func (t quadKeyCodec[K1, K2, K3, K4]) KeyType() string {
return fmt.Sprintf("Quad[%s,%s,%s,%s]", t.keyCodec1.KeyType(), t.keyCodec2.KeyType(), t.keyCodec3.KeyType(), t.keyCodec4.KeyType())
}


func (t quadKeyCodec[K1, K2, K3, K4]) Encode(buffer []byte, key Quad[K1, K2, K3, K4]) (int, error) {
writtenTotal := 0
if key.k1 != nil {
Expand Down Expand Up @@ -244,7 +242,6 @@ func (t quadKeyCodec[K1, K2, K3, K4]) Encode(buffer []byte, key Quad[K1, K2, K3,
return writtenTotal, nil
}


func (t quadKeyCodec[K1, K2, K3, K4]) Decode(buffer []byte) (int, Quad[K1, K2, K3, K4], error) {
readTotal := 0
read, key1, err := t.keyCodec1.DecodeNonTerminal(buffer)
Expand Down
2 changes: 1 addition & 1 deletion core/store/store.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package store

// KVStore describes the basic interface for interacting with key-value stores.
type KVStore interface {
type KVStore = interface {
// Get returns nil iff key doesn't exist. Errors on nil key.
Get(key []byte) ([]byte, error)

Expand Down
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[:]

}

data = append(data, uint32ToBytes(index)...)
Expand Down
7 changes: 6 additions & 1 deletion crypto/keyring/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type autoCLIKeyring interface {
KeyInfo(name string) (string, string, uint, error)
}

// NewAutoCLIKeyring wraps the SDK keyring and make it compatible with the AutoCLI keyring interfaces.
// NewAutoCLIKeyring wraps the SDK keyring and makes it compatible with the AutoCLI keyring interfaces.
func NewAutoCLIKeyring(kr Keyring, ac address.Codec) (autoCLIKeyring, error) {
return &autoCLIKeyringAdapter{kr, ac}, nil
}
Expand All @@ -40,6 +40,7 @@ type autoCLIKeyringAdapter struct {
ac address.Codec
}

// List returns the names of all keys stored in the keyring.
func (a *autoCLIKeyringAdapter) List() ([]string, error) {
list, err := a.Keyring.List()
if err != nil {
Expand Down Expand Up @@ -69,6 +70,7 @@ func (a *autoCLIKeyringAdapter) LookupAddressByKeyName(name string) ([]byte, err
return addr, nil
}

// GetPubKey returns the public key of the key with the given name.
func (a *autoCLIKeyringAdapter) GetPubKey(name string) (cryptotypes.PubKey, error) {
record, err := a.Keyring.Key(name)
if err != nil {
Expand All @@ -78,6 +80,7 @@ func (a *autoCLIKeyringAdapter) GetPubKey(name string) (cryptotypes.PubKey, erro
return record.GetPubKey()
}

// Sign signs the given bytes with the key with the given name.
func (a *autoCLIKeyringAdapter) Sign(name string, msg []byte, signMode signingv1beta1.SignMode) ([]byte, error) {
record, err := a.Keyring.Key(name)
if err != nil {
Expand All @@ -93,6 +96,7 @@ func (a *autoCLIKeyringAdapter) Sign(name string, msg []byte, signMode signingv1
return signBytes, err
}

// KeyType returns the type of the key with the given name.
func (a *autoCLIKeyringAdapter) KeyType(name string) (uint, error) {
record, err := a.Keyring.Key(name)
if err != nil {
Expand All @@ -102,6 +106,7 @@ func (a *autoCLIKeyringAdapter) KeyType(name string) (uint, error) {
return uint(record.GetType()), nil
}

// KeyInfo returns key name, key address, and key type given a key name or address.
func (a *autoCLIKeyringAdapter) KeyInfo(nameOrAddr string) (string, string, uint, error) {
addr, err := a.ac.StringToBytes(nameOrAddr)
if err != nil {
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

0 comments on commit 74de046

Please sign in to comment.