Search in sources :

Example 1 with Script

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

the class BridgeSupportTest method registerBtcTransactionReleaseTx.

@Test
public void registerBtcTransactionReleaseTx() throws BlockStoreException, AddressFormatException, IOException {
    // Federation is the genesis federation ATM
    Federation federation = bridgeConstants.getGenesisFederation();
    Repository repository = new RepositoryImpl(config);
    repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE);
    Repository track = repository.startTracking();
    Block executionBlock = Mockito.mock(Block.class);
    Mockito.when(executionBlock.getNumber()).thenReturn(10L);
    BridgeRegTestConstants bridgeConstants = BridgeRegTestConstants.getInstance();
    BtcTransaction tx = new BtcTransaction(this.btcParams);
    Address address = ScriptBuilder.createP2SHOutputScript(2, Lists.newArrayList(new BtcECKey(), new BtcECKey(), new BtcECKey())).getToAddress(bridgeConstants.getBtcParams());
    tx.addOutput(Coin.COIN, address);
    Address address2 = federation.getAddress();
    tx.addOutput(Coin.COIN, address2);
    // Create previous tx
    BtcTransaction prevTx = new BtcTransaction(btcParams);
    TransactionOutput prevOut = new TransactionOutput(btcParams, prevTx, Coin.FIFTY_COINS, federation.getAddress());
    prevTx.addOutput(prevOut);
    // Create tx input
    tx.addInput(prevOut);
    // Create tx input base script sig
    Script scriptSig = PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation);
    // Create sighash
    Script redeemScript = ScriptBuilder.createRedeemScript(federation.getNumberOfSignaturesRequired(), federation.getPublicKeys());
    Sha256Hash sighash = tx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
    // Sign by federator 0
    BtcECKey.ECDSASignature sig0 = bridgeConstants.getFederatorPrivateKeys().get(0).sign(sighash);
    TransactionSignature txSig0 = new TransactionSignature(sig0, BtcTransaction.SigHash.ALL, false);
    int sigIndex0 = scriptSig.getSigInsertionIndex(sighash, federation.getPublicKeys().get(0));
    scriptSig = ScriptBuilder.updateScriptWithSignature(scriptSig, txSig0.encodeToBitcoin(), sigIndex0, 1, 1);
    // Sign by federator 1
    BtcECKey.ECDSASignature sig1 = bridgeConstants.getFederatorPrivateKeys().get(1).sign(sighash);
    TransactionSignature txSig1 = new TransactionSignature(sig1, BtcTransaction.SigHash.ALL, false);
    int sigIndex1 = scriptSig.getSigInsertionIndex(sighash, federation.getPublicKeys().get(1));
    scriptSig = ScriptBuilder.updateScriptWithSignature(scriptSig, txSig1.encodeToBitcoin(), sigIndex1, 1, 1);
    // Set scipt sign to tx input
    tx.getInput(0).setScriptSig(scriptSig);
    Context btcContext = new Context(bridgeConstants.getBtcParams());
    BtcBlockStore btcBlockStore = new RepositoryBlockStore(config, track, PrecompiledContracts.BRIDGE_ADDR);
    BtcBlockChain btcBlockChain = new SimpleBlockChain(btcContext, btcBlockStore);
    BridgeStorageProvider provider = new BridgeStorageProvider(track, contractAddress, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    BridgeSupport bridgeSupport = new BridgeSupport(config, track, null, config.getBlockchainConfig().getCommonConstants().getBridgeConstants(), provider, btcBlockStore, btcBlockChain);
    Whitebox.setInternalState(bridgeSupport, "rskExecutionBlock", executionBlock);
    byte[] bits = new byte[1];
    bits[0] = 0x01;
    List<Sha256Hash> hashes = new ArrayList<>();
    hashes.add(tx.getHash());
    PartialMerkleTree pmt = new PartialMerkleTree(btcParams, bits, hashes, 1);
    List<Sha256Hash> hashlist = new ArrayList<>();
    Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(hashlist);
    co.rsk.bitcoinj.core.BtcBlock block = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(), merkleRoot, 1, 1, 1, new ArrayList<BtcTransaction>());
    btcBlockChain.add(block);
    ((SimpleBlockChain) btcBlockChain).useHighBlock();
    bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx, 1, pmt);
    bridgeSupport.save();
    ((SimpleBlockChain) btcBlockChain).useBlock();
    track.commit();
    Assert.assertEquals(LIMIT_MONETARY_BASE, repository.getBalance(PrecompiledContracts.BRIDGE_ADDR));
    BridgeStorageProvider provider2 = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    Assert.assertEquals(1, provider2.getNewFederationBtcUTXOs().size());
    Assert.assertEquals(Coin.COIN, provider2.getNewFederationBtcUTXOs().get(0).getValue());
    Assert.assertEquals(0, provider2.getReleaseRequestQueue().getEntries().size());
    Assert.assertEquals(0, provider2.getReleaseTransactionSet().getEntries().size());
    Assert.assertTrue(provider2.getRskTxsWaitingForSignatures().isEmpty());
    Assert.assertEquals(1, provider2.getBtcTxHashesAlreadyProcessed().size());
}
Also used : RskAddress(co.rsk.core.RskAddress) TransactionSignature(co.rsk.bitcoinj.crypto.TransactionSignature) SimpleBlockChain(co.rsk.peg.simples.SimpleBlockChain) BridgeRegTestConstants(co.rsk.config.BridgeRegTestConstants) Script(co.rsk.bitcoinj.script.Script) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) org.ethereum.core(org.ethereum.core) SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) RepositoryImpl(co.rsk.db.RepositoryImpl) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with Script

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

the class BridgeUtilsTest method testIsLockForTwoFederations.

@Test
public void testIsLockForTwoFederations() throws Exception {
    BridgeRegTestConstants bridgeConstants = BridgeRegTestConstants.getInstance();
    NetworkParameters parameters = bridgeConstants.getBtcParams();
    Context btcContext = new Context(parameters);
    List<BtcECKey> federation1Keys = Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")) });
    federation1Keys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation federation1 = new Federation(federation1Keys, Instant.ofEpochMilli(1000L), 0L, parameters);
    List<BtcECKey> federation2Keys = Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03")) });
    federation2Keys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation federation2 = new Federation(federation2Keys, Instant.ofEpochMilli(2000L), 0L, parameters);
    Address address1 = federation1.getAddress();
    Address address2 = federation2.getAddress();
    List<Federation> federations = Arrays.asList(federation1, federation2);
    List<Address> addresses = Arrays.asList(address1, address2);
    // Tx sending less than 1 btc to the first federation, not a lock tx
    BtcTransaction tx = new BtcTransaction(parameters);
    tx.addOutput(Coin.CENT, address1);
    tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertFalse(BridgeUtils.isLockTx(tx, federations, btcContext, bridgeConstants));
    // Tx sending less than 1 btc to the second federation, not a lock tx
    tx = new BtcTransaction(parameters);
    tx.addOutput(Coin.CENT, address2);
    tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertFalse(BridgeUtils.isLockTx(tx, federations, btcContext, bridgeConstants));
    // Tx sending less than 1 btc to both federations, not a lock tx
    tx = new BtcTransaction(parameters);
    tx.addOutput(Coin.CENT, address1);
    tx.addOutput(Coin.CENT, address2);
    tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertFalse(BridgeUtils.isLockTx(tx, federations, btcContext, bridgeConstants));
    // Tx sending 1 btc to the first federation, but also spending from the first federation address, the typical release tx, not a lock tx.
    BtcTransaction tx2 = new BtcTransaction(parameters);
    tx2.addOutput(Coin.COIN, address1);
    TransactionInput txIn = new TransactionInput(parameters, tx2, new byte[] {}, new TransactionOutPoint(parameters, 0, Sha256Hash.ZERO_HASH));
    tx2.addInput(txIn);
    signWithNecessaryKeys(federation1, federation1Keys, txIn, tx2, bridgeConstants);
    assertFalse(BridgeUtils.isLockTx(tx2, federations, btcContext, bridgeConstants));
    // Tx sending 1 btc to the second federation, but also spending from the second federation address, the typical release tx, not a lock tx.
    tx2 = new BtcTransaction(parameters);
    tx2.addOutput(Coin.COIN, address2);
    txIn = new TransactionInput(parameters, tx2, new byte[] {}, new TransactionOutPoint(parameters, 0, Sha256Hash.ZERO_HASH));
    tx2.addInput(txIn);
    signWithNecessaryKeys(federation2, federation2Keys, txIn, tx2, bridgeConstants);
    assertFalse(BridgeUtils.isLockTx(tx2, federations, btcContext, bridgeConstants));
    // Tx sending 1 btc to both federations, but also spending from the first federation address, the typical release tx, not a lock tx.
    tx2 = new BtcTransaction(parameters);
    tx2.addOutput(Coin.COIN, address1);
    tx2.addOutput(Coin.COIN, address2);
    txIn = new TransactionInput(parameters, tx2, new byte[] {}, new TransactionOutPoint(parameters, 0, Sha256Hash.ZERO_HASH));
    tx2.addInput(txIn);
    signWithNecessaryKeys(federation1, federation1Keys, txIn, tx2, bridgeConstants);
    assertFalse(BridgeUtils.isLockTx(tx2, federations, btcContext, bridgeConstants));
    // Tx sending 1 btc to both federations, but also spending from the second federation address, the typical release tx, not a lock tx.
    tx2 = new BtcTransaction(parameters);
    tx2.addOutput(Coin.COIN, address1);
    tx2.addOutput(Coin.COIN, address2);
    txIn = new TransactionInput(parameters, tx2, new byte[] {}, new TransactionOutPoint(parameters, 0, Sha256Hash.ZERO_HASH));
    tx2.addInput(txIn);
    signWithNecessaryKeys(federation2, federation2Keys, txIn, tx2, bridgeConstants);
    assertFalse(BridgeUtils.isLockTx(tx2, federations, btcContext, bridgeConstants));
    // Tx sending 1 btc to the first federation, is a lock tx
    BtcTransaction tx3 = new BtcTransaction(parameters);
    tx3.addOutput(Coin.COIN, address1);
    tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isLockTx(tx3, federations, btcContext, bridgeConstants));
    // Tx sending 1 btc to the second federation, is a lock tx
    tx3 = new BtcTransaction(parameters);
    tx3.addOutput(Coin.COIN, address2);
    tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isLockTx(tx3, federations, btcContext, bridgeConstants));
    // Tx sending 1 btc to the both federations, is a lock tx
    tx3 = new BtcTransaction(parameters);
    tx3.addOutput(Coin.COIN, address1);
    tx3.addOutput(Coin.COIN, address2);
    tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isLockTx(tx3, federations, btcContext, bridgeConstants));
    // Tx sending 50 btc to the first federation, is a lock tx
    BtcTransaction tx4 = new BtcTransaction(parameters);
    tx4.addOutput(Coin.FIFTY_COINS, address1);
    tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isLockTx(tx4, federations, btcContext, bridgeConstants));
    // Tx sending 50 btc to the second federation, is a lock tx
    tx4 = new BtcTransaction(parameters);
    tx4.addOutput(Coin.FIFTY_COINS, address2);
    tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isLockTx(tx4, federations, btcContext, bridgeConstants));
    // Tx sending 50 btc to the both federations, is a lock tx
    tx4 = new BtcTransaction(parameters);
    tx4.addOutput(Coin.FIFTY_COINS, address1);
    tx4.addOutput(Coin.FIFTY_COINS, address2);
    tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isLockTx(tx4, federations, btcContext, bridgeConstants));
}
Also used : Script(co.rsk.bitcoinj.script.Script) RskAddress(co.rsk.core.RskAddress) BridgeRegTestConstants(co.rsk.config.BridgeRegTestConstants) Test(org.junit.Test)

Example 3 with Script

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

the class BridgeUtilsTest method testIsLock.

@Test
public void testIsLock() throws Exception {
    // Lock is for the genesis federation ATM
    NetworkParameters params = RegTestParams.get();
    Context btcContext = new Context(params);
    BridgeRegTestConstants bridgeConstants = BridgeRegTestConstants.getInstance();
    Federation federation = bridgeConstants.getGenesisFederation();
    Wallet wallet = new BridgeBtcWallet(btcContext, Arrays.asList(federation));
    wallet.addWatchedAddress(federation.getAddress(), federation.getCreationTime().toEpochMilli());
    Address address = federation.getAddress();
    // Tx sending less than 1 btc to the federation, not a lock tx
    BtcTransaction tx = new BtcTransaction(params);
    tx.addOutput(Coin.CENT, address);
    tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertFalse(BridgeUtils.isLockTx(tx, federation, btcContext, bridgeConstants));
    // Tx sending 1 btc to the federation, but also spending from the federation addres, the typical release tx, not a lock tx.
    BtcTransaction tx2 = new BtcTransaction(params);
    tx2.addOutput(Coin.COIN, address);
    TransactionInput txIn = new TransactionInput(params, tx2, new byte[] {}, new TransactionOutPoint(params, 0, Sha256Hash.ZERO_HASH));
    tx2.addInput(txIn);
    signWithNecessaryKeys(bridgeConstants.getGenesisFederation(), bridgeConstants.getFederatorPrivateKeys(), txIn, tx2, bridgeConstants);
    assertFalse(BridgeUtils.isLockTx(tx2, federation, btcContext, bridgeConstants));
    // Tx sending 1 btc to the federation, is a lock tx
    BtcTransaction tx3 = new BtcTransaction(params);
    tx3.addOutput(Coin.COIN, address);
    tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isLockTx(tx3, federation, btcContext, bridgeConstants));
    // Tx sending 50 btc to the federation, is a lock tx
    BtcTransaction tx4 = new BtcTransaction(params);
    tx4.addOutput(Coin.FIFTY_COINS, address);
    tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isLockTx(tx4, federation, btcContext, bridgeConstants));
}
Also used : Script(co.rsk.bitcoinj.script.Script) BridgeRegTestConstants(co.rsk.config.BridgeRegTestConstants) RskAddress(co.rsk.core.RskAddress) Wallet(co.rsk.bitcoinj.wallet.Wallet) Test(org.junit.Test)

Example 4 with Script

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

the class BridgeStorageProviderTest method saveLastRetiredFederationP2SHScript_before_RSKIP186.

@Test
public void saveLastRetiredFederationP2SHScript_before_RSKIP186() {
    Repository repository = mock(Repository.class);
    BridgeStorageProvider provider0 = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getNetworkConstants().getBridgeConstants(), activationsBeforeFork);
    Script script = new Script(new byte[] {});
    provider0.setLastRetiredFederationP2SHScript(script);
    provider0.saveLastRetiredFederationP2SHScript();
    // If the network upgrade is not enabled we shouldn't be saving to the repository
    verify(repository, never()).addStorageBytes(eq(PrecompiledContracts.BRIDGE_ADDR), eq(DataWord.fromString("lastRetiredFedP2SHScript")), any());
}
Also used : Script(co.rsk.bitcoinj.script.Script) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with Script

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

the class BridgeSerializationUtilsTest method serializeScript.

@Test
public void serializeScript() {
    Script expectedScript = ScriptBuilder.createP2SHOutputScript(2, Lists.newArrayList(new BtcECKey(), new BtcECKey(), new BtcECKey()));
    byte[] actualData = BridgeSerializationUtils.serializeScript(expectedScript);
    Assert.assertEquals(expectedScript, new Script(((RLPList) RLP.decode2(actualData).get(0)).get(0).getRLPData()));
}
Also used : Script(co.rsk.bitcoinj.script.Script) 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