Skip to content

Commit

Permalink
- Update use of RedeemScriptParser interface to support new changes a…
Browse files Browse the repository at this point in the history
…fter refactoring.

- Update test to use FlyoverRedeemScriptBuilderImpl and get rid of use of the removed FastBridgeRedeemScriptParser
  • Loading branch information
nathanieliov authored and marcos-iov committed Oct 18, 2024
1 parent 4828324 commit 94725a8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import co.rsk.bitcoinj.script.RedeemScriptParser;
import co.rsk.bitcoinj.script.RedeemScriptParserFactory;
import co.rsk.bitcoinj.script.Script;
import co.rsk.bitcoinj.script.ScriptBuilder;
import co.rsk.bitcoinj.script.ScriptChunk;
import co.rsk.bitcoinj.wallet.RedeemData;
import co.rsk.peg.constants.BridgeConstants;
Expand Down Expand Up @@ -479,8 +480,9 @@ protected void removeSignaturesFromTransaction(BtcTransaction pegoutBtcTx, Feder
}

protected Script extractStandardRedeemScript(Script redeemScript) {
RedeemScriptParser parser = RedeemScriptParserFactory.get(redeemScript.getChunks());
return parser.extractStandardRedeemScript();
RedeemScriptParser redeemScriptParser = RedeemScriptParserFactory.get(redeemScript.getChunks());
List<ScriptChunk> defaultRedeemScriptChunks = redeemScriptParser.extractStandardRedeemScriptChunks();
return new ScriptBuilder().addChunks(defaultRedeemScriptChunks).build();
}

private Script extractDefaultRedeemScript(Federation federation) {
Expand All @@ -505,7 +507,6 @@ protected Federation getSpendingFederation(BtcTransaction btcTx) {
List<Federation> spendingFedFilter = observedFederations.stream()
.filter(f -> (extractDefaultRedeemScript(f)).equals(redeemScript)).collect(Collectors.toList());


return spendingFedFilter.get(0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
import co.rsk.bitcoinj.core.TransactionInput;
import co.rsk.bitcoinj.crypto.TransactionSignature;
import co.rsk.bitcoinj.params.RegTestParams;
import co.rsk.bitcoinj.script.FastBridgeRedeemScriptParser;
import co.rsk.bitcoinj.script.Script;
import co.rsk.bitcoinj.script.ScriptBuilder;
import co.rsk.bitcoinj.script.ScriptChunk;
import co.rsk.peg.bitcoin.FlyoverRedeemScriptBuilderImpl;
import co.rsk.peg.constants.BridgeConstants;
import co.rsk.crypto.Keccak256;
import co.rsk.federate.FederatorSupport;
Expand Down Expand Up @@ -820,13 +820,16 @@ void validateTxCanBeSigned_fast_bridge_ok() throws Exception {
Federation federation = TestUtils.createFederation(params, 1);

// Create fast bridge redeem script
Script fastBridgeRedeemScript = FastBridgeRedeemScriptParser.createMultiSigFastBridgeRedeemScript(
federation.getRedeemScript(),
Sha256Hash.wrap(TestUtils.createHash(1).getBytes())

Keccak256 flyoverDerivationHash = createHash(1);

Script flyoverRedeemScript = FlyoverRedeemScriptBuilderImpl.builder().of(
flyoverDerivationHash,
federation.getRedeemScript()
);

// Create a tx from the Fed to a random btc address
BtcTransaction releaseTx = createReleaseTxAndAddInput(federation, fastBridgeRedeemScript);
BtcTransaction releaseTx = createReleaseTxAndAddInput(federation, flyoverRedeemScript);

BtcECKey fed1Key = federation.getBtcPublicKeys().get(0);
ECPublicKey signerPublicKey = new ECPublicKey(fed1Key.getPubKey());
Expand Down Expand Up @@ -1020,13 +1023,14 @@ void getRedeemScriptFromInput_standard_redeem_script() {
@Test
void extractStandardRedeemScript_fast_bridge_redeem_script() {
Federation federation = TestUtils.createFederation(params, 1);
Script fastBridgeRedeemScript =
FastBridgeRedeemScriptParser.createMultiSigFastBridgeRedeemScript(
federation.getRedeemScript(),
Sha256Hash.of(TestUtils.createHash(1).getBytes())
);

test_extractStandardRedeemScript(federation.getRedeemScript(), fastBridgeRedeemScript);
Keccak256 flyoverDerivationHash = createHash(1);
Script flyoverRedeemScript = FlyoverRedeemScriptBuilderImpl.builder().of(
flyoverDerivationHash,
federation.getRedeemScript()
);

test_extractStandardRedeemScript(federation.getRedeemScript(), flyoverRedeemScript);
}

@Test
Expand Down Expand Up @@ -1112,7 +1116,7 @@ private void test_extractStandardRedeemScript(
);
}

private void test_getRedeemScriptFromInput(boolean isFastBridgeRedeemScript) {
private void test_getRedeemScriptFromInput(boolean isFlyoverRedeemScript) {
BtcReleaseClient client = createBtcClient();

BtcECKey ecKey1 = BtcECKey.fromPrivate(BigInteger.valueOf(100));
Expand All @@ -1124,10 +1128,11 @@ private void test_getRedeemScriptFromInput(boolean isFastBridgeRedeemScript) {
Script federationRedeemScript = federation.getRedeemScript();
Script inputScript;

if (isFastBridgeRedeemScript) {
federationRedeemScript = FastBridgeRedeemScriptParser.createMultiSigFastBridgeRedeemScript(
federationRedeemScript,
Sha256Hash.of(new byte[]{1})
if (isFlyoverRedeemScript) {
Keccak256 flyoverDerivationHash = createHash(1);
federationRedeemScript = FlyoverRedeemScriptBuilderImpl.builder().of(
flyoverDerivationHash,
federation.getRedeemScript()
);
}
inputScript = federation.getP2SHScript().createEmptyInputScript(
Expand Down

0 comments on commit 94725a8

Please sign in to comment.