Documentation ¶
Index ¶
- Constants
- Variables
- type DerivationPath
- func (dp DerivationPath) Account() uint32
- func (dp DerivationPath) AddressIndex() uint32
- func (dp DerivationPath) Change() uint32
- func (dp DerivationPath) CoinType() uint32
- func (dp DerivationPath) Increase() error
- func (dp DerivationPath) IncreaseAccount() error
- func (dp DerivationPath) IncreaseAddressIndex() error
- func (dp DerivationPath) Purpose() uint32
- func (dp DerivationPath) SetAccount(account uint32) error
- func (dp DerivationPath) SetAddressIndex(index uint32) error
- func (dp DerivationPath) SetChange(change uint32) error
- type Key
- type Mnemonic
- type PrivateKey
- func NewKeyFromBytes(prv []byte) *PrivateKey
- func NewKeyFromDirectory(path string, passphrase string, address types.Address) (*PrivateKey, error)
- func NewKeyFromECDSA(prv *ecdsa.PrivateKey) *PrivateKey
- func NewKeyFromJSON(path string, passphrase string) (*PrivateKey, error)
- func NewKeyFromJSONContent(content []byte, passphrase string) (*PrivateKey, error)
- func NewKeyFromMnemonic(mnemonic, password string, account, index uint32) (*PrivateKey, error)
- func NewRandomKey() *PrivateKey
- func (k *PrivateKey) Address() types.Address
- func (k *PrivateKey) JSON(passphrase string, scryptN, scryptP int) ([]byte, error)
- func (k *PrivateKey) PrivateKey() *ecdsa.PrivateKey
- func (k *PrivateKey) PublicKey() *ecdsa.PublicKey
- func (k *PrivateKey) SignHash(hash types.Hash) (*types.Signature, error)
- func (k *PrivateKey) SignMessage(data []byte) (*types.Signature, error)
- func (k *PrivateKey) SignTransaction(tx *types.Transaction) error
- func (k *PrivateKey) VerifyHash(hash types.Hash, sig types.Signature) bool
- func (k *PrivateKey) VerifyMessage(data []byte, sig types.Signature) bool
Constants ¶
const ( PurposeComponent = 0 CoinTypeComponent = 1 AccountComponent = 2 ChangeComponent = 3 AddressIndexComponent = 4 )
Indices of the components in the derivation path.
const ( StandardScryptN = 1 << 18 StandardScryptP = 1 LightScryptN = 1 << 12 LightScryptP = 6 )
Variables ¶
var DefaultDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0, 0}
DefaultDerivationPath is the default derivation path for the first key. It is set to m/44'/60'/0'/0/0.
var ErrKeyNotFound = errors.New("key not found")
var RootDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}
RootDerivationPath is the root derivation path used to derive the child keys. It is set to m/44'/60'/0'/0.
Functions ¶
This section is empty.
Types ¶
type DerivationPath ¶
type DerivationPath []uint32
DerivationPath represents derivation path as internal binary format.
Derivation path allows to derive multiple child keys from a single parent key.
The path is defined as:
m / purpose' / coin_type' / account' / change / address_index
Where:
purpose is a constant set to 44' (or 0x8000002C) following the BIP43 recommendation.
coin_type indicates the coin type, as defined in SLIP-0044. For Ethereum, it is 60' (or 0x8000003C).
account is an index that allows users to create multiple identities from a single seed.
change is a constant, set to 0 (or 0x80000000) for external chain and 1 (or 0x80000001) for internal chain.
address_index is an address index that is incremented for each new address.
Reference: BIP-32: https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki BIP-44: https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki SLIP-44: https://github.com/satoshilabs/slips/blob/master/slip-0044.md
func ParseDerivationPath ¶
func ParseDerivationPath(path string) (result DerivationPath, err error)
ParseDerivationPath converts a BIP-33 derivation path string into the internal binary format.
The path is expected to be of the form:
m / purpose' / coin_type' / account' / change / address_index
The single quotes are used to indicate hardened derivation.
If m/ prefix is omitted, then RootDerivationPath (m/44'/60'/0'/0) is prepended to the path.
func (DerivationPath) Account ¶
func (dp DerivationPath) Account() uint32
Account returns the account component of the derivation path. If account component is missing, then 0 is returned.
func (DerivationPath) AddressIndex ¶
func (dp DerivationPath) AddressIndex() uint32
AddressIndex returns the address index component of the derivation path. If address index component is missing, then 0 is returned.
func (DerivationPath) Change ¶
func (dp DerivationPath) Change() uint32
Change returns the change component of the derivation path. If change component is missing, then 0 is returned.
func (DerivationPath) CoinType ¶
func (dp DerivationPath) CoinType() uint32
CoinType returns the coin type component of the derivation path. If coin type component is missing, then 0 is returned.
func (DerivationPath) Increase ¶
func (dp DerivationPath) Increase() error
Increase increases the last component of the derivation path.
func (DerivationPath) IncreaseAccount ¶
func (dp DerivationPath) IncreaseAccount() error
IncreaseAccount increases the account number in the derivation path.
func (DerivationPath) IncreaseAddressIndex ¶
func (dp DerivationPath) IncreaseAddressIndex() error
IncreaseAddressIndex increases the index number in the derivation path.
func (DerivationPath) Purpose ¶
func (dp DerivationPath) Purpose() uint32
Purpose returns the purpose component of the derivation path. If the path is empty, then 0 is returned.
func (DerivationPath) SetAccount ¶
func (dp DerivationPath) SetAccount(account uint32) error
SetAccount sets the account component of the derivation path.
func (DerivationPath) SetAddressIndex ¶
func (dp DerivationPath) SetAddressIndex(index uint32) error
SetAddressIndex sets the address index component of the derivation path.
func (DerivationPath) SetChange ¶
func (dp DerivationPath) SetChange(change uint32) error
SetChange sets the change component of the derivation path.
type Key ¶
type Key interface { // Address returns the address of the key. Address() types.Address // SignHash signs the given hash. SignHash(hash types.Hash) (*types.Signature, error) // SignMessage signs the given message. SignMessage(data []byte) (*types.Signature, error) // SignTransaction signs the given transaction. SignTransaction(tx *types.Transaction) error // VerifyHash whether the given hash is signed by the key. VerifyHash(hash types.Hash, sig types.Signature) bool // VerifyMessage verifies whether the given data is signed by the key. VerifyMessage(data []byte, sig types.Signature) bool }
type Mnemonic ¶
type Mnemonic struct {
// contains filtered or unexported fields
}
Mnemonic is a mnemonic phrase with a password used to derive private keys.
func NewMnemonic ¶
NewMnemonic creates a new mnemonic that can be used to derive private keys.
func (Mnemonic) Derive ¶
func (m Mnemonic) Derive(path DerivationPath) (*PrivateKey, error)
Derive derives a private key from the mnemonic using given derivation path.
type PrivateKey ¶
type PrivateKey struct {
// contains filtered or unexported fields
}
func NewKeyFromBytes ¶
func NewKeyFromBytes(prv []byte) *PrivateKey
NewKeyFromBytes creates a new private key from private key bytes.
func NewKeyFromDirectory ¶
func NewKeyFromDirectory(path string, passphrase string, address types.Address) (*PrivateKey, error)
NewKeyFromDirectory returns a new key from a directory containing JSON files.
func NewKeyFromECDSA ¶
func NewKeyFromECDSA(prv *ecdsa.PrivateKey) *PrivateKey
NewKeyFromECDSA creates a new private key from an ecdsa.PrivateKey.
func NewKeyFromJSON ¶
func NewKeyFromJSON(path string, passphrase string) (*PrivateKey, error)
NewKeyFromJSON loads an Ethereum key from a JSON file.
func NewKeyFromJSONContent ¶
func NewKeyFromJSONContent(content []byte, passphrase string) (*PrivateKey, error)
NewKeyFromJSONContent returns a new key from a JSON.
func NewKeyFromMnemonic ¶
func NewKeyFromMnemonic(mnemonic, password string, account, index uint32) (*PrivateKey, error)
NewKeyFromMnemonic creates a new private key from a mnemonic phrase. The derivation path is set to m/44'/60'/account'/0/index.
func (*PrivateKey) Address ¶
func (k *PrivateKey) Address() types.Address
Address implements the Key interface.
func (*PrivateKey) JSON ¶
func (k *PrivateKey) JSON(passphrase string, scryptN, scryptP int) ([]byte, error)
JSON returns the JSON representation of the private key.
func (*PrivateKey) PrivateKey ¶
func (k *PrivateKey) PrivateKey() *ecdsa.PrivateKey
PrivateKey returns the ECDSA private key.
func (*PrivateKey) PublicKey ¶
func (k *PrivateKey) PublicKey() *ecdsa.PublicKey
PublicKey returns the ECDSA public key.
func (*PrivateKey) SignMessage ¶
func (k *PrivateKey) SignMessage(data []byte) (*types.Signature, error)
SignMessage implements the Key interface.
func (*PrivateKey) SignTransaction ¶
func (k *PrivateKey) SignTransaction(tx *types.Transaction) error
SignTransaction implements the Key interface.
func (*PrivateKey) VerifyHash ¶
VerifyHash implements the Key interface.
func (*PrivateKey) VerifyMessage ¶
func (k *PrivateKey) VerifyMessage(data []byte, sig types.Signature) bool
VerifyMessage implements the Key interface.