Skip to content

Instantly share code, notes, and snippets.

@jpfraneto
Created June 10, 2024 14:45
Show Gist options
  • Save jpfraneto/5d1ca8dc0ba3f97924a740d98a21725f to your computer and use it in GitHub Desktop.
Save jpfraneto/5d1ca8dc0ba3f97924a740d98a21725f to your computer and use it in GitHub Desktop.
package main
import (
"crypto/ecdsa"
"encoding/hex"
"fmt"
"log"
"github.com/ethereum/go-ethereum/crypto"
"github.com/tyler-smith/go-bip39"
"github.com/tyler-smith/go-bip32"
)
func main() {
// Replace this with your 24-word mnemonic
mnemonic := "YOUR_MNEMONIC_PHRASE_HERE"
// Generate seed from mnemonic
seed := bip39.NewSeed(mnemonic, "")
// Generate master key from seed
masterKey, err := bip32.NewMasterKey(seed)
if err != nil {
log.Fatalf("Failed to generate master key: %v", err)
}
// Derive the Ethereum key using the BIP44 path m/44'/60'/0'/0/0
purpose, err := masterKey.NewChildKey(bip32.FirstHardenedChild + 44)
if err != nil {
log.Fatalf("Failed to derive purpose key: %v", err)
}
coinType, err := purpose.NewChildKey(bip32.FirstHardenedChild + 60)
if err != nil {
log.Fatalf("Failed to derive coin type key: %v", err)
}
account, err := coinType.NewChildKey(bip32.FirstHardenedChild + 0)
if err != nil {
log.Fatalf("Failed to derive account key: %v", err)
}
change, err := account.NewChildKey(0)
if err != nil {
log.Fatalf("Failed to derive change key: %v", err)
}
addressIndex, err := change.NewChildKey(0)
if err != nil {
log.Fatalf("Failed to derive address index key: %v", err)
}
// Get the private key in ecdsa format
privateKey, err := crypto.ToECDSA(addressIndex.Key)
if err != nil {
log.Fatalf("Failed to convert to ECDSA: %v", err)
}
// Convert the private key to a hex string
privateKeyHex := hex.EncodeToString(crypto.FromECDSA(privateKey))
// Print the private key
fmt.Println("Private Key:", privateKeyHex)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment