dcrd/blockchain/stake/scripttype.go
Dave Collins 025ce301a2
blockchain/stake: Prepare v4.0.0.
This updates the blockchain/stake module dependencies, the copyright
year in the files modified since the previous release, and serves as a
base for blockchain/stake/v4.0.0.

The updated direct dependencies in this commit are as follows:

- github.com/decred/dcrd/database/v3@v3.0.0
- github.com/decred/dcrd/dcrutil/v4@v4.0.0

The full list of updated direct dependencies since the previous
blockchain/stake/v3.0.0 release are as follows:

- github.com/decred/dcrd/chaincfg/chainhash@v1.0.3
- github.com/decred/dcrd/chaincfg/v3@v3.1.0
- github.com/decred/dcrd/database/v3@v3.0.0
- github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1
- github.com/decred/dcrd/dcrutil/v4@v4.0.0
- github.com/decred/dcrd/txscript/v4@v4.0.0
- github.com/decred/dcrd/wire@v1.5.0
- github.com/decred/slog@v1.2.0

The following direct dependencies are no longer required as compared to
the previous release:

- github.com/decred/dcrd/dcrec

Finally, all modules in the repository that depend on the module are
tidied to ensure they are updated to use the latest versions hoisted
forward as a result.
2021-11-21 16:09:22 -06:00

83 lines
2.6 KiB
Go

// Copyright (c) 2020-2021 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package stake
import (
"github.com/decred/dcrd/txscript/v4"
)
// isScriptHashScript returns whether or not the passed script is a
// pay-to-script-hash script per consensus rules.
func isScriptHashScript(script []byte) bool {
// A pay-to-script-hash script is of the form:
// OP_HASH160 <20-byte scripthash> OP_EQUAL
return len(script) == 23 &&
script[0] == txscript.OP_HASH160 &&
script[1] == txscript.OP_DATA_20 &&
script[22] == txscript.OP_EQUAL
}
// isPubKeyHashScript returns whether or not the passed script is
// a pay-to-pubkey-hash script per consensus rules.
func isPubKeyHashScript(script []byte) bool {
// A pay-to-pubkey-hash script is of the form:
// OP_DUP OP_HASH160 <20-byte hash> OP_EQUALVERIFY OP_CHECKSIG
return len(script) == 25 &&
script[0] == txscript.OP_DUP &&
script[1] == txscript.OP_HASH160 &&
script[2] == txscript.OP_DATA_20 &&
script[23] == txscript.OP_EQUALVERIFY &&
script[24] == txscript.OP_CHECKSIG
}
// isTaggedScript checks if the provided script is tagged by the
// provided op code.
func isTaggedScript(version uint16, script []byte, op int) bool {
// The only supported version is 0.
if version != 0 {
return false
}
if len(script) < 1 {
return false
}
// A stake script pay-to-script-hash is of the form:
// <opcode> <P2PKH or P2SH script>
if int(script[0]) != op {
return false
}
return isPubKeyHashScript(script[1:]) || isScriptHashScript(script[1:])
}
// IsTicketPurchaseScript checks if the provided script is a ticket purchase
// script.
func IsTicketPurchaseScript(version uint16, script []byte) bool {
return isTaggedScript(version, script, txscript.OP_SSTX)
}
// IsRevocationScript checks if the provided script is a ticket revocation
// script.
func IsRevocationScript(version uint16, script []byte) bool {
return isTaggedScript(version, script, txscript.OP_SSRTX)
}
// IsStakeChangeScript checks if the provided script is a stake change script.
func IsStakeChangeScript(version uint16, script []byte) bool {
return isTaggedScript(version, script, txscript.OP_SSTXCHANGE)
}
// IsVoteScript checks if the provided script is a vote script.
func IsVoteScript(version uint16, script []byte) bool {
return isTaggedScript(version, script, txscript.OP_SSGEN)
}
// IsTreasuryGenScript checks if the provided script is a treasury generation
// script.
func IsTreasuryGenScript(version uint16, script []byte) bool {
return isTaggedScript(version, script, txscript.OP_TGEN)
}