use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeUtilsTest method isInputSignedByThisFederator_notSigned.
@Test
public void isInputSignedByThisFederator_notSigned() {
// 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);
// Act
boolean isSigned = BridgeUtils.isInputSignedByThisFederator(federator1Key, sighash, txInput);
// Assert
Assert.assertFalse(isSigned);
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeUtilsTest method signWithErpFederation.
private void signWithErpFederation(Federation erpFederation, List<BtcECKey> privateKeys, TransactionInput txIn, BtcTransaction tx) {
signWithNecessaryKeys(erpFederation, privateKeys, txIn, tx);
// Add OP_0 prefix to make it a valid erp federation script
Script erpInputScript = new ScriptBuilder().number(ScriptOpCodes.OP_0).addChunks(txIn.getScriptSig().getChunks()).build();
txIn.setScriptSig(erpInputScript);
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class ErpFederationTest method getRedeemScript.
@Test
public void getRedeemScript() {
Script redeemScript = federation.getRedeemScript();
Assert.assertEquals(19, redeemScript.getChunks().size());
// First element: OP_0 - Belonging to the standard of BTC
// M elements OP_0 - Belonging to M/N amount of signatures
// OP_0 - Belonging to ERP
// Last element: Program of redeem script
String expectedProgram = "64522102ed3bace23c5e17652e174c835fb72bf53ee306b3406a26890221b4ce" + "f7500f88210385a7b790fc9d962493788317e4874a4ab07f1e9c78c773c47f2f6c96df756f052103cd5" + "a3be41717d65683fe7a9de8ae5b4b8feced69f26a8b55eeefbcc2e74b75fb53670213c7b2755321029c" + "ecea902067992d52c38b28bf0bb2345bda9b21eca76b16a17c477a64e433012103284178e5fbcc63c54" + "c3b38e3ef88adf2da6c526313650041b0ef955763634ebd2103776b1fd8f86da3c1db3d69699e8250a1" + "5877d286734ea9a6da8e9d8ad25d16c12103ab0e2cd7ed158687fc13b88019990860cdb72b1f5777b58" + "513312550ea1584bc2103b9fc46657cf72a1afa007ecf431de1cd27ff5cc8829fa625b66ca47b967e6b" + "245568ae";
Assert.assertEquals(expectedProgram, Hex.toHexString(redeemScript.getProgram()));
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeUtilsTest method testIsMigrationTx.
@Test
public void testIsMigrationTx() {
Context btcContext = new Context(networkParameters);
List<BtcECKey> activeFederationKeys = Stream.of(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
Federation activeFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(2000L), 2L, networkParameters);
List<BtcECKey> retiringFederationKeys = Stream.of(BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
Federation retiringFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(retiringFederationKeys), Instant.ofEpochMilli(1000L), 1L, networkParameters);
List<BtcECKey> retiredFederationKeys = Stream.of(BtcECKey.fromPrivate(Hex.decode("fc01")), BtcECKey.fromPrivate(Hex.decode("fc02"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
Federation retiredFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 1L, networkParameters);
Address activeFederationAddress = activeFederation.getAddress();
BtcTransaction migrationTx = new BtcTransaction(networkParameters);
migrationTx.addOutput(Coin.COIN, activeFederationAddress);
TransactionInput migrationTxInput = new TransactionInput(networkParameters, migrationTx, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
migrationTx.addInput(migrationTxInput);
signWithNecessaryKeys(retiringFederation, retiringFederationKeys, migrationTxInput, migrationTx);
assertTrue(BridgeUtils.isMigrationTx(migrationTx, activeFederation, retiringFederation, null, btcContext, bridgeConstantsRegtest, activations));
BtcTransaction toActiveFederationTx = new BtcTransaction(networkParameters);
toActiveFederationTx.addOutput(Coin.COIN, activeFederationAddress);
toActiveFederationTx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
assertFalse(BridgeUtils.isMigrationTx(toActiveFederationTx, activeFederation, retiringFederation, null, btcContext, bridgeConstantsRegtest, activations));
Address randomAddress = PegTestUtils.createRandomBtcAddress();
BtcTransaction fromRetiringFederationTx = new BtcTransaction(networkParameters);
fromRetiringFederationTx.addOutput(Coin.COIN, randomAddress);
TransactionInput fromRetiringFederationTxInput = new TransactionInput(networkParameters, fromRetiringFederationTx, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
fromRetiringFederationTx.addInput(fromRetiringFederationTxInput);
signWithNecessaryKeys(retiringFederation, retiringFederationKeys, fromRetiringFederationTxInput, fromRetiringFederationTx);
assertFalse(BridgeUtils.isMigrationTx(fromRetiringFederationTx, activeFederation, retiringFederation, null, btcContext, bridgeConstantsRegtest, activations));
assertFalse(BridgeUtils.isMigrationTx(migrationTx, activeFederation, null, null, btcContext, bridgeConstantsRegtest, activations));
BtcTransaction retiredMigrationTx = new BtcTransaction(networkParameters);
retiredMigrationTx.addOutput(Coin.COIN, activeFederationAddress);
TransactionInput retiredMigrationTxInput = new TransactionInput(networkParameters, retiredMigrationTx, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
retiredMigrationTx.addInput(retiredMigrationTxInput);
signWithNecessaryKeys(retiredFederation, retiredFederationKeys, retiredMigrationTxInput, retiredMigrationTx);
Script p2SHScript = retiredFederation.getP2SHScript();
assertTrue(BridgeUtils.isMigrationTx(retiredMigrationTx, activeFederation, null, p2SHScript, btcContext, bridgeConstantsRegtest, activations));
assertTrue(BridgeUtils.isMigrationTx(retiredMigrationTx, activeFederation, retiringFederation, p2SHScript, btcContext, bridgeConstantsRegtest, activations));
assertFalse(BridgeUtils.isMigrationTx(toActiveFederationTx, activeFederation, null, p2SHScript, btcContext, bridgeConstantsRegtest, activations));
assertFalse(BridgeUtils.isMigrationTx(toActiveFederationTx, activeFederation, retiringFederation, p2SHScript, btcContext, bridgeConstantsRegtest, activations));
}
use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.
the class BridgeUtilsTest method testIsPegOutTx_fromFastBridgeErpFederation.
@Test
public void testIsPegOutTx_fromFastBridgeErpFederation() {
when(activations.isActive(ConsensusRule.RSKIP284)).thenReturn(true);
List<BtcECKey> defaultFederationKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")), BtcECKey.fromPrivate(Hex.decode("fa03")));
defaultFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR);
Federation defaultFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(defaultFederationKeys), Instant.ofEpochMilli(1000L), 0L, networkParameters);
List<BtcECKey> erpFederationPublicKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa03")), BtcECKey.fromPrivate(Hex.decode("fa04")), BtcECKey.fromPrivate(Hex.decode("fa05")));
erpFederationPublicKeys.sort(BtcECKey.PUBKEY_COMPARATOR);
Federation erpFederation = new ErpFederation(FederationTestUtils.getFederationMembersWithBtcKeys(defaultFederationKeys), Instant.ofEpochMilli(1000L), 0L, networkParameters, erpFederationPublicKeys, 500L, activations);
Federation standardFederation = bridgeConstantsRegtest.getGenesisFederation();
// Create a tx from the fast bridge erp fed to a random address
Address randomAddress = PegTestUtils.createRandomBtcAddress();
BtcTransaction pegOutTx1 = new BtcTransaction(networkParameters);
pegOutTx1.addOutput(Coin.COIN, randomAddress);
TransactionInput pegOutInput1 = new TransactionInput(networkParameters, pegOutTx1, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
pegOutTx1.addInput(pegOutInput1);
Script fastBridgeErpRedeemScript = FastBridgeErpRedeemScriptParser.createFastBridgeErpRedeemScript(erpFederation.getRedeemScript(), PegTestUtils.createHash(2));
signWithNecessaryKeys(erpFederation, fastBridgeErpRedeemScript, defaultFederationKeys, pegOutInput1, pegOutTx1);
// Before RSKIP 201 activation
when(activations.isActive(ConsensusRule.RSKIP201)).thenReturn(false);
assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, Collections.singletonList(defaultFederation), activations));
assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, Arrays.asList(defaultFederation, standardFederation), activations));
assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, Collections.singletonList(standardFederation), activations));
assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, activations, defaultFederation.getP2SHScript()));
assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, activations, defaultFederation.getP2SHScript(), standardFederation.getP2SHScript()));
assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, activations, standardFederation.getP2SHScript()));
// After RSKIP 201 activation
when(activations.isActive(ConsensusRule.RSKIP201)).thenReturn(true);
assertTrue(BridgeUtils.isPegOutTx(pegOutTx1, Collections.singletonList(defaultFederation), activations));
assertTrue(BridgeUtils.isPegOutTx(pegOutTx1, Arrays.asList(defaultFederation, standardFederation), activations));
assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, Collections.singletonList(standardFederation), activations));
assertTrue(BridgeUtils.isPegOutTx(pegOutTx1, activations, defaultFederation.getP2SHScript()));
assertTrue(BridgeUtils.isPegOutTx(pegOutTx1, activations, defaultFederation.getP2SHScript(), standardFederation.getP2SHScript()));
assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, activations, standardFederation.getP2SHScript()));
}
Aggregations