I need a library in golang to implement HD Wallet.
2 Answers
You can use the package github.com/miguelmota/go-ethereum-hdwallet to implement an HD wallet (which implements the accounts.Wallet
interface from go-ethereum).
Here's a basic example to get started:
import (
"fmt"
"log"
"github.com/miguelmota/go-ethereum-hdwallet"
"github.com/tyler-smith/go-bip39"
)
func main() {
entropy, err := bip39.NewEntropy(128)
if err != nil {
log.Fatal(err)
}
mnemonic, _ := bip39.NewMnemonic(entropy)
seed := bip39.NewSeed(mnemonic, "")
wallet, err := hdwallet.NewFromSeed(seed)
if err != nil {
log.Fatal(err)
}
path := hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/0")
account, err := wallet.Derive(path, false)
if err != nil {
log.Fatal(err)
}
fmt.Println(account.Address.Hex()) // 0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947
path = hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/1")
account, err = wallet.Derive(path, false)
if err != nil {
log.Fatal(err)
}
fmt.Println(account.Address.Hex()) // 0x8230645aC28A4EdD1b0B53E7Cd8019744E9dD559
}
Disclosure: I am author of library.
-
btw, using mnemonic to generate random numbers for private keys is a good way to lose your money. Check this: medium.com/@touchdownFW/…– NulikCommented Jul 8, 2018 at 2:04
-
@Nulik not sure what you mean. Common wallet's such as MetaMask and Ledger Nano's use mnemonics as the seed..– MiguelCommented Jul 8, 2018 at 3:18
-
It doesn't matter what others do, may people cross the street on red light or drive being drunk. In encryption any decrease in randomness is dangerous and should be avoided. The article describes how many Bitcoin accounts (created with mnemonics) were hacked.– NulikCommented Jul 8, 2018 at 16:55
-
1@Nulik "It doesn't matter what others do" - I hope that was sarcasm. The article mentions brain wallets which is a phrase completely created by the user and because of that brain wallets are a bad idea. However a BIP39 mnemonic is not a brain wallet and is generated with 128-256 bits of entropy.– MiguelCommented Jul 8, 2018 at 21:10
You can use the standard GETH accounts package https://godoc.org/github.com/ethereum/go-ethereum/accounts