github.com/decred/dcrlnd@v0.7.6/fuzz/zpay32/encode.go (about)

     1  //go:build gofuzz
     2  // +build gofuzz
     3  
     4  package zpay32fuzz
     5  
     6  import (
     7  	"encoding/hex"
     8  
     9  	"github.com/decred/dcrd/chaincfg/chainhash"
    10  	"github.com/decred/dcrd/chaincfg/v3"
    11  	"github.com/decred/dcrd/dcrec/secp256k1/v4"
    12  	"github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"
    13  	"github.com/decred/dcrlnd/zpay32"
    14  )
    15  
    16  // Fuzz_encode is used by go-fuzz.
    17  func Fuzz_encode(data []byte) int {
    18  	inv, err := zpay32.Decode(string(data), chaincfg.TestNet3Params())
    19  	if err != nil {
    20  		return 1
    21  	}
    22  
    23  	// Call these functions as a sanity check to make sure the invoice
    24  	// is well-formed.
    25  	_ = inv.MinFinalCLTVExpiry()
    26  	_ = inv.Expiry()
    27  
    28  	// Initialize the static key we will be using for this fuzz test.
    29  	testPrivKeyBytes, _ := hex.DecodeString("e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2db734")
    30  	testPrivKey := secp256k1.PrivKeyFromBytes(testPrivKeyBytes)
    31  
    32  	// Then, initialize the testMessageSigner so we can encode out
    33  	// invoices with this private key.
    34  	testMessageSigner := zpay32.MessageSigner{
    35  		SignCompact: func(msg []byte) ([]byte, error) {
    36  			hash := chainhash.HashB(msg)
    37  			sig := ecdsa.SignCompact(
    38  				testPrivKey, hash, true)
    39  			return sig, nil
    40  		},
    41  	}
    42  	_, err = inv.Encode(testMessageSigner)
    43  	if err != nil {
    44  		return 1
    45  	}
    46  
    47  	return 1
    48  }