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