This is a Go-based library that provides functionalities to create and manage hierarchical deterministic (HD) wallets. It supports both Bitcoin (mainnet and testnet) and Ethereum wallets, and allows the derivation of private keys, public keys, and addresses.
- Generate HD wallets using BIP32 and BIP39 standards.
- Support for multiple blockchain networks: Bitcoin (mainnet, testnet) and Ethereum.
- Derivation of keys and addresses based on the provided mnemonic and derivation path.
- Utility functions to return the private key, public key, and address in various formats.
- Validates the mnemonic according to the BIP39 standard.
To use this library, you can import it into your Go project. Make sure you have Go modules enabled.
go get github.com/ariden83/hdwallet.goThen, import it in your Go code:
import "github.com/ariden83/hdwallet.go"To create a new wallet, you need to provide a mnemonic phrase and the derivation path.
Here's an example of how to create a wallet for the Ethereum mainnet:
wallet := &hdwallet.Config{
Mnemonic: "your mnemonic phrase here",
Path: "m/44'/60'/0'/0", // Derivation path for Ethereum
Network: hdwallet.NetworkMainnet,
}
root, err := hdwallet.New(config)
if err != nil {
log.Fatal(err)
}To derive keys from a wallet, you can use the Derive function:
derivedWallet, err := wallet.Derive(0)
if err != nil {
log.Fatal(err)
}You can get the public key and the address (in Ethereum format) as follows:
publicKey := wallet.PublicKeyHex()
address := wallet.AddressHex()
fmt.Println("Public Key:", publicKey)
fmt.Println("Address:", address)The library can validate a Bitcoin address based on the network type (mainnet or testnet).
To retrieve the private key of the wallet, you can use the following method:
privateKey := wallet.PrivateKeyHex()
fmt.Println("Private Key:", privateKey)The Config struct allows you to specify the following fields when creating a wallet:
Mnemonic: The mnemonic phrase (required).Path: The derivation path (optional). If not provided, default paths for mainnet/testnet will be used.Network: Specifies the network (Bitcoin mainnet/testnet or Ethereum).
config := &hdwallet.Config{
Mnemonic: "mnemonic phrase here",
Path: "m/44'/60'/0'/0", // Ethereum path
Network: hdwallet.NetworkMainnet,
}The library defines several error messages for common issues, including:
ErrInvalidMnemonic: If the mnemonic phrase is missing or invalid.ErrUnsupportedNet: If an unsupported network type is provided.ErrInvalidPath: If the derivation path cannot be parsed.ErrKeyDerivation: If key derivation fails.
- Fork from HD Wallet of miguelmota
If you'd like to contribute to this project, feel free to submit a pull request or open an issue on GitHub.
This project is licensed under the MIT License.