diff --git a/src/main/java/com/sparrowwallet/drongo/bip47/PaymentCode.java b/src/main/java/com/sparrowwallet/drongo/bip47/PaymentCode.java index ecdf87e..d997809 100644 --- a/src/main/java/com/sparrowwallet/drongo/bip47/PaymentCode.java +++ b/src/main/java/com/sparrowwallet/drongo/bip47/PaymentCode.java @@ -38,6 +38,9 @@ public class PaymentCode { private final byte[] pubkey; private final byte[] chain; + public static final List SEGWIT_SCRIPT_TYPES = List.of(ScriptType.P2PKH, ScriptType.P2SH_P2WPKH, ScriptType.P2WPKH); + public static final List V1_SCRIPT_TYPES = List.of(ScriptType.P2PKH); + private PaymentCode(String strPaymentCode, byte[] pubkey, byte[] chain) { this.strPaymentCode = strPaymentCode; this.pubkey = pubkey; diff --git a/src/main/java/com/sparrowwallet/drongo/protocol/Bech32.java b/src/main/java/com/sparrowwallet/drongo/protocol/Bech32.java index 7ed7f16..b625fec 100644 --- a/src/main/java/com/sparrowwallet/drongo/protocol/Bech32.java +++ b/src/main/java/com/sparrowwallet/drongo/protocol/Bech32.java @@ -150,10 +150,14 @@ public class Bech32 { /** Decode a Bech32 string. */ public static Bech32Data decode(final String str) { + return decode(str, 90); + } + + public static Bech32Data decode(final String str, int limit) { boolean lower = false, upper = false; if (str.length() < 8) throw new ProtocolException("Input too short: " + str.length()); - if (str.length() > 90) + if (str.length() > limit) throw new ProtocolException("Input too long: " + str.length()); for (int i = 0; i < str.length(); ++i) { char c = str.charAt(i); diff --git a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java index fe48d3e..1aa0129 100644 --- a/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java +++ b/src/main/java/com/sparrowwallet/drongo/wallet/Wallet.java @@ -185,7 +185,7 @@ public class Wallet extends Persistable implements Comparable { throw new IllegalStateException("Cannot add payment code wallet to " + policyType.getName() + " wallet"); } - if(scriptType != P2PKH && scriptType != P2SH_P2WPKH && scriptType != P2WPKH) { + if(!PaymentCode.SEGWIT_SCRIPT_TYPES.contains(scriptType)) { throw new IllegalStateException("Cannot add payment code wallet to " + scriptType.getName() + " wallet"); } @@ -246,7 +246,7 @@ public class Wallet extends Persistable implements Comparable { public boolean hasPaymentCode() { return getKeystores().size() == 1 && getKeystores().get(0).getBip47ExtendedPrivateKey() != null && policyType == PolicyType.SINGLE - && (scriptType == P2PKH || scriptType == P2SH_P2WPKH || scriptType == P2WPKH); + && PaymentCode.SEGWIT_SCRIPT_TYPES.contains(scriptType); } public PaymentCode getPaymentCode() {