Skip to content

Commit

Permalink
Upgrade cosmos-sdk (#9)
Browse files Browse the repository at this point in the history
* cherry pick 2ea9cf4

* move sm2 from tendermint to sdk

* fix bug

* register sm2

* fix bug

* fix test error

* fix bug

* fix bug

* cherry-pick from cosmos#7817

* update tendermint version

Co-authored-by: chengwenxi <vincent.ch.cn@gmail.com>
  • Loading branch information
Zhiqiang Zhang and chengwenxi committed Nov 19, 2020
1 parent 5903586 commit d65ec00
Show file tree
Hide file tree
Showing 32 changed files with 1,109 additions and 165 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=sim \
-X github.com/cosmos/cosmos-sdk/version.AppName=simd \
-X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \
-X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \
-X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags_comma_sep)"
-X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags_comma_sep)" \
-X github.com/tendermint/tendermint/crypto/algo.Algo=sm2

# DB backend selection
ifeq (cleveldb,$(findstring cleveldb,$(COSMOS_BUILD_OPTIONS)))
Expand Down
32 changes: 13 additions & 19 deletions client/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (

"github.com/spf13/cobra"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/algo"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/version"
)

Expand All @@ -37,16 +36,12 @@ func Cmd() *cobra.Command {
func getPubKeyFromString(pkstr string) (crypto.PubKey, error) {
bz, err := hex.DecodeString(pkstr)
if err == nil {
if len(bz) == ed25519.PubKeySize {
return &ed25519.PubKey{Key: bz}, nil
}
return algo.GetPubKeyFromData(algo.Algo, bz), nil
}

bz, err = base64.StdEncoding.DecodeString(pkstr)
if err == nil {
if len(bz) == ed25519.PubKeySize {
return &ed25519.PubKey{Key: bz}, nil
}
return algo.GetPubKeyFromData(algo.Algo, bz), nil
}

pk, err := sdk.GetPubKeyFromBech32(sdk.Bech32PubKeyTypeAccPub, pkstr)
Expand All @@ -64,13 +59,13 @@ func getPubKeyFromString(pkstr string) (crypto.PubKey, error) {
return pk, nil
}

return nil, fmt.Errorf("pubkey '%s' invalid; expected hex, base64, or bech32 of correct size", pkstr)
return nil, fmt.Errorf("pubkey '%s' invalid; expected hex, base64, or bech32", pkstr)
}

func PubkeyCmd() *cobra.Command {
return &cobra.Command{
Use: "pubkey [pubkey]",
Short: "Decode a ED25519 pubkey from hex, base64, or bech32",
Short: "Decode a ED25519 or Sm2 pubkey from hex, base64, or bech32",
Long: fmt.Sprintf(`Decode a pubkey from hex, base64, or bech32.
Example:
Expand All @@ -86,30 +81,29 @@ $ %s debug pubkey cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
return err
}

edPK, ok := pk.(*ed25519.PubKey)
if !ok {
return errors.Wrapf(errors.ErrInvalidType, "invalid pubkey type; expected ED25519")
if !algo.VerifyPubKeyType(pk) {
return fmt.Errorf("invalid pubkey type; expected %s", algo.Algo)
}

pubKeyJSONBytes, err := clientCtx.LegacyAmino.MarshalJSON(edPK)
pubKeyJSONBytes, err := clientCtx.LegacyAmino.MarshalJSON(pk)
if err != nil {
return err
}
accPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, edPK)
accPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk)
if err != nil {
return err
}
valPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeValPub, edPK)
valPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeValPub, pk)
if err != nil {
return err
}
consenusPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, edPK)
consenusPub, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, pk)
if err != nil {
return err
}

cmd.Println("Address:", edPK.Address())
cmd.Printf("Hex: %X\n", edPK.Key)
cmd.Println("Address:", pk.Address())
cmd.Printf("Hex: %X\n", pk)
cmd.Println("JSON (base64):", string(pubKeyJSONBytes))
cmd.Println("Bech32 Acc:", accPub)
cmd.Println("Bech32 Validator Operator:", valPub)
Expand Down
6 changes: 3 additions & 3 deletions client/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const (
// failures due to state changes that might occur between the tx simulation
// and the actual run.
DefaultGasAdjustment = 1.0
DefaultGasLimit = 200000
DefaultGasLimit = 400000
GasFlagAuto = "auto"

// DefaultKeyringBackend
Expand Down Expand Up @@ -90,8 +90,8 @@ func AddTxFlagsToCmd(cmd *cobra.Command) {
cmd.Flags().Uint64P(FlagAccountNumber, "a", 0, "The account number of the signing account (offline mode only)")
cmd.Flags().Uint64P(FlagSequence, "s", 0, "The sequence number of the signing account (offline mode only)")
cmd.Flags().String(FlagMemo, "", "Memo to send along with transaction")
cmd.Flags().String(FlagFees, "", "Fees to pay along with transaction; eg: 10uatom")
cmd.Flags().String(FlagGasPrices, "", "Gas prices in decimal format to determine the transaction fee (e.g. 0.1uatom)")
cmd.Flags().String(FlagFees, "", "Fees to pay along with transaction; eg: 10point")
cmd.Flags().String(FlagGasPrices, "", "Gas prices in decimal format to determine the transaction fee (e.g. 0.1point)")
cmd.Flags().String(FlagNode, "tcp://localhost:26657", "<host>:<port> to tendermint rpc interface for this chain")
cmd.Flags().Bool(FlagUseLedger, false, "Use a connected Ledger device")
cmd.Flags().Float64(FlagGasAdjustment, DefaultGasAdjustment, "adjustment factor to be multiplied against the estimate returned by the tx simulation; if the gas limit is set manually this flag is ignored ")
Expand Down
2 changes: 1 addition & 1 deletion client/keys/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ the flag --nosort is set.
cmd.Flags().Uint32(flagCoinType, sdk.GetConfig().GetCoinType(), "coin type number for HD derivation")
cmd.Flags().Uint32(flagAccount, 0, "Account number for HD derivation")
cmd.Flags().Uint32(flagIndex, 0, "Address index number for HD derivation")
cmd.Flags().String(flags.FlagKeyAlgorithm, string(hd.Secp256k1Type), "Key signing algorithm to generate keys for")
cmd.Flags().String(flags.FlagKeyAlgorithm, string(hd.Sm2Type), "Key signing algorithm to generate keys for")

cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.ErrOrStderr())
Expand Down
10 changes: 10 additions & 0 deletions crypto/codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package codec
import (
"github.com/tendermint/tendermint/crypto"
tmed25519 "github.com/tendermint/tendermint/crypto/ed25519"
tmsm2 "github.com/tendermint/tendermint/crypto/sm2"
"github.com/tendermint/tendermint/crypto/sr25519"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
kmultisig "github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/keys/sm2"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
)

Expand Down Expand Up @@ -36,6 +38,10 @@ func RegisterCrypto(cdc *codec.LegacyAmino) {
ed25519.PubKeyName, nil)
cdc.RegisterConcrete(&secp256k1.PubKey{},
secp256k1.PubKeyName, nil)
cdc.RegisterConcrete(tmsm2.PubKeySm2{},
tmsm2.PubKeyName, nil)
cdc.RegisterConcrete(&sm2.PubKey{},
sm2.PubKeyName, nil)
cdc.RegisterConcrete(&kmultisig.LegacyAminoPubKey{},
kmultisig.PubKeyAminoRoute, nil)

Expand All @@ -49,6 +55,10 @@ func RegisterCrypto(cdc *codec.LegacyAmino) {
ed25519.PrivKeyName, nil)
cdc.RegisterConcrete(&secp256k1.PrivKey{},
secp256k1.PrivKeyName, nil)
cdc.RegisterConcrete(sm2.PrivKey{},
sm2.PrivKeyName, nil)
cdc.RegisterConcrete(tmsm2.PrivKeySm2{},
tmsm2.PrivKeyName, nil)
}

// PrivKeyFromBytes unmarshals private key bytes and returns a PrivKey
Expand Down
10 changes: 6 additions & 4 deletions crypto/codec/proto.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package codec

import (
tmcrypto "github.com/tendermint/tendermint/crypto"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/keys/sm2"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
tmcrypto "github.com/tendermint/tendermint/crypto"
)

// RegisterInterfaces registers the sdk.Tx interface.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
// TODO We now register both Tendermint's PubKey and our own PubKey. In the
// long-term, we should move away from Tendermint's PubKey, and delete
// these lines.
registry.RegisterInterface("tendermint.crypto.PubKey", (*tmcrypto.PubKey)(nil))
registry.RegisterInterface("tendermint.crypto.Pubkey", (*tmcrypto.PubKey)(nil))
registry.RegisterImplementations((*tmcrypto.PubKey)(nil), &sm2.PubKey{})
registry.RegisterImplementations((*tmcrypto.PubKey)(nil), &ed25519.PubKey{})
registry.RegisterImplementations((*tmcrypto.PubKey)(nil), &secp256k1.PubKey{})
registry.RegisterImplementations((*tmcrypto.PubKey)(nil), &multisig.LegacyAminoPubKey{})

registry.RegisterInterface("cosmos.crypto.PubKey", (*cryptotypes.PubKey)(nil))
registry.RegisterInterface("cosmos.crypto.Pubkey", (*cryptotypes.PubKey)(nil))
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &sm2.PubKey{})
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &ed25519.PubKey{})
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &secp256k1.PubKey{})
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &multisig.LegacyAminoPubKey{})
Expand Down
40 changes: 39 additions & 1 deletion crypto/hd/algo.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package hd

import (
bip39 "github.com/cosmos/go-bip39"
"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/keys/sm2"
bip39 "github.com/cosmos/go-bip39"
)

// PubKeyType defines an algorithm to derive key-pairs which can be used for cryptographic signing.
Expand All @@ -20,11 +21,15 @@ const (
Ed25519Type = PubKeyType("ed25519")
// Sr25519Type represents the Sr25519Type signature system.
Sr25519Type = PubKeyType("sr25519")

// Sm2Type represents the Sm2Type signature system.
Sm2Type = PubKeyType("sm2")
)

var (
// Secp256k1 uses the Bitcoin secp256k1 ECDSA parameters.
Secp256k1 = secp256k1Algo{}
Sm2 = sm2Algo{}
)

type DeriveFn func(mnemonic string, bip39Passphrase, hdPath string) ([]byte, error)
Expand Down Expand Up @@ -69,3 +74,36 @@ func (s secp256k1Algo) Generate() GenerateFn {
return &secp256k1.PrivKey{Key: bzArr}
}
}

type sm2Algo struct {
}

func (s sm2Algo) Name() PubKeyType {
return Sm2Type
}

// Derive derives and returns the secp256k1 private key for the given seed and HD path.
func (s sm2Algo) Derive() DeriveFn {
return func(mnemonic string, bip39Passphrase, hdPath string) ([]byte, error) {
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, bip39Passphrase)
if err != nil {
return nil, err
}

masterPriv, ch := ComputeMastersFromSeed(seed)
if len(hdPath) == 0 {
return masterPriv[:], nil
}
derivedKey, err := DerivePrivateKeyForPath(masterPriv, ch, hdPath)
return derivedKey[:], err
}
}

// Generate generates a sm2 private key from the given bytes.
func (s sm2Algo) Generate() GenerateFn {
return func(bz []byte) crypto.PrivKey {
var bzArr [sm2.PrivKeySize]byte
copy(bzArr[:], bz)
return &sm2.PrivKey{Key: bzArr[:]}
}
}
2 changes: 1 addition & 1 deletion crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ type keystore struct {
func newKeystore(kr keyring.Keyring, opts ...Option) keystore {
// Default options for keybase
options := Options{
SupportedAlgos: SigningAlgoList{hd.Secp256k1},
SupportedAlgos: SigningAlgoList{hd.Secp256k1, hd.Sm2},
SupportedAlgosLedger: SigningAlgoList{hd.Secp256k1},
}

Expand Down
22 changes: 22 additions & 0 deletions crypto/keys/algo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package keys

import (
"fmt"

"github.com/tendermint/tendermint/crypto"
tmed25519 "github.com/tendermint/tendermint/crypto/ed25519"
tmsm2 "github.com/tendermint/tendermint/crypto/sm2"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/sm2"
)

func FromTmPubKey(pubkey crypto.PubKey) (crypto.PubKey, error) {
switch pubkey.(type) {
case tmed25519.PubKey:
return &ed25519.PubKey{Key: pubkey.Bytes()}, nil
case tmsm2.PubKeySm2:
return &sm2.PubKey{Key: pubkey.Bytes()}, nil
}
return nil, fmt.Errorf("not support pubkey type: %s", pubkey.Type())
}
Loading

0 comments on commit d65ec00

Please sign in to comment.