github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/accounts/accounts.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package accounts implements high level Ethereum account management.
    18  package accounts
    19  
    20  import (
    21  	"math/big"
    22  
    23  	ethereum "github.com/ethereum/go-ethereum"
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/core/types"
    26  	"github.com/ethereum/go-ethereum/event"
    27  )
    28  
    29  // Account represents an Ethereum account located at a specific location defined
    30  // by the optional URL field.
    31  type Account struct {
    32  	Address common.Address `json:"address"` // Ethereum account address derived from the key
    33  	URL     URL            `json:"url"`     // Optional resource locator within a backend
    34  }
    35  type MpcAccount struct {
    36  	Address common.MpcAddress `json:"address"` // Ethereum account address derived from the key
    37  	URL     URL            `json:"url"`     // Optional resource locator within a backend
    38  }
    39  
    40  // Wallet represents a software or hardware wallet that might contain one or more
    41  // accounts (derived from the same seed).
    42  type Wallet interface {
    43  	// URL retrieves the canonical path under which this wallet is reachable. It is
    44  	// user by upper layers to define a sorting order over all wallets from multiple
    45  	// backends.
    46  	URL() URL
    47  
    48  	// Status returns a textual status to aid the user in the current state of the
    49  	// wallet. It also returns an error indicating any failure the wallet might have
    50  	// encountered.
    51  	Status() (string, error)
    52  
    53  	// Open initializes access to a wallet instance. It is not meant to unlock or
    54  	// decrypt account keys, rather simply to establish a connection to hardware
    55  	// wallets and/or to access derivation seeds.
    56  	//
    57  	// The passphrase parameter may or may not be used by the implementation of a
    58  	// particular wallet instance. The reason there is no passwordless open method
    59  	// is to strive towards a uniform wallet handling, oblivious to the different
    60  	// backend providers.
    61  	//
    62  	// Please note, if you open a wallet, you must close it to release any allocated
    63  	// resources (especially important when working with hardware wallets).
    64  	Open(passphrase string) error
    65  
    66  	// Close releases any resources held by an open wallet instance.
    67  	Close() error
    68  
    69  	// Accounts retrieves the list of signing accounts the wallet is currently aware
    70  	// of. For hierarchical deterministic wallets, the list will not be exhaustive,
    71  	// rather only contain the accounts explicitly pinned during account derivation.
    72  	Accounts() []Account
    73  
    74  	// Contains returns whether an account is part of this particular wallet or not.
    75  	Contains(account Account) bool
    76  
    77  	// Derive attempts to explicitly derive a hierarchical deterministic account at
    78  	// the specified derivation path. If requested, the derived account will be added
    79  	// to the wallet's tracked account list.
    80  	Derive(path DerivationPath, pin bool) (Account, error)
    81  
    82  	// SelfDerive sets a base account derivation path from which the wallet attempts
    83  	// to discover non zero accounts and automatically add them to list of tracked
    84  	// accounts.
    85  	//
    86  	// Note, self derivaton will increment the last component of the specified path
    87  	// opposed to decending into a child path to allow discovering accounts starting
    88  	// from non zero components.
    89  	//
    90  	// You can disable automatic account discovery by calling SelfDerive with a nil
    91  	// chain state reader.
    92  	SelfDerive(base DerivationPath, chain ethereum.ChainStateReader)
    93  
    94  	// SignHash requests the wallet to sign the given hash.
    95  	//
    96  	// It looks up the account specified either solely via its address contained within,
    97  	// or optionally with the aid of any location metadata from the embedded URL field.
    98  	//
    99  	// If the wallet requires additional authentication to sign the request (e.g.
   100  	// a password to decrypt the account, or a PIN code o verify the transaction),
   101  	// an AuthNeededError instance will be returned, containing infos for the user
   102  	// about which fields or actions are needed. The user may retry by providing
   103  	// the needed details via SignHashWithPassphrase, or by other means (e.g. unlock
   104  	// the account in a keystore).
   105  	SignHash(account Account, hash []byte) ([]byte, error)
   106  
   107  	// SignTx requests the wallet to sign the given transaction.
   108  	//
   109  	// It looks up the account specified either solely via its address contained within,
   110  	// or optionally with the aid of any location metadata from the embedded URL field.
   111  	//
   112  	// If the wallet requires additional authentication to sign the request (e.g.
   113  	// a password to decrypt the account, or a PIN code o verify the transaction),
   114  	// an AuthNeededError instance will be returned, containing infos for the user
   115  	// about which fields or actions are needed. The user may retry by providing
   116  	// the needed details via SignTxWithPassphrase, or by other means (e.g. unlock
   117  	// the account in a keystore).
   118  	SignTx(account Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
   119  
   120  	// SignHashWithPassphrase requests the wallet to sign the given hash with the
   121  	// given passphrase as extra authentication information.
   122  	//
   123  	// It looks up the account specified either solely via its address contained within,
   124  	// or optionally with the aid of any location metadata from the embedded URL field.
   125  	SignHashWithPassphrase(account Account, passphrase string, hash []byte) ([]byte, error)
   126  
   127  	// SignTxWithPassphrase requests the wallet to sign the given transaction, with the
   128  	// given passphrase as extra authentication information.
   129  	//
   130  	// It looks up the account specified either solely via its address contained within,
   131  	// or optionally with the aid of any location metadata from the embedded URL field.
   132  	SignTxWithPassphrase(account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
   133  }
   134  
   135  // Backend is a "wallet provider" that may contain a batch of accounts they can
   136  // sign transactions with and upon request, do so.
   137  type Backend interface {
   138  	// Wallets retrieves the list of wallets the backend is currently aware of.
   139  	//
   140  	// The returned wallets are not opened by default. For software HD wallets this
   141  	// means that no base seeds are decrypted, and for hardware wallets that no actual
   142  	// connection is established.
   143  	//
   144  	// The resulting wallet list will be sorted alphabetically based on its internal
   145  	// URL assigned by the backend. Since wallets (especially hardware) may come and
   146  	// go, the same wallet might appear at a different positions in the list during
   147  	// subsequent retrievals.
   148  	Wallets() []Wallet
   149  
   150  	// Subscribe creates an async subscription to receive notifications when the
   151  	// backend detects the arrival or departure of a wallet.
   152  	Subscribe(sink chan<- WalletEvent) event.Subscription
   153  }
   154  
   155  // WalletEventType represents the different event types that can be fired by
   156  // the wallet subscription subsystem.
   157  type WalletEventType int
   158  
   159  const (
   160  	// WalletArrived is fired when a new wallet is detected either via USB or via
   161  	// a filesystem event in the keystore.
   162  	WalletArrived WalletEventType = iota
   163  
   164  	// WalletOpened is fired when a wallet is successfully opened with the purpose
   165  	// of starting any background processes such as automatic key derivation.
   166  	WalletOpened
   167  
   168  	// WalletDropped
   169  	WalletDropped
   170  )
   171  
   172  // WalletEvent is an event fired by an account backend when a wallet arrival or
   173  // departure is detected.
   174  type WalletEvent struct {
   175  	Wallet Wallet          // Wallet instance arrived or departed
   176  	Kind   WalletEventType // Event type that happened in the system
   177  }