Skip to content

Commit

Permalink
Merge pull request #25 from irisnet/cyl/fix-eth-account
Browse files Browse the repository at this point in the history
fix recover eth_secp256k1 key error
  • Loading branch information
Dreamer authored Mar 23, 2023
2 parents f12edca + 7e9d755 commit 68ed671
Show file tree
Hide file tree
Showing 11 changed files with 479 additions and 142 deletions.
5 changes: 5 additions & 0 deletions bank/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
(*auth.Account)(nil),
&auth.BaseAccount{},
)

registry.RegisterImplementations(
(*auth.Account)(nil),
&auth.EthAccount{},
)
}
3 changes: 2 additions & 1 deletion client/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ func (a AccountQuery) QueryAccount(address string) (sdk.BaseAccount, sdk.Error)
return sdk.BaseAccount{}, sdk.Wrap(err)
}

account := baseAccount.(*auth.BaseAccount).ConvertAccount(a.cdc).(sdk.BaseAccount)
a2 := baseAccount.(auth.BaseAccountI)
account := a2.ConvertAccount(a.cdc).(sdk.BaseAccount)

breq := &bank.QueryAllBalancesRequest{
Address: address,
Expand Down
5 changes: 4 additions & 1 deletion client/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"fmt"
ethsecp256k1 "github.com/irisnet/core-sdk-go/common/crypto/keys/eth_secp256k1"

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

Expand Down Expand Up @@ -178,6 +179,8 @@ func FromTmPubKey(Algo string, pubKey tmcrypto.PubKey) commoncryptotypes.PubKey
pubkey = &sm2.PubKey{Key: pubkeyBytes}
case "secp256k1":
pubkey = &secp256k1.PubKey{Key: pubkeyBytes}
case ethsecp256k1.KeyType:
pubkey = &ethsecp256k1.PubKey{Key: pubkeyBytes}
}
return pubkey
}
Expand All @@ -202,7 +205,7 @@ func NewKeysClient(cfg types.ClientConfig, keyManager types.KeyManager) Client {
if err != nil {
panic(err)
}
return keysClient{(*BIP44Params), keyManager}
return keysClient{*BIP44Params, keyManager}
}

func (k keysClient) Add(name, password string) (string, string, types.Error) {
Expand Down
4 changes: 4 additions & 0 deletions common/crypto/codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/irisnet/core-sdk-go/common/codec"
commoncrypto "github.com/irisnet/core-sdk-go/common/codec"
"github.com/irisnet/core-sdk-go/common/crypto/keys/ed25519"
ethsecp256k1 "github.com/irisnet/core-sdk-go/common/crypto/keys/eth_secp256k1"
"github.com/irisnet/core-sdk-go/common/crypto/keys/multisig"
"github.com/irisnet/core-sdk-go/common/crypto/keys/secp256k1"
"github.com/irisnet/core-sdk-go/common/crypto/keys/sm2"
Expand All @@ -20,6 +21,7 @@ func init() {
amino = commoncrypto.NewLegacyAmino()
RegisterCrypto(amino)
}

// RegisterCrypto registers all crypto dependency types with the provided Amino
// codec.
func RegisterCrypto(cdc *codec.LegacyAmino) {
Expand All @@ -33,6 +35,7 @@ func RegisterCrypto(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&ed25519.PubKey{}, ed25519.PubKeyName, nil)
cdc.RegisterConcrete(&secp256k1.PubKey{}, secp256k1.PubKeyName, nil)
cdc.RegisterConcrete(&sm2.PubKey{}, sm2.PubKeyName, nil)
cdc.RegisterConcrete(&ethsecp256k1.PubKey{}, ethsecp256k1.PubKeyName, nil)
cdc.RegisterConcrete(&multisig.LegacyAminoPubKey{}, multisig.PubKeyAminoRoute, nil)

//register private key
Expand All @@ -43,6 +46,7 @@ func RegisterCrypto(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&secp256k1.PrivKey{}, secp256k1.PrivKeyName, nil)
cdc.RegisterConcrete(tmsm2.PrivKeySm2{}, tmsm2.PrivKeyName, nil)
cdc.RegisterConcrete(&sm2.PrivKey{}, sm2.PrivKeyName, nil)
cdc.RegisterConcrete(&ethsecp256k1.PrivKey{}, ethsecp256k1.PrivKeyName, nil)

}

Expand Down
4 changes: 4 additions & 0 deletions common/crypto/codec/proto.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codec

import (
ethsecp256k1 "github.com/irisnet/core-sdk-go/common/crypto/keys/eth_secp256k1"
"github.com/irisnet/core-sdk-go/common/crypto/keys/sm2"
tmcrypto "github.com/tendermint/tendermint/crypto"

Expand All @@ -21,10 +22,13 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations((*tmcrypto.PubKey)(nil), &secp256k1.PubKey{})
registry.RegisterImplementations((*tmcrypto.PubKey)(nil), &sm2.PubKey{})
registry.RegisterImplementations((*tmcrypto.PubKey)(nil), &multisig.LegacyAminoPubKey{})
registry.RegisterImplementations((*tmcrypto.PubKey)(nil), &ethsecp256k1.PubKey{})

registry.RegisterInterface("cosmos.crypto.Pubkey", (*cryptotypes.PubKey)(nil))
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &ed25519.PubKey{})
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &secp256k1.PubKey{})
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &sm2.PubKey{})
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &multisig.LegacyAminoPubKey{})
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &ethsecp256k1.PubKey{})

}
25 changes: 23 additions & 2 deletions common/crypto/hd/hdpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
const (
BIP44Prefix = "44'/118'/"
PartialPath = "0'/0/0"
FullPath = BIP44Prefix + PartialPath
//FullPath = BIP44Prefix + PartialPath
FullPath = "m/" + BIP44Prefix + PartialPath
)

// BIP44Params wraps BIP 44 params (5 level BIP 32 path).
Expand Down Expand Up @@ -45,6 +46,19 @@ func NewParams(purpose, coinType, account uint32, change bool, addressIdx uint32
// Parse the BIP44 path and unmarshal into the struct.
func NewParamsFromPath(path string) (*BIP44Params, error) {
spl := strings.Split(path, "/")

// Handle absolute or relative paths
switch {
case spl[0] == path:
return nil, fmt.Errorf("path %s doesn't contain '/' separators", path)

case strings.TrimSpace(spl[0]) == "":
return nil, fmt.Errorf("ambiguous path %s: use 'm/' prefix for absolute paths, or no leading '/' for relative ones", path)

case strings.TrimSpace(spl[0]) == "m":
spl = spl[1:]
}

if len(spl) != 5 {
return nil, fmt.Errorf("path length is wrong. Expected 5, got %d", len(spl))
}
Expand Down Expand Up @@ -149,7 +163,7 @@ func (p BIP44Params) String() string {
changeStr = "0"
}
// m / Purpose' / coin_type' / Account' / Change / address_index
return fmt.Sprintf("%d'/%d'/%d'/%s/%d",
return fmt.Sprintf("m/%d'/%d'/%d'/%s/%d",
p.Purpose,
p.CoinType,
p.Account,
Expand All @@ -171,6 +185,13 @@ func DerivePrivateKeyForPath(privKeyBytes, chainCode [32]byte, path string) ([]b
data := privKeyBytes
parts := strings.Split(path, "/")

switch {
case parts[0] == path:
return nil, fmt.Errorf("path '%s' doesn't contain '/' separators", path)
case strings.TrimSpace(parts[0]) == "m":
parts = parts[1:]
}

for _, part := range parts {
// do we have an apostrophe?
harden := part[len(part)-1:] == "'"
Expand Down
15 changes: 1 addition & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ go 1.16

require (
github.com/99designs/keyring v1.1.6
github.com/DataDog/zstd v1.4.8 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/armon/go-metrics v0.3.9
github.com/avast/retry-go v3.0.0+incompatible
github.com/bluele/gcache v0.0.2
Expand All @@ -17,34 +15,24 @@ require (
github.com/ethereum/go-ethereum v1.10.16
github.com/gogo/protobuf v1.3.3
github.com/golang/protobuf v1.5.2
github.com/google/btree v1.0.1 // indirect
github.com/gorilla/websocket v1.4.2
github.com/magiconair/properties v1.8.5
github.com/mitchellh/go-homedir v1.1.0
github.com/mtibben/percent v0.2.1
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.17.0 // indirect
github.com/petermattis/goid v0.0.0-20220712135657-ac599d9cba15 // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.29.0
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
github.com/regen-network/cosmos-proto v0.3.1
github.com/rs/cors v1.8.0 // indirect
github.com/sasha-s/go-deadlock v0.2.1-0.20190427202633-1595213edefa // indirect
github.com/sirupsen/logrus v1.7.0
github.com/spf13/viper v1.9.0 // indirect
github.com/stretchr/testify v1.7.0
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15
github.com/tendermint/go-amino v0.16.0
github.com/tendermint/tendermint v0.34.14
github.com/tendermint/tm-db v0.6.4
github.com/tjfoc/gmsm v1.4.0
github.com/tklauser/go-sysconf v0.3.7 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/crypto v0.0.0-20211115234514-b4de73f9ece8
golang.org/x/net v0.0.0-20211111160137-58aab5ef257a // indirect
golang.org/x/sys v0.0.0-20211111213525-f221eed1c01e // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20211116182654-e63d96a377c4
google.golang.org/grpc v1.41.0
google.golang.org/protobuf v1.27.1
Expand All @@ -54,5 +42,4 @@ require (
replace (
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.2-alpha.regen.4
github.com/tendermint/tendermint => github.com/bianjieai/tendermint v0.34.1-irita-210113
github.com/tharsis/ethermint v0.8.1 => github.com/bianjieai/ethermint v0.8.2-0.20220211020007-9ec25dde74d4
)
Loading

0 comments on commit 68ed671

Please sign in to comment.