use of org.ethereum.crypto.signature.ECDSASignature in project rskj by rsksmart.
the class ECKeyTest method testEthereumSign.
@Test
public void testEthereumSign() throws IOException {
// TODO: Understand why key must be decompressed for this to work
ECKey key = ECKey.fromPrivate(privateKey).decompress();
System.out.println("Secret\t: " + ByteUtil.toHexString(key.getPrivKeyBytes()));
System.out.println("Pubkey\t: " + ByteUtil.toHexString(key.getPubKey()));
System.out.println("Data\t: " + exampleMessage);
byte[] messageHash = HashUtil.keccak256(exampleMessage.getBytes());
ECDSASignature signature = ECDSASignature.fromSignature(key.sign(messageHash));
assertEquals(r, signature.getR());
assertEquals(s, signature.getS());
assertEquals(v, signature.getV());
}
use of org.ethereum.crypto.signature.ECDSASignature in project rskj by rsksmart.
the class ProofOfWorkRule method validFallbackBlockSignature.
private static boolean validFallbackBlockSignature(Constants constants, BlockHeader header, byte[] signatureBytesRLP) {
byte[] fallbackMiningPubKeyBytes;
boolean isEvenBlockNumber = header.getNumber() % 2 == 0;
if (isEvenBlockNumber) {
fallbackMiningPubKeyBytes = constants.getFallbackMiningPubKey0();
} else {
fallbackMiningPubKeyBytes = constants.getFallbackMiningPubKey1();
}
ECKey fallbackMiningPubKey = ECKey.fromPublicOnly(fallbackMiningPubKeyBytes);
List<RLPElement> signatureRlpElements = RLP.decode2(signatureBytesRLP);
if (signatureRlpElements.size() != 1) {
return false;
}
RLPList signatureRLP = (RLPList) signatureRlpElements.get(0);
if (signatureRLP.size() != 3) {
return false;
}
byte[] v = signatureRLP.get(0).getRLPData();
byte[] r = signatureRLP.get(1).getRLPData();
byte[] s = signatureRLP.get(2).getRLPData();
if (v == null || v.length != 1) {
return false;
}
ECDSASignature signature = ECDSASignature.fromComponents(r, s, v[0]);
if (!Arrays.equals(r, signature.getR().toByteArray())) {
return false;
}
if (!Arrays.equals(s, signature.getS().toByteArray())) {
return false;
}
if (signature.getV() > 31 || signature.getV() < 27) {
return false;
}
if (signature.getS().compareTo(SECP256K1N_HALF) >= 0) {
return false;
}
ECKey pub = Secp256k1.getInstance().recoverFromSignature(signature.getV() - 27, signature, header.getHashForMergedMining(), false);
return pub.getPubKeyPoint().equals(fallbackMiningPubKey.getPubKeyPoint());
}
use of org.ethereum.crypto.signature.ECDSASignature 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;
}
Aggregations