github.com/halybang/go-ethereum@v1.0.5-0.20180325041310-3b262bc1367c/accounts/accounts.go (about)

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