Search in sources :

Example 6 with BootstrapDataSignature

use of co.rsk.db.importer.provider.index.data.BootstrapDataSignature in project rskj by rsksmart.

the class BootstrapIndexCandidateSelectorTest method getMaximumCommonHeightDataOneEntry.

@Test(expected = BootstrapImportException.class)
public void getMaximumCommonHeightDataOneEntry() {
    List<String> keys = Arrays.asList("key1", "key2");
    BootstrapIndexCandidateSelector indexMCH = new BootstrapIndexCandidateSelector(keys, 2);
    List<BootstrapDataIndex> indexes = new ArrayList<>();
    ArrayList<BootstrapDataEntry> entries = new ArrayList<>();
    entries.add(new BootstrapDataEntry(1, "", "dbPath", "hash", new BootstrapDataSignature("r", "s")));
    indexes.add(new BootstrapDataIndex(entries));
    indexMCH.getHeightData(indexes);
}
Also used : BootstrapDataSignature(co.rsk.db.importer.provider.index.data.BootstrapDataSignature) BootstrapDataEntry(co.rsk.db.importer.provider.index.data.BootstrapDataEntry) ArrayList(java.util.ArrayList) BootstrapDataIndex(co.rsk.db.importer.provider.index.data.BootstrapDataIndex) Test(org.junit.Test)

Example 7 with BootstrapDataSignature

use of co.rsk.db.importer.provider.index.data.BootstrapDataSignature in project rskj by rsksmart.

the class BootstrapIndexCandidateSelectorTest method getMaximumCommonHeightDataTwoEntries.

@Test
public void getMaximumCommonHeightDataTwoEntries() {
    List<String> keys = Arrays.asList("key1", "key2");
    BootstrapIndexCandidateSelector indexMCH = new BootstrapIndexCandidateSelector(keys, 2);
    List<BootstrapDataIndex> indexes = new ArrayList<>();
    ArrayList<BootstrapDataEntry> entries = new ArrayList<>();
    ArrayList<BootstrapDataEntry> entries2 = new ArrayList<>();
    entries.add(new BootstrapDataEntry(1, "", "dbPath", "hash", new BootstrapDataSignature("r", "s")));
    entries2.add(new BootstrapDataEntry(1, "", "dbPath", "hash", new BootstrapDataSignature("r", "s")));
    indexes.add(new BootstrapDataIndex(entries));
    indexes.add(new BootstrapDataIndex(entries2));
    BootstrapIndexCandidateSelector.HeightCandidate heightCandidate = indexMCH.getHeightData(indexes);
    assertEquals(1, heightCandidate.getHeight());
}
Also used : BootstrapDataSignature(co.rsk.db.importer.provider.index.data.BootstrapDataSignature) BootstrapDataEntry(co.rsk.db.importer.provider.index.data.BootstrapDataEntry) ArrayList(java.util.ArrayList) BootstrapDataIndex(co.rsk.db.importer.provider.index.data.BootstrapDataIndex) Test(org.junit.Test)

Example 8 with BootstrapDataSignature

use of co.rsk.db.importer.provider.index.data.BootstrapDataSignature in project rskj by rsksmart.

the class BootstrapIndexCandidateSelectorTest method getMaximumCommonHeightDataManyEntries.

@Test
public void getMaximumCommonHeightDataManyEntries() {
    List<String> keys = Arrays.asList("key1", "key2", "keys3");
    BootstrapIndexCandidateSelector indexMCH = new BootstrapIndexCandidateSelector(keys, 2);
    List<BootstrapDataIndex> indexes = new ArrayList<>();
    ArrayList<BootstrapDataEntry> entries = new ArrayList<>();
    ArrayList<BootstrapDataEntry> entries2 = new ArrayList<>();
    ArrayList<BootstrapDataEntry> entries3 = new ArrayList<>();
    entries.add(new BootstrapDataEntry(1, "", "dbPath", "hash", new BootstrapDataSignature("r", "s")));
    entries2.add(new BootstrapDataEntry(1, "", "dbPath", "hash", new BootstrapDataSignature("r", "s")));
    entries3.add(new BootstrapDataEntry(1, "", "dbPath", "hash", new BootstrapDataSignature("r", "s")));
    entries.add(new BootstrapDataEntry(2, "", "dbPath", "hash", new BootstrapDataSignature("r", "s")));
    entries2.add(new BootstrapDataEntry(2, "", "dbPath", "hash", new BootstrapDataSignature("r", "s")));
    entries3.add(new BootstrapDataEntry(2, "", "dbPath", "hash", new BootstrapDataSignature("r", "s")));
    indexes.add(new BootstrapDataIndex(entries));
    indexes.add(new BootstrapDataIndex(entries2));
    indexes.add(new BootstrapDataIndex(entries3));
    BootstrapIndexCandidateSelector.HeightCandidate heightCandidate = indexMCH.getHeightData(indexes);
    assertEquals(heightCandidate.getHeight(), 2L);
}
Also used : BootstrapDataSignature(co.rsk.db.importer.provider.index.data.BootstrapDataSignature) BootstrapDataEntry(co.rsk.db.importer.provider.index.data.BootstrapDataEntry) ArrayList(java.util.ArrayList) BootstrapDataIndex(co.rsk.db.importer.provider.index.data.BootstrapDataIndex) Test(org.junit.Test)

Example 9 with BootstrapDataSignature

use of co.rsk.db.importer.provider.index.data.BootstrapDataSignature in project rskj by rsksmart.

the class BootstrapDataVerifier method verifyEntries.

public int verifyEntries(Map<String, BootstrapDataEntry> selectedEntries) {
    int verifications = 0;
    if (selectedEntries.isEmpty()) {
        return 0;
    }
    String hashToVerify = selectedEntries.values().iterator().next().getHash();
    byte[] dbHash = Hex.decode(hashToVerify);
    for (Map.Entry<String, BootstrapDataEntry> entry : selectedEntries.entrySet()) {
        BootstrapDataEntry bde = entry.getValue();
        String currentHash = bde.getHash();
        if (!hashToVerify.equals(currentHash)) {
            throw new BootstrapImportException(String.format("Error trying to verify different hashes: %s vs %s", hashToVerify, currentHash));
        }
        BootstrapDataSignature bds = bde.getSig();
        // to use the public key we need to have an extra byte according to x9.62 declaring
        // which format is using. The current format from signer is uncompressed
        byte[] publicKey = Hex.decode(entry.getKey());
        // 1 is for forcing to interpret the values as unsigned integers
        BigInteger r = new BigInteger(1, Hex.decode(bds.getR()));
        BigInteger s = new BigInteger(1, Hex.decode(bds.getS()));
        ECDSASignature signature = new ECDSASignature(r, s);
        if (Secp256k1.getInstance().verify(dbHash, signature, publicKey)) {
            verifications++;
        }
    }
    return verifications;
}
Also used : BootstrapDataSignature(co.rsk.db.importer.provider.index.data.BootstrapDataSignature) BootstrapDataEntry(co.rsk.db.importer.provider.index.data.BootstrapDataEntry) BootstrapImportException(co.rsk.db.importer.BootstrapImportException) BigInteger(java.math.BigInteger) ECDSASignature(org.ethereum.crypto.signature.ECDSASignature) Map(java.util.Map)

Aggregations

BootstrapDataEntry (co.rsk.db.importer.provider.index.data.BootstrapDataEntry)9 BootstrapDataSignature (co.rsk.db.importer.provider.index.data.BootstrapDataSignature)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 BootstrapDataIndex (co.rsk.db.importer.provider.index.data.BootstrapDataIndex)6 HashMap (java.util.HashMap)2 BootstrapImportException (co.rsk.db.importer.BootstrapImportException)1 BootstrapURLProvider (co.rsk.db.importer.BootstrapURLProvider)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BigInteger (java.math.BigInteger)1 URL (java.net.URL)1 Map (java.util.Map)1 ECDSASignature (org.ethereum.crypto.signature.ECDSASignature)1