Java Code Examples for org.bitcoinj.script.Script#getProgram()
The following examples show how to use
org.bitcoinj.script.Script#getProgram() .
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: ScriptTools.java From thunder with GNU Affero General Public License v3.0 | 5 votes |
public static Script produceScript (byte[] template, byte[]... parameters) { try { ScriptBuilder builder = new ScriptBuilder(); int parameter = 0; for (byte chunk : template) { int op = chunk; if (op < 0) { op = op + 256; } if (op == 255) { builder.data(parameters[parameter]); parameter++; } else if (op == 0) { builder.data(new byte[0]); } else { builder.op(op); } } //Bug in bitcoinJ when dealing with OP_0. Gets solved by reserializing. Script s = builder.build(); return new Script(s.getProgram()); } catch (Exception e) { e.printStackTrace(); return null; } }
Example 2
Source File: Tools.java From thunder with GNU Affero General Public License v3.0 | 5 votes |
public static Script getMultisigInputScript (ECDSASignature client, ECDSASignature server) { ArrayList<TransactionSignature> signList = new ArrayList<TransactionSignature>(); signList.add(new TransactionSignature(client, SigHash.ALL, false)); signList.add(new TransactionSignature(server, SigHash.ALL, false)); Script inputScript = ScriptBuilder.createMultiSigInputScript(signList); /* * Seems there is a bug here, * https://groups.google.com/forum/#!topic/bitcoinj/A9R8TdUsXms */ Script workaround = new Script(inputScript.getProgram()); return workaround; }
Example 3
Source File: ScriptTools.java From thundernetwork with GNU Affero General Public License v3.0 | 5 votes |
public static Script produceScript (byte[] template, byte[]... parameters) { try { ScriptBuilder builder = new ScriptBuilder(); int parameter = 0; for (byte chunk : template) { int op = chunk; if (op < 0) { op = op + 256; } if (op == 255) { builder.data(parameters[parameter]); parameter++; } else if (op == 0) { builder.data(new byte[0]); } else { builder.op(op); } } //Bug in bitcoinJ when dealing with OP_0. Gets solved by reserializing. Script s = builder.build(); return new Script(s.getProgram()); } catch (Exception e) { e.printStackTrace(); return null; } }
Example 4
Source File: Tools.java From thundernetwork with GNU Affero General Public License v3.0 | 5 votes |
/** * Gets the multisig input script. * * @param client the client * @param server the server * @return the multisig input script */ public static Script getMultisigInputScript (ECDSASignature client, ECDSASignature server) { ArrayList<TransactionSignature> signList = new ArrayList<TransactionSignature>(); signList.add(new TransactionSignature(client, SigHash.ALL, false)); signList.add(new TransactionSignature(server, SigHash.ALL, false)); Script inputScript = ScriptBuilder.createMultiSigInputScript(signList); /* * Seems there is a bug here, * https://groups.google.com/forum/#!topic/bitcoinj/A9R8TdUsXms */ Script workaround = new Script(inputScript.getProgram()); return workaround; }
Example 5
Source File: TradeWalletService.java From bisq-core with GNU Affero General Public License v3.0 | 4 votes |
public Transaction emergencySignAndPublishPayoutTx(String depositTxHex, Coin buyerPayoutAmount, Coin sellerPayoutAmount, Coin arbitratorPayoutAmount, Coin txFee, String buyerAddressString, String sellerAddressString, String arbitratorAddressString, @Nullable String buyerPrivateKeyAsHex, @Nullable String sellerPrivateKeyAsHex, String arbitratorPrivateKeyAsHex, String buyerPubKeyAsHex, String sellerPubKeyAsHex, String arbitratorPubKeyAsHex, String P2SHMultiSigOutputScript, TxBroadcaster.Callback callback) throws AddressFormatException, TransactionVerificationException, WalletException { log.info("signAndPublishPayoutTx called"); log.info("depositTxHex " + depositTxHex); log.info("buyerPayoutAmount " + buyerPayoutAmount.toFriendlyString()); log.info("sellerPayoutAmount " + sellerPayoutAmount.toFriendlyString()); log.info("arbitratorPayoutAmount " + arbitratorPayoutAmount.toFriendlyString()); log.info("buyerAddressString " + buyerAddressString); log.info("sellerAddressString " + sellerAddressString); log.info("arbitratorAddressString " + arbitratorAddressString); log.info("buyerPrivateKeyAsHex (not displayed for security reasons)"); log.info("sellerPrivateKeyAsHex (not displayed for security reasons)"); log.info("arbitratorPrivateKeyAsHex (not displayed for security reasons)"); log.info("buyerPubKeyAsHex " + buyerPubKeyAsHex); log.info("sellerPubKeyAsHex " + sellerPubKeyAsHex); log.info("arbitratorPubKeyAsHex " + arbitratorPubKeyAsHex); log.info("P2SHMultiSigOutputScript " + P2SHMultiSigOutputScript); checkNotNull((buyerPrivateKeyAsHex != null || sellerPrivateKeyAsHex != null), "either buyerPrivateKeyAsHex or sellerPrivateKeyAsHex must not be null"); byte[] buyerPubKey = ECKey.fromPublicOnly(Utils.HEX.decode(buyerPubKeyAsHex)).getPubKey(); byte[] sellerPubKey = ECKey.fromPublicOnly(Utils.HEX.decode(sellerPubKeyAsHex)).getPubKey(); final byte[] arbitratorPubKey = ECKey.fromPublicOnly(Utils.HEX.decode(arbitratorPubKeyAsHex)).getPubKey(); Script p2SHMultiSigOutputScript = getP2SHMultiSigOutputScript(buyerPubKey, sellerPubKey, arbitratorPubKey); Coin msOutput = buyerPayoutAmount.add(sellerPayoutAmount).add(arbitratorPayoutAmount).add(txFee); TransactionOutput p2SHMultiSigOutput = new TransactionOutput(params, null, msOutput, p2SHMultiSigOutputScript.getProgram()); Transaction depositTx = new Transaction(params); depositTx.addOutput(p2SHMultiSigOutput); Transaction payoutTx = new Transaction(params); Sha256Hash spendTxHash = Sha256Hash.wrap(depositTxHex); payoutTx.addInput(new TransactionInput(params, depositTx, p2SHMultiSigOutputScript.getProgram(), new TransactionOutPoint(params, 0, spendTxHash), msOutput)); if (buyerPayoutAmount.isGreaterThan(Coin.ZERO)) payoutTx.addOutput(buyerPayoutAmount, Address.fromBase58(params, buyerAddressString)); if (sellerPayoutAmount.isGreaterThan(Coin.ZERO)) payoutTx.addOutput(sellerPayoutAmount, Address.fromBase58(params, sellerAddressString)); if (arbitratorPayoutAmount.isGreaterThan(Coin.ZERO)) payoutTx.addOutput(arbitratorPayoutAmount, Address.fromBase58(params, arbitratorAddressString)); // take care of sorting! Script redeemScript = getMultiSigRedeemScript(buyerPubKey, sellerPubKey, arbitratorPubKey); Sha256Hash sigHash = payoutTx.hashForSignature(0, redeemScript, Transaction.SigHash.ALL, false); ECKey.ECDSASignature tradersSignature; if (buyerPrivateKeyAsHex != null && !buyerPrivateKeyAsHex.isEmpty()) { final ECKey buyerPrivateKey = ECKey.fromPrivate(Utils.HEX.decode(buyerPrivateKeyAsHex)); checkNotNull(buyerPrivateKey, "buyerPrivateKey must not be null"); tradersSignature = buyerPrivateKey.sign(sigHash, aesKey).toCanonicalised(); } else { checkNotNull(sellerPrivateKeyAsHex, "sellerPrivateKeyAsHex must not be null"); final ECKey sellerPrivateKey = ECKey.fromPrivate(Utils.HEX.decode(sellerPrivateKeyAsHex)); checkNotNull(sellerPrivateKey, "sellerPrivateKey must not be null"); tradersSignature = sellerPrivateKey.sign(sigHash, aesKey).toCanonicalised(); } final ECKey key = ECKey.fromPrivate(Utils.HEX.decode(arbitratorPrivateKeyAsHex)); checkNotNull(key, "key must not be null"); ECKey.ECDSASignature arbitratorSignature = key.sign(sigHash, aesKey).toCanonicalised(); TransactionSignature tradersTxSig = new TransactionSignature(tradersSignature, Transaction.SigHash.ALL, false); TransactionSignature arbitratorTxSig = new TransactionSignature(arbitratorSignature, Transaction.SigHash.ALL, false); // Take care of order of signatures. See comment below at getMultiSigRedeemScript (sort order needed here: arbitrator, seller, buyer) Script inputScript = ScriptBuilder.createP2SHMultiSigInputScript(ImmutableList.of(arbitratorTxSig, tradersTxSig), redeemScript); TransactionInput input = payoutTx.getInput(0); input.setScriptSig(inputScript); WalletService.printTx("payoutTx", payoutTx); WalletService.verifyTransaction(payoutTx); WalletService.checkWalletConsistency(wallet); broadcastTx(payoutTx, callback, 20); return payoutTx; }
Example 6
Source File: TradeWalletService.java From bisq with GNU Affero General Public License v3.0 | 4 votes |
public void emergencySignAndPublishPayoutTxFrom2of2MultiSig(String depositTxHex, Coin buyerPayoutAmount, Coin sellerPayoutAmount, Coin txFee, String buyerAddressString, String sellerAddressString, String buyerPrivateKeyAsHex, String sellerPrivateKeyAsHex, String buyerPubKeyAsHex, String sellerPubKeyAsHex, TxBroadcaster.Callback callback) throws AddressFormatException, TransactionVerificationException, WalletException { byte[] buyerPubKey = ECKey.fromPublicOnly(Utils.HEX.decode(buyerPubKeyAsHex)).getPubKey(); byte[] sellerPubKey = ECKey.fromPublicOnly(Utils.HEX.decode(sellerPubKeyAsHex)).getPubKey(); Script p2SHMultiSigOutputScript = get2of2MultiSigOutputScript(buyerPubKey, sellerPubKey); Coin msOutput = buyerPayoutAmount.add(sellerPayoutAmount).add(txFee); TransactionOutput p2SHMultiSigOutput = new TransactionOutput(params, null, msOutput, p2SHMultiSigOutputScript.getProgram()); Transaction depositTx = new Transaction(params); depositTx.addOutput(p2SHMultiSigOutput); Transaction payoutTx = new Transaction(params); Sha256Hash spendTxHash = Sha256Hash.wrap(depositTxHex); payoutTx.addInput(new TransactionInput(params, depositTx, p2SHMultiSigOutputScript.getProgram(), new TransactionOutPoint(params, 0, spendTxHash), msOutput)); if (buyerPayoutAmount.isPositive()) { payoutTx.addOutput(buyerPayoutAmount, Address.fromBase58(params, buyerAddressString)); } if (sellerPayoutAmount.isPositive()) { payoutTx.addOutput(sellerPayoutAmount, Address.fromBase58(params, sellerAddressString)); } // take care of sorting! Script redeemScript = get2of2MultiSigRedeemScript(buyerPubKey, sellerPubKey); Sha256Hash sigHash = payoutTx.hashForSignature(0, redeemScript, Transaction.SigHash.ALL, false); ECKey buyerPrivateKey = ECKey.fromPrivate(Utils.HEX.decode(buyerPrivateKeyAsHex)); checkNotNull(buyerPrivateKey, "key must not be null"); ECKey.ECDSASignature buyerECDSASignature = buyerPrivateKey.sign(sigHash, aesKey).toCanonicalised(); ECKey sellerPrivateKey = ECKey.fromPrivate(Utils.HEX.decode(sellerPrivateKeyAsHex)); checkNotNull(sellerPrivateKey, "key must not be null"); ECKey.ECDSASignature sellerECDSASignature = sellerPrivateKey.sign(sigHash, aesKey).toCanonicalised(); TransactionSignature buyerTxSig = new TransactionSignature(buyerECDSASignature, Transaction.SigHash.ALL, false); TransactionSignature sellerTxSig = new TransactionSignature(sellerECDSASignature, Transaction.SigHash.ALL, false); Script inputScript = ScriptBuilder.createP2SHMultiSigInputScript(ImmutableList.of(sellerTxSig, buyerTxSig), redeemScript); TransactionInput input = payoutTx.getInput(0); input.setScriptSig(inputScript); WalletService.printTx("payoutTx", payoutTx); WalletService.verifyTransaction(payoutTx); WalletService.checkWalletConsistency(wallet); broadcastTx(payoutTx, callback, 20); }
Example 7
Source File: Transaction.java From green_android with GNU General Public License v3.0 | 2 votes |
/** * <p>Calculates a signature hash, that is, a hash of a simplified form of the transaction. How exactly the transaction * is simplified is specified by the type and anyoneCanPay parameters.</p> * * <p>This is a low level API and when using the regular {@link Wallet} class you don't have to call this yourself. * When working with more complex transaction types and contracts, it can be necessary. When signing a Witness output * the scriptCode should be the script encoded into the scriptSig field, for normal transactions, it's the * scriptPubKey of the output you're signing for. (See BIP143: https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki)</p> * * @param inputIndex input the signature is being calculated for. Tx signatures are always relative to an input. * @param scriptCode the script that should be in the given input during signing. * @param prevValue the value of the coin being spent * @param type Should be SigHash.ALL * @param anyoneCanPay should be false. */ public synchronized Sha256Hash hashForSignatureWitness(int inputIndex, Script scriptCode, Coin prevValue, SigHash type, boolean anyoneCanPay) { byte[] connectedScript = scriptCode.getProgram(); return hashForSignatureWitness(inputIndex, connectedScript, prevValue, type, anyoneCanPay); }
Example 8
Source File: Transaction.java From GreenBits with GNU General Public License v3.0 | 2 votes |
/** * <p>Calculates a signature hash, that is, a hash of a simplified form of the transaction. How exactly the transaction * is simplified is specified by the type and anyoneCanPay parameters.</p> * * <p>This is a low level API and when using the regular {@link Wallet} class you don't have to call this yourself. * When working with more complex transaction types and contracts, it can be necessary. When signing a Witness output * the scriptCode should be the script encoded into the scriptSig field, for normal transactions, it's the * scriptPubKey of the output you're signing for. (See BIP143: https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki)</p> * * @param inputIndex input the signature is being calculated for. Tx signatures are always relative to an input. * @param scriptCode the script that should be in the given input during signing. * @param prevValue the value of the coin being spent * @param type Should be SigHash.ALL * @param anyoneCanPay should be false. */ public synchronized Sha256Hash hashForSignatureWitness(int inputIndex, Script scriptCode, Coin prevValue, SigHash type, boolean anyoneCanPay) { byte[] connectedScript = scriptCode.getProgram(); return hashForSignatureWitness(inputIndex, connectedScript, prevValue, type, anyoneCanPay); }