-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(genutil): Use sdk types genesis validator (#21678)
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Matt Kocubinski <mkocubinski@gmail.com>
- Loading branch information
1 parent
1780d7d
commit d727f9b
Showing
19 changed files
with
459 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package codec | ||
|
||
import ( | ||
"cosmossdk.io/errors" | ||
|
||
cryptokeys "github.com/cosmos/cosmos-sdk/crypto/keys" | ||
bls12_381 "github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" | ||
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
) | ||
|
||
func PubKeyToProto(pk cryptokeys.JSONPubkey) (cryptotypes.PubKey, error) { | ||
switch pk.KeyType { | ||
case ed25519.PubKeyName: | ||
return &ed25519.PubKey{ | ||
Key: pk.Value, | ||
}, nil | ||
case secp256k1.PubKeyName: | ||
return &secp256k1.PubKey{ | ||
Key: pk.Value, | ||
}, nil | ||
case bls12_381.PubKeyName: | ||
return &bls12_381.PubKey{ | ||
Key: pk.Value, | ||
}, nil | ||
default: | ||
return nil, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to proto public key", pk) | ||
} | ||
} | ||
|
||
func PubKeyFromProto(pk cryptotypes.PubKey) (cryptokeys.JSONPubkey, error) { | ||
switch pk := pk.(type) { | ||
case *ed25519.PubKey: | ||
return cryptokeys.JSONPubkey{ | ||
KeyType: ed25519.PubKeyName, | ||
Value: pk.Bytes(), | ||
}, nil | ||
case *secp256k1.PubKey: | ||
return cryptokeys.JSONPubkey{ | ||
KeyType: secp256k1.PubKeyName, | ||
Value: pk.Bytes(), | ||
}, nil | ||
case *bls12_381.PubKey: | ||
return cryptokeys.JSONPubkey{ | ||
KeyType: bls12_381.PubKeyName, | ||
Value: pk.Bytes(), | ||
}, nil | ||
default: | ||
return cryptokeys.JSONPubkey{}, errors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v from proto public key", pk) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package keys | ||
|
||
import ( | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/bls12_381" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" | ||
"github.com/cosmos/cosmos-sdk/crypto/types" | ||
) | ||
|
||
// JSONPubKey defines a public key that are parse from JSON file. | ||
// convert PubKey to JSONPubKey needs a in between step | ||
type JSONPubkey struct { | ||
KeyType string `json:"type"` | ||
Value []byte `json:"value"` | ||
} | ||
|
||
func (pk JSONPubkey) Address() types.Address { | ||
switch pk.KeyType { | ||
case ed25519.PubKeyName: | ||
ed25519 := ed25519.PubKey{ | ||
Key: pk.Value, | ||
} | ||
return ed25519.Address() | ||
case secp256k1.PubKeyName: | ||
secp256k1 := secp256k1.PubKey{ | ||
Key: pk.Value, | ||
} | ||
return secp256k1.Address() | ||
case bls12_381.PubKeyName: | ||
bls12_381 := bls12_381.PubKey{ | ||
Key: pk.Value, | ||
} | ||
return bls12_381.Address() | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func (pk JSONPubkey) Bytes() []byte { | ||
return pk.Value | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.