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 KeyRPC
- func (k *KeyRPC) Address() types.Address
- func (k *KeyRPC) SignMessage(ctx context.Context, data []byte) (*types.Signature, error)
- func (k *KeyRPC) SignTransaction(ctx context.Context, tx *types.Transaction) error
- func (k *KeyRPC) VerifyMessage(_ context.Context, data []byte, sig types.Signature) bool
- type KeyWithHashSigner
- 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(_ context.Context, hash types.Hash) (*types.Signature, error)
- func (k *PrivateKey) SignMessage(_ context.Context, data []byte) (*types.Signature, error)
- func (k *PrivateKey) SignTransaction(_ context.Context, tx *types.Transaction) error
- func (k *PrivateKey) VerifyHash(_ context.Context, hash types.Hash, sig types.Signature) bool
- func (k *PrivateKey) VerifyMessage(_ context.Context, data []byte, sig types.Signature) bool
- type RPCSigningClient
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 // SignMessage signs the given message. SignMessage(ctx context.Context, data []byte) (*types.Signature, error) // SignTransaction signs the given transaction. SignTransaction(ctx context.Context, tx *types.Transaction) error // VerifyMessage verifies whether the given data is signed by the key. VerifyMessage(ctx context.Context, data []byte, sig types.Signature) bool }
Key is the interface for an Ethereum key.
type KeyRPC ¶ added in v0.6.1
type KeyRPC struct {
// contains filtered or unexported fields
}
KeyRPC is an Ethereum key that uses an RPC client to sign messages and transactions.
func NewKeyRPC ¶ added in v0.6.1
func NewKeyRPC(client RPCSigningClient, address types.Address) *KeyRPC
NewKeyRPC returns a new KeyRPC.
func (*KeyRPC) SignMessage ¶ added in v0.6.1
SignMessage implements the Key interface.
func (*KeyRPC) SignTransaction ¶ added in v0.6.1
SignTransaction implements the Key interface.
type KeyWithHashSigner ¶ added in v0.6.1
type KeyWithHashSigner interface { Key // SignHash signs the given hash without the EIP-191 message prefix. SignHash(ctx context.Context, hash types.Hash) (*types.Signature, error) // VerifyHash whether the given hash is signed by the key without the // EIP-191 message prefix. VerifyHash(ctx context.Context, hash types.Hash, sig types.Signature) bool }
KeyWithHashSigner is the interface for an Ethereum key that can sign data using a private key, skipping the EIP-191 message prefix.
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 ¶
SignMessage implements the Key interface.
func (*PrivateKey) SignTransaction ¶
func (k *PrivateKey) SignTransaction(_ context.Context, tx *types.Transaction) error
SignTransaction implements the Key interface.
func (*PrivateKey) VerifyHash ¶
VerifyHash implements the KeyWithHashSigner interface.
func (*PrivateKey) VerifyMessage ¶
VerifyMessage implements the Key interface.
type RPCSigningClient ¶ added in v0.6.1
type RPCSigningClient interface { Sign(ctx context.Context, account types.Address, data []byte) (*types.Signature, error) SignTransaction(ctx context.Context, tx *types.Transaction) ([]byte, *types.Transaction, error) }
RPCSigningClient is the interface for an Ethereum RPC client that can sign messages and transactions.