hdwallet

package module
v0.0.0-...-bc99149 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 24, 2019 License: MIT Imports: 19 Imported by: 0

README


logo


go-ethereum-hdwallet

Ethereum HD Wallet derivations from [mnemonic] seed in Go (golang). Implements the go-ethereum accounts.Wallet interface.

License Build Status Go Report Card GoDoc

Install

go get -u github.com/miguelmota/go-ethereum-hdwallet

Documenation

https://godoc.org/github.com/miguelmota/go-ethereum-hdwallet

Getting started

package main

import (
	"fmt"
	"log"

	"github.com/miguelmota/go-ethereum-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.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
}
Signing transaction
package main

import (
	"log"
	"math/big"

	"github.com/davecgh/go-spew/spew"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/miguelmota/go-ethereum-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, 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)
	}

	spew.Dump(signedTx)
}

Test

make test

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0}

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/0, the second at m/44'/60'/0'/0/1, etc.

View Source
var DefaultRootDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}

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.

View Source
var LegacyLedgerBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}

LegacyLedgerBaseDerivationPath is the legacy 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.

Functions

func NewMnemonic

func NewMnemonic(bits int) (string, error)

NewMnemonic returns a randomly generated BIP-39 mnemonic using 128-256 bits of entropy.

func NewSeed

func NewSeed() ([]byte, error)

NewSeed returns a randomly generated BIP-39 seed.

func NewSeedFromMnemonic

func NewSeedFromMnemonic(mnemonic string) ([]byte, error)

NewSeedFromMnemonic returns a BIP-39 seed based on a BIP-39 mnemonic.

Types

type DerivationPath

type DerivationPath []uint32

DerivationPath represents the computer friendly version of a hierarchical deterministic wallet account derivaion path.

The BIP-32 spec https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki defines derivation paths to be of the form:

m / purpose' / coin_type' / account' / change / address_index

The BIP-44 spec https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki defines that the `purpose` be 44' (or 0x8000002C) for crypto currencies, and SLIP-44 https://github.com/satoshilabs/slips/blob/master/slip-0044.md assigns the `coin_type` 60' (or 0x8000003C) to Ethereum.

The root path for Ethereum is m/44'/60'/0'/0 according to the specification from https://github.com/ethereum/EIPs/issues/84, albeit it's not set in stone yet whether accounts should increment the last component or the children of that. We will go with the simpler approach of incrementing the last component.

func MustParseDerivationPath

func MustParseDerivationPath(path string) DerivationPath

MustParseDerivationPath parses the derivation path in string format into []uint32 but will panic if it can't parse it.

func ParseDerivationPath

func ParseDerivationPath(path string) (DerivationPath, error)

ParseDerivationPath converts a user specified derivation path string to the internal binary representation.

Full derivation paths need to start with the `m/` prefix, relative derivation paths (which will get appended to the default root path) must not have prefixes in front of the first element. Whitespace is ignored.

func (DerivationPath) MarshalJSON

func (path DerivationPath) MarshalJSON() ([]byte, error)

MarshalJSON turns a derivation path into its json-serialized string

func (DerivationPath) String

func (path DerivationPath) String() string

String implements the stringer interface, converting a binary derivation path to its canonical representation.

func (*DerivationPath) UnmarshalJSON

func (path *DerivationPath) UnmarshalJSON(b []byte) error

UnmarshalJSON a json-serialized string back into a derivation path

type Wallet

type Wallet struct {
	// contains filtered or unexported fields
}

Wallet is the underlying wallet struct.

func NewFromMnemonic

func NewFromMnemonic(mnemonic string) (*Wallet, error)

NewFromMnemonic returns a new wallet from a BIP-39 mnemonic.

func NewFromSeed

func NewFromSeed(seed []byte) (*Wallet, error)

NewFromSeed returns a new wallet from a BIP-39 seed.

func (*Wallet) Accounts

func (w *Wallet) Accounts() []accounts.Account

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) Address

func (w *Wallet) Address(account accounts.Account) (common.Address, error)

Address returns the address of the account.

func (*Wallet) AddressBytes

func (w *Wallet) AddressBytes(account accounts.Account) ([]byte, error)

AddressBytes returns the address in bytes format of the account.

func (*Wallet) AddressHex

func (w *Wallet) AddressHex(account accounts.Account) (string, error)

AddressHex returns the address in hex string format of the account.

func (*Wallet) Close

func (w *Wallet) Close() error

Close implements accounts.Wallet, however this does nothing since this is not a hardware device.

func (*Wallet) Contains

func (w *Wallet) Contains(account accounts.Account) bool

Contains implements accounts.Wallet, returning whether a particular account is or is not pinned into this wallet instance.

func (*Wallet) Derive

func (w *Wallet) Derive(path DerivationPath, pin bool) (accounts.Account, error)

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.

func (*Wallet) Open

func (w *Wallet) Open(passphrase string) error

Open implements accounts.Wallet, however this does nothing since this is not a hardware device.

func (*Wallet) Path

func (w *Wallet) Path(account accounts.Account) (string, error)

Path return the derivation path of the account.

func (*Wallet) PrivateKey

func (w *Wallet) PrivateKey(account accounts.Account) (*ecdsa.PrivateKey, error)

PrivateKey returns the ECDSA private key of the account.

func (*Wallet) PrivateKeyBytes

func (w *Wallet) PrivateKeyBytes(account accounts.Account) ([]byte, error)

PrivateKeyBytes returns the ECDSA private key in bytes format of the account.

func (*Wallet) PrivateKeyHex

func (w *Wallet) PrivateKeyHex(account accounts.Account) (string, error)

PrivateKeyHex return the ECDSA private key in hex string format of the account.

func (*Wallet) PublicKey

func (w *Wallet) PublicKey(account accounts.Account) (*ecdsa.PublicKey, error)

PublicKey returns the ECDSA public key of the account.

func (*Wallet) PublicKeyBytes

func (w *Wallet) PublicKeyBytes(account accounts.Account) ([]byte, error)

PublicKeyBytes returns the ECDSA public key in bytes format of the account.

func (*Wallet) PublicKeyHex

func (w *Wallet) PublicKeyHex(account accounts.Account) (string, error)

PublicKeyHex return the ECDSA public key in hex string format of the account.

func (*Wallet) SelfDerive

func (w *Wallet) SelfDerive(base DerivationPath, chain hpb.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) SignHash

func (w *Wallet) SignHash(account accounts.Account, hash []byte) ([]byte, error)

SignHash implements accounts.Wallet, which allows signing arbitrary data.

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) 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

func (w *Wallet) Status() (string, error)

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.

func (*Wallet) URL

func (w *Wallet) URL() accounts.URL

URL implements accounts.Wallet, returning the URL of the device that the wallet is on, however this does nothing since this is not a hardware device.

func (*Wallet) Unpin

func (w *Wallet) Unpin(account accounts.Account) error

Unpin unpins account from list of pinned accounts.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL