decred.org/dcrdex@v1.0.3/server/asset/btc/script.go (about)

     1  // This code is available on the terms of the project LICENSE.md file,
     2  // also available online at https://blueoakcouncil.org/license/1.0.0.
     3  
     4  package btc
     5  
     6  import (
     7  	"crypto/sha256"
     8  	"fmt"
     9  
    10  	"github.com/btcsuite/btcd/btcec/v2"
    11  	"github.com/btcsuite/btcd/btcec/v2/ecdsa"
    12  )
    13  
    14  // checkSig checks that the message's signature was created with the
    15  // private key for the provided public key.
    16  func checkSig(msg, pkBytes, sigBytes []byte) error {
    17  	pubKey, err := btcec.ParsePubKey(pkBytes)
    18  	if err != nil {
    19  		return fmt.Errorf("error decoding PublicKey from bytes: %w", err)
    20  	}
    21  
    22  	signature, err := ecdsa.ParseDERSignature(sigBytes)
    23  	if err != nil {
    24  		return fmt.Errorf("error decoding Signature from bytes: %w", err)
    25  	}
    26  	hash := sha256.Sum256(msg)
    27  	if !signature.Verify(hash[:], pubKey) {
    28  		return fmt.Errorf("signature verification failed")
    29  	}
    30  	return nil
    31  }