use of co.rsk.bitcoinj.core.TransactionInput in project rskj by rsksmart.
the class BridgeUtilsTest method isInputSignedByThisFederator_isSignedByAnotherFederator.
@Test
public void isInputSignedByThisFederator_isSignedByAnotherFederator() {
// Arrange
BtcECKey federator1Key = new BtcECKey();
BtcECKey federator2Key = new BtcECKey();
Federation federation = new Federation(FederationMember.getFederationMembersFromKeys(Arrays.asList(federator1Key, federator2Key)), Instant.now(), 0, networkParameters);
// Create a tx from the Fed to a random btc address
BtcTransaction tx = new BtcTransaction(networkParameters);
TransactionInput txInput = new TransactionInput(networkParameters, tx, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
// Create script to be signed by federation members
Script inputScript = PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation);
txInput.setScriptSig(inputScript);
tx.addInput(txInput);
List<ScriptChunk> chunks = inputScript.getChunks();
byte[] program = chunks.get(chunks.size() - 1).data;
Script redeemScript = new Script(program);
Sha256Hash sighash = tx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
BtcECKey.ECDSASignature sig = federator1Key.sign(sighash);
TransactionSignature txSig = new TransactionSignature(sig, BtcTransaction.SigHash.ALL, false);
byte[] txSigEncoded = txSig.encodeToBitcoin();
int sigIndex = inputScript.getSigInsertionIndex(sighash, federator1Key);
inputScript = ScriptBuilder.updateScriptWithSignature(inputScript, txSigEncoded, sigIndex, 1, 1);
txInput.setScriptSig(inputScript);
// Act
boolean isSigned = BridgeUtils.isInputSignedByThisFederator(federator2Key, sighash, txInput);
// Assert
Assert.assertFalse(isSigned);
}
use of co.rsk.bitcoinj.core.TransactionInput in project rskj by rsksmart.
the class BridgeUtilsTest method testIsValidPegInTx_hasChangeUtxoFromFastBridgeErpRetiredFederation_beforeRskip201_isPegin.
@Test
public void testIsValidPegInTx_hasChangeUtxoFromFastBridgeErpRetiredFederation_beforeRskip201_isPegin() {
Context btcContext = new Context(networkParameters);
Federation activeFederation = bridgeConstantsRegtest.getGenesisFederation();
when(activations.isActive(ConsensusRule.RSKIP201)).thenReturn(false);
when(activations.isActive(ConsensusRule.RSKIP284)).thenReturn(true);
List<BtcECKey> retiredFederationKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")));
retiredFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR);
Federation retiredFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, networkParameters);
List<BtcECKey> erpFederationPublicKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa03")), BtcECKey.fromPrivate(Hex.decode("fa04")));
erpFederationPublicKeys.sort(BtcECKey.PUBKEY_COMPARATOR);
Federation erpFederation = new ErpFederation(FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, networkParameters, erpFederationPublicKeys, 500L, activations);
// Create a tx from the retired fast bridge fed to the active fed
BtcTransaction tx = new BtcTransaction(networkParameters);
tx.addOutput(Coin.COIN, activeFederation.getAddress());
TransactionInput txInput = new TransactionInput(networkParameters, tx, new byte[0], new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
tx.addInput(txInput);
Script fastBridgeErpRedeemScript = FastBridgeErpRedeemScriptParser.createFastBridgeErpRedeemScript(erpFederation.getRedeemScript(), PegTestUtils.createHash(2));
signWithNecessaryKeys(erpFederation, fastBridgeErpRedeemScript, retiredFederationKeys, txInput, tx);
assertTrue(BridgeUtils.isValidPegInTx(tx, Collections.singletonList(activeFederation), retiredFederation.getP2SHScript(), btcContext, bridgeConstantsRegtest, activations));
}
use of co.rsk.bitcoinj.core.TransactionInput in project rskj by rsksmart.
the class BridgeUtilsTest method testIsPegOutTx_invalidRedeemScript.
@Test
public void testIsPegOutTx_invalidRedeemScript() {
Federation federation = bridgeConstantsRegtest.getGenesisFederation();
Address randomAddress = PegTestUtils.createRandomBtcAddress();
Script invalidRedeemScript = ScriptBuilder.createRedeemScript(2, Arrays.asList(new BtcECKey(), new BtcECKey()));
BtcTransaction pegOutTx1 = new BtcTransaction(networkParameters);
pegOutTx1.addOutput(Coin.COIN, randomAddress);
TransactionInput pegOutInput1 = new TransactionInput(networkParameters, pegOutTx1, invalidRedeemScript.getProgram(), new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
pegOutTx1.addInput(pegOutInput1);
assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, Collections.singletonList(federation), activations));
}
use of co.rsk.bitcoinj.core.TransactionInput 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.TransactionInput 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