github.com/klaytn/klaytn@v1.12.1/blockchain/types/tx_signature.go (about)

     1  // Copyright 2019 The klaytn Authors
     2  // This file is part of the klaytn library.
     3  //
     4  // The klaytn 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 klaytn 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 klaytn library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package types
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"encoding/json"
    22  	"math/big"
    23  
    24  	"github.com/klaytn/klaytn/common"
    25  	"github.com/klaytn/klaytn/common/hexutil"
    26  	"github.com/klaytn/klaytn/crypto"
    27  )
    28  
    29  // TxSignature contains a signature of tx (V, R, S).
    30  type TxSignature struct {
    31  	V *big.Int
    32  	R *big.Int
    33  	S *big.Int
    34  }
    35  
    36  // TxSignature contains a signature of tx (V, R, S) as types of hexutil.Big.
    37  type TxSignatureJSON struct {
    38  	V *hexutil.Big
    39  	R *hexutil.Big
    40  	S *hexutil.Big
    41  }
    42  
    43  func NewTxSignature() *TxSignature {
    44  	return &TxSignature{
    45  		big.NewInt(0),
    46  		big.NewInt(0),
    47  		big.NewInt(0),
    48  	}
    49  }
    50  
    51  func NewTxSignatureWithValues(signer Signer, tx *Transaction, txhash common.Hash, prv *ecdsa.PrivateKey) (*TxSignature, error) {
    52  	sig, err := crypto.Sign(txhash[:], prv)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  
    57  	txsig := &TxSignature{}
    58  
    59  	txsig.R, txsig.S, txsig.V, err = signer.SignatureValues(tx, sig)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  
    64  	return txsig, nil
    65  }
    66  
    67  func (t *TxSignature) ChainId() *big.Int {
    68  	return deriveChainId(t.V)
    69  }
    70  
    71  func (t *TxSignature) RawSignatureValues() *TxSignature {
    72  	return t
    73  }
    74  
    75  func (t *TxSignature) ValidateSignature() bool {
    76  	return validateSignature(t.V, t.R, t.S)
    77  }
    78  
    79  // RecoverAddress returns an address with given parameters.
    80  // txhash: a hash generated by tx.Hash().
    81  // homestead: true if Homestead or later.
    82  // vfunc: V in the signature is treated differently by Signer. This function is for the treatment.
    83  func (t *TxSignature) RecoverAddress(txhash common.Hash, homestead bool, vfunc func(*big.Int) *big.Int) (common.Address, error) {
    84  	V := vfunc(t.V)
    85  	return recoverPlain(txhash, t.R, t.S, V, homestead)
    86  }
    87  
    88  // RecoverPubKey returns a list of public keys with given parameters.
    89  // txhash: a hash generated by tx.Hash().
    90  // homestead: true if Homestead or later.
    91  // vfunc: V in the signature is treated differently by Signer. This function is for the treatment.
    92  func (t *TxSignature) RecoverPubkey(txhash common.Hash, homestead bool, vfunc func(*big.Int) *big.Int) (*ecdsa.PublicKey, error) {
    93  	v := vfunc(t.V)
    94  	return recoverPlainPubkey(txhash, t.R, t.S, v, homestead)
    95  }
    96  
    97  func (t *TxSignature) equal(tb *TxSignature) bool {
    98  	return t.V.Cmp(tb.V) == 0 &&
    99  		t.R.Cmp(tb.R) == 0 &&
   100  		t.S.Cmp(tb.S) == 0
   101  }
   102  
   103  func (t *TxSignature) string() string {
   104  	b, _ := json.Marshal(t)
   105  
   106  	return string(b)
   107  }