Search in sources :

Example 66 with Script

use of co.rsk.bitcoinj.script.Script 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)

Example 67 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeUtilsTest method isInputSignedByThisFederator_isSigned.

@Test
public void isInputSignedByThisFederator_isSigned() {
    // 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(federator1Key, sighash, txInput);
    // Assert
    Assert.assertTrue(isSigned);
}
Also used : Script(co.rsk.bitcoinj.script.Script) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) TransactionSignature(co.rsk.bitcoinj.crypto.TransactionSignature) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) ScriptChunk(co.rsk.bitcoinj.script.ScriptChunk) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 68 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeUtilsTest method signWithNKeys.

private void signWithNKeys(Federation federation, Script federationRedeemScript, List<BtcECKey> privateKeys, TransactionInput txIn, BtcTransaction tx, int numberOfSignatures) {
    Script scriptPubKey = federation.getP2SHScript();
    RedeemData redeemData = RedeemData.of(federation.getBtcPublicKeys(), federationRedeemScript);
    Script inputScript = scriptPubKey.createEmptyInputScript(redeemData.keys.get(0), redeemData.redeemScript);
    txIn.setScriptSig(inputScript);
    Sha256Hash sighash = tx.hashForSignature(0, federationRedeemScript, BtcTransaction.SigHash.ALL, false);
    for (int i = 0; i < numberOfSignatures; i++) {
        inputScript = signWithOneKey(federation, privateKeys, inputScript, sighash, i);
    }
    txIn.setScriptSig(inputScript);
}
Also used : Script(co.rsk.bitcoinj.script.Script) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) RedeemData(co.rsk.bitcoinj.wallet.RedeemData) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint)

Example 69 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeUtilsTest method testIsValidPegInTx_hasChangeUtxoFromFastBridgeRetiredFederation_beforeRskip201_isPegin.

@Test
public void testIsValidPegInTx_hasChangeUtxoFromFastBridgeRetiredFederation_beforeRskip201_isPegin() {
    Context btcContext = new Context(networkParameters);
    Federation activeFederation = bridgeConstantsRegtest.getGenesisFederation();
    when(activations.isActive(ConsensusRule.RSKIP201)).thenReturn(false);
    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);
    assertTrue(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 70 with Script

use of co.rsk.bitcoinj.script.Script 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);
}
Also used : Script(co.rsk.bitcoinj.script.Script) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) TransactionSignature(co.rsk.bitcoinj.crypto.TransactionSignature) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) ScriptChunk(co.rsk.bitcoinj.script.ScriptChunk) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Aggregations

Script (co.rsk.bitcoinj.script.Script)123 Test (org.junit.Test)91 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)60 RskAddress (co.rsk.core.RskAddress)47 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)40 Repository (org.ethereum.core.Repository)34 MutableRepository (org.ethereum.db.MutableRepository)33 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)29 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)26 Block (org.ethereum.core.Block)24 Context (co.rsk.bitcoinj.core.Context)21 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)20 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)20 Address (co.rsk.bitcoinj.core.Address)17 TransactionOutPoint (co.rsk.bitcoinj.core.TransactionOutPoint)17 PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation)17 PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation)17 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)17 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)17 InternalTransaction (org.ethereum.vm.program.InternalTransaction)17