Script { @Deprecated public void correctlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey) throws ScriptException { correctlySpends(txContainingThis, scriptSigIndex, scriptPubKey, ALL_VERIFY_FLAGS); } private  Script();   Script(List<ScriptChunk> chunks);  Script(byte[] programBytes);  Script(byte[] programBytes, long creationTimeSeconds); long getCreationTimeSeconds(); void setCreationTimeSeconds(long creationTimeSeconds); @Override String toString(); byte[] getProgram(); List<ScriptChunk> getChunks(); boolean isSentToRawPubKey(); boolean isSentToAddress(); @Deprecated boolean isSentToP2SH(); byte[] getPubKeyHash(); byte[] getPubKey(); byte[] getCLTVPaymentChannelSenderPubKey(); byte[] getCLTVPaymentChannelRecipientPubKey(); BigInteger getCLTVPaymentChannelExpiry(); @Deprecated Address getFromAddress(NetworkParameters params); Address getToAddress(NetworkParameters params); Address getToAddress(NetworkParameters params, boolean forcePayToPubKey); static void writeBytes(OutputStream os, byte[] buf); static byte[] createMultiSigOutputScript(int threshold, List<ECKey> pubkeys); static byte[] createInputScript(byte[] signature, byte[] pubkey); static byte[] createInputScript(byte[] signature); Script createEmptyInputScript(@Nullable ECKey key, @Nullable Script redeemScript); Script getScriptSigWithSignature(Script scriptSig, byte[] sigBytes, int index); int getSigInsertionIndex(Sha256Hash hash, ECKey signingKey); List<ECKey> getPubKeys(); static int getSigOpCount(byte[] program); static long getP2SHSigOpCount(byte[] scriptSig); int getNumberOfSignaturesRequiredToSpend(); int getNumberOfBytesRequiredToSpend(@Nullable ECKey pubKey, @Nullable Script redeemScript); boolean isPayToScriptHash(); boolean isSentToMultiSig(); boolean isSentToCLTVPaymentChannel(); static byte[] removeAllInstancesOf(byte[] inputScript, byte[] chunkToRemove); static byte[] removeAllInstancesOfOp(byte[] inputScript, int opCode); boolean isOpReturn(); boolean isUnspendable(); @Deprecated static void executeScript(@Nullable Transaction txContainingThis, long index,
                                     Script script, LinkedList<byte[]> stack, boolean enforceNullDummy); static void executeScript(@Nullable Transaction txContainingThis, long index,
                                     Script script, LinkedList<byte[]> stack, Set<VerifyFlag> verifyFlags); @Deprecated void correctlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey); void correctlySpends(Transaction txContainingThis, long scriptSigIndex, Script scriptPubKey,
                                Set<VerifyFlag> verifyFlags); ScriptType getScriptType(); @Override boolean equals(Object o); @Override int hashCode(); static final EnumSet<VerifyFlag> ALL_VERIFY_FLAGS; static final long MAX_SCRIPT_ELEMENT_SIZE; static final int SIG_SIZE; static final int MAX_P2SH_SIGOPS; }
@Test public void dataDrivenValidScripts() throws Exception { JsonNode json = new ObjectMapper().readTree(new InputStreamReader(getClass().getResourceAsStream( "script_valid.json"), Charsets.UTF_8)); for (JsonNode test : json) { Script scriptSig = parseScriptString(test.get(0).asText()); Script scriptPubKey = parseScriptString(test.get(1).asText()); Set<VerifyFlag> verifyFlags = parseVerifyFlags(test.get(2).asText()); try { scriptSig.correctlySpends(new Transaction(PARAMS), 0, scriptPubKey, verifyFlags); } catch (ScriptException e) { System.err.println(test); System.err.flush(); throw e; } } }
@Test public void dataDrivenInvalidScripts() throws Exception { JsonNode json = new ObjectMapper().readTree(new InputStreamReader(getClass().getResourceAsStream( "script_invalid.json"), Charsets.UTF_8)); for (JsonNode test : json) { try { Script scriptSig = parseScriptString(test.get(0).asText()); Script scriptPubKey = parseScriptString(test.get(1).asText()); Set<VerifyFlag> verifyFlags = parseVerifyFlags(test.get(2).asText()); scriptSig.correctlySpends(new Transaction(PARAMS), 0, scriptPubKey, verifyFlags); System.err.println(test); System.err.flush(); fail(); } catch (VerificationException e) { } } }
@Test public void dataDrivenValidTransactions() throws Exception { JsonNode json = new ObjectMapper().readTree(new InputStreamReader(getClass().getResourceAsStream( "tx_valid.json"), Charsets.UTF_8)); for (JsonNode test : json) { if (test.isArray() && test.size() == 1 && test.get(0).isTextual()) continue; Transaction transaction = null; try { Map<TransactionOutPoint, Script> scriptPubKeys = parseScriptPubKeys(test.get(0)); transaction = PARAMS.getDefaultSerializer().makeTransaction(HEX.decode(test.get(1).asText().toLowerCase())); transaction.verify(); Set<VerifyFlag> verifyFlags = parseVerifyFlags(test.get(2).asText()); for (int i = 0; i < transaction.getInputs().size(); i++) { TransactionInput input = transaction.getInputs().get(i); if (input.getOutpoint().getIndex() == 0xffffffffL) input.getOutpoint().setIndex(-1); assertTrue(scriptPubKeys.containsKey(input.getOutpoint())); input.getScriptSig().correctlySpends(transaction, i, scriptPubKeys.get(input.getOutpoint()), verifyFlags); } } catch (Exception e) { System.err.println(test); if (transaction != null) System.err.println(transaction); throw e; } } }
@Test public void dataDrivenInvalidTransactions() throws Exception { JsonNode json = new ObjectMapper().readTree(new InputStreamReader(getClass().getResourceAsStream( "tx_invalid.json"), Charsets.UTF_8)); for (JsonNode test : json) { if (test.isArray() && test.size() == 1 && test.get(0).isTextual()) continue; Map<TransactionOutPoint, Script> scriptPubKeys = parseScriptPubKeys(test.get(0)); Transaction transaction = PARAMS.getDefaultSerializer().makeTransaction(HEX.decode(test.get(1).asText().toLowerCase())); Set<VerifyFlag> verifyFlags = parseVerifyFlags(test.get(2).asText()); boolean valid = true; try { transaction.verify(); } catch (VerificationException e) { valid = false; } HashSet<TransactionOutPoint> set = new HashSet<TransactionOutPoint>(); for (TransactionInput input : transaction.getInputs()) { if (set.contains(input.getOutpoint())) valid = false; set.add(input.getOutpoint()); } for (int i = 0; i < transaction.getInputs().size() && valid; i++) { TransactionInput input = transaction.getInputs().get(i); assertTrue(scriptPubKeys.containsKey(input.getOutpoint())); try { input.getScriptSig().correctlySpends(transaction, i, scriptPubKeys.get(input.getOutpoint()), verifyFlags); } catch (VerificationException e) { valid = false; } } if (valid) fail(); } }