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