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