类 ScriptBuilder


  • public class ScriptBuilder
    extends Object

    Tools for the construction of commonly used script types. You don't normally need this as it's hidden behind convenience methods on but they are useful when working with the protocol at a lower level.

    • 字段详细资料

      • PUBKEY_BYTE_COMPARATOR

        public static final Comparator<byte[]> PUBKEY_BYTE_COMPARATOR
    • 构造器详细资料

      • ScriptBuilder

        public ScriptBuilder()
        Creates a fresh ScriptBuilder with an empty program.
      • ScriptBuilder

        public ScriptBuilder​(Script template)
        Creates a fresh ScriptBuilder with the given program as the starting point.
    • 方法详细资料

      • addChunk

        public ScriptBuilder addChunk​(int index,
                                      ScriptChunk chunk)
        Adds the given chunk at the given index in the program 添加创建好的命令到指定的下表
      • op

        public ScriptBuilder op​(int opcode)
        Adds the given opcode to the end of the program. 添加指定命令到列表最后
      • op

        public ScriptBuilder op​(int index,
                                int opcode)
        Adds the given opcode to the given index in the program 添加指定命令到列表指定位置
      • data

        public ScriptBuilder data​(byte[] data)
        Adds a copy of the given byte array as a entity element (i.e. PUSHDATA) at the end of the program. 添加数据命令(只包含数据)到命令列表最后
      • data

        public ScriptBuilder data​(int index,
                                  byte[] data)
        Adds a copy of the given byte array as a entity element (i.e. PUSHDATA) at the given index in the program. 添加数据命令(只包含数据)到命令列表指定位置
      • number

        public ScriptBuilder number​(long num)
        Adds the given number to the end of the program. Automatically uses shortest encoding possible.
      • number

        public ScriptBuilder number​(int index,
                                    long num)
        Adds the given number to the given index in the program. Automatically uses shortest encoding possible. 添加数据命令(一个long数据)到命令列表指定位置
      • smallNum

        public ScriptBuilder smallNum​(int num)
        Adds the given number as a OP_N opcode to the end of the program. Only handles values 0-16 inclusive.
      • bigNum

        protected ScriptBuilder bigNum​(long num)
        Adds the given number as a push entity chunk. This is intended to use for negative numbers or values > 16, and although it will accept numbers in the range 0-16 inclusive, the encoding would be considered non-standard.
      • smallNum

        public ScriptBuilder smallNum​(int index,
                                      int num)
        Adds the given number as a OP_N opcode to the given index in the program. Only handles values 0-16 inclusive.
      • bigNum

        protected ScriptBuilder bigNum​(int index,
                                       long num)
        Adds the given number as a push entity chunk to the given index in the program. This is intended to use for negative numbers or values > 16, and although it will accept numbers in the range 0-16 inclusive, the encoding would be considered non-standard. 将给定的数字作为命令添加到程序中的给定索引中
      • build

        public Script build()
        Creates a new immutable Script based on the state of the builder. 根据当前命令列表创建一个不可变脚本
      • createOutputScript

        public static Script createOutputScript​(byte[] address,
                                                int type)
        Creates a scriptPubKey that encodes payment to the given address. 根据地址创建一个OutputScript/scriptPublicKry 转账
      • createOutputScript

        public static Script createOutputScript​(ECKey key)
        Creates a scriptPubKey that encodes payment to the given raw public key. 根据公钥创建一个OutputScript/scriptPublicKry 创建P2PK(Pay-to-Public-Key)锁定脚本
      • createInputScript

        public static Script createInputScript​(@Nullable
                                               TransactionSignature signature,
                                               ECKey pubKey)
        Creates a scriptSig that can redeem a pay-to-address output. If given signature is null, incomplete scriptSig will be created with OP_0 instead of signature 根据签名和公钥创建一个pay-to-address的inputScript/scriptSig用于解锁交易的OutputScript/scriptPublicKry
      • createNulsInputScript

        public static Script createNulsInputScript​(@Nullable
                                                   byte[] signBytes,
                                                   byte[] pubKeyBytes)
        Creates a scriptSig that can redeem a pay-to-address output. If given signature is null, incomplete scriptSig will be created with OP_0 instead of signature 根据签名和公钥创建一个pay-to-address的inputScript/scriptSig用于解锁交易的OutputScript/scriptPublicKry
      • createInputScript

        public static Script createInputScript​(@Nullable
                                               TransactionSignature signature)
        Creates a scriptSig that can redeem a pay-to-pubkey output. If given signature is null, incomplete scriptSig will be created with OP_0 instead of signature 根据签名创建一个pay-to-public_key的inputScript/scriptSig用于解锁交易的OutputScript/scriptPublicKry
      • createMultiSigOutputScript

        public static Script 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
      • createNulsMultiSigOutputScript

        public static Script createNulsMultiSigOutputScript​(int threshold,
                                                            List<String> pubkeys)
        Creates a program that requires at least N of the given keys to sign, using OP_CHECKMULTISIG. 根据多个公钥创建多重签名的OutputScript/scriptPublicKry
      • createByteNulsMultiSigOutputScript

        public static Script createByteNulsMultiSigOutputScript​(int threshold,
                                                                List<byte[]> pubkeys)
        Create a program that satisfies an OP_CHECKMULTISIG program. 根据多个签名创建inputScript/scriptSig解锁脚本
      • createMultiSigInputScript

        public static Script createMultiSigInputScript​(List<TransactionSignature> signatures)
        Create a program that satisfies an OP_CHECKMULTISIG program. 根据多个签名创建inputScript/scriptSig解锁脚本
      • createMultiSigInputScript

        public static Script createMultiSigInputScript​(TransactionSignature... signatures)
        Create a program that satisfies an OP_CHECKMULTISIG program.
      • createMultiSigInputScriptBytes

        public static Script createMultiSigInputScriptBytes​(List<byte[]> signatures)
        Create a program that satisfies an OP_CHECKMULTISIG program, using pre-encoded signatures.
      • createP2SHMultiSigInputScript

        public static Script createP2SHMultiSigInputScript​(@Nullable
                                                           List<TransactionSignature> signatures,
                                                           Script multisigProgram)
        Create a program that satisfies a pay-to-script hashed OP_CHECKMULTISIG program. If given signature list is null, incomplete scriptSig will be created with OP_0 instead of signatures P2SH(支付到脚本模式,使用多重签名就需要用到这种模式)
      • createNulsP2SHMultiSigInputScript

        public static Script createNulsP2SHMultiSigInputScript​(@Nullable
                                                               List<byte[]> signatures,
                                                               Script multisigProgram)
        Create a program that satisfies a pay-to-script hashed OP_CHECKMULTISIG program. If given signature list is null, incomplete scriptSig will be created with OP_0 instead of signatures P2SH(支付到脚本模式,使用多重签名就需要用到这种模式)
      • createMultiSigInputScriptBytes

        public static Script createMultiSigInputScriptBytes​(List<byte[]> signatures,
                                                            @Nullable
                                                            byte[] multisigProgramBytes)
        Create a program that satisfies an OP_CHECKMULTISIG program, using pre-encoded signatures. Optionally, appends the script program bytes if spending a P2SH output.
      • updateScriptWithSignature

        public static Script updateScriptWithSignature​(Script scriptSig,
                                                       byte[] signature,
                                                       int targetIndex,
                                                       int sigsPrefixCount,
                                                       int sigsSuffixCount)
        Returns a copy of the given scriptSig with the signature inserted in the given position.

        This function assumes that any missing sigs have OP_0 placeholders. If given scriptSig already has all the signatures in place, IllegalArgumentException will be thrown.

        参数:
        targetIndex - where to insert the signature
        sigsPrefixCount - how many items to copy verbatim (e.g. initial OP_0 for multisig)
        sigsSuffixCount - how many items to copy verbatim at end (e.g. redeemScript for P2SH)
      • createP2SHOutputScript

        public static Script createP2SHOutputScript​(byte[] hash)
        Creates a scriptPubKey that sends to the given script hash. Read BIP 16 to learn more about this kind of script. 根据hash创建P2SH锁定脚本
      • createRedeemScript

        public static Script createRedeemScript​(int threshold,
                                                List<ECKey> pubkeys)
        Creates redeem script with given public keys and threshold. Given public keys will be placed in redeem script in the lexicographical sorting order.
      • createNulsRedeemScript

        public static Script createNulsRedeemScript​(int threshold,
                                                    List<String> pubkeys)
        Creates redeem script with given public keys and threshold. Given public keys will be placed in redeem script in the lexicographical sorting order.
      • createByteNulsRedeemScript

        public static Script createByteNulsRedeemScript​(int threshold,
                                                        List<byte[]> pubkeys)
        Creates redeem script with given public keys and threshold. Given public keys will be placed in redeem script in the lexicographical sorting order.
      • createOpReturnScript

        public static Script createOpReturnScript​(byte[] data)
        Creates a script of the form OP_RETURN [entity]. This feature allows you to attach a small piece of entity (like a hash of something stored elsewhere) to a zero valued output which can never be spent and thus does not pollute the ledger.