org.bitcoinj.wallet.RedeemData Java Examples
The following examples show how to use
org.bitcoinj.wallet.RedeemData.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GenerateLowSTests.java From green_android with GNU General Public License v3.0 | 5 votes |
private static void addOutputs(final Transaction outputTransaction, final KeyBag bag) throws ScriptException { int numInputs = outputTransaction.getInputs().size(); for (int i = 0; i < numInputs; i++) { TransactionInput txIn = outputTransaction.getInput(i); Script scriptPubKey = txIn.getConnectedOutput().getScriptPubKey(); RedeemData redeemData = txIn.getConnectedRedeemData(bag); checkNotNull(redeemData, "Transaction exists in wallet that we cannot redeem: %s", txIn.getOutpoint().getHash()); txIn.setScriptSig(scriptPubKey.createEmptyInputScript(redeemData.keys.get(0), redeemData.redeemScript)); } }
Example #2
Source File: GenerateLowSTests.java From GreenBits with GNU General Public License v3.0 | 5 votes |
private static void addOutputs(final Transaction outputTransaction, final KeyBag bag) throws ScriptException { int numInputs = outputTransaction.getInputs().size(); for (int i = 0; i < numInputs; i++) { TransactionInput txIn = outputTransaction.getInput(i); Script scriptPubKey = txIn.getConnectedOutput().getScriptPubKey(); RedeemData redeemData = txIn.getConnectedRedeemData(bag); checkNotNull(redeemData, "Transaction exists in wallet that we cannot redeem: %s", txIn.getOutpoint().getHash()); txIn.setScriptSig(scriptPubKey.createEmptyInputScript(redeemData.keys.get(0), redeemData.redeemScript)); } }
Example #3
Source File: CustomTransactionSigner.java From bcm-android with GNU General Public License v3.0 | 4 votes |
@Override public boolean signInputs(ProposedTransaction propTx, KeyBag keyBag) { Transaction tx = propTx.partialTx; int numInputs = tx.getInputs().size(); for (int i = 0; i < numInputs; i++) { TransactionInput txIn = tx.getInput(i); TransactionOutput txOut = txIn.getConnectedOutput(); if (txOut == null) { continue; } Script scriptPubKey = txOut.getScriptPubKey(); if (!ScriptPattern.isPayToScriptHash(scriptPubKey)) { log.warn("CustomTransactionSigner works only with P2SH transactions"); return false; } Script inputScript = checkNotNull(txIn.getScriptSig()); try { // We assume if its already signed, its hopefully got a SIGHASH type that will not invalidate when // we sign missing pieces (to check this would require either assuming any signatures are signing // standard output types or a way to get processed signatures out of script execution) txIn.getScriptSig().correctlySpends(tx, i, txIn.getConnectedOutput().getScriptPubKey()); log.warn("Input {} already correctly spends output, assuming SIGHASH type used will be safe and skipping signing.", i); continue; } catch (ScriptException e) { // Expected. } RedeemData redeemData = txIn.getConnectedRedeemData(keyBag); if (redeemData == null) { log.warn("No redeem data found for input {}", i); continue; } Sha256Hash sighash = tx.hashForSignature(i, redeemData.redeemScript, Transaction.SigHash.ALL, false); SignatureAndKey sigKey = getSignature(sighash, propTx.keyPaths.get(scriptPubKey)); TransactionSignature txSig = new TransactionSignature(sigKey.sig, Transaction.SigHash.ALL, false); int sigIndex = inputScript.getSigInsertionIndex(sighash, sigKey.pubKey); inputScript = scriptPubKey.getScriptSigWithSignature(inputScript, txSig.encodeToBitcoin(), sigIndex); txIn.setScriptSig(inputScript); } return true; }
Example #4
Source File: GenerateLowSTests.java From green_android with GNU General Public License v3.0 | 4 votes |
public static void main(final String[] argv) throws NoSuchAlgorithmException, IOException { final NetworkParameters params = new MainNetParams(); final LocalTransactionSigner signer = new LocalTransactionSigner(); final SecureRandom secureRandom = SecureRandom.getInstanceStrong(); final ECKey key = new ECKey(secureRandom); final KeyBag bag = new KeyBag() { @Override public ECKey findKeyFromPubHash(byte[] pubkeyHash) { return key; } @Override public ECKey findKeyFromPubKey(byte[] pubkey) { return key; } @Override public RedeemData findRedeemDataFromScriptHash(byte[] scriptHash) { return null; } }; // Generate a fictional output transaction we take values from, and // an input transaction for the test case final Transaction outputTransaction = new Transaction(params); final Transaction inputTransaction = new Transaction(params); final TransactionOutput output = new TransactionOutput(params, inputTransaction, Coin.ZERO, key.toAddress(params)); inputTransaction.addOutput(output); outputTransaction.addInput(output); outputTransaction.addOutput(Coin.ZERO, new ECKey(secureRandom).toAddress(params)); addOutputs(outputTransaction, bag); // Sign the transaction final ProposedTransaction proposedTransaction = new ProposedTransaction(outputTransaction); signer.signInputs(proposedTransaction, bag); final TransactionInput input = proposedTransaction.partialTx.getInput(0); input.verify(output); input.getScriptSig().correctlySpends(outputTransaction, 0, output.getScriptPubKey(), EnumSet.of(Script.VerifyFlag.DERSIG, Script.VerifyFlag.P2SH)); final Script scriptSig = input.getScriptSig(); final TransactionSignature signature = TransactionSignature.decodeFromBitcoin(scriptSig.getChunks().get(0).data, true, false); // First output a conventional low-S transaction with the LOW_S flag, for the tx_valid.json set System.out.println("[\"A transaction with a low-S signature.\"],"); System.out.println("[[[\"" + inputTransaction.getHashAsString() + "\", " + output.getIndex() + ", \"" + scriptToString(output.getScriptPubKey()) + "\"]],\n" + "\"" + Utils.HEX.encode(proposedTransaction.partialTx.unsafeBitcoinSerialize()) + "\", \"" + Script.VerifyFlag.P2SH.name() + "," + Script.VerifyFlag.LOW_S.name() + "\"],"); final BigInteger highS = HIGH_S_DIFFERENCE.subtract(signature.s); final TransactionSignature highSig = new TransactionSignature(signature.r, highS); input.setScriptSig(new ScriptBuilder().data(highSig.encodeToBitcoin()).data(scriptSig.getChunks().get(1).data).build()); input.getScriptSig().correctlySpends(outputTransaction, 0, output.getScriptPubKey(), EnumSet.of(Script.VerifyFlag.P2SH)); // A high-S transaction without the LOW_S flag, for the tx_valid.json set System.out.println("[\"A transaction with a high-S signature.\"],"); System.out.println("[[[\"" + inputTransaction.getHashAsString() + "\", " + output.getIndex() + ", \"" + scriptToString(output.getScriptPubKey()) + "\"]],\n" + "\"" + Utils.HEX.encode(proposedTransaction.partialTx.unsafeBitcoinSerialize()) + "\", \"" + Script.VerifyFlag.P2SH.name() + "\"],"); // Lastly a conventional high-S transaction with the LOW_S flag, for the tx_invalid.json set System.out.println("[\"A transaction with a high-S signature.\"],"); System.out.println("[[[\"" + inputTransaction.getHashAsString() + "\", " + output.getIndex() + ", \"" + scriptToString(output.getScriptPubKey()) + "\"]],\n" + "\"" + Utils.HEX.encode(proposedTransaction.partialTx.unsafeBitcoinSerialize()) + "\", \"" + Script.VerifyFlag.P2SH.name() + "," + Script.VerifyFlag.LOW_S.name() + "\"],"); }
Example #5
Source File: CustomTransactionSigner.java From green_android with GNU General Public License v3.0 | 4 votes |
@Override public boolean signInputs(ProposedTransaction propTx, KeyBag keyBag) { Transaction tx = propTx.partialTx; int numInputs = tx.getInputs().size(); for (int i = 0; i < numInputs; i++) { TransactionInput txIn = tx.getInput(i); TransactionOutput txOut = txIn.getConnectedOutput(); if (txOut == null) { continue; } Script scriptPubKey = txOut.getScriptPubKey(); if (!scriptPubKey.isPayToScriptHash()) { log.warn("CustomTransactionSigner works only with P2SH transactions"); return false; } Script inputScript = checkNotNull(txIn.getScriptSig()); try { // We assume if its already signed, its hopefully got a SIGHASH type that will not invalidate when // we sign missing pieces (to check this would require either assuming any signatures are signing // standard output types or a way to get processed signatures out of script execution) txIn.getScriptSig().correctlySpends(tx, i, txIn.getConnectedOutput().getScriptPubKey()); log.warn("Input {} already correctly spends output, assuming SIGHASH type used will be safe and skipping signing.", i); continue; } catch (ScriptException e) { // Expected. } RedeemData redeemData = txIn.getConnectedRedeemData(keyBag); if (redeemData == null) { log.warn("No redeem data found for input {}", i); continue; } Sha256Hash sighash = tx.hashForSignature(i, redeemData.redeemScript, Transaction.SigHash.ALL, false); SignatureAndKey sigKey = getSignature(sighash, propTx.keyPaths.get(scriptPubKey)); TransactionSignature txSig = new TransactionSignature(sigKey.sig, Transaction.SigHash.ALL, false); int sigIndex = inputScript.getSigInsertionIndex(sighash, sigKey.pubKey); inputScript = scriptPubKey.getScriptSigWithSignature(inputScript, txSig.encodeToBitcoin(), sigIndex); txIn.setScriptSig(inputScript); } return true; }
Example #6
Source File: TransactionInput.java From green_android with GNU General Public License v3.0 | 4 votes |
/** * Alias for getOutpoint().getConnectedRedeemData(keyBag) * @see TransactionOutPoint#getConnectedRedeemData(org.bitcoinj.wallet.KeyBag) */ @Nullable public RedeemData getConnectedRedeemData(KeyBag keyBag) throws ScriptException { return getOutpoint().getConnectedRedeemData(keyBag); }
Example #7
Source File: GenerateLowSTests.java From GreenBits with GNU General Public License v3.0 | 4 votes |
public static void main(final String[] argv) throws NoSuchAlgorithmException, IOException { final NetworkParameters params = new MainNetParams(); final LocalTransactionSigner signer = new LocalTransactionSigner(); final SecureRandom secureRandom = SecureRandom.getInstanceStrong(); final ECKey key = new ECKey(secureRandom); final KeyBag bag = new KeyBag() { @Override public ECKey findKeyFromPubHash(byte[] pubkeyHash) { return key; } @Override public ECKey findKeyFromPubKey(byte[] pubkey) { return key; } @Override public RedeemData findRedeemDataFromScriptHash(byte[] scriptHash) { return null; } }; // Generate a fictional output transaction we take values from, and // an input transaction for the test case final Transaction outputTransaction = new Transaction(params); final Transaction inputTransaction = new Transaction(params); final TransactionOutput output = new TransactionOutput(params, inputTransaction, Coin.ZERO, key.toAddress(params)); inputTransaction.addOutput(output); outputTransaction.addInput(output); outputTransaction.addOutput(Coin.ZERO, new ECKey(secureRandom).toAddress(params)); addOutputs(outputTransaction, bag); // Sign the transaction final ProposedTransaction proposedTransaction = new ProposedTransaction(outputTransaction); signer.signInputs(proposedTransaction, bag); final TransactionInput input = proposedTransaction.partialTx.getInput(0); input.verify(output); input.getScriptSig().correctlySpends(outputTransaction, 0, output.getScriptPubKey(), EnumSet.of(Script.VerifyFlag.DERSIG, Script.VerifyFlag.P2SH)); final Script scriptSig = input.getScriptSig(); final TransactionSignature signature = TransactionSignature.decodeFromBitcoin(scriptSig.getChunks().get(0).data, true, false); // First output a conventional low-S transaction with the LOW_S flag, for the tx_valid.json set System.out.println("[\"A transaction with a low-S signature.\"],"); System.out.println("[[[\"" + inputTransaction.getHashAsString() + "\", " + output.getIndex() + ", \"" + scriptToString(output.getScriptPubKey()) + "\"]],\n" + "\"" + Utils.HEX.encode(proposedTransaction.partialTx.unsafeBitcoinSerialize()) + "\", \"" + Script.VerifyFlag.P2SH.name() + "," + Script.VerifyFlag.LOW_S.name() + "\"],"); final BigInteger highS = HIGH_S_DIFFERENCE.subtract(signature.s); final TransactionSignature highSig = new TransactionSignature(signature.r, highS); input.setScriptSig(new ScriptBuilder().data(highSig.encodeToBitcoin()).data(scriptSig.getChunks().get(1).data).build()); input.getScriptSig().correctlySpends(outputTransaction, 0, output.getScriptPubKey(), EnumSet.of(Script.VerifyFlag.P2SH)); // A high-S transaction without the LOW_S flag, for the tx_valid.json set System.out.println("[\"A transaction with a high-S signature.\"],"); System.out.println("[[[\"" + inputTransaction.getHashAsString() + "\", " + output.getIndex() + ", \"" + scriptToString(output.getScriptPubKey()) + "\"]],\n" + "\"" + Utils.HEX.encode(proposedTransaction.partialTx.unsafeBitcoinSerialize()) + "\", \"" + Script.VerifyFlag.P2SH.name() + "\"],"); // Lastly a conventional high-S transaction with the LOW_S flag, for the tx_invalid.json set System.out.println("[\"A transaction with a high-S signature.\"],"); System.out.println("[[[\"" + inputTransaction.getHashAsString() + "\", " + output.getIndex() + ", \"" + scriptToString(output.getScriptPubKey()) + "\"]],\n" + "\"" + Utils.HEX.encode(proposedTransaction.partialTx.unsafeBitcoinSerialize()) + "\", \"" + Script.VerifyFlag.P2SH.name() + "," + Script.VerifyFlag.LOW_S.name() + "\"],"); }
Example #8
Source File: CustomTransactionSigner.java From GreenBits with GNU General Public License v3.0 | 4 votes |
@Override public boolean signInputs(ProposedTransaction propTx, KeyBag keyBag) { Transaction tx = propTx.partialTx; int numInputs = tx.getInputs().size(); for (int i = 0; i < numInputs; i++) { TransactionInput txIn = tx.getInput(i); TransactionOutput txOut = txIn.getConnectedOutput(); if (txOut == null) { continue; } Script scriptPubKey = txOut.getScriptPubKey(); if (!scriptPubKey.isPayToScriptHash()) { log.warn("CustomTransactionSigner works only with P2SH transactions"); return false; } Script inputScript = checkNotNull(txIn.getScriptSig()); try { // We assume if its already signed, its hopefully got a SIGHASH type that will not invalidate when // we sign missing pieces (to check this would require either assuming any signatures are signing // standard output types or a way to get processed signatures out of script execution) txIn.getScriptSig().correctlySpends(tx, i, txIn.getConnectedOutput().getScriptPubKey()); log.warn("Input {} already correctly spends output, assuming SIGHASH type used will be safe and skipping signing.", i); continue; } catch (ScriptException e) { // Expected. } RedeemData redeemData = txIn.getConnectedRedeemData(keyBag); if (redeemData == null) { log.warn("No redeem data found for input {}", i); continue; } Sha256Hash sighash = tx.hashForSignature(i, redeemData.redeemScript, Transaction.SigHash.ALL, false); SignatureAndKey sigKey = getSignature(sighash, propTx.keyPaths.get(scriptPubKey)); TransactionSignature txSig = new TransactionSignature(sigKey.sig, Transaction.SigHash.ALL, false); int sigIndex = inputScript.getSigInsertionIndex(sighash, sigKey.pubKey); inputScript = scriptPubKey.getScriptSigWithSignature(inputScript, txSig.encodeToBitcoin(), sigIndex); txIn.setScriptSig(inputScript); } return true; }
Example #9
Source File: TransactionInput.java From GreenBits with GNU General Public License v3.0 | 4 votes |
/** * Alias for getOutpoint().getConnectedRedeemData(keyBag) * @see TransactionOutPoint#getConnectedRedeemData(org.bitcoinj.wallet.KeyBag) */ @Nullable public RedeemData getConnectedRedeemData(KeyBag keyBag) throws ScriptException { return getOutpoint().getConnectedRedeemData(keyBag); }
Example #10
Source File: TransactionInput.java From bcm-android with GNU General Public License v3.0 | 2 votes |
/** * Alias for getOutpoint().getConnectedRedeemData(keyBag) * * @see TransactionOutPoint#getConnectedRedeemData(KeyBag) */ @Nullable public RedeemData getConnectedRedeemData(KeyBag keyBag) throws ScriptException { return getOutpoint().getConnectedRedeemData(keyBag); }