Search in sources :

Example 11 with Wallet

use of co.rsk.bitcoinj.wallet.Wallet in project rskj by rsksmart.

the class BridgeUtils method isValidPegInTx.

/**
 * It checks if the tx doesn't spend any of the federations' funds and if it sends more than
 * the minimum ({@see BridgeConstants::getMinimumLockTxValue}) to any of the federations
 * @param tx the BTC transaction to check
 * @param activeFederations the active federations
 * @param retiredFederationP2SHScript the retired federation P2SHScript. Could be {@code null}.
 * @param btcContext the BTC Context
 * @param bridgeConstants the Bridge constants
 * @param activations the network HF activations configuration
 * @return true if this is a valid peg-in transaction
 */
public static boolean isValidPegInTx(BtcTransaction tx, List<Federation> activeFederations, Script retiredFederationP2SHScript, Context btcContext, BridgeConstants bridgeConstants, ActivationConfig.ForBlock activations) {
    // optionally sending some change to any of the federation addresses)
    for (int i = 0; i < tx.getInputs().size(); i++) {
        final int index = i;
        if (activeFederations.stream().anyMatch(federation -> scriptCorrectlySpendsTx(tx, index, federation.getP2SHScript()))) {
            return false;
        }
        if (retiredFederationP2SHScript != null && scriptCorrectlySpendsTx(tx, index, retiredFederationP2SHScript)) {
            return false;
        }
        // erp federation, or even a retired fast bridge or erp federation
        if (activations.isActive(ConsensusRule.RSKIP201)) {
            RedeemScriptParser redeemScriptParser = RedeemScriptParserFactory.get(tx.getInput(index).getScriptSig().getChunks());
            try {
                Script inputStandardRedeemScript = redeemScriptParser.extractStandardRedeemScript();
                if (activeFederations.stream().anyMatch(federation -> federation.getStandardRedeemScript().equals(inputStandardRedeemScript))) {
                    return false;
                }
                Script outputScript = ScriptBuilder.createP2SHOutputScript(inputStandardRedeemScript);
                if (outputScript.equals(retiredFederationP2SHScript)) {
                    return false;
                }
            } catch (ScriptException e) {
            // There is no redeem script, could be a peg-in from a P2PKH address
            }
        }
    }
    Wallet federationsWallet = BridgeUtils.getFederationsNoSpendWallet(btcContext, activeFederations, false, null);
    Coin valueSentToMe = tx.getValueSentToMe(federationsWallet);
    Coin minimumPegInTxValue = activations.isActive(ConsensusRule.RSKIP219) ? bridgeConstants.getMinimumPeginTxValueInSatoshis() : bridgeConstants.getLegacyMinimumPeginTxValueInSatoshis();
    if (valueSentToMe.isLessThan(minimumPegInTxValue)) {
        logger.warn("[btctx:{}] Someone sent to the federation less than {} satoshis", tx.getHash(), minimumPegInTxValue);
    }
    return valueSentToMe.isPositive() && !valueSentToMe.isLessThan(minimumPegInTxValue);
}
Also used : Script(co.rsk.bitcoinj.script.Script) Wallet(co.rsk.bitcoinj.wallet.Wallet) RedeemScriptParser(co.rsk.bitcoinj.script.RedeemScriptParser)

Example 12 with Wallet

use of co.rsk.bitcoinj.wallet.Wallet in project rskj by rsksmart.

the class BridgeSupportTest method getFastBridgeWallet_ok.

@Test
public void getFastBridgeWallet_ok() {
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP176)).thenReturn(true);
    Context btcContext = mock(Context.class);
    when(btcContext.getParams()).thenReturn(bridgeConstants.getBtcParams());
    BridgeSupport bridgeSupport = new BridgeSupport(bridgeConstants, mock(BridgeStorageProvider.class), mock(BridgeEventLogger.class), new BtcLockSenderProvider(), new PeginInstructionsProvider(), mock(Repository.class), mock(Block.class), btcContext, mock(FederationSupport.class), mock(BtcBlockStoreWithCache.Factory.class), activations);
    Federation fed = bridgeConstants.getGenesisFederation();
    Keccak256 derivationHash = PegTestUtils.createHash3(1);
    Script fastBridgeRedeemScript = FastBridgeRedeemScriptParser.createMultiSigFastBridgeRedeemScript(fed.getRedeemScript(), Sha256Hash.wrap(derivationHash.getBytes()));
    Script fastBridgeP2SH = ScriptBuilder.createP2SHOutputScript(fastBridgeRedeemScript);
    FastBridgeFederationInformation fastBridgeFederationInformation = new FastBridgeFederationInformation(derivationHash, fed.getP2SHScript().getPubKeyHash(), fastBridgeP2SH.getPubKeyHash());
    BtcTransaction tx = new BtcTransaction(bridgeConstants.getBtcParams());
    tx.addOutput(Coin.COIN, fastBridgeFederationInformation.getFastBridgeFederationAddress(bridgeConstants.getBtcParams()));
    List<UTXO> utxoList = new ArrayList<>();
    UTXO utxo = new UTXO(tx.getHash(), 0, Coin.COIN, 0, false, fastBridgeP2SH);
    utxoList.add(utxo);
    Wallet obtainedWallet = bridgeSupport.getFastBridgeWallet(btcContext, utxoList, fastBridgeFederationInformation);
    Assert.assertEquals(Coin.COIN, obtainedWallet.getBalance());
}
Also used : Script(co.rsk.bitcoinj.script.Script) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) Wallet(co.rsk.bitcoinj.wallet.Wallet) Keccak256(co.rsk.crypto.Keccak256) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation) PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation) Block(org.ethereum.core.Block) FastBridgeFederationInformation(co.rsk.peg.fastbridge.FastBridgeFederationInformation) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 13 with Wallet

use of co.rsk.bitcoinj.wallet.Wallet in project rskj by rsksmart.

the class BridgeSupportTestPowerMock method getActiveFederationWallet.

@Test
public void getActiveFederationWallet() throws IOException {
    Federation expectedFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(Arrays.asList(new BtcECKey[] { BtcECKey.fromPublicOnly(Hex.decode("036bb9eab797eadc8b697f0e82a01d01cabbfaaca37e5bafc06fdc6fdd38af894a")), BtcECKey.fromPublicOnly(Hex.decode("031da807c71c2f303b7f409dd2605b297ac494a563be3b9ca5f52d95a43d183cc5")) })), Instant.ofEpochMilli(5005L), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
    BridgeSupport bridgeSupport = getBridgeSupportWithMocksForFederationTests(false, expectedFederation, null, null, null, null, null);
    Context expectedContext = mock(Context.class);
    Whitebox.setInternalState(bridgeSupport, "btcContext", expectedContext);
    BridgeStorageProvider provider = (BridgeStorageProvider) Whitebox.getInternalState(bridgeSupport, "provider");
    Object expectedUtxos = provider.getNewFederationBtcUTXOs();
    final Wallet expectedWallet = mock(Wallet.class);
    PowerMockito.mockStatic(BridgeUtils.class);
    PowerMockito.when(BridgeUtils.getFederationSpendWallet(any(), any(), any(), anyBoolean(), any())).then((InvocationOnMock m) -> {
        Assert.assertEquals(m.<Context>getArgument(0), expectedContext);
        Assert.assertEquals(m.<Federation>getArgument(1), expectedFederation);
        Assert.assertEquals(m.<Object>getArgument(2), expectedUtxos);
        return expectedWallet;
    });
    Assert.assertSame(expectedWallet, bridgeSupport.getActiveFederationWallet(false));
}
Also used : Wallet(co.rsk.bitcoinj.wallet.Wallet) SimpleWallet(co.rsk.peg.simples.SimpleWallet) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with Wallet

use of co.rsk.bitcoinj.wallet.Wallet in project rskj by rsksmart.

the class BridgeUtilsTest method test_getNoSpendWallet.

private void test_getNoSpendWallet(boolean isFastBridgeCompatible) {
    Federation federation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(Arrays.asList(BtcECKey.fromPublicOnly(Hex.decode("036bb9eab797eadc8b697f0e82a01d01cabbfaaca37e5bafc06fdc6fdd38af894a")), BtcECKey.fromPublicOnly(Hex.decode("031da807c71c2f303b7f409dd2605b297ac494a563be3b9ca5f52d95a43d183cc5")))), Instant.ofEpochMilli(5005L), 0L, networkParameters);
    Context mockedBtcContext = mock(Context.class);
    when(mockedBtcContext.getParams()).thenReturn(networkParameters);
    Wallet wallet = BridgeUtils.getFederationNoSpendWallet(mockedBtcContext, federation, isFastBridgeCompatible, null);
    if (isFastBridgeCompatible) {
        Assert.assertEquals(FastBridgeCompatibleBtcWalletWithStorage.class, wallet.getClass());
    } else {
        Assert.assertEquals(BridgeBtcWallet.class, wallet.getClass());
    }
    assertIsWatching(federation.getAddress(), wallet, networkParameters);
}
Also used : Context(co.rsk.bitcoinj.core.Context) Wallet(co.rsk.bitcoinj.wallet.Wallet)

Example 15 with Wallet

use of co.rsk.bitcoinj.wallet.Wallet in project rskj by rsksmart.

the class BridgeUtilsTest method test_getSpendWallet.

private void test_getSpendWallet(boolean isFastBridgeCompatible) throws UTXOProviderException {
    Federation federation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(Arrays.asList(BtcECKey.fromPublicOnly(Hex.decode("036bb9eab797eadc8b697f0e82a01d01cabbfaaca37e5bafc06fdc6fdd38af894a")), BtcECKey.fromPublicOnly(Hex.decode("031da807c71c2f303b7f409dd2605b297ac494a563be3b9ca5f52d95a43d183cc5")))), Instant.ofEpochMilli(5005L), 0L, networkParameters);
    Context mockedBtcContext = mock(Context.class);
    when(mockedBtcContext.getParams()).thenReturn(networkParameters);
    List<UTXO> mockedUtxos = new ArrayList<>();
    mockedUtxos.add(mock(UTXO.class));
    mockedUtxos.add(mock(UTXO.class));
    mockedUtxos.add(mock(UTXO.class));
    Wallet wallet = BridgeUtils.getFederationSpendWallet(mockedBtcContext, federation, mockedUtxos, isFastBridgeCompatible, null);
    if (isFastBridgeCompatible) {
        Assert.assertEquals(FastBridgeCompatibleBtcWalletWithStorage.class, wallet.getClass());
    } else {
        Assert.assertEquals(BridgeBtcWallet.class, wallet.getClass());
    }
    assertIsWatching(federation.getAddress(), wallet, networkParameters);
    CoinSelector selector = wallet.getCoinSelector();
    Assert.assertEquals(RskAllowUnconfirmedCoinSelector.class, selector.getClass());
    UTXOProvider utxoProvider = wallet.getUTXOProvider();
    Assert.assertEquals(RskUTXOProvider.class, utxoProvider.getClass());
    Assert.assertEquals(mockedUtxos, utxoProvider.getOpenTransactionOutputs(Collections.emptyList()));
}
Also used : Context(co.rsk.bitcoinj.core.Context) UTXO(co.rsk.bitcoinj.core.UTXO) UTXOProvider(co.rsk.bitcoinj.core.UTXOProvider) Wallet(co.rsk.bitcoinj.wallet.Wallet) ArrayList(java.util.ArrayList) CoinSelector(co.rsk.bitcoinj.wallet.CoinSelector) RskAllowUnconfirmedCoinSelector(co.rsk.peg.bitcoin.RskAllowUnconfirmedCoinSelector)

Aggregations

Wallet (co.rsk.bitcoinj.wallet.Wallet)29 Test (org.junit.Test)11 Script (co.rsk.bitcoinj.script.Script)8 RskAddress (co.rsk.core.RskAddress)7 RskAllowUnconfirmedCoinSelector (co.rsk.peg.bitcoin.RskAllowUnconfirmedCoinSelector)7 Keccak256 (co.rsk.crypto.Keccak256)6 Context (co.rsk.bitcoinj.core.Context)5 SendRequest (co.rsk.bitcoinj.wallet.SendRequest)5 BridgeConstants (co.rsk.config.BridgeConstants)5 IOException (java.io.IOException)5 Block (org.ethereum.core.Block)5 Repository (org.ethereum.core.Repository)5 Address (co.rsk.bitcoinj.core.Address)4 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)4 Coin (co.rsk.bitcoinj.core.Coin)4 TransactionSignature (co.rsk.bitcoinj.crypto.TransactionSignature)4 ScriptBuilder (co.rsk.bitcoinj.script.ScriptBuilder)4 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)4 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)4 PanicProcessor (co.rsk.panic.PanicProcessor)4