Skip to content

Commit f4fa32b

Browse files
committed
Added signature validator tool
1 parent 6a4bb70 commit f4fa32b

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/main/scala/io/iohk/ethereum/App.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.iohk.ethereum
22

33
import io.iohk.ethereum.cli.CliLauncher
4-
import io.iohk.ethereum.crypto.EcKeyGen
4+
import io.iohk.ethereum.crypto.{EcKeyGen, SignatureValidator}
55
import io.iohk.ethereum.extvm.VmServerApp
66
import io.iohk.ethereum.faucet.Faucet
77
import io.iohk.ethereum.mallet.main.Mallet
@@ -19,6 +19,7 @@ object App extends Logger {
1919
val faucet = "faucet"
2020
val ecKeyGen = "eckeygen"
2121
val cli = "cli"
22+
val sigValidator = "signature-validator"
2223

2324
args.headOption match {
2425
case None => Mantis.main(args)
@@ -33,12 +34,13 @@ object App extends Logger {
3334
case Some(`mallet`) => Mallet.main(args.tail)
3435
case Some(`faucet`) => Faucet.main(args.tail)
3536
case Some(`ecKeyGen`) => EcKeyGen.main(args.tail)
37+
case Some(`sigValidator`) => SignatureValidator.main(args.tail)
3638
case Some(`cli`) => CliLauncher.main(args.tail)
3739
case Some(unknown) =>
3840
log.error(
3941
s"Unrecognised launcher option, " +
4042
s"first parameter must be $launchKeytool, $downloadBootstrap, $launchMantis, " +
41-
s"$mallet, $faucet, $vmServer, $ecKeyGen or $cli"
43+
s"$mallet, $faucet, $vmServer, $ecKeyGen, $sigValidator or $cli"
4244
)
4345
}
4446

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

src/universal/bin/signatureValidator

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4+
cd $DIR/..
5+
./bin/mantis -- signature-validator "$@"

0 commit comments

Comments
 (0)