Skip to content

Commit

Permalink
Script, ScriptBuilder: don't use current time as a default for creati…
Browse files Browse the repository at this point in the history
…onTime

Rather, leave it `null`.
  • Loading branch information
schildbach committed Dec 3, 2024
1 parent f70182c commit 2589c0d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
14 changes: 6 additions & 8 deletions core/src/main/java/org/bitcoinj/script/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public enum VerifyFlag {
* @return script that wraps the chunks
*/
public static Script of(List<ScriptChunk> chunks) {
return of(chunks, TimeUtils.currentTime());
return of(chunks, null);
}

/**
Expand All @@ -263,7 +263,7 @@ public static Script of(List<ScriptChunk> 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);
}

/**
Expand Down Expand Up @@ -343,30 +343,28 @@ private static void parseIntoChunksPartial(byte[] program, List<ScriptChunk> chu
*/
@Deprecated
public Script(byte[] program) {
this(program, TimeUtils.currentTime());
this(program, null);
}

/**
* @deprecated Use {@link Script#parse(byte[], Instant)}
*/
@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<ScriptChunk> chunks, Instant creationTime) {
private Script(List<ScriptChunk> 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;
Expand Down
11 changes: 6 additions & 5 deletions core/src/main/java/org/bitcoinj/script/ScriptBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class ScriptBuilder {
* their addresses.
*/
@Nullable
private Instant creationTime = TimeUtils.currentTime();
private Instant creationTime = null;

/** Creates a fresh ScriptBuilder with an empty program. */
public ScriptBuilder() {
Expand All @@ -82,8 +82,6 @@ public ScriptBuilder(Script template) {
/**
* Associates this script to be built with a given creation time. This is currently used in the context of
* watching wallets only, where the scriptPubKeys being watched actually represent public keys and their addresses.
* <p>
* If this is not set, the current time is used by the builder.
*
* @param creationTime creation time to associate the script with
* @return this builder
Expand Down Expand Up @@ -286,7 +284,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. */
Expand All @@ -306,7 +307,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
Expand Down

0 comments on commit 2589c0d

Please sign in to comment.