From 1272823923db8335453134e0fbd7b8bde91f5985 Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Thu, 17 Oct 2024 23:18:50 +0200 Subject: [PATCH] WIP Script, ScriptBuilder: don't use current time as a default for creationTime --- core/src/main/java/org/bitcoinj/script/Script.java | 14 ++++++-------- .../java/org/bitcoinj/script/ScriptBuilder.java | 11 +++++++---- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/org/bitcoinj/script/Script.java b/core/src/main/java/org/bitcoinj/script/Script.java index 9407c4c9d03..7c5c3f68925 100644 --- a/core/src/main/java/org/bitcoinj/script/Script.java +++ b/core/src/main/java/org/bitcoinj/script/Script.java @@ -236,7 +236,7 @@ public enum VerifyFlag { * @return script that wraps the chunks */ public static Script of(List chunks) { - return of(chunks, TimeUtils.currentTime()); + return of(chunks, null); } /** @@ -259,7 +259,7 @@ public static Script of(List chunks, Instant creationTime) { * @throws ScriptException if the program could not be parsed */ public static Script parse(byte[] program) throws ScriptException { - return parse(program, TimeUtils.currentTime()); + return parse(program, null); } /** @@ -340,7 +340,7 @@ private static void parseIntoChunksPartial(byte[] program, List chu */ @Deprecated public Script(byte[] program) { - this(program, TimeUtils.currentTime()); + this(program, null); } /** @@ -348,22 +348,20 @@ public Script(byte[] program) { */ @Deprecated public Script(byte[] program, long creationTimeInSeconds) { - this(program, Instant.ofEpochSecond(creationTimeInSeconds)); + this(program, creationTimeInSeconds != 0 ? Instant.ofEpochSecond(creationTimeInSeconds) : null); } // When constructing from a program, we store both program and chunks - private Script(byte[] program, Instant creationTime) { + private Script(byte[] program, @Nullable Instant creationTime) { Objects.requireNonNull(program); - Objects.requireNonNull(creationTime); this.program = Arrays.copyOf(program, program.length); // defensive copy; this.chunks = parseIntoChunks(this.program); this.creationTime = creationTime; } // When constructing from chunks, we store only chunks, and generate program when getter is called - private Script(List chunks, Instant creationTime) { + private Script(List chunks, @Nullable Instant creationTime) { Objects.requireNonNull(chunks); - Objects.requireNonNull(creationTime); this.program = null; this.chunks = Collections.unmodifiableList(new ArrayList<>(chunks)); // defensive copy this.creationTime = creationTime; diff --git a/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java b/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java index 9322c22b607..9bfd9ae6dc3 100644 --- a/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java +++ b/core/src/main/java/org/bitcoinj/script/ScriptBuilder.java @@ -61,7 +61,7 @@ */ public class ScriptBuilder { private final List chunks; - private Instant creationTime = TimeUtils.currentTime(); + private Instant creationTime = null; /** Creates a fresh ScriptBuilder with an empty program. */ public ScriptBuilder() { @@ -74,7 +74,7 @@ public ScriptBuilder(Script template) { } /** - * Sets the creation time to build the script with. If this is not set, the current time is used by the builder. + * Sets the creation time to build the script with. * * @param creationTime creation time to build the script with * @return this builder @@ -277,7 +277,10 @@ public ScriptBuilder opFalse(int index) { /** Creates a new immutable Script based on the state of the builder. */ public Script build() { - return Script.of(chunks, creationTime); + if (creationTime != null) + return Script.of(chunks, creationTime); + else + return Script.of(chunks); } /** Creates an empty script. */ @@ -297,7 +300,7 @@ public static Script createOutputScript(Address to, Instant creationTime) { } /** - * Creates a scriptPubKey that encodes payment to the given address. The creation time will be the current time. + * Creates a scriptPubKey that encodes payment to the given address. * * @param to address to send payment to * @return scriptPubKey