类 Script
- java.lang.Object
-
- io.nuls.base.script.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.
-
-
嵌套类概要
嵌套类 修饰符和类型 类 说明 static classScript.ScriptTypeEnumeration to encapsulate the type of this script.static classScript.VerifyFlagFlags to pass to Note currently only P2SH, DERSIG and NULLDUMMY are actually supported.
-
字段概要
字段 修饰符和类型 字段 说明 static EnumSet<Script.VerifyFlag>ALL_VERIFY_FLAGSprotected List<ScriptChunk>chunksstatic intMAX_P2SH_SIGOPSMax number of sigops allowed in a standard p2sh redeem scriptstatic longMAX_SCRIPT_ELEMENT_SIZEprotected byte[]programstatic intSIG_SIZE
-
方法概要
所有方法 静态方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 booleancorrectlyNulsSpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey)booleancorrectlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey)已过时。booleancorrectlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey, Set<Script.VerifyFlag> verifyFlags)Verifies that this script (interpreted as a scriptSig) correctly spends the given scriptPubKey.ScriptcreateEmptyInputScript(ECKey key, Script redeemScript)Creates an incomplete scriptSig that, once filled with signatures, can redeem output containing this scriptPubKey.static byte[]createInputScript(byte[] signature)根据签名的字节数组转为inputScript/scriptSig 字节数组用于网络传播static byte[]createInputScript(byte[] signature, byte[] pubkey)根据签名和公钥的字节数组转为inputScript/scriptSig 字节数组用于网络传播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.static intdecodeFromOpN(int opcode)booleanequals(Object o)static voidexecuteScript(Transaction txContainingThis, long index, Script script, LinkedList<byte[]> stack, boolean enforceNullDummy)已过时。static voidexecuteScript(Transaction txContainingThis, long index, Script script, LinkedList<byte[]> stack, Set<Script.VerifyFlag> verifyFlags)Exposes the script interpreter.List<ScriptChunk>getChunks()Returns an immutable list of the scripts parsed form.BigIntegergetCLTVPaymentChannelExpiry()byte[]getCLTVPaymentChannelRecipientPubKey()Retrieves the recipient public key from a LOCKTIMEVERIFY transactionbyte[]getCLTVPaymentChannelSenderPubKey()Retrieves the sender public key from a LOCKTIMEVERIFY transactionlonggetCreationTimeSeconds()intgetNumberOfBytesRequiredToSpend(ECKey pubKey, Script redeemScript)Returns number of bytes required to spend this script.intgetNumberOfSignaturesRequiredToSpend()Returns number of signatures required to satisfy this script.static longgetP2SHSigOpCount(byte[] scriptSig)Gets the count of P2SH Sig Ops in the Script scriptSigbyte[]getProgram()Returns the serialized program as a newly created byte array.byte[]getPubKey()Returns the public key in this script.byte[]getPubKeyHash()If a program matches the standard template DUP HASH160 <pubkey hash> EQUALVERIFY CHECKSIG then this function retrieves the third element.List<ECKey>getPubKeys()Returns a list of the keys required by this script, assuming a multi-sig script.ScriptgetScriptSigWithSignature(Script scriptSig, byte[] sigBytes, int index)Returns a copy of the given scriptSig with the signature inserted in the given position.Script.ScriptTypegetScriptType()intgetSigInsertionIndex(Sha256Hash hash, ECKey signingKey)Returns the index where a signature by the key should be inserted.static intgetSigOpCount(byte[] program)Gets the count of regular SigOps in the script program (counting multisig ops as 20)inthashCode()booleanisOpReturn()booleanisPayToScriptHash()Whether or not this is a scriptPubKey representing a pay-to-script-hash output.booleanisSentToAddress()Returns true if this script is of the form DUP HASH160EQUALVERIFY CHECKSIG, ie, payment to an address like 1VayNert3x1KzbpzMGt2qdqrAThiRovi8. booleanisSentToCLTVPaymentChannel()booleanisSentToMultiSig()Returns whether this script matches the format used for multisig outputs: [n] [keys...]booleanisSentToP2SH()已过时。booleanisSentToRawPubKey()Returns true if this script is of the formOP_CHECKSIG. static byte[]removeAllInstancesOf(byte[] inputScript, byte[] chunkToRemove)Returns the script bytes of inputScript with all instances of the specified script object removedstatic byte[]removeAllInstancesOfOp(byte[] inputScript, int opCode)Returns the script bytes of inputScript with all instances of the given op code removedvoidsetCreationTimeSeconds(long creationTimeSeconds)StringtoString()Returns the program opcodes as a string, for example "[1234] DUP HASH160"static booleanverifySign(byte[] digestData, byte[] signData, byte[] publicKey)签名验证static voidwriteBytes(OutputStream os, byte[] buf)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)));
-
-
-
字段详细资料
-
ALL_VERIFY_FLAGS
public static final EnumSet<Script.VerifyFlag> ALL_VERIFY_FLAGS
-
MAX_SCRIPT_ELEMENT_SIZE
public static final long MAX_SCRIPT_ELEMENT_SIZE
- 另请参阅:
- 常量字段值
-
SIG_SIZE
public static final int SIG_SIZE
- 另请参阅:
- 常量字段值
-
MAX_P2SH_SIGOPS
public static final int MAX_P2SH_SIGOPS
Max number of sigops allowed in a standard p2sh redeem script- 另请参阅:
- 常量字段值
-
chunks
protected List<ScriptChunk> chunks
-
program
protected byte[] program
-
-
构造器详细资料
-
Script
public Script(byte[] programBytes) throws ScriptExceptionConstruct 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
-
Script
public Script(byte[] programBytes, long creationTimeSeconds) throws ScriptException- 抛出:
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"
-
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 formOP_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 HASH160EQUALVERIFY 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 ScriptExceptionIf 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 ScriptExceptionReturns 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 ScriptExceptionRetrieves the sender public key from a LOCKTIMEVERIFY transaction- 返回:
- 抛出:
ScriptException
-
getCLTVPaymentChannelRecipientPubKey
public byte[] getCLTVPaymentChannelRecipientPubKey() throws ScriptExceptionRetrieves 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 ScriptExceptionGets the count of regular SigOps in the script program (counting multisig ops as 20)- 抛出:
ScriptException
-
getP2SHSigOpCount
public static long getP2SHSigOpCount(byte[] scriptSig) throws ScriptExceptionGets 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
@Deprecated public static void executeScript(@Nullable Transaction txContainingThis, long index, Script script, LinkedList<byte[]> stack, boolean enforceNullDummy) throws ScriptException
已过时。Exposes the script interpreter. Normally you should not use this directly, instead use instead.- 抛出:
ScriptException
-
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, usecorrectlySpends(Transaction, long, Script)which sets all flags.验证此脚本scriptSig是否正确地使用给定的scriptPubKey
-
getScriptType
public Script.ScriptType getScriptType()
- 返回:
- The script type.
-
verifySign
public static boolean verifySign(byte[] digestData, byte[] signData, byte[] publicKey)签名验证
-
-