/** * Returns the pubkey script from the connected output. * @throws java.lang.NullPointerException if there is no connected output. */ public byte[] getConnectedPubKeyScript() { byte[] result = checkNotNull(getConnectedOutput()).getScriptBytes(); checkState(result.length > 0); return result; }
/** * Returns the connected output, assuming the input was connected with * {@link TransactionInput#connect(TransactionOutput)} or variants at some point. If it wasn't connected, then * this method returns null. */ @Nullable public TransactionOutput getConnectedOutput() { return getOutpoint().getConnectedOutput(); }
/** * Returns the connected output, assuming the input was connected with * {@link TransactionInput#connect(TransactionOutput)} or variants at some point. If it wasn't connected, then * this method returns null. */ @Nullable public TransactionOutput getConnectedOutput() { return getOutpoint().getConnectedOutput(); }
/** * Returns the pubkey script from the connected output. * @throws java.lang.NullPointerException if there is no connected output. */ public byte[] getConnectedPubKeyScript() { byte[] result = checkNotNull(getConnectedOutput()).getScriptBytes(); checkState(result.length > 0); return result; }
/** * Returns the connected output, assuming the input was connected with * {@link TransactionInput#connect(TransactionOutput)} or variants at some point. If it wasn't connected, then * this method returns null. */ @Nullable public TransactionOutput getConnectedOutput() { return getOutpoint().getConnectedOutput(); }
/** * Returns the pubkey script from the connected output. * @throws java.lang.NullPointerException if there is no connected output. */ public byte[] getConnectedPubKeyScript() { byte[] result = checkNotNull(getConnectedOutput()).getScriptBytes(); checkState(result.length > 0); return result; }
/** * Returns the pubkey script from the connected output. * @throws java.lang.NullPointerException if there is no connected output. */ public byte[] getConnectedPubKeyScript() { byte[] result = checkNotNull(getConnectedOutput()).getScriptBytes(); checkState(result.length > 0); return result; }
/** * Returns the connected output, assuming the input was connected with * {@link TransactionInput#connect(TransactionOutput)} or variants at some point. If it wasn't connected, then * this method returns null. */ @Nullable public TransactionOutput getConnectedOutput() { return getOutpoint().getConnectedOutput(); }
continue; if (txInput.getOutpoint().getConnectedOutput() == null) { log.warn("Connected output for input {} is null", txInput); continue; Transaction parentTx = txInput.getOutpoint().getConnectedOutput().getParentTransaction(); if (parentTx == null) { log.warn("Parent transaction for input {} is null", txInput);
/** * Returns the ECKey identified in the connected output, for either pay-to-address scripts or pay-to-key scripts. * For P2SH scripts you can use {@link #getConnectedRedeemData(org.bitcoinj.wallet.KeyBag)} and then get the * key from RedeemData. * If the script form cannot be understood, throws ScriptException. * * @return an ECKey or null if the connected key cannot be found in the wallet. */ @Nullable public ECKey getConnectedKey(KeyBag keyBag) throws ScriptException { TransactionOutput connectedOutput = getConnectedOutput(); checkNotNull(connectedOutput, "Input is not connected so cannot retrieve key"); Script connectedScript = connectedOutput.getScriptPubKey(); if (connectedScript.isSentToAddress()) { byte[] addressBytes = connectedScript.getPubKeyHash(); return keyBag.findKeyFromPubHash(addressBytes); } else if (connectedScript.isSentToRawPubKey()) { byte[] pubkeyBytes = connectedScript.getPubKey(); return keyBag.findKeyFromPubKey(pubkeyBytes); } else { throw new ScriptException("Could not understand form of connected output script: " + connectedScript); } }
/** * Returns the ECKey identified in the connected output, for either pay-to-address scripts or pay-to-key scripts. * For P2SH scripts you can use {@link #getConnectedRedeemData(org.bitcoinj.wallet.KeyBag)} and then get the * key from RedeemData. * If the script form cannot be understood, throws ScriptException. * * @return an ECKey or null if the connected key cannot be found in the wallet. */ @Nullable public ECKey getConnectedKey(KeyBag keyBag) throws ScriptException { TransactionOutput connectedOutput = getConnectedOutput(); checkNotNull(connectedOutput, "Input is not connected so cannot retrieve key"); Script connectedScript = connectedOutput.getScriptPubKey(); if (connectedScript.isSentToAddress()) { byte[] addressBytes = connectedScript.getPubKeyHash(); return keyBag.findKeyFromPubHash(addressBytes); } else if (connectedScript.isSentToRawPubKey()) { byte[] pubkeyBytes = connectedScript.getPubKey(); return keyBag.findKeyFromPubKey(pubkeyBytes); } else { throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Could not understand form of connected output script: " + connectedScript); } }
/** * Returns the ECKey identified in the connected output, for either pay-to-address scripts or pay-to-key scripts. * For P2SH scripts you can use {@link #getConnectedRedeemData(org.bitcoinj.wallet.KeyBag)} and then get the * key from RedeemData. * If the script form cannot be understood, throws ScriptException. * * @return an ECKey or null if the connected key cannot be found in the wallet. */ @Nullable public ECKey getConnectedKey(KeyBag keyBag) throws ScriptException { TransactionOutput connectedOutput = getConnectedOutput(); checkNotNull(connectedOutput, "Input is not connected so cannot retrieve key"); Script connectedScript = connectedOutput.getScriptPubKey(); if (connectedScript.isSentToAddress()) { byte[] addressBytes = connectedScript.getPubKeyHash(); return keyBag.findKeyFromPubHash(addressBytes); } else if (connectedScript.isSentToRawPubKey()) { byte[] pubkeyBytes = connectedScript.getPubKey(); return keyBag.findKeyFromPubKey(pubkeyBytes); } else { throw new ScriptException("Could not understand form of connected output script: " + connectedScript); } }
/** * Returns the ECKey identified in the connected output, for either pay-to-address scripts or pay-to-key scripts. * For P2SH scripts you can use {@link #getConnectedRedeemData(org.bitcoinj.wallet.KeyBag)} and then get the * key from RedeemData. * If the script form cannot be understood, throws ScriptException. * * @return an ECKey or null if the connected key cannot be found in the wallet. */ @Nullable public ECKey getConnectedKey(KeyBag keyBag) throws ScriptException { TransactionOutput connectedOutput = getConnectedOutput(); checkNotNull(connectedOutput, "Input is not connected so cannot retrieve key"); Script connectedScript = connectedOutput.getScriptPubKey(); if (connectedScript.isSentToAddress()) { byte[] addressBytes = connectedScript.getPubKeyHash(); return keyBag.findKeyFromPubHash(addressBytes); } else if (connectedScript.isSentToRawPubKey()) { byte[] pubkeyBytes = connectedScript.getPubKey(); return keyBag.findKeyFromPubKey(pubkeyBytes); } else { throw new ScriptException("Could not understand form of connected output script: " + connectedScript); } }
/** * Adds a new and fully signed input for the given parameters. Note that this method is <b>not</b> thread safe * and requires external synchronization. Please refer to general documentation on Bitcoin scripting and contracts * to understand the values of sigHash and anyoneCanPay: otherwise you can use the other form of this method * that sets them to typical defaults. * * @throws ScriptException if the scriptPubKey is not a pay to address or pay to pubkey script. */ public TransactionInput addSignedInput(TransactionOutPoint prevOut, Script scriptPubKey, ECKey sigKey, SigHash sigHash, boolean anyoneCanPay, boolean forkId) throws ScriptException { // Verify the API user didn't try to do operations out of order. checkState(!outputs.isEmpty(), "Attempting to sign tx without outputs."); TransactionInput input = new TransactionInput(params, this, new byte[]{}, prevOut); addInput(input); Sha256Hash hash = forkId ? hashForSignatureWitness(inputs.size() -1, scriptPubKey, prevOut.getConnectedOutput().getValue(), sigHash, anyoneCanPay) : hashForSignature(inputs.size() - 1, scriptPubKey, sigHash, anyoneCanPay); ECKey.ECDSASignature ecSig = sigKey.sign(hash); TransactionSignature txSig = new TransactionSignature(ecSig, sigHash, anyoneCanPay, forkId); if (scriptPubKey.isSentToRawPubKey()) input.setScriptSig(ScriptBuilder.createInputScript(txSig)); else if (scriptPubKey.isSentToAddress()) input.setScriptSig(ScriptBuilder.createInputScript(txSig, sigKey)); else throw new ScriptException("Don't know how to sign for this kind of scriptPubKey: " + scriptPubKey); return input; }
/** * Returns the RedeemData identified in the connected output, for either pay-to-address scripts, pay-to-key * or P2SH scripts. * If the script forms cannot be understood, throws ScriptException. * * @return a RedeemData or null if the connected data cannot be found in the wallet. */ @Nullable public RedeemData getConnectedRedeemData(KeyBag keyBag) throws ScriptException { TransactionOutput connectedOutput = getConnectedOutput(); checkNotNull(connectedOutput, "Input is not connected so cannot retrieve key"); Script connectedScript = connectedOutput.getScriptPubKey(); if (connectedScript.isSentToAddress()) { byte[] addressBytes = connectedScript.getPubKeyHash(); return RedeemData.of(keyBag.findKeyFromPubHash(addressBytes), connectedScript); } else if (connectedScript.isSentToRawPubKey()) { byte[] pubkeyBytes = connectedScript.getPubKey(); return RedeemData.of(keyBag.findKeyFromPubKey(pubkeyBytes), connectedScript); } else if (connectedScript.isPayToScriptHash()) { byte[] scriptHash = connectedScript.getPubKeyHash(); return keyBag.findRedeemDataFromScriptHash(scriptHash); } else { throw new ScriptException("Could not understand form of connected output script: " + connectedScript); } }
/** * Returns the RedeemData identified in the connected output, for either pay-to-address scripts, pay-to-key * or P2SH scripts. * If the script forms cannot be understood, throws ScriptException. * * @return a RedeemData or null if the connected data cannot be found in the wallet. */ @Nullable public RedeemData getConnectedRedeemData(KeyBag keyBag) throws ScriptException { TransactionOutput connectedOutput = getConnectedOutput(); checkNotNull(connectedOutput, "Input is not connected so cannot retrieve key"); Script connectedScript = connectedOutput.getScriptPubKey(); if (connectedScript.isSentToAddress()) { byte[] addressBytes = connectedScript.getPubKeyHash(); return RedeemData.of(keyBag.findKeyFromPubHash(addressBytes), connectedScript); } else if (connectedScript.isSentToRawPubKey()) { byte[] pubkeyBytes = connectedScript.getPubKey(); return RedeemData.of(keyBag.findKeyFromPubKey(pubkeyBytes), connectedScript); } else if (connectedScript.isPayToScriptHash()) { byte[] scriptHash = connectedScript.getPubKeyHash(); return keyBag.findRedeemDataFromScriptHash(scriptHash); } else { throw new ScriptException(ScriptError.SCRIPT_ERR_UNKNOWN_ERROR, "Could not understand form of connected output script: " + connectedScript); } }
/** * Returns the RedeemData identified in the connected output, for either pay-to-address scripts, pay-to-key * or P2SH scripts. * If the script forms cannot be understood, throws ScriptException. * * @return a RedeemData or null if the connected data cannot be found in the wallet. */ @Nullable public RedeemData getConnectedRedeemData(KeyBag keyBag) throws ScriptException { TransactionOutput connectedOutput = getConnectedOutput(); checkNotNull(connectedOutput, "Input is not connected so cannot retrieve key"); Script connectedScript = connectedOutput.getScriptPubKey(); if (connectedScript.isSentToAddress()) { byte[] addressBytes = connectedScript.getPubKeyHash(); return RedeemData.of(keyBag.findKeyFromPubHash(addressBytes), connectedScript); } else if (connectedScript.isSentToRawPubKey()) { byte[] pubkeyBytes = connectedScript.getPubKey(); return RedeemData.of(keyBag.findKeyFromPubKey(pubkeyBytes), connectedScript); } else if (connectedScript.isPayToScriptHash()) { byte[] scriptHash = connectedScript.getPubKeyHash(); return keyBag.findRedeemDataFromScriptHash(scriptHash); } else { throw new ScriptException("Could not understand form of connected output script: " + connectedScript); } }
/** * Returns the RedeemData identified in the connected output, for either pay-to-address scripts, pay-to-key * or P2SH scripts. * If the script forms cannot be understood, throws ScriptException. * * @return a RedeemData or null if the connected data cannot be found in the wallet. */ @Nullable public RedeemData getConnectedRedeemData(KeyBag keyBag) throws ScriptException { TransactionOutput connectedOutput = getConnectedOutput(); checkNotNull(connectedOutput, "Input is not connected so cannot retrieve key"); Script connectedScript = connectedOutput.getScriptPubKey(); if (connectedScript.isSentToAddress()) { byte[] addressBytes = connectedScript.getPubKeyHash(); return RedeemData.of(keyBag.findKeyFromPubHash(addressBytes), connectedScript); } else if (connectedScript.isSentToRawPubKey()) { byte[] pubkeyBytes = connectedScript.getPubKey(); return RedeemData.of(keyBag.findKeyFromPubKey(pubkeyBytes), connectedScript); } else if (connectedScript.isPayToScriptHash()) { byte[] scriptHash = connectedScript.getPubKeyHash(); return keyBag.findRedeemDataFromScriptHash(scriptHash); } else { throw new ScriptException("Could not understand form of connected output script: " + connectedScript); } }
/** * These constants are a part of a scriptSig signature on the inputs. They define the details of how a * transaction can be redeemed, specifically, they control how the hash of the transaction is calculated. */ public enum SigHash { ALL(1), NONE(2), SINGLE(3), ANYONECANPAY(0x80), // Caution: Using this type in isolation is non-standard. Treated similar to ANYONECANPAY_ALL. ANYONECANPAY_ALL(0x81), ANYONECANPAY_NONE(0x82), ANYONECANPAY_SINGLE(0x83), UNSET(0); // Caution: Using this type in isolation is non-standard. Treated similar to ALL. public final int value; /** * @param value */ private SigHash(final int value) { this.value = value; } /** * @return the value as a byte */ public byte byteValue() { return (byte) this.value; } }
throw new IllegalArgumentException("Malformed input (no output)"); if (input.getOutpoint().getConnectedOutput() == null) { throw new IllegalArgumentException("Malformed input (no connected output to outpoint)"); if (input.getOutpoint().getConnectedOutput().getParentTransaction() == null) { throw new IllegalArgumentException("Malformed input (connected output to outpoint has no parent)"); Transaction prevTx = input.getOutpoint().getConnectedOutput().getParentTransaction();