diff --git a/btcd/txscript/hashcache_test.go b/btcd/txscript/hashcache_test.go index cee59b9956..18eb365371 100644 --- a/btcd/txscript/hashcache_test.go +++ b/btcd/txscript/hashcache_test.go @@ -5,10 +5,13 @@ package txscript import ( + "io" "math/rand" + "os" "testing" "time" + "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/davecgh/go-spew/spew" ) @@ -53,6 +56,57 @@ func genTestTx() (*wire.MsgTx, error) { return tx, nil } +func captureStdout(t *testing.T, f func()) string { + t.Helper() + + oldStdout := os.Stdout + r, w, err := os.Pipe() + if err != nil { + t.Fatalf("unable to create stdout pipe: %v", err) + } + + os.Stdout = w + defer func() { + os.Stdout = oldStdout + }() + + f() + + if err := w.Close(); err != nil { + t.Fatalf("unable to close stdout pipe: %v", err) + } + + output, err := io.ReadAll(r) + if err != nil { + t.Fatalf("unable to read stdout pipe: %v", err) + } + + return string(output) +} + +func TestSignatureHashFunctionsDoNotWriteStdout(t *testing.T) { + var prevHash chainhash.Hash + tx := wire.NewMsgTx(2) + tx.AddTxIn(wire.NewTxIn(wire.NewOutPoint(&prevHash, 0), nil, nil)) + tx.AddTxOut(wire.NewTxOut(1000, []byte{OP_TRUE})) + + output := captureStdout(t, func() { + sigHashes := NewTxSigHashes(tx) + + if _, err := CalcSignatureHash([]byte{OP_TRUE}, SigHashAll, tx, 0); err != nil { + t.Fatalf("unable to calculate signature hash: %v", err) + } + + if _, err := CalcWitnessSigHash([]byte{OP_TRUE}, sigHashes, SigHashAll, tx, 0, 1000); err != nil { + t.Fatalf("unable to calculate witness signature hash: %v", err) + } + }) + + if output != "" { + t.Fatalf("signature hash functions wrote to stdout: %q", output) + } +} + // TestHashCacheAddContainsHashes tests that after items have been added to the // hash cache, the ContainsHashes method returns true for all the items // inserted. Conversely, ContainsHashes should return false for any items diff --git a/btcd/txscript/script.go b/btcd/txscript/script.go index 39e4d43977..cdba7cce45 100644 --- a/btcd/txscript/script.go +++ b/btcd/txscript/script.go @@ -375,7 +375,6 @@ func removeOpcodeByData(pkscript []parsedOpcode, data []byte) []parsedOpcode { // hashing computation, reducing the complexity of validating SigHashAll inputs // from O(N^2) to O(N). func calcHashPrevOuts(tx *wire.MsgTx) chainhash.Hash { - fmt.Println("Keccak256") var b bytes.Buffer for _, in := range tx.TxIn { // First write out the 32-byte transaction ID one of whose @@ -399,8 +398,6 @@ func calcHashPrevOuts(tx *wire.MsgTx) chainhash.Hash { // hashing computation, reducing the complexity of validating SigHashAll inputs // from O(N^2) to O(N). func calcHashSequence(tx *wire.MsgTx) chainhash.Hash { - fmt.Println("Keccak256") - var b bytes.Buffer for _, in := range tx.TxIn { var buf [4]byte @@ -417,8 +414,6 @@ func calcHashSequence(tx *wire.MsgTx) chainhash.Hash { // signatures using the SigHashAll sighash type. This allows computation to be // cached, reducing the total hashing complexity from O(N^2) to O(N). func calcHashOutputs(tx *wire.MsgTx) chainhash.Hash { - fmt.Println("Keccak256") - var b bytes.Buffer for _, out := range tx.TxOut { wire.WriteTxOut(&b, 0, 0, out) @@ -440,8 +435,6 @@ func calcHashOutputs(tx *wire.MsgTx) chainhash.Hash { // the produced signature to be invalid. func calcWitnessSignatureHash(subScript []parsedOpcode, sigHashes *TxSigHashes, hashType SigHashType, tx *wire.MsgTx, idx int, amt int64) ([]byte, error) { - fmt.Println("Keccak256") - // As a sanity check, ensure the passed input index for the transaction // is valid. if idx > len(tx.TxIn)-1 { @@ -601,8 +594,6 @@ func CalcSignatureHash(script []byte, hashType SigHashType, tx *wire.MsgTx, idx // engine instance, calculate the signature hash to be used for signing and // verification. func calcSignatureHash(script []parsedOpcode, hashType SigHashType, tx *wire.MsgTx, idx int) []byte { - fmt.Println("Keccak256") - // The SigHashSingle signature type signs only the corresponding input // and output (the output with the same index number as the input). //