Coverage Summary for Class: BootstrapDataVerifier (co.rsk.db.importer.provider)

Class Class, % Method, % Line, %
BootstrapDataVerifier 0% (0/1) 0% (0/2) 0% (0/20)


1 /* 2  * This file is part of RskJ 3  * Copyright (C) 2019 RSK Labs Ltd. 4  * 5  * This program is free software: you can redistribute it and/or modify 6  * it under the terms of the GNU Lesser General Public License as published by 7  * the Free Software Foundation, either version 3 of the License, or 8  * (at your option) any later version. 9  * 10  * This program is distributed in the hope that it will be useful, 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13  * GNU Lesser General Public License for more details. 14  * 15  * You should have received a copy of the GNU Lesser General Public License 16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 17  */ 18  19 package co.rsk.db.importer.provider; 20  21 import co.rsk.db.importer.BootstrapImportException; 22 import co.rsk.db.importer.provider.index.data.BootstrapDataEntry; 23 import co.rsk.db.importer.provider.index.data.BootstrapDataSignature; 24 import org.bouncycastle.util.encoders.Hex; 25 import org.ethereum.crypto.signature.ECDSASignature; 26 import org.ethereum.crypto.signature.Secp256k1; 27  28 import java.math.BigInteger; 29 import java.util.Map; 30  31 public class BootstrapDataVerifier { 32  33  public int verifyEntries(Map<String, BootstrapDataEntry> selectedEntries) { 34  int verifications = 0; 35  if (selectedEntries.isEmpty()) { 36  return 0; 37  } 38  String hashToVerify = selectedEntries.values().iterator().next().getHash(); 39  byte[] dbHash = Hex.decode(hashToVerify); 40  41  for (Map.Entry<String, BootstrapDataEntry> entry : selectedEntries.entrySet()) { 42  BootstrapDataEntry bde = entry.getValue(); 43  String currentHash = bde.getHash(); 44  if (!hashToVerify.equals(currentHash)){ 45  throw new BootstrapImportException(String.format( 46  "Error trying to verify different hashes: %s vs %s", hashToVerify, currentHash)); 47  } 48  49  BootstrapDataSignature bds = bde.getSig(); 50  51  // to use the public key we need to have an extra byte according to x9.62 declaring 52  // which format is using. The current format from signer is uncompressed 53  byte[] publicKey = Hex.decode(entry.getKey()); 54  55  // 1 is for forcing to interpret the values as unsigned integers 56  BigInteger r = new BigInteger(1, Hex.decode(bds.getR())); 57  BigInteger s = new BigInteger(1, Hex.decode(bds.getS())); 58  59  ECDSASignature signature = new ECDSASignature(r, s); 60  if (Secp256k1.getInstance().verify(dbHash, signature, publicKey)) { 61  verifications++; 62  } 63  } 64  65  return verifications; 66  } 67 }