diff --git a/src/main/java/co/rsk/bitcoinj/script/ErpFederationRedeemScriptParser.java b/src/main/java/co/rsk/bitcoinj/script/ErpFederationRedeemScriptParser.java index 538278106..cb40a005a 100644 --- a/src/main/java/co/rsk/bitcoinj/script/ErpFederationRedeemScriptParser.java +++ b/src/main/java/co/rsk/bitcoinj/script/ErpFederationRedeemScriptParser.java @@ -15,14 +15,10 @@ public class ErpFederationRedeemScriptParser extends StandardRedeemScriptParser public static long MAX_CSV_VALUE = 65_535L; // 2^16 - 1, since bitcoin will interpret up to 16 bits as the CSV value public ErpFederationRedeemScriptParser( - ScriptType scriptType, - List redeemScriptChunks, - List rawChunks + List redeemScriptChunks ) { super( - scriptType, - extractStandardRedeemScript(redeemScriptChunks).getChunks(), - rawChunks + extractStandardRedeemScript(redeemScriptChunks).getChunks() ); this.multiSigType = MultiSigType.ERP_FED; } diff --git a/src/main/java/co/rsk/bitcoinj/script/FastBridgeErpRedeemScriptParser.java b/src/main/java/co/rsk/bitcoinj/script/FastBridgeErpRedeemScriptParser.java index e88ccef58..4b99cdf8d 100644 --- a/src/main/java/co/rsk/bitcoinj/script/FastBridgeErpRedeemScriptParser.java +++ b/src/main/java/co/rsk/bitcoinj/script/FastBridgeErpRedeemScriptParser.java @@ -10,14 +10,10 @@ public class FastBridgeErpRedeemScriptParser extends StandardRedeemScriptParser private static final Logger logger = LoggerFactory.getLogger(FastBridgeErpRedeemScriptParser.class); public FastBridgeErpRedeemScriptParser( - ScriptType scriptType, - List redeemScriptChunks, - List rawChunks + List redeemScriptChunks ) { super( - scriptType, - extractStandardRedeemScript(redeemScriptChunks).getChunks(), - rawChunks + extractStandardRedeemScript(redeemScriptChunks).getChunks() ); this.multiSigType = MultiSigType.FAST_BRIDGE_ERP_FED; } diff --git a/src/main/java/co/rsk/bitcoinj/script/FastBridgeP2shErpRedeemScriptParser.java b/src/main/java/co/rsk/bitcoinj/script/FastBridgeP2shErpRedeemScriptParser.java index a4b6430ee..d384b3257 100644 --- a/src/main/java/co/rsk/bitcoinj/script/FastBridgeP2shErpRedeemScriptParser.java +++ b/src/main/java/co/rsk/bitcoinj/script/FastBridgeP2shErpRedeemScriptParser.java @@ -11,14 +11,10 @@ public class FastBridgeP2shErpRedeemScriptParser extends StandardRedeemScriptPar private static final Logger logger = LoggerFactory.getLogger(FastBridgeP2shErpRedeemScriptParser.class); public FastBridgeP2shErpRedeemScriptParser( - ScriptType scriptType, - List redeemScriptChunks, - List rawChunks + List redeemScriptChunks ) { super( - scriptType, - extractStandardRedeemScript(redeemScriptChunks).getChunks(), - rawChunks + extractStandardRedeemScript(redeemScriptChunks).getChunks() ); this.multiSigType = MultiSigType.FAST_BRIDGE_P2SH_ERP_FED; } diff --git a/src/main/java/co/rsk/bitcoinj/script/FastBridgeRedeemScriptParser.java b/src/main/java/co/rsk/bitcoinj/script/FastBridgeRedeemScriptParser.java index 763e92cbb..36fba8ef4 100644 --- a/src/main/java/co/rsk/bitcoinj/script/FastBridgeRedeemScriptParser.java +++ b/src/main/java/co/rsk/bitcoinj/script/FastBridgeRedeemScriptParser.java @@ -12,14 +12,10 @@ public class FastBridgeRedeemScriptParser extends StandardRedeemScriptParser { protected final byte[] derivationHash; public FastBridgeRedeemScriptParser( - ScriptType scriptType, - List redeemScriptChunks, - List rawChunks + List redeemScriptChunks ) { super( - scriptType, - redeemScriptChunks.subList(2, redeemScriptChunks.size()), - rawChunks + redeemScriptChunks.subList(2, redeemScriptChunks.size()) ); this.multiSigType = MultiSigType.FAST_BRIDGE_MULTISIG; this.derivationHash = redeemScriptChunks.get(0).data; diff --git a/src/main/java/co/rsk/bitcoinj/script/NoRedeemScriptParser.java b/src/main/java/co/rsk/bitcoinj/script/NoRedeemScriptParser.java index 381e24f86..b51e5af71 100644 --- a/src/main/java/co/rsk/bitcoinj/script/NoRedeemScriptParser.java +++ b/src/main/java/co/rsk/bitcoinj/script/NoRedeemScriptParser.java @@ -12,20 +12,11 @@ public MultiSigType getMultiSigType() { return MultiSigType.NO_MULTISIG_TYPE; } - @Override - public ScriptType getScriptType() { - return ScriptType.UNDEFINED; - } - @Override public int getM() { return -1; } - @Override - public int getSigInsertionIndex(Sha256Hash hash, BtcECKey signingKey) { - return 0; - } @Override public int findKeyInRedeem(BtcECKey key) { diff --git a/src/main/java/co/rsk/bitcoinj/script/P2shErpFederationRedeemScriptParser.java b/src/main/java/co/rsk/bitcoinj/script/P2shErpFederationRedeemScriptParser.java index 2ab47dcca..d9304ea6d 100644 --- a/src/main/java/co/rsk/bitcoinj/script/P2shErpFederationRedeemScriptParser.java +++ b/src/main/java/co/rsk/bitcoinj/script/P2shErpFederationRedeemScriptParser.java @@ -14,14 +14,10 @@ public class P2shErpFederationRedeemScriptParser extends StandardRedeemScriptPar public static long MAX_CSV_VALUE = 65_535L; // 2^16 - 1, since bitcoin will interpret up to 16 bits as the CSV value public P2shErpFederationRedeemScriptParser( - ScriptType scriptType, - List redeemScriptChunks, - List rawChunks + List redeemScriptChunks ) { super( - scriptType, - extractStandardRedeemScript(redeemScriptChunks).getChunks(), - rawChunks + extractStandardRedeemScript(redeemScriptChunks).getChunks() ); this.multiSigType = MultiSigType.P2SH_ERP_FED; } diff --git a/src/main/java/co/rsk/bitcoinj/script/RedeemScriptParser.java b/src/main/java/co/rsk/bitcoinj/script/RedeemScriptParser.java index 4a9457b98..69cd1d0f1 100644 --- a/src/main/java/co/rsk/bitcoinj/script/RedeemScriptParser.java +++ b/src/main/java/co/rsk/bitcoinj/script/RedeemScriptParser.java @@ -16,20 +16,12 @@ enum MultiSigType { FAST_BRIDGE_P2SH_ERP_FED } - enum ScriptType { - P2SH, - REDEEM_SCRIPT, - UNDEFINED - } MultiSigType getMultiSigType(); - ScriptType getScriptType(); int getM(); - int getSigInsertionIndex(Sha256Hash hash, BtcECKey signingKey); - int findKeyInRedeem(BtcECKey key); List getPubKeys(); diff --git a/src/main/java/co/rsk/bitcoinj/script/RedeemScriptParserFactory.java b/src/main/java/co/rsk/bitcoinj/script/RedeemScriptParserFactory.java index 7e5f29beb..16774246c 100644 --- a/src/main/java/co/rsk/bitcoinj/script/RedeemScriptParserFactory.java +++ b/src/main/java/co/rsk/bitcoinj/script/RedeemScriptParserFactory.java @@ -1,8 +1,6 @@ package co.rsk.bitcoinj.script; -import co.rsk.bitcoinj.core.ScriptException; import co.rsk.bitcoinj.core.Utils; -import co.rsk.bitcoinj.script.RedeemScriptParser.ScriptType; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,57 +25,39 @@ public static RedeemScriptParser get(List chunks) { return new NoRedeemScriptParser(); } - ParseResult result = extractRedeemScriptFromChunks(chunks); - - if (result == null) { - return new NoRedeemScriptParser(); - } - - if (FastBridgeRedeemScriptParser.isFastBridgeMultiSig(result.internalScript)) { + if (FastBridgeRedeemScriptParser.isFastBridgeMultiSig(chunks)) { logger.debug("[get] Return FastBridgeRedeemScriptParser"); return new FastBridgeRedeemScriptParser( - result.scriptType, - result.internalScript, chunks ); } - if (StandardRedeemScriptParser.isStandardMultiSig(result.internalScript)) { + if (StandardRedeemScriptParser.isStandardMultiSig(chunks)) { logger.debug("[get] Return StandardRedeemScriptParser"); return new StandardRedeemScriptParser( - result.scriptType, - result.internalScript, chunks ); } - if (P2shErpFederationRedeemScriptParser.isP2shErpFed(result.internalScript)) { + if (P2shErpFederationRedeemScriptParser.isP2shErpFed(chunks)) { logger.debug("[get] Return P2shErpFederationRedeemScriptParser"); return new P2shErpFederationRedeemScriptParser( - result.scriptType, - result.internalScript, chunks ); } - if (FastBridgeP2shErpRedeemScriptParser.isFastBridgeP2shErpFed(result.internalScript)) { + if (FastBridgeP2shErpRedeemScriptParser.isFastBridgeP2shErpFed(chunks)) { logger.debug("[get] Return FastBridgeP2shErpRedeemScriptParser"); return new FastBridgeP2shErpRedeemScriptParser( - result.scriptType, - result.internalScript, chunks ); } - if (ErpFederationRedeemScriptParser.isErpFed(result.internalScript)) { + if (ErpFederationRedeemScriptParser.isErpFed(chunks)) { logger.debug("[get] Return ErpFederationRedeemScriptParser"); return new ErpFederationRedeemScriptParser( - result.scriptType, - result.internalScript, chunks ); } - if (FastBridgeErpRedeemScriptParser.isFastBridgeErpFed(result.internalScript)) { + if (FastBridgeErpRedeemScriptParser.isFastBridgeErpFed(chunks)) { logger.debug("[get] Return FastBridgeErpRedeemScriptParser"); return new FastBridgeErpRedeemScriptParser( - result.scriptType, - result.internalScript, chunks ); } @@ -85,40 +65,4 @@ public static RedeemScriptParser get(List chunks) { logger.debug("[get] Return NoRedeemScriptParser"); return new NoRedeemScriptParser(); } - - private static ParseResult extractRedeemScriptFromChunks(List chunks) { - ScriptChunk lastChunk = chunks.get(chunks.size() - 1); - if (RedeemScriptValidator.isRedeemLikeScript(chunks)) { - return new ParseResult(chunks, ScriptType.REDEEM_SCRIPT); - } - if (lastChunk.data != null && lastChunk.data.length > 0) { - int lastByte = lastChunk.data[lastChunk.data.length - 1] & 0xff; - // ERP and standard (+fastBridge) finish with OP_CHECKMULTISIG and P2SHERP (+fastBridge) finish with OP_ENDIF - if ( - lastByte == ScriptOpCodes.OP_CHECKMULTISIG || - lastByte == ScriptOpCodes.OP_CHECKMULTISIGVERIFY || - lastByte == ScriptOpCodes.OP_ENDIF - ) { - ScriptParserResult result = ScriptParser.parseScriptProgram(lastChunk.data); - if (result.getException().isPresent()) { - String message = String.format("Error trying to parse inner script. %s", result.getException().get()); - logger.debug("[extractRedeemScriptFromChunks] {}", message); - throw new ScriptException(message); - } - return new ParseResult(result.getChunks(), ScriptType.P2SH); - } - } - logger.debug("[extractRedeemScriptFromChunks] Could not get redeem script from given chunks"); - return null; - } - - private static class ParseResult { - public final List internalScript; - public final ScriptType scriptType; - - public ParseResult(List internalScript, ScriptType scriptType) { - this.internalScript = internalScript; - this.scriptType = scriptType; - } - } } diff --git a/src/main/java/co/rsk/bitcoinj/script/RedeemScriptUtils.java b/src/main/java/co/rsk/bitcoinj/script/RedeemScriptUtils.java new file mode 100644 index 000000000..76938371c --- /dev/null +++ b/src/main/java/co/rsk/bitcoinj/script/RedeemScriptUtils.java @@ -0,0 +1,39 @@ +package co.rsk.bitcoinj.script; + +import co.rsk.bitcoinj.core.ScriptException; + +import java.util.List; +import java.util.Optional; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class RedeemScriptUtils { + private static final Logger logger = LoggerFactory.getLogger(RedeemScriptUtils.class); + + private RedeemScriptUtils() { } + public static Optional