github.com/60ke/go-ethereum@v1.10.2/ethclient/signer.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 ethclient
    18  
    19  import (
    20  	"errors"
    21  	"math/big"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/core/types"
    25  )
    26  
    27  // senderFromServer is a types.Signer that remembers the sender address returned by the RPC
    28  // server. It is stored in the transaction's sender address cache to avoid an additional
    29  // request in TransactionSender.
    30  type senderFromServer struct {
    31  	addr      common.Address
    32  	blockhash common.Hash
    33  }
    34  
    35  var errNotCached = errors.New("sender not cached")
    36  
    37  func setSenderFromServer(tx *types.Transaction, addr common.Address, block common.Hash) {
    38  	// Use types.Sender for side-effect to store our signer into the cache.
    39  	types.Sender(&senderFromServer{addr, block}, tx)
    40  }
    41  
    42  func (s *senderFromServer) Equal(other types.Signer) bool {
    43  	os, ok := other.(*senderFromServer)
    44  	return ok && os.blockhash == s.blockhash
    45  }
    46  
    47  func (s *senderFromServer) Sender(tx *types.Transaction) (common.Address, error) {
    48  	if s.blockhash == (common.Hash{}) {
    49  		return common.Address{}, errNotCached
    50  	}
    51  	return s.addr, nil
    52  }
    53  
    54  func (s *senderFromServer) ChainID() *big.Int {
    55  	panic("can't sign with senderFromServer")
    56  }
    57  func (s *senderFromServer) Hash(tx *types.Transaction) common.Hash {
    58  	panic("can't sign with senderFromServer")
    59  }
    60  func (s *senderFromServer) SignatureValues(tx *types.Transaction, sig []byte) (R, S, V *big.Int, err error) {
    61  	panic("can't sign with senderFromServer")
    62  }