github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/accounts/accounts.go (about)

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