Search in sources :

Example 96 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeUtilsTest method testIsValidPegInTx_hasChangeUtxoFromErpFederation_beforeRskip201_isPegin.

@Test
public void testIsValidPegInTx_hasChangeUtxoFromErpFederation_beforeRskip201_isPegin() {
    Context btcContext = new Context(networkParameters);
    when(activations.isActive(ConsensusRule.RSKIP201)).thenReturn(false);
    Federation activeFederation = bridgeConstantsRegtest.getGenesisFederation();
    List<BtcECKey> erpFederationKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")));
    erpFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation erpFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(erpFederationKeys), Instant.ofEpochMilli(1000L), 0L, networkParameters);
    Script erpRedeemScript = ErpFederationRedeemScriptParser.createErpRedeemScript(activeFederation.getRedeemScript(), erpFederation.getRedeemScript(), 500L);
    // Create a tx from the erp fed to the active fed
    BtcTransaction tx = new BtcTransaction(networkParameters);
    tx.addOutput(Coin.COIN, activeFederation.getAddress());
    tx.addInput(Sha256Hash.ZERO_HASH, 0, erpRedeemScript);
    Assert.assertTrue(BridgeUtils.isValidPegInTx(tx, activeFederation, btcContext, bridgeConstantsRegtest, activations));
}
Also used : Context(co.rsk.bitcoinj.core.Context) Script(co.rsk.bitcoinj.script.Script) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 97 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeUtilsTest method scriptCorrectlySpends_invalidScript.

@Test
public void scriptCorrectlySpends_invalidScript() {
    Federation genesisFederation = bridgeConstantsRegtest.getGenesisFederation();
    Address destinationAddress = PegTestUtils.createRandomBtcAddress();
    BtcTransaction tx = new BtcTransaction(networkParameters);
    tx.addOutput(Coin.COIN, destinationAddress);
    TransactionInput txIn = new TransactionInput(networkParameters, tx, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    tx.addInput(txIn);
    signWithNecessaryKeys(genesisFederation, BridgeRegTestConstants.REGTEST_FEDERATION_PRIVATE_KEYS, txIn, tx);
    // Add script op codes to the tx input script sig to make it invalid
    ScriptBuilder scriptBuilder = new ScriptBuilder(tx.getInput(0).getScriptSig());
    Script invalidScript = scriptBuilder.op(ScriptOpCodes.OP_IF).op(ScriptOpCodes.OP_ENDIF).build();
    tx.getInput(0).setScriptSig(invalidScript);
    assertFalse(BridgeUtils.scriptCorrectlySpendsTx(tx, 0, genesisFederation.getP2SHScript()));
}
Also used : Script(co.rsk.bitcoinj.script.Script) Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) ScriptBuilder(co.rsk.bitcoinj.script.ScriptBuilder) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 98 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeUtilsTest method createPegOutTx.

private BtcTransaction createPegOutTx(List<byte[]> signatures, int inputsToAdd, Federation federation, boolean isFastBridge) {
    // Setup
    Address address;
    byte[] program;
    if (federation == null) {
        federation = BridgeRegTestConstants.getInstance().getGenesisFederation();
    }
    if (isFastBridge) {
        // Create fast bridge redeem script
        Sha256Hash derivationArgumentsHash = Sha256Hash.of(new byte[] { 1 });
        Script fastBridgeRedeemScript;
        if (federation instanceof ErpFederation) {
            fastBridgeRedeemScript = FastBridgeErpRedeemScriptParser.createFastBridgeErpRedeemScript(federation.getRedeemScript(), derivationArgumentsHash);
        } else {
            fastBridgeRedeemScript = FastBridgeRedeemScriptParser.createMultiSigFastBridgeRedeemScript(federation.getRedeemScript(), derivationArgumentsHash);
        }
        Script fastBridgeP2SH = ScriptBuilder.createP2SHOutputScript(fastBridgeRedeemScript);
        address = Address.fromP2SHHash(networkParameters, fastBridgeP2SH.getPubKeyHash());
        program = fastBridgeRedeemScript.getProgram();
    } else {
        address = federation.getAddress();
        program = federation.getRedeemScript().getProgram();
    }
    // Build prev btc tx
    BtcTransaction prevTx = new BtcTransaction(networkParameters);
    TransactionOutput prevOut = new TransactionOutput(networkParameters, prevTx, Coin.FIFTY_COINS, address);
    prevTx.addOutput(prevOut);
    // Build btc tx to be signed
    BtcTransaction btcTx = new BtcTransaction(networkParameters);
    // Add inputs
    for (int i = 0; i < inputsToAdd; i++) {
        btcTx.addInput(prevOut);
    }
    Script scriptSig;
    if (signatures.isEmpty()) {
        scriptSig = PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation);
    } else {
        scriptSig = ScriptBuilder.createMultiSigInputScriptBytes(signatures, program);
    }
    // Sign inputs
    for (int i = 0; i < inputsToAdd; i++) {
        btcTx.getInput(i).setScriptSig(scriptSig);
    }
    TransactionOutput output = new TransactionOutput(networkParameters, btcTx, Coin.COIN, new BtcECKey().toAddress(networkParameters));
    btcTx.addOutput(output);
    TransactionOutput changeOutput = new TransactionOutput(networkParameters, btcTx, Coin.COIN, federation.getAddress());
    btcTx.addOutput(changeOutput);
    return btcTx;
}
Also used : Script(co.rsk.bitcoinj.script.Script) TransactionOutput(co.rsk.bitcoinj.core.TransactionOutput) Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint)

Example 99 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeUtilsTest method testIsValidPegInTxForTwoFederations.

@Test
public void testIsValidPegInTxForTwoFederations() {
    Context btcContext = new Context(networkParameters);
    when(activations.isActive(any(ConsensusRule.class))).thenReturn(false);
    List<BtcECKey> federation1Keys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")));
    federation1Keys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation federation1 = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, networkParameters);
    List<BtcECKey> federation2Keys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03")));
    federation2Keys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation federation2 = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(federation2Keys), Instant.ofEpochMilli(2000L), 0L, networkParameters);
    Address address1 = federation1.getAddress();
    Address address2 = federation2.getAddress();
    List<Federation> federations = Arrays.asList(federation1, federation2);
    // Tx sending less than 1 btc to the first federation, not a peg-in tx
    BtcTransaction tx = new BtcTransaction(networkParameters);
    tx.addOutput(Coin.CENT, address1);
    tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertFalse(BridgeUtils.isValidPegInTx(tx, federations, null, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending less than 1 btc to the second federation, not a peg-in tx
    tx = new BtcTransaction(networkParameters);
    tx.addOutput(Coin.CENT, address2);
    tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertFalse(BridgeUtils.isValidPegInTx(tx, federations, null, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending less than 1 btc to both federations, not a peg-in tx
    tx = new BtcTransaction(networkParameters);
    tx.addOutput(Coin.CENT, address1);
    tx.addOutput(Coin.CENT, address2);
    tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertFalse(BridgeUtils.isValidPegInTx(tx, federations, null, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 1 btc to the first federation, but also spending from the first federation address, the typical peg-out tx, not a peg-in tx.
    BtcTransaction tx2 = new BtcTransaction(networkParameters);
    tx2.addOutput(Coin.COIN, address1);
    TransactionInput txIn = new TransactionInput(networkParameters, tx2, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    tx2.addInput(txIn);
    signWithNecessaryKeys(federation1, federation1Keys, txIn, tx2);
    assertFalse(BridgeUtils.isValidPegInTx(tx2, federations, null, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 1 btc to the second federation, but also spending from the second federation address,
    // the typical peg-out tx, not a peg-in tx.
    tx2 = new BtcTransaction(networkParameters);
    tx2.addOutput(Coin.COIN, address2);
    txIn = new TransactionInput(networkParameters, tx2, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    tx2.addInput(txIn);
    signWithNecessaryKeys(federation2, federation2Keys, txIn, tx2);
    assertFalse(BridgeUtils.isValidPegInTx(tx2, federations, null, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 1 btc to both federations, but also spending from the first federation address,
    // the typical peg-out tx, not a peg-in tx.
    tx2 = new BtcTransaction(networkParameters);
    tx2.addOutput(Coin.COIN, address1);
    tx2.addOutput(Coin.COIN, address2);
    txIn = new TransactionInput(networkParameters, tx2, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    tx2.addInput(txIn);
    signWithNecessaryKeys(federation1, federation1Keys, txIn, tx2);
    assertFalse(BridgeUtils.isValidPegInTx(tx2, federations, null, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 1 btc to both federations, but also spending from the second federation address,
    // the typical peg-out tx, not a peg-in tx.
    tx2 = new BtcTransaction(networkParameters);
    tx2.addOutput(Coin.COIN, address1);
    tx2.addOutput(Coin.COIN, address2);
    txIn = new TransactionInput(networkParameters, tx2, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    tx2.addInput(txIn);
    signWithNecessaryKeys(federation2, federation2Keys, txIn, tx2);
    assertFalse(BridgeUtils.isValidPegInTx(tx2, federations, null, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 1 btc from federation1 to federation2, the typical migration tx, not a peg-in tx.
    tx2 = new BtcTransaction(networkParameters);
    tx2.addOutput(Coin.COIN, address2);
    txIn = new TransactionInput(networkParameters, tx2, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    tx2.addInput(txIn);
    signWithNecessaryKeys(federation1, federation1Keys, txIn, tx2);
    assertFalse(BridgeUtils.isValidPegInTx(tx2, federations, null, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 1 btc from federation1 to federation2, the typical migration tx from the retired federation,
    // not a peg-in tx.
    tx2 = new BtcTransaction(networkParameters);
    tx2.addOutput(Coin.COIN, address2);
    txIn = new TransactionInput(networkParameters, tx2, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    tx2.addInput(txIn);
    signWithNecessaryKeys(federation1, federation1Keys, txIn, tx2);
    assertFalse(BridgeUtils.isValidPegInTx(tx2, Collections.singletonList(federation2), federation1.getP2SHScript(), btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 1 btc to the first federation, is a peg-in tx
    BtcTransaction tx3 = new BtcTransaction(networkParameters);
    tx3.addOutput(Coin.COIN, address1);
    tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isValidPegInTx(tx3, federations, null, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 1 btc to the second federation, is a peg-in tx
    tx3 = new BtcTransaction(networkParameters);
    tx3.addOutput(Coin.COIN, address2);
    tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isValidPegInTx(tx3, federations, null, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 1 btc to the both federations, is a peg-in tx
    tx3 = new BtcTransaction(networkParameters);
    tx3.addOutput(Coin.COIN, address1);
    tx3.addOutput(Coin.COIN, address2);
    tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isValidPegInTx(tx3, federations, null, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 50 btc to the first federation, is a peg-in tx
    BtcTransaction tx4 = new BtcTransaction(networkParameters);
    tx4.addOutput(Coin.FIFTY_COINS, address1);
    tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isValidPegInTx(tx4, federations, null, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 50 btc to the second federation, is a peg-in tx
    tx4 = new BtcTransaction(networkParameters);
    tx4.addOutput(Coin.FIFTY_COINS, address2);
    tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isValidPegInTx(tx4, federations, null, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 50 btc to the both federations, is a peg-in tx
    tx4 = new BtcTransaction(networkParameters);
    tx4.addOutput(Coin.FIFTY_COINS, address1);
    tx4.addOutput(Coin.FIFTY_COINS, address2);
    tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isValidPegInTx(tx4, federations, null, btcContext, bridgeConstantsRegtest, activations));
}
Also used : Context(co.rsk.bitcoinj.core.Context) Script(co.rsk.bitcoinj.script.Script) Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 100 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeUtilsTest method testIsValidPegInTx_less_than_minimum_not_pegin_after_iris.

@Test
public void testIsValidPegInTx_less_than_minimum_not_pegin_after_iris() {
    // Tx sending less than the minimum allowed, not a peg-in tx
    Context btcContext = new Context(networkParameters);
    Federation federation = this.getGenesisFederationForTest(bridgeConstantsRegtest, btcContext);
    when(activations.isActive(ConsensusRule.RSKIP219)).thenReturn(true);
    Coin minimumPegInValueAfterIris = bridgeConstantsRegtest.getMinimumPeginTxValueInSatoshis();
    // Tx sending less than the minimum allowed, not a peg-in tx
    BtcTransaction tx = new BtcTransaction(networkParameters);
    tx.addOutput(minimumPegInValueAfterIris.subtract(Coin.CENT), federation.getAddress());
    tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertFalse(BridgeUtils.isValidPegInTx(tx, federation, btcContext, bridgeConstantsRegtest, activations));
}
Also used : Context(co.rsk.bitcoinj.core.Context) Coin(co.rsk.bitcoinj.core.Coin) Script(co.rsk.bitcoinj.script.Script) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Aggregations

Script (co.rsk.bitcoinj.script.Script)123 Test (org.junit.Test)91 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)60 RskAddress (co.rsk.core.RskAddress)47 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)40 Repository (org.ethereum.core.Repository)34 MutableRepository (org.ethereum.db.MutableRepository)33 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)29 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)26 Block (org.ethereum.core.Block)24 Context (co.rsk.bitcoinj.core.Context)21 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)20 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)20 Address (co.rsk.bitcoinj.core.Address)17 TransactionOutPoint (co.rsk.bitcoinj.core.TransactionOutPoint)17 PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation)17 PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation)17 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)17 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)17 InternalTransaction (org.ethereum.vm.program.InternalTransaction)17