github.com/annchain/OG@v0.0.9/og/verifier/tx_format_verifier.go (about)

     1  package verifier
     2  
     3  import (
     4  	"crypto/sha256"
     5  	"encoding/hex"
     6  	"fmt"
     7  	types2 "github.com/annchain/OG/arefactor/og/types"
     8  	"github.com/annchain/OG/arefactor/og_interface"
     9  	"github.com/annchain/OG/common"
    10  	"github.com/annchain/OG/common/crypto"
    11  	ogcrypto2 "github.com/annchain/OG/deprecated/ogcrypto"
    12  	"github.com/annchain/OG/og/types"
    13  	"github.com/annchain/OG/ogcore/miner"
    14  
    15  	"github.com/sirupsen/logrus"
    16  	"math/big"
    17  )
    18  
    19  type TxFormatVerifier struct {
    20  	MaxTxHash         types2.Hash // The difficulty of TxHash
    21  	MaxMinedHash      types2.Hash // The difficulty of MinedHash
    22  	NoVerifyHash      bool
    23  	NoVerifySignatrue bool
    24  	powMiner          miner.PoWMiner
    25  }
    26  
    27  func (v *TxFormatVerifier) Name() string {
    28  	return "TxFormatVerifier"
    29  }
    30  
    31  func (c *TxFormatVerifier) String() string {
    32  	return c.Name()
    33  }
    34  
    35  func (v *TxFormatVerifier) Independent() bool {
    36  	return true
    37  }
    38  
    39  func (v *TxFormatVerifier) Verify(t types.Txi) bool {
    40  	// NOTE: disabled shortcut verification because of complexity.
    41  	//if t.IsVerified().IsFormatVerified() {
    42  	//	return true
    43  	//}
    44  	if !v.NoVerifyHash && !v.VerifyHash(t) {
    45  		logrus.WithField("tx", t).Debug("Hash not valid")
    46  		return false
    47  	}
    48  	if !v.NoVerifySignatrue && !v.VerifySignature(t) {
    49  		logrus.WithField("sig targets ", hex.EncodeToString(t.SignatureTargets())).WithField("tx", t).Warn("signature not valid")
    50  		return false
    51  	}
    52  	return true
    53  }
    54  
    55  func (v *TxFormatVerifier) VerifyHash(t types.Txi) bool {
    56  	txType := t.GetType()
    57  
    58  	switch txType {
    59  	case types.TxBaseTypeTx:
    60  		// hash under pow
    61  		if !v.powMiner.IsGoodTx(t.(*types.Tx), v.MaxMinedHash, v.MaxTxHash) {
    62  			logrus.WithField("tx", t).Debug("Hash is not under pow limit")
    63  			return false
    64  		}
    65  	case types.TxBaseTypeSequencer:
    66  		// hash under pow
    67  		if !v.powMiner.IsGoodSequencer(t.(*types.Sequencer), v.MaxMinedHash, v.MaxTxHash) {
    68  			logrus.WithField("tx", t).Debug("Hash is not under pow limit")
    69  			return false
    70  		}
    71  	}
    72  	return true
    73  }
    74  
    75  func (v *TxFormatVerifier) VerifyFrom(t types.Txi) bool {
    76  	//if t.GetSender() == nil {
    77  	//	logrus.Warn("verify sig failed, from is nil")
    78  	//	return false
    79  	//}
    80  	if og_interface.Signer.CanRecoverPubFromSig() {
    81  
    82  	}
    83  	return true
    84  }
    85  
    86  func (v *TxFormatVerifier) VerifySignature(t types.Txi) bool {
    87  	//if t.GetType() == archive2.TxBaseTypeArchive {
    88  	//	return true
    89  	//}
    90  	txType := t.GetType()
    91  
    92  	switch txType {
    93  	case types.TxBaseTypeTx:
    94  		tx := t.(*types.Tx)
    95  		ok := og_interface.Signer.Verify(tx.PublicKey, tx.Signature, t.SignatureTargets())
    96  		if og_interface.Signer.CanRecoverPubFromSig() {
    97  			pub, err := crypto.PublicKeyFromSignature(tx.Hash, &tx.Signature)
    98  			if err != nil {
    99  				logrus.WithError(err).Warn("error on recovering pubkey from sig")
   100  				return false
   101  			}
   102  			var addr common.Address
   103  			copy(addr.Bytes[:], ogcrypto2.Keccak256(pub.KeyBytes)[12:])
   104  			t.SetSender(addr)
   105  		}
   106  		return ok
   107  	case types.TxBaseTypeSequencer:
   108  		//tx := t.(*types.Sequencer)
   109  		// TODO: what should happen here?
   110  		//ok := ogcrypto.Signer.Verify(tx.PublicKey, tx.Signature, t.SignatureTargets())
   111  		return true
   112  		//return ok
   113  	}
   114  	return false
   115  }
   116  
   117  //func (v *TxFormatVerifier) VerifySourceAddress(t types.Txi) bool {
   118  //	if ogcrypto.Signer.CanRecoverPubFromSig() {
   119  //		//address was set by recovering signature ,
   120  //		return true
   121  //	}
   122  //	switch t.(type) {
   123  //	case *archive2.Tx:
   124  //		return t.(*archive2.Tx).From.Bytes == ogcrypto.Signer.Address(ogcrypto.Signer.PublicKeyFromBytes(t.GetBase().PublicKey)).Bytes
   125  //	case *types.Sequencer:
   126  //		return t.(*types.Sequencer).Issuer.Bytes == ogcrypto.Signer.Address(ogcrypto.Signer.PublicKeyFromBytes(t.GetBase().PublicKey)).Bytes
   127  //	case *campaign.Campaign:
   128  //		return t.(*campaign.Campaign).Issuer.Bytes == ogcrypto.Signer.Address(ogcrypto.Signer.PublicKeyFromBytes(t.GetBase().PublicKey)).Bytes
   129  //	case *campaign.TermChange:
   130  //		return t.(*campaign.TermChange).Issuer.Bytes == ogcrypto.Signer.Address(ogcrypto.Signer.PublicKeyFromBytes(t.GetBase().PublicKey)).Bytes
   131  //	case *archive.Archive:
   132  //		return true
   133  //	default:
   134  //		return true
   135  //	}
   136  //}
   137  
   138  // SignatureValues returns signature values. This signature
   139  // needs to be in the [R || S || V] format where V is 0 or 1.
   140  func (t *TxFormatVerifier) SignatureValues(sig []byte) (r, s, v *big.Int, err error) {
   141  	if len(sig) != 65 {
   142  		return r, s, v, fmt.Errorf("wrong size for signature: got %d, want 65", len(sig))
   143  	}
   144  	r = new(big.Int).SetBytes(sig[:32])
   145  	s = new(big.Int).SetBytes(sig[32:64])
   146  	v = new(big.Int).SetBytes([]byte{sig[64] + 27})
   147  	return r, s, v, nil
   148  }
   149  
   150  func Sha256(bytes []byte) []byte {
   151  	hasher := sha256.New()
   152  	hasher.Write(bytes)
   153  	return hasher.Sum(nil)
   154  }