Search in sources :

Example 11 with TransactionInput

use of co.rsk.bitcoinj.core.TransactionInput in project rskj by rsksmart.

the class BridgeUtilsTest method scriptCorrectlySpends_fromGenesisFederation_ok.

@Test
public void scriptCorrectlySpends_fromGenesisFederation_ok() {
    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);
    assertTrue(BridgeUtils.scriptCorrectlySpendsTx(tx, 0, genesisFederation.getP2SHScript()));
}
Also used : Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 12 with TransactionInput

use of co.rsk.bitcoinj.core.TransactionInput in project rskj by rsksmart.

the class BridgeUtilsTest method testIsValidPegInTx_hasChangeUtxoFromFastBridgeRetiredFederation_afterRskip201_notPegin.

@Test
public void testIsValidPegInTx_hasChangeUtxoFromFastBridgeRetiredFederation_afterRskip201_notPegin() {
    Context btcContext = new Context(networkParameters);
    Federation activeFederation = bridgeConstantsRegtest.getGenesisFederation();
    when(activations.isActive(ConsensusRule.RSKIP201)).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);
    // 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 fastBridgeRedeemScript = FastBridgeRedeemScriptParser.createMultiSigFastBridgeRedeemScript(retiredFederation.getRedeemScript(), PegTestUtils.createHash(2));
    signWithNecessaryKeys(retiredFederation, fastBridgeRedeemScript, retiredFederationKeys, txInput, tx);
    assertFalse(BridgeUtils.isValidPegInTx(tx, Collections.singletonList(activeFederation), retiredFederation.getP2SHScript(), btcContext, bridgeConstantsRegtest, activations));
}
Also used : Context(co.rsk.bitcoinj.core.Context) Script(co.rsk.bitcoinj.script.Script) 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)

Example 13 with TransactionInput

use of co.rsk.bitcoinj.core.TransactionInput in project rskj by rsksmart.

the class BridgeUtilsTest method testIsPegOutTx_noRedeemScript.

@Test
public void testIsPegOutTx_noRedeemScript() {
    Federation federation = bridgeConstantsRegtest.getGenesisFederation();
    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);
    assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, Collections.singletonList(federation), activations));
}
Also used : Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 14 with TransactionInput

use of co.rsk.bitcoinj.core.TransactionInput in project rskj by rsksmart.

the class BridgeUtilsTest method testChangeBetweenFederations.

@Test
public void testChangeBetweenFederations() {
    Address randomAddress = PegTestUtils.createRandomBtcAddress();
    Context btcContext = new Context(networkParameters);
    List<BtcECKey> federation1Keys = Stream.of("fa01", "fa02").map(Hex::decode).map(BtcECKey::fromPrivate).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
    Federation federation1 = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, networkParameters);
    List<BtcECKey> federation2Keys = Stream.of("fb01", "fb02", "fb03").map(Hex::decode).map(BtcECKey::fromPrivate).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
    Federation federation2 = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(federation2Keys), Instant.ofEpochMilli(2000L), 0L, networkParameters);
    Address federation2Address = federation2.getAddress();
    List<Federation> federations = Arrays.asList(federation1, federation2);
    BtcTransaction pegOutWithChange = new BtcTransaction(networkParameters);
    pegOutWithChange.addOutput(Coin.COIN, randomAddress);
    pegOutWithChange.addOutput(Coin.COIN, federation2Address);
    TransactionInput pegOutFromFederation2 = new TransactionInput(networkParameters, pegOutWithChange, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    pegOutWithChange.addInput(pegOutFromFederation2);
    signWithNecessaryKeys(federation2, federation2Keys, pegOutFromFederation2, pegOutWithChange);
    assertFalse(BridgeUtils.isValidPegInTx(pegOutWithChange, federations, null, btcContext, bridgeConstantsRegtest, mock(ActivationConfig.ForBlock.class)));
    assertTrue(BridgeUtils.isPegOutTx(pegOutWithChange, federations, activations));
}
Also used : Context(co.rsk.bitcoinj.core.Context) Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 15 with TransactionInput

use of co.rsk.bitcoinj.core.TransactionInput in project rskj by rsksmart.

the class BridgeUtilsTest method testIsValidPegInTx.

@Test
public void testIsValidPegInTx() {
    // Peg-in is for the genesis federation ATM
    Context btcContext = new Context(networkParameters);
    Federation federation = bridgeConstantsRegtest.getGenesisFederation();
    Wallet wallet = new BridgeBtcWallet(btcContext, Collections.singletonList(federation));
    Address federationAddress = federation.getAddress();
    wallet.addWatchedAddress(federationAddress, federation.getCreationTime().toEpochMilli());
    when(activations.isActive(any(ConsensusRule.class))).thenReturn(false);
    // Tx sending less than the minimum allowed, not a peg-in tx
    Coin minimumLockValue = bridgeConstantsRegtest.getLegacyMinimumPeginTxValueInSatoshis();
    BtcTransaction tx = new BtcTransaction(networkParameters);
    tx.addOutput(minimumLockValue.subtract(Coin.CENT), federationAddress);
    tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertFalse(BridgeUtils.isValidPegInTx(tx, federation, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 1 btc to the federation, but also spending from the federation address,
    // the typical peg-out tx, not a peg-in tx.
    BtcTransaction tx2 = new BtcTransaction(networkParameters);
    tx2.addOutput(Coin.COIN, federationAddress);
    TransactionInput txIn = new TransactionInput(networkParameters, tx2, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    tx2.addInput(txIn);
    signWithNecessaryKeys(bridgeConstantsRegtest.getGenesisFederation(), BridgeRegTestConstants.REGTEST_FEDERATION_PRIVATE_KEYS, txIn, tx2);
    assertFalse(BridgeUtils.isValidPegInTx(tx2, federation, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 1 btc to the federation, is a peg-in tx
    BtcTransaction tx3 = new BtcTransaction(networkParameters);
    tx3.addOutput(Coin.COIN, federationAddress);
    tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isValidPegInTx(tx3, federation, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 50 btc to the federation, is a peg-in tx
    BtcTransaction tx4 = new BtcTransaction(networkParameters);
    tx4.addOutput(Coin.FIFTY_COINS, federationAddress);
    tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isValidPegInTx(tx4, federation, btcContext, bridgeConstantsRegtest, activations));
}
Also used : Context(co.rsk.bitcoinj.core.Context) Coin(co.rsk.bitcoinj.core.Coin) Script(co.rsk.bitcoinj.script.Script) Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) Wallet(co.rsk.bitcoinj.wallet.Wallet) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Aggregations

TransactionInput (co.rsk.bitcoinj.core.TransactionInput)27 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)25 Test (org.junit.Test)24 TransactionOutPoint (co.rsk.bitcoinj.core.TransactionOutPoint)22 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)22 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)19 Script (co.rsk.bitcoinj.script.Script)16 Address (co.rsk.bitcoinj.core.Address)13 RskAddress (co.rsk.core.RskAddress)13 Context (co.rsk.bitcoinj.core.Context)12 Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)5 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)5 TransactionSignature (co.rsk.bitcoinj.crypto.TransactionSignature)4 Coin (co.rsk.bitcoinj.core.Coin)3 ConsensusRule (org.ethereum.config.blockchain.upgrades.ConsensusRule)3 ScriptBuilder (co.rsk.bitcoinj.script.ScriptBuilder)2 Wallet (co.rsk.bitcoinj.wallet.Wallet)2 Keccak256 (co.rsk.crypto.Keccak256)2 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)2 AddressFormatException (co.rsk.bitcoinj.core.AddressFormatException)1