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);
}
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());
}
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));
}
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);
}
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()));
}
Aggregations