Search in sources :

Example 16 with Sha256Hash

use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.

the class BridgeUtilsTest method getRegularPegoutTxSize_has_proper_calculations.

@Test
public void getRegularPegoutTxSize_has_proper_calculations() {
    BtcECKey key1 = new BtcECKey();
    BtcECKey key2 = new BtcECKey();
    BtcECKey key3 = new BtcECKey();
    List<BtcECKey> keys = Arrays.asList(key1, key2, key3);
    Federation fed = new Federation(FederationMember.getFederationMembersFromKeys(keys), Instant.now(), 0, networkParameters);
    // Create a pegout tx with two inputs and two outputs
    int inputs = 2;
    BtcTransaction pegoutTx = createPegOutTx(Collections.emptyList(), inputs, fed, false);
    for (int inputIndex = 0; inputIndex < inputs; inputIndex++) {
        Script inputScript = pegoutTx.getInput(inputIndex).getScriptSig();
        Sha256Hash sighash = pegoutTx.hashForSignature(inputIndex, fed.getRedeemScript(), BtcTransaction.SigHash.ALL, false);
        for (int keyIndex = 0; keyIndex < keys.size() - 1; keyIndex++) {
            BtcECKey key = keys.get(keyIndex);
            BtcECKey.ECDSASignature sig = key.sign(sighash);
            TransactionSignature txSig = new TransactionSignature(sig, BtcTransaction.SigHash.ALL, false);
            byte[] txSigEncoded = txSig.encodeToBitcoin();
            int sigIndex = inputScript.getSigInsertionIndex(sighash, key);
            inputScript = ScriptBuilder.updateScriptWithSignature(inputScript, txSigEncoded, sigIndex, 1, 1);
            pegoutTx.getInput(inputIndex).setScriptSig(inputScript);
        }
    }
    int pegoutTxSize = BridgeUtils.getRegularPegoutTxSize(fed);
    // The difference between the calculated size and a real tx size should be smaller than 10% in any direction
    int difference = pegoutTx.bitcoinSerialize().length - pegoutTxSize;
    double tolerance = pegoutTxSize * .1;
    assertTrue(difference < tolerance && difference > -tolerance);
}
Also used : Script(co.rsk.bitcoinj.script.Script) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) TransactionSignature(co.rsk.bitcoinj.crypto.TransactionSignature) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 17 with Sha256Hash

use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.

the class BridgeUtilsTest method isInputSignedByThisFederator_notSigned.

@Test
public void isInputSignedByThisFederator_notSigned() {
    // Arrange
    BtcECKey federator1Key = new BtcECKey();
    BtcECKey federator2Key = new BtcECKey();
    Federation federation = new Federation(FederationMember.getFederationMembersFromKeys(Arrays.asList(federator1Key, federator2Key)), Instant.now(), 0, networkParameters);
    // Create a tx from the Fed to a random btc address
    BtcTransaction tx = new BtcTransaction(networkParameters);
    TransactionInput txInput = new TransactionInput(networkParameters, tx, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    // Create script to be signed by federation members
    Script inputScript = PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation);
    txInput.setScriptSig(inputScript);
    tx.addInput(txInput);
    List<ScriptChunk> chunks = inputScript.getChunks();
    byte[] program = chunks.get(chunks.size() - 1).data;
    Script redeemScript = new Script(program);
    Sha256Hash sighash = tx.hashForSignature(0, redeemScript, BtcTransaction.SigHash.ALL, false);
    // Act
    boolean isSigned = BridgeUtils.isInputSignedByThisFederator(federator1Key, sighash, txInput);
    // Assert
    Assert.assertFalse(isSigned);
}
Also used : Script(co.rsk.bitcoinj.script.Script) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) ScriptChunk(co.rsk.bitcoinj.script.ScriptChunk) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 18 with Sha256Hash

use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.

the class BridgeUtilsTest method calculateMerkleRoot_hashes_in_pmt.

@Test
public void calculateMerkleRoot_hashes_in_pmt() {
    BtcTransaction tx = new BtcTransaction(networkParameters);
    byte[] bits = new byte[1];
    bits[0] = 0x5;
    List<Sha256Hash> hashes = new ArrayList<>();
    hashes.add(Sha256Hash.ZERO_HASH);
    hashes.add(tx.getHash());
    PartialMerkleTree pmt = new PartialMerkleTree(networkParameters, bits, hashes, 2);
    Sha256Hash merkleRoot = BridgeUtils.calculateMerkleRoot(networkParameters, pmt.bitcoinSerialize(), tx.getHash());
    Assert.assertNotNull(merkleRoot);
}
Also used : BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) ArrayList(java.util.ArrayList) PartialMerkleTree(co.rsk.bitcoinj.core.PartialMerkleTree) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 19 with Sha256Hash

use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.

the class BridgeUtilsTest method calculateMerkleRoot_hashes_not_in_pmt.

@Test
public void calculateMerkleRoot_hashes_not_in_pmt() {
    byte[] bits = new byte[1];
    bits[0] = 0x01;
    List<Sha256Hash> hashes = new ArrayList<>();
    hashes.add(PegTestUtils.createHash(2));
    BtcTransaction tx = new BtcTransaction(networkParameters);
    PartialMerkleTree pmt = new PartialMerkleTree(networkParameters, bits, hashes, 1);
    Assert.assertNull(BridgeUtils.calculateMerkleRoot(networkParameters, pmt.bitcoinSerialize(), tx.getHash()));
}
Also used : Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) ArrayList(java.util.ArrayList) PartialMerkleTree(co.rsk.bitcoinj.core.PartialMerkleTree) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 20 with Sha256Hash

use of co.rsk.bitcoinj.core.Sha256Hash in project rskj by rsksmart.

the class GenesisMerkleProofValidator method isValid.

@Override
public boolean isValid(Sha256Hash expectedRoot, Sha256Hash coinbaseHash) {
    PartialMerkleTree merkleTree = new PartialMerkleTree(bitcoinNetworkParameters, pmtSerialized, 0);
    List<Sha256Hash> txHashes = new ArrayList<>();
    Sha256Hash root = merkleTree.getTxnHashAndMerkleRoot(txHashes);
    return root.equals(expectedRoot) && txHashes.contains(coinbaseHash);
}
Also used : Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) PartialMerkleTree(co.rsk.bitcoinj.core.PartialMerkleTree) ArrayList(java.util.ArrayList)

Aggregations

Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)40 Test (org.junit.Test)17 StoredBlock (co.rsk.bitcoinj.core.StoredBlock)15 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)11 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)9 Script (co.rsk.bitcoinj.script.Script)7 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)7 Repository (org.ethereum.core.Repository)7 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)6 TransactionOutPoint (co.rsk.bitcoinj.core.TransactionOutPoint)6 ArrayList (java.util.ArrayList)6 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)6 IOException (java.io.IOException)5 PartialMerkleTree (co.rsk.bitcoinj.core.PartialMerkleTree)4 TransactionInput (co.rsk.bitcoinj.core.TransactionInput)4 TransactionSignature (co.rsk.bitcoinj.crypto.TransactionSignature)4 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)4 BigInteger (java.math.BigInteger)4 MutableRepository (org.ethereum.db.MutableRepository)4 VerificationException (co.rsk.bitcoinj.core.VerificationException)3