Documentation ¶
Overview ¶
Package accounts implements high level Ethereum account management.
Index ¶
- Variables
- func NewAuthNeededError(needed string) error
- type Account
- type AuthNeededError
- type Backend
- type DerivationPath
- type Manager
- func (am *Manager) Backends(kind reflect.Type) []Backend
- func (am *Manager) Close() error
- func (am *Manager) Find(account Account) (Wallet, error)
- func (am *Manager) Subscribe(sink chan<- WalletEvent) event.Subscription
- func (am *Manager) Wallet(url string) (Wallet, error)
- func (am *Manager) Wallets() []Wallet
- type URL
- type Wallet
- type WalletEvent
- type WalletEventType
Constants ¶
This section is empty.
Variables ¶
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, the second at m/44'/60'/0'/1, etc.
var DefaultLedgerBaseDerivationPath = DerivationPath{0x80000000 + 44, 0x80000000 + 60, 0x80000000 + 0, 0}
DefaultLedgerBaseDerivationPath 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 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.
var ErrInvalidPassphrase = errors.New("invalid passphrase")
ErrInvalidPassphrase is returned when a decryption operation receives a bad passphrase.
var ErrNotSupported = errors.New("not supported")
ErrNotSupported is returned when an operation is requested from an account backend that it does not support.
var ErrUnknownAccount = errors.New("unknown account")
ErrUnknownAccount is returned for any requested operation for which no backend provides the specified account.
var ErrUnknownWallet = errors.New("unknown wallet")
ErrUnknownWallet is returned for any requested operation for which no backend provides the specified wallet.
var ErrWalletAlreadyOpen = errors.New("wallet already open")
ErrWalletAlreadyOpen is returned if a wallet is attempted to be opened the second time.
var ErrWalletClosed = errors.New("wallet closed")
ErrWalletClosed is returned if a wallet is attempted to be opened the secodn time.
Functions ¶
func NewAuthNeededError ¶
NewAuthNeededError creates a new authentication error with the extra details about the needed fields set.
Types ¶
type Account ¶
type Account struct { // 20字节的数据 Address common.Address `json:"address"` // Ethereum account address derived from the key URL URL `json:"url"` // Optional resource locator within a backend }
Account represents an Ethereum account located at a specific location defined by the optional URL field. 以太坊地址类型结构
type AuthNeededError ¶
type AuthNeededError struct {
Needed string // Extra authentication the user needs to provide
}
AuthNeededError is returned by backends for signing requests where the user is required to provide further authentication before signing can succeed.
This usually means either that a password needs to be supplied, or perhaps a one time PIN code displayed by some hardware device.
func (*AuthNeededError) Error ¶
func (err *AuthNeededError) Error() string
Error implements the standard error interfacel.
type Backend ¶
type Backend interface { // Wallets retrieves the list of wallets the backend is currently aware of. // // The returned wallets are not opened by default. For software HD wallets this // means that no base seeds are decrypted, and for hardware wallets that no actual // connection is established. // // The resulting wallet list will be sorted alphabetically based on its internal // URL assigned by the backend. Since wallets (especially hardware) may come and // go, the same wallet might appear at a different positions in the list during // subsequent retrievals. // 获取钱包列表 Wallets() []Wallet // Subscribe creates an async subscription to receive notifications when the // backend detects the arrival or departure of a wallet. // 订阅钱包相关事件 Subscribe(sink chan<- WalletEvent) event.Subscription }
Backend is a "wallet provider" that may contain a batch of accounts they can sign transactions with and upon request, do so. 钱包的后端实现(服务),主要实现keystore钱包以及USB硬件钱包
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 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) String ¶
func (path DerivationPath) String() string
String implements the stringer interface, converting a binary derivation path to its canonical representation.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager is an overarching account manager that can communicate with various backends for signing transactions. 账户管理工具,可以和所有的backends进行通信来签名交易
func NewManager ¶
NewManager creates a generic account manager to sign transaction via various supported backends. 新建管理器对象
func (*Manager) Backends ¶
Backends retrieves the backend(s) with the given type from the account manager. 返回指定的服务列表
func (*Manager) Close ¶
Close terminates the account manager's internal notification processes. 关闭账号管理器
func (*Manager) Find ¶
Find attempts to locate the wallet corresponding to a specific account. Since accounts can be dynamically added to and removed from wallets, this method has a linear runtime in the number of wallets. 通过指定的ACCOUNT查找钱包
func (*Manager) Subscribe ¶
func (am *Manager) Subscribe(sink chan<- WalletEvent) event.Subscription
Subscribe creates an async subscription to receive notifications when the manager detects the arrival or departure of a wallet from any of its backends. 订阅事件
type URL ¶
type URL struct { Scheme string // Protocol scheme to identify a capable account backend Path string // Path for the backend to identify a unique entity }
URL represents the canonical identification URL of a wallet or account.
It is a simplified version of url.URL, with the important limitations (which are considered features here) that it contains value-copyable components only, as well as that it doesn't do any URL encoding/decoding of special characters.
The former is important to allow an account to be copied without leaving live references to the original version, whereas the latter is important to ensure one single canonical form opposed to many allowed ones by the RFC 3986 spec.
As such, these URLs should not be used outside of the scope of an Ethereum wallet or account.
func (URL) MarshalJSON ¶
MarshalJSON implements the json.Marshaller interface.
func (URL) TerminalString ¶
TerminalString implements the log.TerminalStringer interface.
type Wallet ¶
type Wallet interface { // URL retrieves the canonical path under which this wallet is reachable. It is // user by upper layers to define a sorting order over all wallets from multiple // backends. // 获取该钱包可以访问的规范路径 URL() URL // Status returns a textual status to aid the user in the current state of the // wallet. It also returns an error indicating any failure the wallet might have // encountered. // 钱包状态 Status() (string, error) // Open initializes access to a wallet instance. It is not meant to unlock or // decrypt account keys, rather simply to establish a connection to hardware // wallets and/or to access derivation seeds. // // The passphrase parameter may or may not be used by the implementation of a // particular wallet instance. The reason there is no passwordless open method // is to strive towards a uniform wallet handling, oblivious to the different // backend providers. // // Please note, if you open a wallet, you must close it to release any allocated // resources (especially important when working with hardware wallets). // 初始化对钱包实例的访问 Open(passphrase string) error // Close releases any resources held by an open wallet instance. // 释放open方法占用的资源 Close() error // Accounts retrieves the list of signing accounts the wallet is currently aware // of. For hierarchical deterministic wallets, the list will not be exhaustive, // rather only contain the accounts explicitly pinned during account derivation. // 账号列表 Accounts() []Account // Contains returns whether an account is part of this particular wallet or not. // 查询指定账户是否属于该钱包 Contains(account Account) bool // Derive attempts to explicitly derive a hierarchical deterministic account at // the specified derivation path. If requested, the derived account will be added // to the wallet's tracked account list. // 专门给确定性钱包使用的 Derive(path DerivationPath, pin bool) (Account, error) // SelfDerive sets a base account derivation path from which the wallet attempts // to discover non zero accounts and automatically add them to list of tracked // accounts. // // Note, self derivaton will increment the last component of the specified path // opposed to decending into a child path to allow discovering accounts starting // from non zero components. // // You can disable automatic account discovery by calling SelfDerive with a nil // chain state reader. // 设置一个账户导出路径 SelfDerive(base DerivationPath, chain ethereum.ChainStateReader) // SignHash requests the wallet to sign the given hash. // // It looks up the account specified either solely via its address contained within, // or optionally with the aid of any location metadata from the embedded URL field. // // If the wallet requires additional authentication to sign the request (e.g. // a password to decrypt the account, or a PIN code o verify the transaction), // an AuthNeededError instance will be returned, containing infos for the user // about which fields or actions are needed. The user may retry by providing // the needed details via SignHashWithPassphrase, or by other means (e.g. unlock // the account in a keystore). // 请求钱包为传入的哈希进行签名 SignHash(account Account, hash []byte) ([]byte, error) // SignTx requests the wallet to sign the given transaction. // // It looks up the account specified either solely via its address contained within, // or optionally with the aid of any location metadata from the embedded URL field. // // If the wallet requires additional authentication to sign the request (e.g. // a password to decrypt the account, or a PIN code o verify the transaction), // an AuthNeededError instance will be returned, containing infos for the user // about which fields or actions are needed. The user may retry by providing // the needed details via SignTxWithPassphrase, or by other means (e.g. unlock // the account in a keystore). // 请求钱包为传入的交易进行签名 SignTx(account Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) // SignHashWithPassphrase requests the wallet to sign the given hash with the // given passphrase as extra authentication information. // // It looks up the account specified either solely via its address contained within, // or optionally with the aid of any location metadata from the embedded URL field. // 请求钱包使用指定 的passphrase为传入的哈希进行签名 SignHashWithPassphrase(account Account, passphrase string, hash []byte) ([]byte, error) // SignTxWithPassphrase requests the wallet to sign the given transaction, with the // given passphrase as extra authentication information. // // It looks up the account specified either solely via its address contained within, // or optionally with the aid of any location metadata from the embedded URL field. // 请求钱包使用指定 的passphrase为传入的交易进行签名 SignTxWithPassphrase(account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) }
Wallet represents a software or hardware wallet that might contain one or more accounts (derived from the same seed). 钱包接口
type WalletEvent ¶
type WalletEvent struct { Wallet Wallet // Wallet instance arrived or departed Kind WalletEventType // Event type that happened in the system }
WalletEvent is an event fired by an account backend when a wallet arrival or departure is detected. 在检测到钱包账户发生改变时所触发的事件
type WalletEventType ¶
type WalletEventType int
WalletEventType represents the different event types that can be fired by the wallet subscription subsystem. 钱包事件的类型
const ( // WalletArrived is fired when a new wallet is detected either via USB or via // a filesystem event in the keystore. // 在通过USB或者密码文件检测到新钱包的时候 ,会触发该事件 WalletArrived WalletEventType = iota // WalletOpened is fired when a wallet is successfully opened with the purpose // of starting any background processes such as automatic key derivation. // 开户钱包所触发的事件 WalletOpened // WalletDropped WalletDropped )
Directories ¶
Path | Synopsis |
---|---|
Package abi implements the Ethereum ABI (Application Binary Interface).
|
Package abi implements the Ethereum ABI (Application Binary Interface). |
bind
Package bind generates Ethereum contract Go bindings.
|
Package bind generates Ethereum contract Go bindings. |
Package keystore implements encrypted storage of secp256k1 private keys.
|
Package keystore implements encrypted storage of secp256k1 private keys. |
Package usbwallet implements support for USB hardware wallets.
|
Package usbwallet implements support for USB hardware wallets. |
internal/trezor
Package trezor contains the wire protocol wrapper in Go.
|
Package trezor contains the wire protocol wrapper in Go. |