github.com/klaytn/klaytn@v1.10.2/accounts/accounts.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     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  // This file is derived from accounts/accounts.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package accounts
    22  
    23  import (
    24  	"math/big"
    25  
    26  	"github.com/klaytn/klaytn"
    27  	"github.com/klaytn/klaytn/blockchain/types"
    28  	"github.com/klaytn/klaytn/common"
    29  	"github.com/klaytn/klaytn/event"
    30  )
    31  
    32  // Account represents a Klaytn account located at a specific location defined
    33  // by the optional URL field.
    34  type Account struct {
    35  	Address common.Address `json:"address"` // Klaytn account address derived from the key
    36  	URL     URL            `json:"url"`     // Optional resource locator within a backend
    37  }
    38  
    39  // Wallet represents a software or hardware wallet that might contain one or more
    40  // accounts (derived from the same seed).
    41  type Wallet interface {
    42  	// URL retrieves the canonical path under which this wallet is reachable. It is
    43  	// user by upper layers to define a sorting order over all wallets from multiple
    44  	// backends.
    45  	URL() URL
    46  
    47  	// Status returns a textual status to aid the user in the current state of the
    48  	// wallet. It also returns an error indicating any failure the wallet might have
    49  	// encountered.
    50  	Status() (string, error)
    51  
    52  	// Open initializes access to a wallet instance. It is not meant to unlock or
    53  	// decrypt account keys, rather simply to establish a connection to hardware
    54  	// wallets and/or to access derivation seeds.
    55  	//
    56  	// The passphrase parameter may or may not be used by the implementation of a
    57  	// particular wallet instance. The reason there is no passwordless open method
    58  	// is to strive towards a uniform wallet handling, oblivious to the different
    59  	// backend providers.
    60  	//
    61  	// Please note, if you open a wallet, you must close it to release any allocated
    62  	// resources (especially important when working with hardware wallets).
    63  	Open(passphrase string) error
    64  
    65  	// Close releases any resources held by an open wallet instance.
    66  	Close() error
    67  
    68  	// Accounts retrieves the list of signing accounts the wallet is currently aware
    69  	// of. For hierarchical deterministic wallets, the list will not be exhaustive,
    70  	// rather only contain the accounts explicitly pinned during account derivation.
    71  	Accounts() []Account
    72  
    73  	// Contains returns whether an account is part of this particular wallet or not.
    74  	Contains(account Account) bool
    75  
    76  	// Derive attempts to explicitly derive a hierarchical deterministic account at
    77  	// the specified derivation path. If requested, the derived account will be added
    78  	// to the wallet's tracked account list.
    79  	Derive(path DerivationPath, pin bool) (Account, error)
    80  
    81  	// SelfDerive sets a base account derivation path from which the wallet attempts
    82  	// to discover non zero accounts and automatically add them to list of tracked
    83  	// accounts.
    84  	//
    85  	// Note, self derivaton will increment the last component of the specified path
    86  	// opposed to decending into a child path to allow discovering accounts starting
    87  	// from non zero components.
    88  	//
    89  	// You can disable automatic account discovery by calling SelfDerive with a nil
    90  	// chain state reader.
    91  	SelfDerive(base DerivationPath, chain klaytn.ChainReader)
    92  
    93  	// SignHash requests the wallet to sign the given hash.
    94  	//
    95  	// It looks up the account specified either solely via its address contained within,
    96  	// or optionally with the aid of any location metadata from the embedded URL field.
    97  	//
    98  	// If the wallet requires additional authentication to sign the request (e.g.
    99  	// a password to decrypt the account, or a PIN code o verify the transaction),
   100  	// an AuthNeededError instance will be returned, containing infos for the user
   101  	// about which fields or actions are needed. The user may retry by providing
   102  	// the needed details via SignHashWithPassphrase, or by other means (e.g. unlock
   103  	// the account in a keystore).
   104  	SignHash(account Account, hash []byte) ([]byte, error)
   105  
   106  	// SignTx requests the wallet to sign the given transaction.
   107  	//
   108  	// It looks up the account specified either solely via its address contained within,
   109  	// or optionally with the aid of any location metadata from the embedded URL field.
   110  	//
   111  	// If the wallet requires additional authentication to sign the request (e.g.
   112  	// a password to decrypt the account, or a PIN code o verify the transaction),
   113  	// an AuthNeededError instance will be returned, containing infos for the user
   114  	// about which fields or actions are needed. The user may retry by providing
   115  	// the needed details via SignTxWithPassphrase, or by other means (e.g. unlock
   116  	// the account in a keystore).
   117  	SignTx(account Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
   118  
   119  	// SignTxAsFeePayer requests the wallet to sign the given transaction as a fee payer.
   120  	SignTxAsFeePayer(account Account, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
   121  
   122  	// SignHashWithPassphrase requests the wallet to sign the given hash with the
   123  	// given passphrase as extra authentication information.
   124  	//
   125  	// It looks up the account specified either solely via its address contained within,
   126  	// or optionally with the aid of any location metadata from the embedded URL field.
   127  	SignHashWithPassphrase(account Account, passphrase string, hash []byte) ([]byte, error)
   128  
   129  	// SignTxWithPassphrase requests the wallet to sign the given transaction, with the
   130  	// given passphrase as extra authentication information.
   131  	//
   132  	// It looks up the account specified either solely via its address contained within,
   133  	// or optionally with the aid of any location metadata from the embedded URL field.
   134  	SignTxWithPassphrase(account Account, passphrase string, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
   135  
   136  	// SignTxAsFeePayerWithPassphrase requests the wallet to sign the given transaction
   137  	// as a fee payer, with the given passphrase as extra authentication information.
   138  	SignTxAsFeePayerWithPassphrase(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  }