github.com/gochain-io/gochain@v2.2.26+incompatible/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  	"context"
    22  	"math/big"
    23  
    24  	"github.com/gochain-io/gochain"
    25  	"github.com/gochain-io/gochain/common"
    26  	"github.com/gochain-io/gochain/core/types"
    27  	"github.com/gochain-io/gochain/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 gochain.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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
   130  }
   131  
   132  // Backend is a "wallet provider" that may contain a batch of accounts they can
   133  // sign transactions with and upon request, do so.
   134  type Backend interface {
   135  	// Wallets retrieves the list of wallets the backend is currently aware of.
   136  	//
   137  	// The returned wallets are not opened by default. For software HD wallets this
   138  	// means that no base seeds are decrypted, and for hardware wallets that no actual
   139  	// connection is established.
   140  	//
   141  	// The resulting wallet list will be sorted alphabetically based on its internal
   142  	// URL assigned by the backend. Since wallets (especially hardware) may come and
   143  	// go, the same wallet might appear at a different positions in the list during
   144  	// subsequent retrievals.
   145  	Wallets() []Wallet
   146  
   147  	// Subscribe creates an async subscription to receive notifications when the
   148  	// backend detects the arrival or departure of a wallet.
   149  	Subscribe(sink chan<- WalletEvent) event.Subscription
   150  }
   151  
   152  // WalletEventType represents the different event types that can be fired by
   153  // the wallet subscription subsystem.
   154  type WalletEventType int
   155  
   156  const (
   157  	// WalletArrived is fired when a new wallet is detected either via USB or via
   158  	// a filesystem event in the keystore.
   159  	WalletArrived WalletEventType = iota
   160  
   161  	// WalletOpened is fired when a wallet is successfully opened with the purpose
   162  	// of starting any background processes such as automatic key derivation.
   163  	WalletOpened
   164  
   165  	// WalletDropped
   166  	WalletDropped
   167  )
   168  
   169  // WalletEvent is an event fired by an account backend when a wallet arrival or
   170  // departure is detected.
   171  type WalletEvent struct {
   172  	Wallet Wallet          // Wallet instance arrived or departed
   173  	Kind   WalletEventType // Event type that happened in the system
   174  }