Search in sources :

Example 21 with Script

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

the class PeginInstructionsProviderTest method extractOpReturnData_opReturnWithDataLengthShorterThanExpected.

@Test(expected = NoOpReturnException.class)
public void extractOpReturnData_opReturnWithDataLengthShorterThanExpected() throws PeginInstructionsException {
    // Arrange
    Script opReturnScript = ScriptBuilder.createOpReturnScript("1".getBytes());
    BtcTransaction btcTransaction = new BtcTransaction(params);
    btcTransaction.addOutput(Coin.ZERO, opReturnScript);
    // Act
    PeginInstructionsProvider.extractOpReturnData(btcTransaction);
}
Also used : Script(co.rsk.bitcoinj.script.Script) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) Test(org.junit.Test)

Example 22 with Script

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

the class PeginInstructionsProviderTest method buildPeginInstructions_v1_withBtcrefundAddress.

@Test
public void buildPeginInstructions_v1_withBtcrefundAddress() throws Exception {
    // Arrange
    int protocolVersion = 1;
    BtcECKey key = new BtcECKey();
    RskAddress rskDestinationAddress = new RskAddress(ECKey.fromPublicOnly(key.getPubKey()).getAddress());
    Address btcRefundAddress = key.toAddress(params);
    Script opReturnScript = PegTestUtils.createOpReturnScriptForRsk(protocolVersion, rskDestinationAddress, Optional.of(btcRefundAddress));
    BtcTransaction btcTransaction = new BtcTransaction(params);
    btcTransaction.addOutput(Coin.ZERO, opReturnScript);
    // Act
    PeginInstructionsProvider peginInstructionsProvider = new PeginInstructionsProvider();
    Optional<PeginInstructions> peginInstructions = peginInstructionsProvider.buildPeginInstructions(btcTransaction);
    // Assert
    Assert.assertTrue(peginInstructions.isPresent());
    Assert.assertEquals(protocolVersion, peginInstructions.get().getProtocolVersion());
    Assert.assertEquals(rskDestinationAddress, peginInstructions.get().getRskDestinationAddress());
    PeginInstructionsVersion1 peginInstructionsVersion1 = (PeginInstructionsVersion1) peginInstructions.get();
    Assert.assertTrue(peginInstructionsVersion1.getBtcRefundAddress().isPresent());
    Assert.assertEquals(btcRefundAddress, peginInstructionsVersion1.getBtcRefundAddress().get());
}
Also used : Script(co.rsk.bitcoinj.script.Script) Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) RskAddress(co.rsk.core.RskAddress) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) Test(org.junit.Test)

Example 23 with Script

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

the class FastBridgeCompatibleBtcWalletWithStorageTest method findRedeemDataFromScriptHash_with_fastBridgeInformation_in_storage.

@Test
public void findRedeemDataFromScriptHash_with_fastBridgeInformation_in_storage() {
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    Keccak256 derivationArgumentsHash = PegTestUtils.createHash3(1);
    Script fastBridgeRedeemScript = FastBridgeRedeemScriptParser.createMultiSigFastBridgeRedeemScript(federation.getRedeemScript(), Sha256Hash.wrap(derivationArgumentsHash.getBytes()));
    Script p2SHOutputScript = ScriptBuilder.createP2SHOutputScript(fastBridgeRedeemScript);
    byte[] fastBridgeFederationP2SH = p2SHOutputScript.getPubKeyHash();
    FastBridgeFederationInformation fastBridgeFederationInformation = new FastBridgeFederationInformation(derivationArgumentsHash, federation.getP2SHScript().getPubKeyHash(), fastBridgeFederationP2SH);
    when(provider.getFastBridgeFederationInformation(fastBridgeFederationP2SH)).thenReturn(Optional.of(fastBridgeFederationInformation));
    FastBridgeCompatibleBtcWalletWithStorage fastBridgeCompatibleBtcWalletWithStorage = new FastBridgeCompatibleBtcWalletWithStorage(mock(Context.class), federationList, provider);
    RedeemData redeemData = fastBridgeCompatibleBtcWalletWithStorage.findRedeemDataFromScriptHash(fastBridgeFederationP2SH);
    Assert.assertNotNull(redeemData);
    Assert.assertEquals(fastBridgeRedeemScript, redeemData.redeemScript);
}
Also used : Context(co.rsk.bitcoinj.core.Context) Script(co.rsk.bitcoinj.script.Script) Keccak256(co.rsk.crypto.Keccak256) RedeemData(co.rsk.bitcoinj.wallet.RedeemData) FastBridgeFederationInformation(co.rsk.peg.fastbridge.FastBridgeFederationInformation) Test(org.junit.Test)

Example 24 with Script

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

the class BridgeUtilsTest method assertIsWatching.

private void assertIsWatching(Address address, Wallet wallet, NetworkParameters parameters) {
    List<Script> watchedScripts = wallet.getWatchedScripts();
    Assert.assertEquals(1, watchedScripts.size());
    Script watchedScript = watchedScripts.get(0);
    Assert.assertTrue(watchedScript.isPayToScriptHash());
    Assert.assertEquals(address.toString(), watchedScript.getToAddress(parameters).toString());
}
Also used : Script(co.rsk.bitcoinj.script.Script)

Example 25 with Script

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

the class BridgeUtilsTest method getRegularPegoutTxSize_has_proper_calculations.

@Test
public void getRegularPegoutTxSize_has_proper_calculations() {
    BtcECKey key1 = new BtcECKey();
    BtcECKey key2 = new BtcECKey();
    BtcECKey key3 = new BtcECKey();
    List<BtcECKey> keys = Arrays.asList(key1, key2, key3);
    Federation fed = new Federation(FederationMember.getFederationMembersFromKeys(keys), Instant.now(), 0, networkParameters);
    // Create a pegout tx with two inputs and two outputs
    int inputs = 2;
    BtcTransaction pegoutTx = createPegOutTx(Collections.emptyList(), inputs, fed, false);
    for (int inputIndex = 0; inputIndex < inputs; inputIndex++) {
        Script inputScript = pegoutTx.getInput(inputIndex).getScriptSig();
        Sha256Hash sighash = pegoutTx.hashForSignature(inputIndex, fed.getRedeemScript(), BtcTransaction.SigHash.ALL, false);
        for (int keyIndex = 0; keyIndex < keys.size() - 1; keyIndex++) {
            BtcECKey key = keys.get(keyIndex);
            BtcECKey.ECDSASignature sig = key.sign(sighash);
            TransactionSignature txSig = new TransactionSignature(sig, BtcTransaction.SigHash.ALL, false);
            byte[] txSigEncoded = txSig.encodeToBitcoin();
            int sigIndex = inputScript.getSigInsertionIndex(sighash, key);
            inputScript = ScriptBuilder.updateScriptWithSignature(inputScript, txSigEncoded, sigIndex, 1, 1);
            pegoutTx.getInput(inputIndex).setScriptSig(inputScript);
        }
    }
    int pegoutTxSize = BridgeUtils.getRegularPegoutTxSize(fed);
    // The difference between the calculated size and a real tx size should be smaller than 10% in any direction
    int difference = pegoutTx.bitcoinSerialize().length - pegoutTxSize;
    double tolerance = pegoutTxSize * .1;
    assertTrue(difference < tolerance && difference > -tolerance);
}
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) 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