Getting Address and Private Key for Mnemonic – Go

by
Maya Patel
django ethereum go-ethereum

The Problem:

Given a mnemonic or seed phrase in Golang, how do I generate the corresponding Ethereum account’s public and private keys using the derivation path m/44'/60'/0'/0/0? I have the master key, but I’m unsure how to derive the child keys.

The Solutions:

Solution 1: Deriving Public and Private Keys from Master Key

In the given code, you have successfully generated the master public and private keys using the NewMasterKey function from the go-bip32 library.

To derive the public and private keys for a specific account, follow these steps:

  1. Create Child Keys: Using the master key, you can generate child keys for different accounts using the NewChildKey function. These child keys will be used to derive the public and private keys.

  2. BIP44 Derivation Path: To derive child keys according to the BIP44 standard, you can use the following path: m/44'/60'/0'/0/0. This path specifies the purpose, coin type, account, change, and address index.

  3. Deriving Account Keys: For each account, you need to derive the following keys:

    • Purpose Key: m/44'
    • Coin Type Key: m/44'/60'
    • Account Key: m/44'/60'/0'
    • Change Key: m/44'/60'/0'/0
    • Address Key: m/44'/60'/0'/0/0
  4. Extracting Public Key: Once you have derived the address key, you can extract the public key by calling the PublicKey method on the address key.

  5. Converting to ECDSA Keys: To convert the derived public key into an ECDSA public key, which is the standard representation of public keys in Ethereum, you can use the crypto.FromECDSAPub function from the github.com/ethereum/go-ethereum/crypto package.

  6. Extracting Private Key: Similarly, you can use the Key method on the address key to extract the private key. To convert it to an ECDSA private key, use the crypto.ToECDSA function.

By following these steps, you can derive public and private keys for specific accounts from the master key. These keys can then be used for signing transactions and other cryptographic operations.

Solution 2: Using go-ethereum Library

To efficiently derive public and private keys from a mnemonic phrase for Ethereum accounts using Go, you can follow these steps:

  1. Install the go-ethereum Library: Run
go get github.com/ethereum/go-ethereum/crypto

in your terminal.

  1. Create a New HD Wallet: Initialize an HD Wallet instance.
package main

import (
	"fmt"
	"github.com/ethereum/go-ethereum/accounts/hd"
)

func main() {
	// Seed phrase
	seedPhrase := "YOUR_SEED_PHRASE"

	// Create HD Wallet
	hdWallet, err := hd.NewHDKeyFromMnemonic(seedPhrase)
	if err != nil {
		fmt.Println("Error creating HD Wallet:", err)
		return
	}

}
  1. Derive the Account: Use the DerivePath() method to derive the account at the specified index.
// Derivation path for first account (m/44'/60'/0'/0/0)
derivationPath := hd.MustParseDerivationPath("m/44'/60'/0'/0/0")
key, err := hdWallet.Derive(derivationPath)
if err != nil {
	fmt.Println("Error deriving account:", err)
	return
}
  1. Extract the Public and Private Keys: Obtain the private and public keys from the derived key.
privateKey := key.PrivateKey
publicKey := key.PublicKey

// Convert private key to hex string
privateKeyHex := hex.EncodeToString(privateKey.D.Bytes())
// Convert public key to hex string
publicKeyHex := hex.EncodeToString(publicKey.X.Bytes())
  1. Display the Keys: Print the derived private and public keys.
fmt.Println("Private Key:", privateKeyHex)
fmt.Println("Public Key:", publicKeyHex)
}

Run the program to retrieve the public and private keys for your Ethereum account. Remember to replace YOUR_SEED_PHRASE with your actual seed phrase and modify the derivation path if needed.

Q&A

How to convert a generated master private and public keys to ECDSA hex string (account keys)?

Use crypto.ToECDSA to convert the generated master private and public keys.

How to check whether the generated public key as hex string belongs to the private key hex string?

Decode both keys from hex strings and compare the derived public key with the given public key.

Video Explanation:

The following video, titled "Crypto Key Management and Hardware Wallets: Bonus Livestream ...", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... mnemonic seed phrase. Crypto steel backup. 51:30 - What to use, what is best ... 1:26:45 - Can somebody ever generate the same private key as me?