Skip to content

Commit

Permalink
stdscript: Reject multisig neg thresholds.
Browse files Browse the repository at this point in the history
This modifies the MultiSigScriptV0 convenience func for creating a
multisig script to return an error if the caller improperly calls it
with a negative threshold.
  • Loading branch information
davecgh committed Jan 1, 2022
1 parent 1f67993 commit cc74183
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions txscript/stdscript/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const (
// supported.
ErrUnsupportedScriptVersion = ErrorKind("ErrUnsupportedScriptVersion")

// ErrNegativeRequiredSigs is returned from MultiSigScript when the
// specified number of required signatures is negative.
ErrNegativeRequiredSigs = ErrorKind("ErrNegativeRequiredSigs")

// ErrTooManyRequiredSigs is returned from MultiSigScript when the
// specified number of required signatures is larger than the number of
// provided public keys.
Expand Down
1 change: 1 addition & 0 deletions txscript/stdscript/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func TestErrorKindStringer(t *testing.T) {
want string
}{
{ErrUnsupportedScriptVersion, "ErrUnsupportedScriptVersion"},
{ErrNegativeRequiredSigs, "ErrNegativeRequiredSigs"},
{ErrTooManyRequiredSigs, "ErrTooManyRequiredSigs"},
{ErrPubKeyType, "ErrPubKeyType"},
{ErrTooMuchNullData, "ErrTooMuchNullData"},
Expand Down
5 changes: 5 additions & 0 deletions txscript/stdscript/scriptv0.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,11 @@ func DetermineRequiredSigsV0(script []byte) uint16 {
// An Error with kind ErrTooManyRequiredSigs will be returned if the threshold
// is larger than the number of keys provided.
func MultiSigScriptV0(threshold int, pubKeys ...[]byte) ([]byte, error) {
if threshold < 0 {
str := fmt.Sprintf("unable to generate multisig script with %d "+
"required signatures", threshold)
return nil, makeError(ErrNegativeRequiredSigs, str)
}
if len(pubKeys) < threshold {
str := fmt.Sprintf("unable to generate multisig script with %d "+
"required signatures when there are only %d public keys available",
Expand Down
6 changes: 6 additions & 0 deletions txscript/stdscript/scriptv0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,12 @@ func TestMultiSigScriptV0(t *testing.T) {
threshold: 1,
expected: "",
err: ErrPubKeyType,
}, {
name: "reject negative threshold",
pubKeys: [][]byte{p2pkUncompressedMain},
threshold: -1,
expected: "",
err: ErrNegativeRequiredSigs,
}}

for _, test := range tests {
Expand Down

0 comments on commit cc74183

Please sign in to comment.