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()));
}
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));
}
Aggregations