Search in sources :

Example 21 with TransactionOutPoint

use of co.rsk.bitcoinj.core.TransactionOutPoint 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 22 with TransactionOutPoint

use of co.rsk.bitcoinj.core.TransactionOutPoint 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)

Aggregations

BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)22 TransactionInput (co.rsk.bitcoinj.core.TransactionInput)22 TransactionOutPoint (co.rsk.bitcoinj.core.TransactionOutPoint)22 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)22 Test (org.junit.Test)22 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)17 Script (co.rsk.bitcoinj.script.Script)14 Address (co.rsk.bitcoinj.core.Address)12 RskAddress (co.rsk.core.RskAddress)12 Context (co.rsk.bitcoinj.core.Context)11 Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)3 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)3 TransactionSignature (co.rsk.bitcoinj.crypto.TransactionSignature)2 ConsensusRule (org.ethereum.config.blockchain.upgrades.ConsensusRule)2 Coin (co.rsk.bitcoinj.core.Coin)1 ScriptBuilder (co.rsk.bitcoinj.script.ScriptBuilder)1 Wallet (co.rsk.bitcoinj.wallet.Wallet)1 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)1