diff --git a/core/src/main/java/org/bitcoinj/core/SegwitAddress.java b/core/src/main/java/org/bitcoinj/core/SegwitAddress.java index 28257b8b3..cdacb3d86 100644 --- a/core/src/main/java/org/bitcoinj/core/SegwitAddress.java +++ b/core/src/main/java/org/bitcoinj/core/SegwitAddress.java @@ -24,6 +24,8 @@ import org.bitcoinj.params.Networks; import org.bitcoinj.script.Script; import org.bitcoinj.script.Script.ScriptType; +import static com.google.common.base.Preconditions.checkState; + /** *
* Implementation of native segwit addresses. They are composed of two parts: @@ -139,15 +141,13 @@ public class SegwitAddress extends Address { @Override public ScriptType getOutputScriptType() { int version = getWitnessVersion(); - if (version != 0) - return ScriptType.NO_TYPE; + checkState(version == 0); int programLength = getWitnessProgram().length; if (programLength == WITNESS_PROGRAM_LENGTH_PKH) return ScriptType.P2WPKH; - else if (programLength == WITNESS_PROGRAM_LENGTH_SH) + if (programLength == WITNESS_PROGRAM_LENGTH_SH) return ScriptType.P2WSH; - else - return ScriptType.NO_TYPE; + throw new IllegalStateException("Cannot happen."); } /** diff --git a/core/src/main/java/org/bitcoinj/script/Script.java b/core/src/main/java/org/bitcoinj/script/Script.java index c64a4f314..4d1e9511a 100644 --- a/core/src/main/java/org/bitcoinj/script/Script.java +++ b/core/src/main/java/org/bitcoinj/script/Script.java @@ -56,7 +56,6 @@ public class Script { /** Enumeration to encapsulate the type of this script. */ public enum ScriptType { - NO_TYPE(0), P2PKH(1), // pay to pubkey hash (aka pay to address) P2PK(2), // pay to pubkey P2SH(3), // pay to script hash @@ -1606,18 +1605,16 @@ public class Script { /** * Get the {@link org.bitcoinj.script.Script.ScriptType}. - * @return The script type. + * @return The script type, or null if the script is of unknown type */ - public ScriptType getScriptType() { - ScriptType type = ScriptType.NO_TYPE; - if (ScriptPattern.isPayToPubKeyHash(this)) { - type = ScriptType.P2PKH; - } else if (ScriptPattern.isPayToPubKey(this)) { - type = ScriptType.P2PK; - } else if (ScriptPattern.isPayToScriptHash(this)) { - type = ScriptType.P2SH; - } - return type; + public @Nullable ScriptType getScriptType() { + if (ScriptPattern.isPayToPubKeyHash(this)) + return ScriptType.P2PKH; + if (ScriptPattern.isPayToPubKey(this)) + return ScriptType.P2PK; + if (ScriptPattern.isPayToScriptHash(this)) + return ScriptType.P2SH; + return null; } @Override diff --git a/core/src/main/java/org/bitcoinj/store/DatabaseFullPrunedBlockStore.java b/core/src/main/java/org/bitcoinj/store/DatabaseFullPrunedBlockStore.java index 025b516c5..e26fa4132 100644 --- a/core/src/main/java/org/bitcoinj/store/DatabaseFullPrunedBlockStore.java +++ b/core/src/main/java/org/bitcoinj/store/DatabaseFullPrunedBlockStore.java @@ -21,6 +21,7 @@ package org.bitcoinj.store; import com.google.common.collect.Lists; import org.bitcoinj.core.*; import org.bitcoinj.script.Script; +import org.bitcoinj.script.Script.ScriptType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -959,7 +960,8 @@ public abstract class DatabaseFullPrunedBlockStore implements FullPrunedBlockSto s.setLong(4, out.getValue().value); s.setBytes(5, out.getScript().getProgram()); s.setString(6, out.getAddress()); - s.setInt(7, out.getScript().getScriptType().id); + ScriptType scriptType = out.getScript().getScriptType(); + s.setInt(7, scriptType != null ? scriptType.id : 0); s.setBoolean(8, out.isCoinbase()); s.executeUpdate(); s.close();