github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/mnemonic/mnemonic.go (about) 1 /* 2 * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved. 3 * This software is released under GPL3. 4 * The full license information can be found under: 5 * https://www.gnu.org/licenses/gpl-3.0.en.html 6 * 7 */ 8 9 package mnemonic 10 11 import ( 12 "crypto/ecdsa" 13 14 "github.com/ethereum/go-ethereum/accounts" 15 "github.com/ethereum/go-ethereum/crypto" 16 "github.com/jbenet/go-base58" 17 "github.com/tyler-smith/go-bip32" 18 "github.com/tyler-smith/go-bip39" 19 ) 20 21 // DerivationPath for menemonic generated seed. 22 const DerivationPath = "m/0'/0" 23 24 // ToECDSA creates a private key from a BIP39 mnemonic. 25 // Provided mnemonic must not protected with any passphrase. 26 // The resulting private key is derived in accordance to DerivationPath. 27 func ToECDSA(mnemonic string) (privateKeyECDSA *ecdsa.PrivateKey, err error) { 28 seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "") 29 if err != nil { 30 return 31 } 32 33 masterKey, err := bip32.NewMasterKey(seed) 34 if err != nil { 35 return 36 } 37 38 paths, err := accounts.ParseDerivationPath(DerivationPath) 39 if err != nil { 40 return 41 } 42 key := masterKey 43 for _, p := range paths { 44 key, err = key.NewChildKey(p) 45 if err != nil { 46 return 47 } 48 } 49 50 decoded := base58.Decode(key.B58Serialize()) 51 privateKey := decoded[46:78] 52 return crypto.ToECDSA(privateKey) 53 }