script

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2018 License: BSD-2-Clause Imports: 10 Imported by: 38

Documentation

Index

Constants

View Source
const (
	AddressBytesLength = 25
	Hash160BytesLength = 20
	PublicKeyToAddress = 0x00
	ScriptToAddress    = 5
)
View Source
const (
	DefaultSize = 28

	// MaxPubKeysPerMultiSig :  maximum number of public keys per multiSig
	MaxPubKeysPerMultiSig = 20

	// LockTimeThreshold threshold for nLockTime: below this value it is interpreted as block number,
	// otherwise as UNIX timestamp. Threshold is Tue Nov 5 00:53:20 1985 UTC
	LockTimeThreshold = 500000000

	// SequenceFinal setting sequence to this value for every input in a transaction
	// disables nLockTime.
	SequenceFinal = 0xffffffff

	MaxScriptSize        = 10000
	MaxScriptElementSize = 520
	MaxScriptOpCodes     = 201
	MaxOpsPerScript      = 201

	// MaxTxInStandardScriptSigSize is
	// Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
	// keys (remember the 520 byte limit on redeemScript size). That works
	// out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
	// bytes of scriptSig, which we round off to 1650 bytes for some minor
	// future-proofing. That's also enough to spend a 20-of-20 CHECKMULTISIG
	// scriptPubKey, though such a scriptPubKey is not considered standard.
	MaxTxInStandardScriptSigSize = 1650
)
View Source
const (
	// SequenceLockTimeDisableFlag below flags apply in the context of BIP 68*/
	// If this flag set, CTxIn::nSequence is NOT interpreted as a
	// relative lock-time. */
	SequenceLockTimeDisableFlag = 1 << 31

	// SequenceLockTimeTypeFlag if CTxIn::nSequence encodes a relative lock-time and this flag
	// is set, the relative lock-time has units of 512 seconds,
	// otherwise it specifies blocks with a granularity of 1.
	SequenceLockTimeTypeFlag = 1 << 22

	// SequenceLockTimeMask if CTxIn::nSequence encodes a relative lock-time, this mask is
	// applied to extract that lock-time from the sequence field.
	SequenceLockTimeMask = 0x0000ffff

	// SequenceLockTimeGranularity in order to use the same number of bits to encode roughly the
	// same wall-clock duration, and because blocks are naturally
	// limited to occur every 600s on average, the minimum granularity
	// for time-based relative lock-time is fixed at 512 seconds.
	// Converting from CTxIn::nSequence to seconds is performed by
	// multiplying by 512 = 2^9, or equivalently shifting up by
	// 9 bits.
	SequenceLockTimeGranularity = 9
)
View Source
const (
	ScriptVerifyNone = 0

	// Evaluate P2SH subscripts (softfork safe, BIP16).
	ScriptVerifyP2SH = (1 << 0)

	// Passing a non-strict-DER signature or one with undefined hashtype to a
	// checksig operation causes script failure. Evaluating a pubkey that is not
	// (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes script
	// failure.
	ScriptVerifyStrictEnc = (1 << 1)

	// Passing a non-strict-DER signature to a checksig operation causes script
	// failure (softfork safe, BIP62 rule 1)
	ScriptVerifyDersig = (1 << 2)

	// Passing a non-strict-DER signature or one with S > order/2 to a checksig
	// operation causes script failure
	// (softfork safe, BIP62 rule 5).
	ScriptVerifyLowS = (1 << 3)

	// verify dummy stack item consumed by CHECKMULTISIG is of zero-length
	// (softfork safe, BIP62 rule 7).
	ScriptVerifyNullDummy = (1 << 4)

	// Using a non-push operator in the scriptSig causes script failure
	// (softfork safe, BIP62 rule 2).
	ScriptVerifySigPushOnly = (1 << 5)

	// Require minimal encodings for all push operations (OP_0... OP_16,
	// OP_1NEGATE where possible, direct pushes up to 75 bytes, OP_PUSHDATA up
	// to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating any other
	// push causes the script to fail (BIP62 rule 3). In addition, whenever a
	// stack element is interpreted as a number, it must be of minimal length
	// (BIP62 rule 4).
	// (softfork safe)
	ScriptVerifyMinmalData = (1 << 6)

	// Discourage use of Nops reserved for upgrades (NOP1-10)
	//
	// Provided so that nodes can avoid accepting or mining transactions
	// containing executed NOP's whose meaning may change after a soft-fork,
	// thus rendering the script invalid; with this flag set executing
	// discouraged NOPs fails the script. This verification flag will never be a
	// mandatory flag applied to scripts in a block. NOPs that are not executed,
	// e.g.  within an unexecuted IF ENDIF block, are *not* rejected.
	ScriptVerifyDiscourageUpgradableNops = (1 << 7)

	// Require that only a single stack element remains after evaluation. This
	// changes the success criterion from "At least one stack element must
	// remain, and when interpreted as a boolean, it must be true" to "Exactly
	// one stack element must remain, and when interpreted as a boolean, it must
	// be true".
	// (softfork safe, BIP62 rule 6)
	// Note: CLEANSTACK should never be used without P2SH or WITNESS.
	ScriptVerifyCleanStack = (1 << 8)

	// Verify CHECKLOCKTIMEVERIFY
	//
	// See BIP65 for details.
	ScriptVerifyCheckLockTimeVerify = (1 << 9)

	// support CHECKSEQUENCEVERIFY opcode
	//
	// See BIP112 for details
	ScriptVerifyCheckSequenceVerify = (1 << 10)

	// Segwit script only: Require the argument of OP_IF/NOTIF to be exactly
	// 0x01 or empty vector
	//
	ScriptVerifyMinimalIf = (1 << 13)

	// Signature(s) must be empty vector if an CHECK(MULTI)SIG operation failed
	//
	ScriptVerifyNullFail = (1 << 14)

	// Public keys in scripts must be compressed
	//
	ScriptVerifyCompressedPubkeyType = (1 << 15)

	// ScriptEnableSigHashForkID Do we accept signature using SIGHASH_FORKID
	//
	ScriptEnableSigHashForkID = (1 << 16)

	// Do we accept activate replay protection using a different fork id.
	//
	ScriptEnableReplayProtection = (1 << 17)

	// Is OP_CHECKDATASIG and variant are enabled.
	//
	ScriptEnableCheckDataSig = (1 << 18)

	ScriptMaxOpReturnRelay uint = 223
)

* Script verification flags

View Source
const (
	ScriptNonStandard = iota
	// ScriptPubkey and following are 'standard' transaction types:
	ScriptPubkey
	ScriptPubkeyHash
	ScriptHash
	ScriptMultiSig
	ScriptNullData
)
View Source
const (

	// MandatoryScriptVerifyFlags failing one of these tests may trigger a DoS ban - see CheckInputs() for
	// details.
	MandatoryScriptVerifyFlags uint = ScriptVerifyP2SH | ScriptVerifyStrictEnc | ScriptEnableSigHashForkID |
		ScriptVerifyLowS | ScriptVerifyNullFail

	//StandardScriptVerifyFlags standard script verification flags that standard transactions will comply
	// with. However scripts violating these flags may still be present in valid
	// blocks and we must accept those blocks.
	StandardScriptVerifyFlags uint = MandatoryScriptVerifyFlags | ScriptVerifyDersig | ScriptVerifyLowS |
		ScriptVerifyNullDummy | ScriptVerifySigPushOnly |
		ScriptVerifyMinmalData | ScriptVerifyDiscourageUpgradableNops |
		ScriptVerifyCleanStack | ScriptVerifyCheckLockTimeVerify |
		ScriptVerifyCheckSequenceVerify | ScriptVerifyNullFail

	//StandardNotMandatoryVerifyFlags for convenience, standard but not mandatory verify flags.
	StandardNotMandatoryVerifyFlags uint = StandardScriptVerifyFlags & (^MandatoryScriptVerifyFlags)

	//StandardCheckDataSigVerifyFlags used as the flags parameters to check for sigops as if OP_CHECKDATASIG is
	//enabled. Can be removed after OP_CHECKDATASIG is activated as the flag is
	//made standard.
	StandardCheckDataSigVerifyFlags = StandardScriptVerifyFlags | ScriptEnableCheckDataSig
)
View Source
const (
	DefaultMaxNumSize = 4

	MaxInt32 = 1<<31 - 1
	MinInt32 = -1 << 31
)
View Source
const (
	MaxMessagePayload = 32 * 1024 * 1024
)

Variables

This section is empty.

Functions

func AddressVerPubKey

func AddressVerPubKey() byte

func AddressVerScript

func AddressVerScript() byte

func BytesToBool

func BytesToBool(bytes []byte) bool

func CheckDataSignatureEncoding added in v0.0.4

func CheckDataSignatureEncoding(vchSig []byte, flags uint32) (bool, error)

func CheckPubKeyEncoding

func CheckPubKeyEncoding(vchPubKey []byte, flags uint32) error

func CheckTransactionSignatureEncoding added in v0.0.4

func CheckTransactionSignatureEncoding(vchSig []byte, flags uint32) error

func DecodeOPN

func DecodeOPN(opcode byte) int

func EncodeOPN

func EncodeOPN(n int) (int, error)

func Hash160ToAddressStr

func Hash160ToAddressStr(hash160 []byte, version byte) (str string, err error)

func InitAddressParam added in v0.0.3

func InitAddressParam(addressParam *AddressParam)

func IsMinimallyEncoded

func IsMinimallyEncoded(data []byte, nMaxNumSize int64) bool

func IsOpCodeDisabled

func IsOpCodeDisabled(opCode byte, flags uint32) bool

func MinimallyEncode

func MinimallyEncode(data []byte) []byte

func ReadScript

func ReadScript(reader io.Reader, maxAllowed uint32, fieldName string) (script []byte, err error)

Types

type Address

type Address struct {
	// contains filtered or unexported fields
}

func AddressFromHash160

func AddressFromHash160(hash160 []byte, version byte) (address *Address, err error)

func AddressFromPrivateKey

func AddressFromPrivateKey(priKeyStr string) (*Address, error)

func AddressFromPublicKey

func AddressFromPublicKey(publicKey []byte) (address *Address, err error)

func AddressFromScriptHash

func AddressFromScriptHash(script []byte) (*Address, error)

func AddressFromString

func AddressFromString(addressStr string) (btcAddress *Address, err error)

func (*Address) EncodeToPubKeyHash

func (addr *Address) EncodeToPubKeyHash() []byte

func (*Address) GetVersion

func (addr *Address) GetVersion() byte

func (*Address) String

func (addr *Address) String() string

type AddressParam added in v0.0.3

type AddressParam struct {
	PubKeyHashAddressVer byte
	ScriptHashAddressVer byte
}

type Script

type Script struct {
	ParsedOpCodes []opcodes.ParsedOpCode
	// contains filtered or unexported fields
}

func NewEmptyScript

func NewEmptyScript() *Script

func NewScriptOps

func NewScriptOps(oldParsedOpCodes []opcodes.ParsedOpCode) *Script

func NewScriptRaw

func NewScriptRaw(bytes []byte) *Script

func (*Script) Bytes added in v0.0.2

func (s *Script) Bytes() []byte

func (*Script) CheckScriptSigStandard

func (s *Script) CheckScriptSigStandard() (bool, string)

func (*Script) Decode

func (s *Script) Decode(reader io.Reader, isCoinBase bool) (err error)

func (*Script) Encode

func (s *Script) Encode(writer io.Writer) (err error)

func (*Script) EncodeSize

func (s *Script) EncodeSize() uint32

func (*Script) ExtractDestinations

func (s *Script) ExtractDestinations() (sType int, addresses []*Address, sigCountRequired int, err error)

func (*Script) GetBadOpCode

func (s *Script) GetBadOpCode() bool

func (*Script) GetData

func (s *Script) GetData() []byte

func (*Script) GetPubKeyP2SHSigOpCount added in v0.0.4

func (s *Script) GetPubKeyP2SHSigOpCount(flags uint32, scriptSig *Script) int

func (*Script) GetSigOpCount

func (s *Script) GetSigOpCount(flags uint32, accurate bool) int

func (*Script) IsCommitment

func (s *Script) IsCommitment(data []byte) bool

func (*Script) IsEqual

func (s *Script) IsEqual(script2 *Script) bool

func (*Script) IsPayToScriptHash

func (s *Script) IsPayToScriptHash() bool

func (*Script) IsPushOnly

func (s *Script) IsPushOnly() bool

func (*Script) IsSpendable

func (s *Script) IsSpendable() bool

func (*Script) IsStandardScriptPubKey added in v0.0.3

func (s *Script) IsStandardScriptPubKey() (pubKeyType int, pubKeys [][]byte, isStandard bool)

func (*Script) IsUnspendable

func (s *Script) IsUnspendable() bool

func (*Script) PushData added in v0.0.2

func (s *Script) PushData(data []byte) error

func (*Script) PushInt64

func (s *Script) PushInt64(n int64) error

func (*Script) PushMultData

func (s *Script) PushMultData(data [][]byte) error

func (*Script) PushOpCode

func (s *Script) PushOpCode(n int) error

func (*Script) PushScriptNum

func (s *Script) PushScriptNum(sn *ScriptNum) error

func (*Script) PushSingleData

func (s *Script) PushSingleData(data []byte) error

func (*Script) RemoveOpCodeByIndex

func (s *Script) RemoveOpCodeByIndex(index int) *Script

func (*Script) RemoveOpcode

func (s *Script) RemoveOpcode(code byte) *Script

func (*Script) RemoveOpcodeByData

func (s *Script) RemoveOpcodeByData(data []byte) *Script

func (*Script) Serialize

func (s *Script) Serialize(writer io.Writer) (err error)

func (*Script) SerializeSize

func (s *Script) SerializeSize() uint32

func (*Script) Size

func (s *Script) Size() int

func (*Script) Unserialize

func (s *Script) Unserialize(reader io.Reader, isCoinBase bool) (err error)

type ScriptNum

type ScriptNum struct {
	Value int64
}

func GetScriptNum

func GetScriptNum(vch []byte, requireMinimal bool, maxNumSize int) (scriptNum *ScriptNum, err error)

GetScriptNum interprets the passed serialized bytes as an encoded integer and returns the result as a script number.

Since the consensus rules dictate that serialized bytes interpreted as ints are only allowed to be in the range determined by a maximum number of bytes, on a per opcode basis, an error will be returned when the provided bytes would result in a number outside of that range. In particular, the range for the vast majority of opcodes dealing with numeric values are limited to 4 bytes and therefore will pass that value to this function resulting in an allowed range of [-2^31 + 1, 2^31 - 1].

The requireMinimal flag causes an error to be returned if additional checks on the encoding determine it is not represented with the smallest possible number of bytes or is the negative 0 encoding, [0x80]. For example, consider the number 127. It could be encoded as [0x7f], [0x7f 0x00], [0x7f 0x00 0x00 ...], etc. All forms except [0x7f] will return an error with requireMinimal enabled.

The scriptNumLen is the maximum number of bytes the encoded value can be before an ErrStackNumberTooBig is returned. This effectively limits the range of allowed values. WARNING: Great care should be taken if passing a value larger than defaultScriptNumLen, which could lead to addition and multiplication overflows.

func NewScriptNum

func NewScriptNum(v int64) *ScriptNum

func (ScriptNum) Bytes

func (n ScriptNum) Bytes() []byte

Bytes returns the number serialized as a little endian with a sign bit.

Example encodings:

   127 -> [0x7f]
  -127 -> [0xff]
   128 -> [0x80 0x00]
  -128 -> [0x80 0x80]
   129 -> [0x81 0x00]
  -129 -> [0x81 0x80]
   256 -> [0x00 0x01]
  -256 -> [0x00 0x81]
 32767 -> [0xff 0x7f]
-32767 -> [0xff 0xff]
 32768 -> [0x00 0x80 0x00]
-32768 -> [0x00 0x80 0x80]

func (*ScriptNum) Serialize

func (n *ScriptNum) Serialize() (bytes []byte)

func (*ScriptNum) ToInt32

func (n *ScriptNum) ToInt32() int32

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL