From e4e4e45a475b81ef624140c01c3f8ca6fa94d2f6 Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Sat, 12 Jan 2013 14:47:43 +0100 Subject: [PATCH] Allow re-signing of transactions and re-sign after adjusting the sequence number. --- core/src/main/java/com/google/bitcoin/core/Transaction.java | 4 ++-- tools/src/main/java/com/google/bitcoin/tools/WalletTool.java | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/Transaction.java b/core/src/main/java/com/google/bitcoin/core/Transaction.java index eaacd5b0..58c57180 100644 --- a/core/src/main/java/com/google/bitcoin/core/Transaction.java +++ b/core/src/main/java/com/google/bitcoin/core/Transaction.java @@ -709,7 +709,8 @@ public class Transaction extends ChildMessage implements Serializable { ECKey[] signingKeys = new ECKey[inputs.size()]; for (int i = 0; i < inputs.size(); i++) { TransactionInput input = inputs.get(i); - Preconditions.checkState(input.getScriptBytes().length == 0, "Attempting to sign a non-fresh transaction"); + if (input.getScriptBytes().length != 0) + log.warn("Re-signing an already signed transaction! Be sure this is what you want."); // Find the signing key we'll need to use. ECKey key = input.getOutpoint().getConnectedKey(wallet); // This assert should never fire. If it does, it means the wallet is inconsistent. @@ -743,7 +744,6 @@ public class Transaction extends ChildMessage implements Serializable { // 2) For pay-to-key outputs: just a signature. for (int i = 0; i < inputs.size(); i++) { TransactionInput input = inputs.get(i); - Preconditions.checkState(input.getScriptBytes().length == 0); ECKey key = signingKeys[i]; Script scriptPubKey = input.getOutpoint().getConnectedOutput().getScriptPubKey(); if (scriptPubKey.isSentToAddress()) { diff --git a/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java b/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java index 94e59dc9..d9d67329 100644 --- a/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java +++ b/tools/src/main/java/com/google/bitcoin/tools/WalletTool.java @@ -424,10 +424,14 @@ public class WalletTool { t.setLockTime(Transaction.parseLockTimeStr(lockTimeStr)); // For lock times to take effect, at least one output must have a non-final sequence number. t.getInputs().get(0).setSequenceNumber(0); + // And because we modified the transaction after it was completed, we must re-sign the inputs. + t.signInputs(Transaction.SigHash.ALL, wallet); } } catch (ParseException e) { System.err.println("Could not understand --locktime of " + lockTimeStr); return; + } catch (ScriptException e) { + throw new RuntimeException(e); } t = req.tx; // Not strictly required today. setup();