Svarog cebcab7f5c
Implement BIP-143-like sighash (#1598)
* Move CalculateSignatureHash to consensushashing

* Added CalcSignatureHash_BIP143 with all parameters except the re-used hashes

* Add handling of outputHash

* Add sequencesHash to the mix

* Add previousOutputsHash to the mix

* Replace legacy CalculateSigHash with new one, and re-wire to all non-test code

* Add missing types to WriteElement

* Fix tests in txscript

* Fix tests in rest of code

* Add missing comments

* Add SubnetworkID and Gas to sigHash

* Add TestCalculateSignatureHash

* Invert condition in SigHashSingle getOutputsHash

* Explicitly define that payloadHash for native transactions is 0

* added benchmark to CalculateSignatureHash

* Reformat call for signAndCheck

* Change SigHashes to be true bit-fields

* Add check for transaction version

* Write length of byte array in WriteElement

* hashOutpoint should get outpoint, not txIn

* Use inputIndex instead of i to determine SigHashType

* Use correct transaction version + fix some typos

* Fix hashes in test

* Reformat an overly-long line

* Replace checkHashTypeEncoding with caalls to hashType.IsStandardSigHashType

* Convert hashType to uint8

* Add comment
2021-03-17 15:17:38 +02:00

107 lines
3.1 KiB
Go

// Copyright (c) 2017 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package txscript
import (
"testing"
)
// TestErrorCodeStringer tests the stringized output for the ErrorCode type.
func TestErrorCodeStringer(t *testing.T) {
t.Parallel()
tests := []struct {
in ErrorCode
want string
}{
{ErrInternal, "ErrInternal"},
{ErrInvalidFlags, "ErrInvalidFlags"},
{ErrInvalidIndex, "ErrInvalidIndex"},
{ErrUnsupportedAddress, "ErrUnsupportedAddress"},
{ErrTooManyRequiredSigs, "ErrTooManyRequiredSigs"},
{ErrNotMultisigScript, "ErrNotMultisigScript"},
{ErrEarlyReturn, "ErrEarlyReturn"},
{ErrEmptyStack, "ErrEmptyStack"},
{ErrEvalFalse, "ErrEvalFalse"},
{ErrScriptUnfinished, "ErrScriptUnfinished"},
{ErrInvalidProgramCounter, "ErrInvalidProgramCounter"},
{ErrScriptTooBig, "ErrScriptTooBig"},
{ErrElementTooBig, "ErrElementTooBig"},
{ErrTooManyOperations, "ErrTooManyOperations"},
{ErrStackOverflow, "ErrStackOverflow"},
{ErrInvalidPubKeyCount, "ErrInvalidPubKeyCount"},
{ErrInvalidSignatureCount, "ErrInvalidSignatureCount"},
{ErrNumberTooBig, "ErrNumberTooBig"},
{ErrVerify, "ErrVerify"},
{ErrEqualVerify, "ErrEqualVerify"},
{ErrNumEqualVerify, "ErrNumEqualVerify"},
{ErrCheckSigVerify, "ErrCheckSigVerify"},
{ErrCheckMultiSigVerify, "ErrCheckMultiSigVerify"},
{ErrDisabledOpcode, "ErrDisabledOpcode"},
{ErrReservedOpcode, "ErrReservedOpcode"},
{ErrMalformedPush, "ErrMalformedPush"},
{ErrInvalidStackOperation, "ErrInvalidStackOperation"},
{ErrUnbalancedConditional, "ErrUnbalancedConditional"},
{ErrMinimalData, "ErrMinimalData"},
{ErrInvalidSigHashType, "ErrInvalidSigHashType"},
{ErrSigLength, "ErrSigLength"},
{ErrSigHighS, "ErrSigHighS"},
{ErrNotPushOnly, "ErrNotPushOnly"},
{ErrPubKeyFormat, "ErrPubKeyFormat"},
{ErrCleanStack, "ErrCleanStack"},
{ErrNullFail, "ErrNullFail"},
{ErrDiscourageUpgradableNOPs, "ErrDiscourageUpgradableNOPs"},
{ErrNegativeLockTime, "ErrNegativeLockTime"},
{ErrUnsatisfiedLockTime, "ErrUnsatisfiedLockTime"},
{ErrMinimalIf, "ErrMinimalIf"},
{0xffff, "Unknown ErrorCode (65535)"},
}
// Detect additional error codes that don't have the stringer added.
if len(tests)-1 != int(numErrorCodes) {
t.Errorf("It appears an error code was added without adding an " +
"associated stringer test")
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result := test.in.String()
if result != test.want {
t.Errorf("String #%d\n got: %s want: %s", i, result,
test.want)
continue
}
}
}
// TestError tests the error output for the Error type.
func TestError(t *testing.T) {
t.Parallel()
tests := []struct {
in Error
want string
}{
{
Error{Description: "some error"},
"some error",
},
{
Error{Description: "human-readable error"},
"human-readable error",
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
result := test.in.Error()
if result != test.want {
t.Errorf("Error #%d\n got: %s want: %s", i, result,
test.want)
continue
}
}
}