This commit is contained in:
Manfred Karrer 2015-03-14 23:58:19 +01:00
parent c553f77335
commit a244d0c999

View file

@ -709,7 +709,7 @@ public class WalletService {
*/ */
// Now we construct the real 2of3 multiSig tx from the serialized offerers tx // Now we construct the real 2of3 multiSig tx from the serialized offerers tx
preparedDepositTx = new Transaction(params, preparedDepositTx.bitcoinSerialize());
// The serialized offerers tx looks like: // The serialized offerers tx looks like:
/* /*
IN[0] any input offerer > offererInputAmount + fee (unsigned) IN[0] any input offerer > offererInputAmount + fee (unsigned)
@ -798,6 +798,7 @@ public class WalletService {
printInputs("preparedDepositTx", preparedDepositTx); printInputs("preparedDepositTx", preparedDepositTx);
log.trace("preparedDepositTx = " + preparedDepositTx); log.trace("preparedDepositTx = " + preparedDepositTx);
preparedDepositTx = new Transaction(params, preparedDepositTx.bitcoinSerialize());
// add input // add input
Transaction offerersFirstTxConnOut = wallet.getTransaction(preparedDepositTx.getInput(0).getOutpoint().getHash()); Transaction offerersFirstTxConnOut = wallet.getTransaction(preparedDepositTx.getInput(0).getOutpoint().getHash());
TransactionOutPoint offerersFirstTxOutPoint = new TransactionOutPoint(params, offererTxOutIndex, offerersFirstTxConnOut); TransactionOutPoint offerersFirstTxOutPoint = new TransactionOutPoint(params, offererTxOutIndex, offerersFirstTxConnOut);
@ -930,13 +931,29 @@ public class WalletService {
// We create the signature for that tx // We create the signature for that tx
TransactionOutput multiSigOutput = tx.getInput(0).getConnectedOutput(); TransactionOutput multiSigOutput = tx.getInput(0).getConnectedOutput();
Script multiSigScript = multiSigOutput.getScriptPubKey(); Script multiSigScript = multiSigOutput.getScriptPubKey();
Sha256Hash sigHash = tx.hashForSignature(0, multiSigScript, Transaction.SigHash.ALL, false); Sha256Hash sigHash = tx.hashForSignature(0, multiSigScript, Transaction.SigHash.ALL, false);
ECKey.ECDSASignature offererSignature = getAddressInfo(tradeID).getKey().sign(sigHash); ECKey.ECDSASignature offererSignature = getAddressInfo(tradeID).getKey().sign(sigHash);
TransactionSignature offererTxSig = new TransactionSignature(offererSignature, Transaction.SigHash.ALL, false); TransactionSignature offererTxSig = new TransactionSignature(offererSignature, Transaction.SigHash.ALL, false);
Script inputScript = ScriptBuilder.createMultiSigInputScript(ImmutableList.of(offererTxSig)); Script inputScript = ScriptBuilder.createP2SHMultiSigInputScript(ImmutableList.of(offererTxSig), multiSigScript);
tx.getInput(0).setScriptSig(inputScript); tx.getInput(0).setScriptSig(inputScript);
log.trace("check if it can be correctly spent for ms input");
try {
tx.getInput(0).getScriptSig().correctlySpends(tx, 0, inputScript);
} catch (Throwable t) {
t.printStackTrace();
log.error(t.getMessage());
}
log.trace("verify multiSigOutput");
try {
tx.getInput(0).verify(multiSigOutput);
} catch (Throwable t) {
t.printStackTrace();
log.error(t.getMessage());
}
log.trace("sigHash=" + sigHash); log.trace("sigHash=" + sigHash);
return new Pair<>(offererSignature, depositTx); return new Pair<>(offererSignature, depositTx);
} }
@ -959,31 +976,49 @@ public class WalletService {
log.trace("callback=" + callback); log.trace("callback=" + callback);
// We create the payout tx // We create the payout tx
depositTx = new Transaction(params, depositTx.bitcoinSerialize());
Transaction tx = createPayoutTx(depositTx, offererPaybackAmount, takerPaybackAmount, offererAddress, getAddressInfo(tradeID).getAddressString()); Transaction tx = createPayoutTx(depositTx, offererPaybackAmount, takerPaybackAmount, offererAddress, getAddressInfo(tradeID).getAddressString());
// We sign that tx with our key and apply the signature form the offerer // We sign that tx with our key and apply the signature form the offerer
TransactionOutput multiSigOutput = tx.getInput(0).getConnectedOutput(); TransactionOutput multiSigOutput = tx.getInput(0).getConnectedOutput();
Script multiSigScript = multiSigOutput.getScriptPubKey(); Script multiSigScript = multiSigOutput.getScriptPubKey();
Sha256Hash sigHash = tx.hashForSignature(0, multiSigScript, Transaction.SigHash.ALL, false); Sha256Hash sigHash = tx.hashForSignature(0, multiSigScript, Transaction.SigHash.ALL, false);
log.trace("sigHash=" + sigHash); log.trace("sigHash=" + sigHash);
ECKey.ECDSASignature takerSignature = getAddressInfo(tradeID).getKey().sign(sigHash); ECKey.ECDSASignature takerSignature = getAddressInfo(tradeID).getKey().sign(sigHash);
TransactionSignature takerTxSig = new TransactionSignature(takerSignature, Transaction.SigHash.ALL, false); TransactionSignature takerTxSig = new TransactionSignature(takerSignature, Transaction.SigHash.ALL, false);
TransactionSignature offererTxSig = new TransactionSignature(offererSignature, Transaction.SigHash.ALL, false); TransactionSignature offererTxSig = new TransactionSignature(offererSignature, Transaction.SigHash.ALL, false);
Script inputScript = ScriptBuilder.createP2SHMultiSigInputScript(ImmutableList.of(offererTxSig, takerTxSig), multiSigScript);
Script inputScript = ScriptBuilder.createMultiSigInputScript(ImmutableList.of(offererTxSig, takerTxSig));
tx.getInput(0).setScriptSig(inputScript); tx.getInput(0).setScriptSig(inputScript);
log.trace("verify tx"); log.trace("verify tx");
tx.verify(); tx.verify();
log.trace("check if it can be correctly spent for ms input"); log.trace("check if it can be correctly spent for ms input");
tx.getInput(0).getScriptSig().correctlySpends(tx, 0, multiSigScript); try {
tx.getInput(0).getScriptSig().correctlySpends(tx, 0, inputScript);
} catch (Throwable t) {
t.printStackTrace();
log.error(t.getMessage());
}
try {
tx.getInput(0).getScriptSig().correctlySpends(tx, 0, inputScript);
} catch (Throwable t) {
t.printStackTrace();
log.error(t.getMessage());
}
log.trace("verify multiSigOutput"); log.trace("verify multiSigOutput");
tx.getInput(0).verify(multiSigOutput); try {
tx.getInput(0).verify(multiSigOutput);
} catch (Throwable t) {
t.printStackTrace();
log.error(t.getMessage());
}
ListenableFuture<Transaction> broadcastComplete = walletAppKit.peerGroup().broadcastTransaction(tx); ListenableFuture<Transaction> broadcastComplete = walletAppKit.peerGroup().broadcastTransaction(tx);
Futures.addCallback(broadcastComplete, callback); Futures.addCallback(broadcastComplete, callback);
@ -1014,7 +1049,7 @@ public class WalletService {
ECKey arbitratorKey = ECKey.fromPublicOnly(arbitratorPubKey); ECKey arbitratorKey = ECKey.fromPublicOnly(arbitratorPubKey);
List<ECKey> keys = ImmutableList.of(offererKey, takerKey, arbitratorKey); List<ECKey> keys = ImmutableList.of(offererKey, takerKey, arbitratorKey);
return ScriptBuilder.createMultiSigOutputScript(2, keys); return ScriptBuilder.createP2SHOutputScript(2, keys);
} }
private Transaction createPayoutTx(Transaction depositTx, Coin offererPaybackAmount, Coin takerPaybackAmount, private Transaction createPayoutTx(Transaction depositTx, Coin offererPaybackAmount, Coin takerPaybackAmount,