Search in sources :

Example 1 with Wallet

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

the class BridgeSupportTest method getRetiringFederationWallet_nonEmpty.

@Test
public void getRetiringFederationWallet_nonEmpty() throws IOException {
    Federation mockedNewFederation = new Federation(getTestFederationPublicKeys(2), Instant.ofEpochMilli(2000), 10L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
    Federation expectedFederation = new Federation(Arrays.asList(new BtcECKey[] { BtcECKey.fromPublicOnly(Hex.decode("036bb9eab797eadc8b697f0e82a01d01cabbfaaca37e5bafc06fdc6fdd38af894a")), BtcECKey.fromPublicOnly(Hex.decode("031da807c71c2f303b7f409dd2605b297ac494a563be3b9ca5f52d95a43d183cc5")) }), Instant.ofEpochMilli(5005L), 0L, NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
    Block mockedBlock = mock(Block.class);
    when(mockedBlock.getNumber()).thenReturn(25L);
    BridgeSupport bridgeSupport = getBridgeSupportWithMocksForFederationTests(false, mockedNewFederation, null, expectedFederation, null, null, mockedBlock);
    Context expectedContext = mock(Context.class);
    Whitebox.setInternalState(bridgeSupport, "btcContext", expectedContext);
    BridgeStorageProvider provider = (BridgeStorageProvider) Whitebox.getInternalState(bridgeSupport, "provider");
    Object expectedUtxos = provider.getOldFederationBtcUTXOs();
    final Wallet expectedWallet = mock(Wallet.class);
    PowerMockito.mockStatic(BridgeUtils.class);
    PowerMockito.when(BridgeUtils.getFederationSpendWallet(any(), any(), any())).then((InvocationOnMock m) -> {
        Assert.assertEquals(m.getArgumentAt(0, Context.class), expectedContext);
        Assert.assertEquals(m.getArgumentAt(1, Federation.class), expectedFederation);
        Assert.assertEquals(m.getArgumentAt(2, Object.class), expectedUtxos);
        return expectedWallet;
    });
    Assert.assertSame(expectedWallet, bridgeSupport.getRetiringFederationWallet());
}
Also used : Wallet(co.rsk.bitcoinj.wallet.Wallet) SimpleWallet(co.rsk.peg.simples.SimpleWallet) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with Wallet

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

the class BridgeUtilsTest method getFederationNoSpendWallet.

@Test
public void getFederationNoSpendWallet() {
    NetworkParameters regTestParameters = NetworkParameters.fromID(NetworkParameters.ID_REGTEST);
    Federation federation = new Federation(Arrays.asList(new BtcECKey[] { BtcECKey.fromPublicOnly(Hex.decode("036bb9eab797eadc8b697f0e82a01d01cabbfaaca37e5bafc06fdc6fdd38af894a")), BtcECKey.fromPublicOnly(Hex.decode("031da807c71c2f303b7f409dd2605b297ac494a563be3b9ca5f52d95a43d183cc5")) }), Instant.ofEpochMilli(5005L), 0L, regTestParameters);
    Context mockedBtcContext = mock(Context.class);
    when(mockedBtcContext.getParams()).thenReturn(regTestParameters);
    Wallet wallet = BridgeUtils.getFederationNoSpendWallet(mockedBtcContext, federation);
    Assert.assertEquals(BridgeBtcWallet.class, wallet.getClass());
    assertIsWatching(federation.getAddress(), wallet, regTestParameters);
}
Also used : Wallet(co.rsk.bitcoinj.wallet.Wallet) Test(org.junit.Test)

Example 3 with Wallet

use of co.rsk.bitcoinj.wallet.Wallet 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 Wallet

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

the class BridgeUtils method isLockTx.

public static boolean isLockTx(BtcTransaction tx, List<Federation> federations, Context btcContext, BridgeConstants bridgeConstants) {
    // optionally sending some change to any of the federation addresses)
    for (int i = 0; i < tx.getInputs().size(); i++) {
        final int index = i;
        if (federations.stream().anyMatch(federation -> scriptCorrectlySpendsTx(tx, index, federation.getP2SHScript()))) {
            return false;
        }
    }
    Wallet federationsWallet = BridgeUtils.getFederationsNoSpendWallet(btcContext, federations);
    Coin valueSentToMe = tx.getValueSentToMe(federationsWallet);
    int valueSentToMeSignum = valueSentToMe.signum();
    if (valueSentToMe.isLessThan(bridgeConstants.getMinimumLockTxValue())) {
        logger.warn("Someone sent to the federation less than {} satoshis", bridgeConstants.getMinimumLockTxValue());
    }
    return (valueSentToMeSignum > 0 && !valueSentToMe.isLessThan(bridgeConstants.getMinimumLockTxValue()));
}
Also used : Wallet(co.rsk.bitcoinj.wallet.Wallet)

Example 5 with Wallet

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

the class BridgeUtils method getFederationsNoSpendWallet.

public static Wallet getFederationsNoSpendWallet(Context btcContext, List<Federation> federations) {
    Wallet wallet = new BridgeBtcWallet(btcContext, federations);
    federations.forEach(federation -> wallet.addWatchedAddress(federation.getAddress(), federation.getCreationTime().toEpochMilli()));
    return wallet;
}
Also used : Wallet(co.rsk.bitcoinj.wallet.Wallet)

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