Documentation ¶
Index ¶
- Constants
- Variables
- func MustParseDerivationPath(path string) accounts.DerivationPath
- func NewMnemonic(bits int) (string, error)
- func NewSeed() ([]byte, error)
- func NewSeedFromMnemonic(mnemonic string, passphrase string) ([]byte, error)
- func ParseDerivationPath(path string) (accounts.DerivationPath, error)
- type Wallet
- func (w *Wallet) Accounts() []accounts.Account
- func (w *Wallet) Address(account accounts.Account) (common.Address, error)
- func (w *Wallet) AddressBytes(account accounts.Account) ([]byte, error)
- func (w *Wallet) AddressHex(account accounts.Account) (string, error)
- func (w *Wallet) Close() error
- func (w *Wallet) Contains(account accounts.Account) bool
- func (w *Wallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error)
- func (w *Wallet) MasterKey() *hdkeychain.ExtendedKey
- func (w *Wallet) Open(passphrase string) error
- func (w *Wallet) Path(account accounts.Account) (string, error)
- func (w *Wallet) PrivateKey(account accounts.Account) (*ecdsa.PrivateKey, error)
- func (w *Wallet) PrivateKeyBytes(account accounts.Account) ([]byte, error)
- func (w *Wallet) PrivateKeyHex(account accounts.Account) (string, error)
- func (w *Wallet) PublicKey(account accounts.Account) (*ecdsa.PublicKey, error)
- func (w *Wallet) PublicKeyBytes(account accounts.Account) ([]byte, error)
- func (w *Wallet) PublicKeyHex(account accounts.Account) (string, error)
- func (w *Wallet) SelfDerive(base []accounts.DerivationPath, chain ethereum.ChainStateReader)
- func (w *Wallet) SignData(account accounts.Account, mimeType string, data []byte) ([]byte, error)
- func (w *Wallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error)
- func (w *Wallet) SignHash(account accounts.Account, hash []byte) ([]byte, error)
- func (w *Wallet) SignHashWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error)
- func (w *Wallet) SignText(account accounts.Account, text []byte) ([]byte, error)
- func (w *Wallet) SignTextWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error)
- func (w *Wallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
- func (w *Wallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, ...) (*types.Transaction, error)
- func (w *Wallet) Status() (string, error)
- func (w *Wallet) URL() accounts.URL
- func (w *Wallet) Unpin(account accounts.Account) error
Examples ¶
Constants ¶
const ( MaxPath = uint32(0xffffffff) FirstHardenedChild = uint32(0x80000000) )
Variables ¶
var DefaultBaseDerivationLen = len(DefaultBaseDerivationPath)
var DefaultBaseDerivationPath = accounts.DefaultBaseDerivationPath
DefaultBaseDerivationPath is the base path from which custom derivation endpoints are incremented. As such, the first account will be at m/44'/60'/0'/0, the second at m/44'/60'/0'/1, etc
var DefaultNet = &chaincfg.MainNetParams
var DefaultRootDerivationLen = len(DefaultRootDerivationPath)
var DefaultRootDerivationPath = accounts.DefaultRootDerivationPath
DefaultRootDerivationPath is the root path to which custom derivation endpoints are appended. As such, the first account will be at m/44'/60'/0'/0, the second at m/44'/60'/0'/1, etc.
Functions ¶
func MustParseDerivationPath ¶
func MustParseDerivationPath(path string) accounts.DerivationPath
MustParseDerivationPath parses the derivation path in string format into []uint32 but will panic if it can't parse it.
func NewMnemonic ¶
NewMnemonic returns a randomly generated BIP-39 mnemonic using 128-256 bits of entropy.
func NewSeed ¶
NewSeed returns a randomly generated BIP-39 seed.
Example ¶
package main import ( "fmt" "log" "github.com/aquachain/hdwallet" ) func main() { seed, _ := hdwallet.NewSeed() 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()) 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()) }
Output:
func NewSeedFromMnemonic ¶
NewSeedFromMnemonic returns a BIP-39 seed based on a BIP-39 mnemonic with passphrase.
func ParseDerivationPath ¶
func ParseDerivationPath(path string) (accounts.DerivationPath, error)
ParseDerivationPath parses the derivation path in string format into []uint32
Types ¶
type Wallet ¶
type Wallet struct {
// contains filtered or unexported fields
}
Wallet is the underlying wallet struct.
func NewFromMnemonic ¶
NewFromMnemonic returns a new wallet from a BIP-39 mnemonic.
Example ¶
package main import ( "fmt" "log" "github.com/aquachain/hdwallet" ) func main() { mnemonic := "tag volcano eight thank tide danger coast health above argue embrace heavy" wallet, err := hdwallet.NewFromMnemonic(mnemonic, "") 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) } path = hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/1") account2, err := wallet.Derive(path, false) if err != nil { log.Fatal(err) } fmt.Println(account.Address.Hex()) fmt.Println(account2.Address.Hex()) }
Output: 0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947 0x8230645aC28A4EdD1b0B53E7Cd8019744E9dD559
Example (Sign) ¶
package main import ( "fmt" "log" "math/big" "github.com/aquachain/hdwallet" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" ) func main() { mnemonic := "tag volcano eight thank tide danger coast health above argue embrace heavy" wallet, err := hdwallet.NewFromMnemonic(mnemonic, "") if err != nil { log.Fatal(err) } path := hdwallet.MustParseDerivationPath("m/44'/60'/0'/0/0") account, err := wallet.Derive(path, true) if err != nil { log.Fatal(err) } nonce := uint64(0) value := big.NewInt(1000000000000000000) toAddress := common.HexToAddress("0x0") gasLimit := uint64(21000) gasPrice := big.NewInt(21000000000) var data []byte tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data) signedTx, err := wallet.SignTx(account, tx, nil) if err != nil { log.Fatal(err) } chainid := params.MainnetChainConfig.ChainID tx2, err := signedTx.AsMessage(types.NewEIP155Signer(chainid)) if err != nil { log.Println(err) return } from := tx2.From() fmt.Println(from.Hex()) fmt.Println(account.Address.Hex()) }
Output: 0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947 0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947
func NewFromMnemonicCfg ¶ added in v0.0.2
NewFromMnemonicCfg returns a new wallet from a BIP-39 mnemonic.
func NewFromSeed ¶
NewFromSeed returns a new wallet from a BIP-39 seed.
func NewFromSeedCustom ¶ added in v0.0.2
func (*Wallet) Accounts ¶
Accounts implements accounts.Wallet, returning the list of accounts pinned to the wallet. If self-derivation was enabled, the account list is periodically expanded based on current chain state.
func (*Wallet) AddressBytes ¶
AddressBytes returns the address in bytes format of the account.
func (*Wallet) AddressHex ¶
AddressHex returns the address in hex string format of the account.
func (*Wallet) Close ¶
Close implements accounts.Wallet, however this does nothing since this is not a hardware device.
func (*Wallet) Contains ¶
Contains implements accounts.Wallet, returning whether a particular account is or is not pinned into this wallet instance.
func (*Wallet) Derive ¶
Derive implements accounts.Wallet, deriving a new account at the specific derivation path. If pin is set to true, the account will be added to the list of tracked accounts.
Example ¶
package main import ( "fmt" "log" "github.com/aquachain/hdwallet" ) func main() { mnemonic := "tag volcano eight thank tide danger coast health above argue embrace heavy" wallet, err := hdwallet.NewFromMnemonic(mnemonic, "") 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.Printf("Account address: %s\n", account.Address.Hex()) privateKey, err := wallet.PrivateKeyHex(account) if err != nil { log.Fatal(err) } fmt.Printf("Private key in hex: %s\n", privateKey) publicKey, _ := wallet.PublicKeyHex(account) if err != nil { log.Fatal(err) } fmt.Printf("Public key in hex: %s\n", publicKey) }
Output: Account address: 0xC49926C4124cEe1cbA0Ea94Ea31a6c12318df947 Private key in hex: 63e21d10fd50155dbba0e7d3f7431a400b84b4c2ac1ee38872f82448fe3ecfb9 Public key in hex: 6005c86a6718f66221713a77073c41291cc3abbfcd03aa4955e9b2b50dbf7f9b6672dad0d46ade61e382f79888a73ea7899d9419becf1d6c9ec2087c1188fa18
func (*Wallet) MasterKey ¶ added in v0.0.2
func (w *Wallet) MasterKey() *hdkeychain.ExtendedKey
MasterKey returns the underlying hdkeychain.ExtendedKey used by the wallet
func (*Wallet) Open ¶
Open implements accounts.Wallet, however this does nothing since this is not a hardware device.
func (*Wallet) PrivateKey ¶
PrivateKey returns the ECDSA private key of the account.
func (*Wallet) PrivateKeyBytes ¶
PrivateKeyBytes returns the ECDSA private key in bytes format of the account.
func (*Wallet) PrivateKeyHex ¶
PrivateKeyHex return the ECDSA private key in hex string format of the account.
func (*Wallet) PublicKeyBytes ¶
PublicKeyBytes returns the ECDSA public key in bytes format of the account.
func (*Wallet) PublicKeyHex ¶
PublicKeyHex return the ECDSA public key in hex string format of the account.
func (*Wallet) SelfDerive ¶
func (w *Wallet) SelfDerive(base []accounts.DerivationPath, chain ethereum.ChainStateReader)
SelfDerive implements accounts.Wallet, trying to discover accounts that the user used previously (based on the chain state), but ones that he/she did not explicitly pin to the wallet manually. To avoid chain head monitoring, self derivation only runs during account listing (and even then throttled).
func (*Wallet) SignDataWithPassphrase ¶
func (w *Wallet) SignDataWithPassphrase(account accounts.Account, passphrase, mimeType string, data []byte) ([]byte, error)
SignDataWithPassphrase is not implemented
func (*Wallet) SignHashWithPassphrase ¶
func (w *Wallet) SignHashWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error)
SignHashWithPassphrase implements accounts.Wallet, attempting to sign the given hash with the given account using the passphrase as extra authentication.
func (*Wallet) SignTextWithPassphrase ¶
func (w *Wallet) SignTextWithPassphrase(account accounts.Account, passphrase string, hash []byte) ([]byte, error)
SignTextWithPassphrase is not implemented
func (*Wallet) SignTx ¶
func (w *Wallet) SignTx(account accounts.Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
SignTx implements accounts.Wallet, which allows the account to sign an Ethereum transaction.
func (*Wallet) SignTxWithPassphrase ¶
func (w *Wallet) SignTxWithPassphrase(account accounts.Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
SignTxWithPassphrase implements accounts.Wallet, attempting to sign the given transaction with the given account using passphrase as extra authentication.
func (*Wallet) Status ¶
Status implements accounts.Wallet, returning a custom status message from the underlying vendor-specific hardware wallet implementation, however this does nothing since this is not a hardware device.