1. 程式人生 > >Ethereum 原始碼分析之 accounts

Ethereum 原始碼分析之 accounts

一、Account

// Account represents an Ethereum account located at a specific location defined
// by the optional URL field.
// Account,代表一個位於由可選的URL欄位定義的具體位置的以太坊賬戶
// Address,地址是20個位元組,由私鑰衍生。。URL,可選欄位,後端的資源位置
type Account struct {
    Address common.Address `json:"address"` // Ethereum account address derived from
the key URL URL `json:"url"` // Optional resource locator within a backend }

二、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 用來獲取這個錢包可以訪問的規範路徑。被上層使用,為所有來自於各種backend的錢包定義一個排序 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 返回錢包的當前狀態的文字來幫助使用者。同時返回一個錯誤來指示錢包可能會遇到的任何錯誤 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 初始化對錢包例項的訪問。這個方法並不意味著解鎖或者解密賬戶祕鑰,而是簡單地建立與硬體錢包的連線和/或訪問衍生種子。 // 請注意,如果你open了一個錢包,你必須close它。不然有些資源可能沒有釋放。特別是使用硬體錢包的時候需要特別注意。 Open(passphrase string) error // Close releases any resources held by an open wallet instance. // Close 釋放一個開啟的錢包例項的佔用的任何資源。 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 用來獲取錢包正發現的賬戶列表。對於分層次的錢包,這個列表不會詳盡的列出所有的賬號,而是隻包含在帳戶派生期間明確固定的帳戶。 Accounts() []Account // Contains returns whether an account is part of this particular wallet or not. // Contains 返回一個賬號是否屬於本錢包。 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 嘗試在指定的派生路徑上顯式派生出分層確定性帳戶。如果pin為true,派生帳戶將被新增到錢包的跟蹤帳戶列表中。 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 設定一個基本帳戶匯出路徑,從中錢包嘗試發現非零帳戶,並自動將其新增到跟蹤帳戶列表中。 // 注意,SelfDerive將遞增指定路徑的最後一個元件,而不是下降到子路徑,以允許從非零元件開始發現帳戶。 // 你可以通過傳遞一個nil的ChainStateReader來禁用自動賬號發現。 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 請求錢包來給傳入的hash進行簽名。 // 它可以通過其中包含的地址(或可選地藉助嵌入式URL欄位中的任何位置元資料)來查詢指定的帳戶。 // 如果錢包需要額外的驗證才能簽名(比如說需要密碼來解鎖賬號,或者是需要一個PIN程式碼來驗證交易), // 會返回一個AuthNeededError的錯誤,裡面包含了使用者的資訊,以及哪些欄位或者操作需要提供。 // 使用者可以通過SignHashWithPassphrase來簽名或者通過其他手段(在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 to 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 請求錢包對指定的交易進行簽名。 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. // SignHashWithPassphrase 請求錢包使用給定的passphrase來作為額外的認證資訊去簽名給定的hash 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. // SignHashWithPassphrase 請求錢包使用給定的passphrase來作為額外的認證資訊去簽名給定的transaction SignTxWithPassphrase(account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error) }
View Code

三、Backend

// Backend is a "wallet provider" that may contain a batch of accounts they can
// sign transactions with and upon request, do so.
// 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.
    // 檢索backend當前知道的一批錢包,返回的錢包預設是關閉的
    // 所產生的錢包列表將根據後端分配的內部URL按字母順序排序。由於錢包(特別是硬體錢包)可能會開啟和關閉,
    // 所以在隨後的檢索過程中,相同的錢包可能會出現在列表中的不同位置。
    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
}