|
| 1 | +package io.iohk.ethereum.crypto |
| 2 | + |
| 3 | +import akka.util.ByteString |
| 4 | +import io.iohk.ethereum.crypto |
| 5 | +import io.iohk.ethereum.jsonrpc.JsonMethodsImplicits |
| 6 | +import io.iohk.ethereum.nodebuilder.SecureRandomBuilder |
| 7 | +import io.iohk.ethereum.utils.ByteStringUtils |
| 8 | + |
| 9 | +import scala.util.{Failure, Success, Try} |
| 10 | + |
| 11 | +object SignatureValidator extends App with SecureRandomBuilder with JsonMethodsImplicits { |
| 12 | + |
| 13 | + args match { |
| 14 | + case Array(pk, sig, msgHash) => |
| 15 | + Try { |
| 16 | + val signature = ECDSASignature.fromBytes(ByteString(decode(sig))) |
| 17 | + val msg = ByteStringUtils.string2hash(msgHash) |
| 18 | + |
| 19 | + signature.flatMap(_.publicKey(msg)) |
| 20 | + } match { |
| 21 | + case Failure(exception) => |
| 22 | + System.err.println(s"Can't recover public key from signature [$sig] and msg [$msgHash]: ERROR: ${exception.getMessage}") |
| 23 | + sys.exit(1) |
| 24 | + case Success(recoveredPk) => |
| 25 | + val publicKey = ByteStringUtils.string2hash(pk) |
| 26 | + recoveredPk match { |
| 27 | + case Some(pk) => |
| 28 | + if(pk == publicKey) { |
| 29 | + System.err.println(s"Recovered public key [${ByteStringUtils.hash2string(pk)}] is the same as given one") |
| 30 | + } else { |
| 31 | + System.err.println(s"Recovered public key [${ByteStringUtils.hash2string(pk)}] is different than given [${ByteStringUtils.hash2string(publicKey)}]") |
| 32 | + sys.exit(1) |
| 33 | + } |
| 34 | + case None => |
| 35 | + System.err.println(s"Can't recover public key from signature [$sig] and msg [$msgHash]") |
| 36 | + sys.exit(1) |
| 37 | + } |
| 38 | + } |
| 39 | + case _ => |
| 40 | + val keyPair = crypto.generateKeyPair(secureRandom) |
| 41 | + val pkStr = ByteStringUtils.hash2string(ByteString(crypto.pubKeyFromKeyPair(keyPair))) |
| 42 | + val hash = kec256(Array(1.toByte)) |
| 43 | + val hashStr = ByteStringUtils.hash2string(ByteString(hash)) |
| 44 | + val signature = ECDSASignature.sign(hash, keyPair) |
| 45 | + val sigStr = ByteStringUtils.hash2string(signature.toBytes) |
| 46 | + System.err.println(s"Bad Input. Example usage: [signature-validator publicKey signature message_hash]. Example: [signature-validator $pkStr $sigStr $hashStr]") |
| 47 | + sys.exit(1) |
| 48 | + } |
| 49 | +} |
0 commit comments