Skip to content

Commit

Permalink
fixing address derivation
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell committed Aug 18, 2023
1 parent 6401056 commit 9329587
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
41 changes: 27 additions & 14 deletions handlers/wallet_handler/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/std"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

ecies "github.com/ecies/go/v2"
Expand All @@ -38,7 +38,7 @@ type WalletHandler struct {
clientCtx client.Context
address string
flags *pflag.FlagSet
key *cryptotypes.PrivKey
key types.PrivKey
eciesKey *ecies.PrivateKey
}

Expand Down Expand Up @@ -72,17 +72,30 @@ func NewWalletHandler(seedPhrase string, rpc string, chainId string) (*WalletHan
// cfg.SetAddressVerifier(wasmtypes.VerifyAddressLen())
cfg.Seal()

var pKey *cryptotypes.PrivKey = nil
address := ""
if len(seedPhrase) > 0 {
pKey = cryptotypes.GenPrivKeyFromSecret([]byte(seedPhrase))
var err error
address, err = bech32.ConvertAndEncode(Bech32PrefixAccAddr, pKey.PubKey().Address().Bytes())
if err != nil {
return nil, err
}
hdPath := hd.CreateHDPath(118, 0, 0).String()
// create master key and derive first key for keyring
derivedPriv, err := hd.Secp256k1.Derive()(seedPhrase, "", hdPath)
if err != nil {
return nil, err
}

pKey := hd.Secp256k1.Generate()(derivedPriv)

// check if the key already exists with the same address and return an error
// if found
address := sdk.AccAddress(pKey.PubKey().Address())

//var pKey *cryptotypes.PrivKey = nil
//address := ""
//if len(seedPhrase) > 0 {
// pKey = cryptotypes.GenPrivKeyFromSecret([]byte(seedPhrase))
// var err error
// address, err = bech32.ConvertAndEncode(Bech32PrefixAccAddr, pKey.PubKey().Address().Bytes())
// if err != nil {
// return nil, err
// }
//}

cl, err := client.NewClientFromNode(rpc)
if err != nil {
return nil, err
Expand All @@ -108,13 +121,13 @@ func NewWalletHandler(seedPhrase string, rpc string, chainId string) (*WalletHan

eciesKey := ecies.NewPrivateKeyFromBytes(newpkey[:32])

flags := createFlags("auto", address)
flags := createFlags("auto", address.String())

w := WalletHandler{
clientCtx: clientCtx,
flags: flags,
key: pKey,
address: address,
address: address.String(),
eciesKey: eciesKey,
}

Expand Down
21 changes: 21 additions & 0 deletions handlers/wallet_handler/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package wallet_handler_test

import (
"testing"

"github.com/JackalLabs/jackalgo/handlers/wallet_handler"
"github.com/stretchr/testify/require"
)

func TestNewWallet(t *testing.T) {
r := require.New(t)

seed := "slim odor fiscal swallow piece tide naive river inform shell dune crunch canyon ten time universe orchard roast horn ritual siren cactus upon forum"

wallet, err := wallet_handler.NewWalletHandler(seed, "https://testnet.jackalprotocol.com:443", "lupulella-2")
r.NoError(err)

address := "jkl15cwg7teruwldgelxdg96g4cqxhsar7ye3zhv9f"

r.Equal(wallet.GetAddress(), address)
}

0 comments on commit 9329587

Please sign in to comment.