From 9d15c27bfd8bd6c51f03fe081d6e70718ecf2eb0 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Mon, 6 Apr 2020 14:53:16 +0200 Subject: [PATCH] relative time locking improvements --- .../drongo/protocol/Transaction.java | 4 ++++ .../drongo/protocol/TransactionInput.java | 16 +++++++++++----- .../drongo/protocol/TransactionPart.java | 4 ++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java b/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java index d999d86..4ebebc8 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/Transaction.java @@ -69,6 +69,10 @@ public class Transaction extends TransactionPart { return false; } + public boolean isRelativeLocktimeAllowed() { + return version >= 2L; + } + public boolean isReplaceByFee() { if(locktime == 0) return false; diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionInput.java b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionInput.java index 9c3f88a..5f0ace3 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionInput.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionInput.java @@ -8,8 +8,10 @@ import java.io.OutputStream; public class TransactionInput extends TransactionPart { public static final long SEQUENCE_LOCKTIME_DISABLED = 4294967295L; public static final long SEQUENCE_RBF_ENABLED = 4294967293L; - public static final long MAX_RELATIVE_TIMELOCK = 0x40FFFF; - public static final long MAX_RELATIVE_TIMELOCK_IN_BLOCKS = 0xFFFF; + public static final long MAX_RELATIVE_TIMELOCK = 2147483647L; + public static final long RELATIVE_TIMELOCK_VALUE_MASK = 0xFFFF; + public static final long RELATIVE_TIMELOCK_TYPE_FLAG = 0x400000; + public static final int RELATIVE_TIMELOCK_SECONDS_INCREMENT = 512; // Allows for altering transactions after they were broadcast. Values below NO_SEQUENCE-1 mean it can be altered. private long sequence; @@ -113,15 +115,19 @@ public class TransactionInput extends TransactionPart { } public boolean isRelativeTimeLocked() { - return sequence <= MAX_RELATIVE_TIMELOCK; + return getTransaction().isRelativeLocktimeAllowed() && sequence <= MAX_RELATIVE_TIMELOCK; } public boolean isRelativeTimeLockedInBlocks() { - return sequence <= MAX_RELATIVE_TIMELOCK_IN_BLOCKS; + return isRelativeTimeLocked() && ((sequence & RELATIVE_TIMELOCK_TYPE_FLAG) == 0); } public long getRelativeLocktime() { - return sequence & MAX_RELATIVE_TIMELOCK_IN_BLOCKS; + return sequence & RELATIVE_TIMELOCK_VALUE_MASK; + } + + public Transaction getTransaction() { + return (Transaction)getParent(); } protected void bitcoinSerializeToStream(OutputStream stream) throws IOException { diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionPart.java b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionPart.java index ad09c2a..73d011a 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/TransactionPart.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/TransactionPart.java @@ -34,6 +34,10 @@ public abstract class TransactionPart { protected abstract void parse() throws ProtocolException; + public TransactionPart getParent() { + return parent; + } + public final void setParent(TransactionPart parent) { this.parent = parent; }