Search in sources :

Example 66 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class BridgeState method create.

public static BridgeState create(BridgeConstants bridgeConstants, byte[] data) throws IOException {
    RLPList rlpList = (RLPList) RLP.decode2(data).get(0);
    byte[] btcBlockchainBestChainHeightBytes = rlpList.get(0).getRLPData();
    int btcBlockchainBestChainHeight = btcBlockchainBestChainHeightBytes == null ? 0 : (new BigInteger(1, btcBlockchainBestChainHeightBytes)).intValue();
    byte[] btcTxHashesAlreadyProcessedBytes = rlpList.get(1).getRLPData();
    Map<Sha256Hash, Long> btcTxHashesAlreadyProcessed = BridgeSerializationUtils.deserializeMapOfHashesToLong(btcTxHashesAlreadyProcessedBytes);
    byte[] btcUTXOsBytes = rlpList.get(2).getRLPData();
    List<UTXO> btcUTXOs = BridgeSerializationUtils.deserializeUTXOList(btcUTXOsBytes);
    byte[] rskTxsWaitingForSignaturesBytes = rlpList.get(3).getRLPData();
    SortedMap<Keccak256, BtcTransaction> rskTxsWaitingForSignatures = BridgeSerializationUtils.deserializeMap(rskTxsWaitingForSignaturesBytes, bridgeConstants.getBtcParams(), false);
    byte[] releaseRequestQueueBytes = rlpList.get(4).getRLPData();
    ReleaseRequestQueue releaseRequestQueue = BridgeSerializationUtils.deserializeReleaseRequestQueue(releaseRequestQueueBytes, bridgeConstants.getBtcParams());
    byte[] releaseTransactionSetBytes = rlpList.get(5).getRLPData();
    ReleaseTransactionSet releaseTransactionSet = BridgeSerializationUtils.deserializeReleaseTransactionSet(releaseTransactionSetBytes, bridgeConstants.getBtcParams());
    return new BridgeState(btcBlockchainBestChainHeight, btcTxHashesAlreadyProcessed, btcUTXOs, rskTxsWaitingForSignatures, releaseRequestQueue, releaseTransactionSet);
}
Also used : Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) Keccak256(co.rsk.crypto.Keccak256) RLPList(org.ethereum.util.RLPList) UTXO(co.rsk.bitcoinj.core.UTXO) BigInteger(java.math.BigInteger)

Example 67 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class BridgeSupport method processSigning.

private void processSigning(long executionBlockNumber, BtcECKey federatorPublicKey, List<byte[]> signatures, byte[] rskTxHash, BtcTransaction btcTx) throws IOException {
    // Build input hashes for signatures
    int numInputs = btcTx.getInputs().size();
    List<Sha256Hash> sighashes = new ArrayList<>();
    List<TransactionSignature> txSigs = new ArrayList<>();
    for (int i = 0; i < numInputs; i++) {
        TransactionInput txIn = btcTx.getInput(i);
        Script inputScript = txIn.getScriptSig();
        List<ScriptChunk> chunks = inputScript.getChunks();
        byte[] program = chunks.get(chunks.size() - 1).data;
        Script redeemScript = new Script(program);
        sighashes.add(btcTx.hashForSignature(i, redeemScript, BtcTransaction.SigHash.ALL, false));
    }
    // Verify given signatures are correct before proceeding
    for (int i = 0; i < numInputs; i++) {
        BtcECKey.ECDSASignature sig;
        try {
            sig = BtcECKey.ECDSASignature.decodeFromDER(signatures.get(i));
        } catch (RuntimeException e) {
            logger.warn("Malformed signature for input {} of tx {}: {}", i, new Keccak256(rskTxHash), Hex.toHexString(signatures.get(i)));
            return;
        }
        Sha256Hash sighash = sighashes.get(i);
        if (!federatorPublicKey.verify(sighash, sig)) {
            logger.warn("Signature {} {} is not valid for hash {} and public key {}", i, Hex.toHexString(sig.encodeToDER()), sighash, federatorPublicKey);
            return;
        }
        TransactionSignature txSig = new TransactionSignature(sig, BtcTransaction.SigHash.ALL, false);
        txSigs.add(txSig);
        if (!txSig.isCanonical()) {
            logger.warn("Signature {} {} is not canonical.", i, Hex.toHexString(signatures.get(i)));
            return;
        }
    }
    // All signatures are correct. Proceed to signing
    for (int i = 0; i < numInputs; i++) {
        Sha256Hash sighash = sighashes.get(i);
        TransactionInput input = btcTx.getInput(i);
        Script inputScript = input.getScriptSig();
        boolean alreadySignedByThisFederator = isInputSignedByThisFederator(federatorPublicKey, sighash, input);
        // Sign the input if it wasn't already
        if (!alreadySignedByThisFederator) {
            try {
                int sigIndex = inputScript.getSigInsertionIndex(sighash, federatorPublicKey);
                inputScript = ScriptBuilder.updateScriptWithSignature(inputScript, txSigs.get(i).encodeToBitcoin(), sigIndex, 1, 1);
                input.setScriptSig(inputScript);
                logger.debug("Tx input {} for tx {} signed.", i, new Keccak256(rskTxHash));
            } catch (IllegalStateException e) {
                Federation retiringFederation = getRetiringFederation();
                if (getActiveFederation().hasPublicKey(federatorPublicKey)) {
                    logger.debug("A member of the active federation is trying to sign a tx of the retiring one");
                    return;
                } else if (retiringFederation != null && retiringFederation.hasPublicKey(federatorPublicKey)) {
                    logger.debug("A member of the retiring federation is trying to sign a tx of the active one");
                    return;
                }
                throw e;
            }
        } else {
            logger.warn("Input {} of tx {} already signed by this federator.", i, new Keccak256(rskTxHash));
            break;
        }
    }
    // If tx fully signed
    if (hasEnoughSignatures(btcTx)) {
        logger.info("Tx fully signed {}. Hex: {}", btcTx, Hex.toHexString(btcTx.bitcoinSerialize()));
        provider.getRskTxsWaitingForSignatures().remove(new Keccak256(rskTxHash));
        eventLogger.logReleaseBtc(btcTx);
    } else {
        logger.debug("Tx not yet fully signed {}.", new Keccak256(rskTxHash));
    }
}
Also used : Script(co.rsk.bitcoinj.script.Script) TransactionSignature(co.rsk.bitcoinj.crypto.TransactionSignature) Keccak256(co.rsk.crypto.Keccak256) ScriptChunk(co.rsk.bitcoinj.script.ScriptChunk)

Example 68 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class BlockCacheTest method addAndRetrieveBlock.

@Test
public void addAndRetrieveBlock() {
    BlockCache store = getSubject();
    Block block = Mockito.mock(Block.class);
    when(block.getHash()).thenReturn(new Keccak256(HASH_1));
    store.addBlock(block);
    assertThat(store.getBlockByHash(HASH_1), is(block));
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 69 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class BlockCacheTest method putMoreThanSizeAndCheckCleanup.

@Test
public void putMoreThanSizeAndCheckCleanup() {
    BlockCache store = getSubject();
    store.put(new Keccak256(HASH_1), Mockito.mock(Block.class));
    store.put(new Keccak256(HASH_2), Mockito.mock(Block.class));
    store.put(new Keccak256(HASH_3), Mockito.mock(Block.class));
    store.put(new Keccak256(HASH_4), Mockito.mock(Block.class));
    store.put(new Keccak256(HASH_5), Mockito.mock(Block.class));
    assertThat(store.getBlockByHash(HASH_1), nullValue());
    assertThat(store.getBlockByHash(HASH_2), notNullValue());
    assertThat(store.getBlockByHash(HASH_3), notNullValue());
    assertThat(store.getBlockByHash(HASH_4), notNullValue());
    assertThat(store.getBlockByHash(HASH_5), notNullValue());
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 70 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class BlockCacheTest method addAndRemoveBlock.

@Test
public void addAndRemoveBlock() {
    BlockCache store = getSubject();
    Block block = Mockito.mock(Block.class);
    when(block.getHash()).thenReturn(new Keccak256(HASH_1));
    store.addBlock(block);
    store.removeBlock(block);
    assertThat(store.getBlockByHash(HASH_1), nullValue());
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Aggregations

Keccak256 (co.rsk.crypto.Keccak256)102 Test (org.junit.Test)53 Block (org.ethereum.core.Block)40 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)15 BigInteger (java.math.BigInteger)14 RskSystemProperties (co.rsk.config.RskSystemProperties)8 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)8 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)8 HashMapDB (org.ethereum.datasource.HashMapDB)8 RepositoryImpl (co.rsk.db.RepositoryImpl)7 ArrayList (java.util.ArrayList)7 Blockchain (org.ethereum.core.Blockchain)7 BlockStore (org.ethereum.db.BlockStore)7 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)7 RLPList (org.ethereum.util.RLPList)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Script (co.rsk.bitcoinj.script.Script)5 Coin (co.rsk.core.Coin)5 IOException (java.io.IOException)5 BlockHeader (org.ethereum.core.BlockHeader)5