You received this message because you are subscribed to a topic in the Google Groups "bitcoinj" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bitcoinj/pHv4XyMbhZo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bitcoinj+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bitcoinj/qbokc3%241480%241%40blaine.gmane.org.
NetworkParameters network = new TestNet3Params().get();
String fromAddress = "2N8qKVKuR3QAy2cA5fF2QbuCpBGfGCmbUHx";
String toAddress = "mxHqrQBWuCndNaubTYUbcEVzeNPsT34TP6";
String changeAddress = fromAddress;
String privateKey = "c5964e19634e473e9c8fed3b149c4d44b6d62d62724035aad3eb911a1495ae27";
String inputAmount = "0.00773516";
String inputScriptPubKeyHash = "a914aafc9f09b99cbb1ba46001ec4868ba4fb32543ca87";
String prevTxHash = "bb49e7c6da6b35845a798cc6c18e9cf959daf30d9a5e5b82e983757246092f50";
long prevTxIndex = 0;
BigDecimal inputAmountToBigDecimal = new BigDecimal(inputAmount).multiply(BTC_TO_SATS);
long inputAmountToLong = inputAmountToBigDecimal.longValue();
long outputAmountToLong = 1000;
long feeAmountToLong = 1000;
long changeAmountToLong = inputAmountToLong - outputAmountToLong - feeAmountToLong;
Transaction tx = new Transaction(network);
ecKey = ECKey.fromPrivate(new BigInteger(privateKey, 16));
tx.addOutput(Coin.valueOf(outputAmountToLong), Address.fromString(network, toAddress));
tx.addOutput(Coin.valueOf(changeAmountToLong), Address.fromString(network, changeAddress));
Script script = new Script(Hex.decode(inputScriptPubKeyHash.getBytes()));
System.out.println("isP2SH: " + ScriptPattern.isP2SH(script));
TransactionOutPoint prevTx = new TransactionOutPoint(
network,
prevTxIndex,
Sha256Hash.wrap(prevTxHash)
);
if (ScriptPattern.isP2SH(script)) {
System.out.println("This is P2SH input.");
TransactionInput input = new TransactionInput(
tx.getParams(),
tx,
new byte[0],
prevTx,
Coin.valueOf(inputAmountToLong)
);
tx.addInput(input);
Script scriptCode = new ScriptBuilder().data(ScriptBuilder.createP2PKHOutputScript(ecKey).getProgram()).build();
TransactionSignature signature = tx.calculateWitnessSignature(0, ecKey, scriptCode, input.getValue(), Transaction.SigHash.ALL, true);
input.setScriptSig(new ScriptBuilder().data(ScriptBuilder.createP2WPKHOutputScript(ecKey).getProgram()).build());
input.setWitness(TransactionWitness.redeemP2WPKH(signature, ecKey));
}
else {
tx.addSignedInput(prevTx, script, ecKey);
}
System.out.println("tx serialized: " + Hex.toHexString(tx.bitcoinSerialize()));
> an email to bitc...@googlegroups.com
> <mailto:bitc...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/bitcoinj/0accc4bf-a75f-4d39-9597-8e86afcfe572%40googlegroups.com
> <https://groups.google.com/d/msgid/bitcoinj/0accc4bf-a75f-4d39-9597-8e86afcfe572%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to a topic in the Google Groups "bitcoinj" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/bitcoinj/pHv4XyMbhZo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to bitc...@googlegroups.com.