use of co.rsk.peg.fastbridge.FastBridgeFederationInformation in project rskj by rsksmart.
the class BridgeSupport method registerFastBridgeBtcTransaction.
public BigInteger registerFastBridgeBtcTransaction(Transaction rskTx, byte[] btcTxSerialized, int height, byte[] pmtSerialized, Keccak256 derivationArgumentsHash, Address userRefundAddress, RskAddress lbcAddress, Address lpBtcAddress, boolean shouldTransferToContract) throws BlockStoreException, IOException, BridgeIllegalArgumentException {
if (!BridgeUtils.isContractTx(rskTx)) {
logger.debug("[registerFastBridgeBtcTransaction] (rskTx:{}) Sender not a contract", rskTx.getHash());
return BigInteger.valueOf(FAST_BRIDGE_UNPROCESSABLE_TX_NOT_CONTRACT_ERROR_CODE);
}
if (!rskTx.getSender().equals(lbcAddress)) {
logger.debug("[registerFastBridgeBtcTransaction] Expected sender to be the same as lbcAddress. (sender: {}) (lbcAddress:{})", rskTx.getSender(), lbcAddress);
return BigInteger.valueOf(FAST_BRIDGE_UNPROCESSABLE_TX_INVALID_SENDER_ERROR_CODE);
}
Context.propagate(btcContext);
Sha256Hash btcTxHash = BtcTransactionFormatUtils.calculateBtcTxHash(btcTxSerialized);
Keccak256 fastBridgeDerivationHash = getFastBridgeDerivationHash(derivationArgumentsHash, userRefundAddress, lpBtcAddress, lbcAddress);
if (provider.isFastBridgeFederationDerivationHashUsed(btcTxHash, fastBridgeDerivationHash)) {
logger.debug("[registerFastBridgeBtcTransaction] Transaction and derivation hash already used");
return BigInteger.valueOf(FAST_BRIDGE_UNPROCESSABLE_TX_ALREADY_PROCESSED_ERROR_CODE);
}
if (!validationsForRegisterBtcTransaction(btcTxHash, height, pmtSerialized, btcTxSerialized)) {
logger.debug("[registerFastBridgeBtcTransaction] (btcTx:{}) error during validationsForRegisterBtcTransaction", btcTxHash);
return BigInteger.valueOf(FAST_BRIDGE_UNPROCESSABLE_TX_VALIDATIONS_ERROR);
}
BtcTransaction btcTx = new BtcTransaction(bridgeConstants.getBtcParams(), btcTxSerialized);
btcTx.verify();
Sha256Hash btcTxHashWithoutWitness = btcTx.getHash(false);
if (!btcTxHashWithoutWitness.equals(btcTxHash) && provider.isFastBridgeFederationDerivationHashUsed(btcTxHashWithoutWitness, derivationArgumentsHash)) {
logger.debug("[registerFastBridgeBtcTransaction] Transaction and derivation hash already used");
return BigInteger.valueOf(FAST_BRIDGE_UNPROCESSABLE_TX_ALREADY_PROCESSED_ERROR_CODE);
}
FastBridgeFederationInformation fastBridgeFederationInformation = createFastBridgeFederationInformation(fastBridgeDerivationHash);
Address fastBridgeFedAddress = fastBridgeFederationInformation.getFastBridgeFederationAddress(bridgeConstants.getBtcParams());
Coin totalAmount = getAmountSentToAddress(btcTx, fastBridgeFedAddress);
if (totalAmount == Coin.ZERO) {
logger.debug("[registerFastBridgeBtcTransaction] Amount sent can't be 0");
return BigInteger.valueOf(FAST_BRIDGE_UNPROCESSABLE_TX_VALUE_ZERO_ERROR);
}
if (!verifyLockDoesNotSurpassLockingCap(btcTx, totalAmount)) {
InternalTransaction internalTx = (InternalTransaction) rskTx;
logger.info("[registerFastBridgeBtcTransaction] Locking cap surpassed, going to return funds!");
WalletProvider walletProvider = createFastBridgeWalletProvider(fastBridgeFederationInformation);
provider.markFastBridgeFederationDerivationHashAsUsed(btcTxHash, fastBridgeDerivationHash);
if (shouldTransferToContract) {
logger.debug("[registerFastBridgeBtcTransaction] Returning to liquidity provider");
generateRejectionRelease(btcTx, lpBtcAddress, fastBridgeFedAddress, new Keccak256(internalTx.getOriginHash()), totalAmount, walletProvider);
return BigInteger.valueOf(FAST_BRIDGE_REFUNDED_LP_ERROR_CODE);
} else {
logger.debug("[registerFastBridgeBtcTransaction] Returning to user");
generateRejectionRelease(btcTx, userRefundAddress, fastBridgeFedAddress, new Keccak256(internalTx.getOriginHash()), totalAmount, walletProvider);
return BigInteger.valueOf(FAST_BRIDGE_REFUNDED_USER_ERROR_CODE);
}
}
transferTo(lbcAddress, co.rsk.core.Coin.fromBitcoin(totalAmount));
saveFastBridgeDataInStorage(btcTxHashWithoutWitness, fastBridgeDerivationHash, fastBridgeFederationInformation, getUTXOsForAddress(btcTx, fastBridgeFedAddress));
logger.info("[registerFastBridgeBtcTransaction] (btcTx:{}) transaction registered successfully", btcTxHashWithoutWitness);
return co.rsk.core.Coin.fromBitcoin(totalAmount).asBigInteger();
}
use of co.rsk.peg.fastbridge.FastBridgeFederationInformation in project rskj by rsksmart.
the class FastBridgeCompatibleBtcWallet method findRedeemDataFromScriptHash.
@Nullable
@Override
public RedeemData findRedeemDataFromScriptHash(byte[] payToScriptHash) {
Optional<FastBridgeFederationInformation> fastBridgeFederationInformation = this.getFastBridgeFederationInformation(payToScriptHash);
if (fastBridgeFederationInformation.isPresent()) {
FastBridgeFederationInformation fastBridgeFederationInformationInstance = fastBridgeFederationInformation.get();
Optional<Federation> destinationFederation = getDestinationFederation(fastBridgeFederationInformationInstance.getFederationRedeemScriptHash());
if (!destinationFederation.isPresent()) {
return null;
}
Federation destinationFederationInstance = destinationFederation.get();
Script fedRedeemScript = destinationFederationInstance.getRedeemScript();
RedeemScriptParser parser = RedeemScriptParserFactory.get(fedRedeemScript.getChunks());
Script fastBridgeRedeemScript;
if (parser.getMultiSigType() == MultiSigType.ERP_FED) {
fastBridgeRedeemScript = FastBridgeErpRedeemScriptParser.createFastBridgeErpRedeemScript(fedRedeemScript, Sha256Hash.wrap(fastBridgeFederationInformationInstance.getDerivationHash().getBytes()));
} else {
fastBridgeRedeemScript = FastBridgeRedeemScriptParser.createMultiSigFastBridgeRedeemScript(fedRedeemScript, Sha256Hash.wrap(fastBridgeFederationInformationInstance.getDerivationHash().getBytes()));
}
return RedeemData.of(destinationFederationInstance.getBtcPublicKeys(), fastBridgeRedeemScript);
}
return super.findRedeemDataFromScriptHash(payToScriptHash);
}
use of co.rsk.peg.fastbridge.FastBridgeFederationInformation in project rskj by rsksmart.
the class BridgeSerializationUtils method deserializeFastBridgeFederationInformation.
public static FastBridgeFederationInformation deserializeFastBridgeFederationInformation(byte[] data, byte[] fastBridgeScriptHash) {
if (data == null || data.length == 0) {
return null;
}
RLPList rlpList = (RLPList) RLP.decode2(data).get(0);
if (rlpList.size() != 2) {
throw new RuntimeException(String.format("Invalid serialized Fast Bridge Federation: expected 2 value but got %d", rlpList.size()));
}
Keccak256 derivationHash = new Keccak256(rlpList.get(0).getRLPData());
byte[] federationP2SH = rlpList.get(1).getRLPData();
return new FastBridgeFederationInformation(derivationHash, federationP2SH, fastBridgeScriptHash);
}
use of co.rsk.peg.fastbridge.FastBridgeFederationInformation in project rskj by rsksmart.
the class BridgeStorageProviderTest method getFastBridgeFederationInformation_notFound.
@Test
public void getFastBridgeFederationInformation_notFound() {
Repository repository = mock(Repository.class);
byte[] fastBridgeFederationRedeemScriptHash = new byte[] { (byte) 0xaa };
when(repository.getStorageBytes(PrecompiledContracts.BRIDGE_ADDR, DataWord.fromLongString("fastBridgeFederationInformation-" + Hex.toHexString(fastBridgeFederationRedeemScriptHash)))).thenReturn(null);
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP176)).thenReturn(true);
BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getNetworkConstants().getBridgeConstants(), activations);
Optional<FastBridgeFederationInformation> result = provider.getFastBridgeFederationInformation(fastBridgeFederationRedeemScriptHash);
assertFalse(result.isPresent());
}
use of co.rsk.peg.fastbridge.FastBridgeFederationInformation in project rskj by rsksmart.
the class BridgeSerializationUtilsTest method deserializeFastBridgeFederationInformation_no_data.
@Test
public void deserializeFastBridgeFederationInformation_no_data() {
FastBridgeFederationInformation result = BridgeSerializationUtils.deserializeFastBridgeFederationInformation(new byte[] {}, new byte[] {});
Assert.assertNull(result);
}
Aggregations