-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from bcdevtools/imp/support-mnemonic-as-secret…
…-key imp: support mnemonic as account secret
- Loading branch information
Showing
5 changed files
with
140 additions
and
28 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package utils | ||
|
||
import ( | ||
"crypto/ecdsa" | ||
|
||
"github.com/btcsuite/btcd/btcutil/hdkeychain" | ||
"github.com/btcsuite/btcd/chaincfg" | ||
cosmoshd "github.com/cosmos/cosmos-sdk/crypto/hd" | ||
"github.com/cosmos/go-bip39" | ||
"github.com/ethereum/go-ethereum/accounts" | ||
) | ||
|
||
func FromMnemonicToPrivateKey(mnemonic, password string) (*ecdsa.PrivateKey, error) { | ||
hdPathStr := cosmoshd.CreateHDPath(60, 0, 0).String() | ||
hdPath, err := accounts.ParseDerivationPath(hdPathStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, password) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// create a BTC-utils hd-derivation key chain | ||
masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
key := masterKey | ||
for _, n := range hdPath { | ||
key, err = key.Derive(n) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
// btc-utils representation of a secp256k1 private key | ||
privateKey, err := key.ECPrivKey() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// cast private key to a convertible form (single scalar field element of secp256k1) | ||
// and then load into ethcrypto private key format. | ||
return privateKey.ToECDSA(), nil | ||
} |
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.