类 Script


  • public class Script
    extends Object

    Programs embedded inside transactions that control redemption of payments.

    Bitcoin transactions don't specify what they do directly. Instead a small binary stack language is used to define programs that when evaluated return whether the transaction "accepts" or rejects the other transactions connected to it.

    In SPV mode, scripts are not run, because that would require all transactions to be available and lightweight clients don't have that entity. In full mode, this class is used to run the interpreted language. It also has static methods for building scripts.

    • 字段详细资料

      • MAX_SCRIPT_ELEMENT_SIZE

        public static final long MAX_SCRIPT_ELEMENT_SIZE
        另请参阅:
        常量字段值
      • MAX_P2SH_SIGOPS

        public static final int MAX_P2SH_SIGOPS
        Max number of sigops allowed in a standard p2sh redeem script
        另请参阅:
        常量字段值
      • program

        protected byte[] program
    • 构造器详细资料

      • Script

        public Script​(byte[] programBytes)
               throws ScriptException
        Construct a Script that copies and wraps the programBytes array. The array is parsed and checked for syntactic validity.
        参数:
        programBytes - Array of program bytes from a transaction.
        抛出:
        ScriptException
    • 方法详细资料

      • getCreationTimeSeconds

        public long getCreationTimeSeconds()
      • setCreationTimeSeconds

        public void setCreationTimeSeconds​(long creationTimeSeconds)
      • toString

        public String toString()
        Returns the program opcodes as a string, for example "[1234] DUP HASH160"
        覆盖:
        toString 在类中 Object
      • getProgram

        public byte[] getProgram()
        Returns the serialized program as a newly created byte array.
      • getChunks

        public List<ScriptChunk> getChunks()
        Returns an immutable list of the scripts parsed form. Each chunk is either an opcode or entity element.
      • isSentToRawPubKey

        public boolean isSentToRawPubKey()
        Returns true if this script is of the form OP_CHECKSIG. This form was originally intended for transactions where the peers talked to each other directly via TCP/IP, but has fallen out of favor with time due to that mode of operation being susceptible to man-in-the-middle attacks. It is still used in coinbase outputs and can be useful more exotic types of transaction, but today most payments are to addresses.

        根据公钥生成的脚本(scriptPublicKey)

      • isSentToAddress

        public boolean isSentToAddress()
        Returns true if this script is of the form DUP HASH160 EQUALVERIFY CHECKSIG, ie, payment to an address like 1VayNert3x1KzbpzMGt2qdqrAThiRovi8. This form was originally intended for the case where you wish to send somebody money with a written code because their node is offline, but over time has become the standard way to make payments due to the short and recognizable base58 form addresses come in.

        判断脚本类型是否为发送到地址

      • isSentToP2SH

        @Deprecated
        public boolean isSentToP2SH()
        已过时。
        An alias for isPayToScriptHash.
      • getPubKeyHash

        public byte[] getPubKeyHash()
                             throws ScriptException

        If a program matches the standard template DUP HASH160 <pubkey hash> EQUALVERIFY CHECKSIG then this function retrieves the third element. In this case, this is useful for fetching the destination address of a transaction.

        If a program matches the standard template HASH160 <script hash> EQUAL then this function retrieves the second element. In this case, this is useful for fetching the hash of the redeem script of a transaction.

        Otherwise it throws a ScriptException.

        获取公钥HASH

        抛出:
        ScriptException
      • getPubKey

        public byte[] getPubKey()
                         throws ScriptException
        Returns the public key in this script. If a script contains two constants and nothing else, it is assumed to be a scriptSig (input) for a pay-to-address output and the second constant is returned (the first is the signature). If a script contains a constant and an OP_CHECKSIG opcode, the constant is returned as it is assumed to be a direct pay-to-key scriptPubKey (output) and the first constant is the public key.
        抛出:
        ScriptException - if the script is none of the named forms.

        获取公钥

      • getCLTVPaymentChannelSenderPubKey

        public byte[] getCLTVPaymentChannelSenderPubKey()
                                                 throws ScriptException
        Retrieves the sender public key from a LOCKTIMEVERIFY transaction
        返回:
        抛出:
        ScriptException
      • getCLTVPaymentChannelRecipientPubKey

        public byte[] getCLTVPaymentChannelRecipientPubKey()
                                                    throws ScriptException
        Retrieves the recipient public key from a LOCKTIMEVERIFY transaction
        返回:
        抛出:
        ScriptException
      • getCLTVPaymentChannelExpiry

        public BigInteger getCLTVPaymentChannelExpiry()
      • writeBytes

        public static void writeBytes​(OutputStream os,
                                      byte[] buf)
                               throws IOException
        Writes out the given byte buffer to the output stream with the correct opcode prefix To write an integer call writeBytes(out, SerializeUtils.reverseBytes(SerializeUtils.encodeMPI(val, false)));

        将输出流按正确的方式写入到字节缓冲区

        抛出:
        IOException
      • createMultiSigOutputScript

        public static byte[] createMultiSigOutputScript​(int threshold,
                                                        List<ECKey> pubkeys)
        Creates a program that requires at least N of the given keys to sign, using OP_CHECKMULTISIG. 将OutputScript/scriptPublicKry 转为字节数组 用于传输
      • createInputScript

        public static byte[] createInputScript​(byte[] signature,
                                               byte[] pubkey)
        根据签名和公钥的字节数组转为inputScript/scriptSig 字节数组用于网络传播
      • createInputScript

        public static byte[] createInputScript​(byte[] signature)
        根据签名的字节数组转为inputScript/scriptSig 字节数组用于网络传播
      • createEmptyInputScript

        public Script createEmptyInputScript​(@Nullable
                                             ECKey key,
                                             @Nullable
                                             Script redeemScript)
        Creates an incomplete scriptSig that, once filled with signatures, can redeem output containing this scriptPubKey. Instead of the signatures resulting script has OP_0. Having incomplete input script allows to pass around partially signed tx. It is expected that this program later on will be updated with proper signatures.

        创建一个空Script

      • getScriptSigWithSignature

        public Script getScriptSigWithSignature​(Script scriptSig,
                                                byte[] sigBytes,
                                                int index)
        Returns a copy of the given scriptSig with the signature inserted in the given position.
      • getSigInsertionIndex

        public int getSigInsertionIndex​(Sha256Hash hash,
                                        ECKey signingKey)
        Returns the index where a signature by the key should be inserted. Only applicable to a P2SH scriptSig.
      • getPubKeys

        public List<ECKey> getPubKeys()
        Returns a list of the keys required by this script, assuming a multi-sig script.
        抛出:
        ScriptException - if the script type is not understood or is pay to address or is P2SH (run this method on the "Redeem script" instead).
      • decodeFromOpN

        public static int decodeFromOpN​(int opcode)
      • getSigOpCount

        public static int getSigOpCount​(byte[] program)
                                 throws ScriptException
        Gets the count of regular SigOps in the script program (counting multisig ops as 20)
        抛出:
        ScriptException
      • getP2SHSigOpCount

        public static long getP2SHSigOpCount​(byte[] scriptSig)
                                      throws ScriptException
        Gets the count of P2SH Sig Ops in the Script scriptSig
        抛出:
        ScriptException
      • getNumberOfSignaturesRequiredToSpend

        public int getNumberOfSignaturesRequiredToSpend()
        Returns number of signatures required to satisfy this script.
      • getNumberOfBytesRequiredToSpend

        public int getNumberOfBytesRequiredToSpend​(@Nullable
                                                   ECKey pubKey,
                                                   @Nullable
                                                   Script redeemScript)
        Returns number of bytes required to spend this script. It accepts optional ECKey and redeemScript that may be required for certain types of script to estimate target size.
      • isPayToScriptHash

        public boolean isPayToScriptHash()

        Whether or not this is a scriptPubKey representing a pay-to-script-hash output. In such outputs, the logic that controls reclamation is not actually in the output at all. Instead there's just a hash, and it's up to the spending input to provide a program matching that hash. This rule is "soft enforced" by the network as it does not exist in Bitcoin Core. It means blocks containing P2SH transactions that don't match correctly are considered valid, but won't be mined upon, so they'll be rapidly re-orgd out of the chain. This logic is defined by BIP 16.

        bitcoinj does not support creation of P2SH transactions today. The goal of P2SH is to allow short addresses even for complex scripts (eg, multi-sig outputs) so they are convenient to work with in things like QRcodes or with copy/paste, and also to minimize the size of the unspent output set (which improves performance of the Bitcoin system).

      • isSentToMultiSig

        public boolean isSentToMultiSig()
        Returns whether this script matches the format used for multisig outputs: [n] [keys...] [m] CHECKMULTISIG
      • isSentToCLTVPaymentChannel

        public boolean isSentToCLTVPaymentChannel()
      • removeAllInstancesOf

        public static byte[] removeAllInstancesOf​(byte[] inputScript,
                                                  byte[] chunkToRemove)
        Returns the script bytes of inputScript with all instances of the specified script object removed
      • removeAllInstancesOfOp

        public static byte[] removeAllInstancesOfOp​(byte[] inputScript,
                                                    int opCode)
        Returns the script bytes of inputScript with all instances of the given op code removed
      • isOpReturn

        public boolean isOpReturn()
      • executeScript

        public static void executeScript​(@Nullable
                                         Transaction txContainingThis,
                                         long index,
                                         Script script,
                                         LinkedList<byte[]> stack,
                                         Set<Script.VerifyFlag> verifyFlags)
                                  throws ScriptException
        Exposes the script interpreter. Normally you should not use this directly, instead use is useful if you need more precise control or access to the final state of the stack. This interface is very likely to change in future.
        参数:
        txContainingThis - 交易
        index - 解锁脚本的索引
        script - 锁定脚本
        stack - 命令队列
        verifyFlags - 验证类型集合
        抛出:
        ScriptException
      • correctlySpends

        @Deprecated
        public boolean correctlySpends​(Transaction txContainingThis,
                                       long scriptSigIndex,
                                       Script scriptPubKey)
        已过时。
        Verifies that this script (interpreted as a scriptSig) correctly spends the given scriptPubKey, enabling all validation rules.验证此脚本(解锁脚本)是否正确地使用给定的scriptPubKey(锁定脚本),启用所有验证规则
        参数:
        txContainingThis - The transaction in which this input scriptSig resides. Accessing txContainingThis from another thread while this method runs results in undefined behavior.
        scriptSigIndex - 交易中包含的解锁脚本的索引The index in txContainingThis of the scriptSig (note: NOT the index of the scriptPubKey).
        scriptPubKey - 锁定脚本The connected scriptPubKey containing the conditions needed to claim the value. instead so that verification flags do not change as new verification options are added. 交易,解锁脚本下标,锁定脚本 txContainingThis+scriptSigIndex用于定位解锁脚本(因为一笔交易中有可能有多个Input)
      • correctlyNulsSpends

        public boolean correctlyNulsSpends​(Transaction txContainingThis,
                                           long scriptSigIndex,
                                           Script scriptPubKey)
      • correctlySpends

        public boolean correctlySpends​(Transaction txContainingThis,
                                       long scriptSigIndex,
                                       Script scriptPubKey,
                                       Set<Script.VerifyFlag> verifyFlags)
        Verifies that this script (interpreted as a scriptSig) correctly spends the given scriptPubKey.
        参数:
        txContainingThis - 交易 The transaction in which this input scriptSig resides. Accessing txContainingThis from another thread while this method runs results in undefined behavior.
        scriptSigIndex - scriptSig脚本索引 The index in txContainingThis of the scriptSig (note: NOT the index of the scriptPubKey).
        scriptPubKey - scriptPubKey The connected scriptPubKey containing the conditions needed to claim the value.
        verifyFlags - 验证标识 Each flag enables one validation rule. If in doubt, use correctlySpends(Transaction, long, Script) which sets all flags.

        验证此脚本scriptSig是否正确地使用给定的scriptPubKey

      • verifySign

        public static boolean verifySign​(byte[] digestData,
                                         byte[] signData,
                                         byte[] publicKey)
        签名验证
      • hashCode

        public int hashCode()
        覆盖:
        hashCode 在类中 Object