Skip to content

Commit

Permalink
Added signature validator tool
Browse files Browse the repository at this point in the history
  • Loading branch information
pslaski committed Nov 30, 2020
1 parent 6a4bb70 commit dbba322
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/main/scala/io/iohk/ethereum/App.scala
@@ -1,7 +1,7 @@
package io.iohk.ethereum

import io.iohk.ethereum.cli.CliLauncher
import io.iohk.ethereum.crypto.EcKeyGen
import io.iohk.ethereum.crypto.{EcKeyGen, SignatureValidator}
import io.iohk.ethereum.extvm.VmServerApp
import io.iohk.ethereum.faucet.Faucet
import io.iohk.ethereum.mallet.main.Mallet
Expand All @@ -19,6 +19,7 @@ object App extends Logger {
val faucet = "faucet"
val ecKeyGen = "eckeygen"
val cli = "cli"
val sigValidator = "signature-validator"

args.headOption match {
case None => Mantis.main(args)
Expand All @@ -33,12 +34,13 @@ object App extends Logger {
case Some(`mallet`) => Mallet.main(args.tail)
case Some(`faucet`) => Faucet.main(args.tail)
case Some(`ecKeyGen`) => EcKeyGen.main(args.tail)
case Some(`sigValidator`) => SignatureValidator.main(args.tail)
case Some(`cli`) => CliLauncher.main(args.tail)
case Some(unknown) =>
log.error(
s"Unrecognised launcher option, " +
s"first parameter must be $launchKeytool, $downloadBootstrap, $launchMantis, " +
s"$mallet, $faucet, $vmServer, $ecKeyGen or $cli"
s"$mallet, $faucet, $vmServer, $ecKeyGen, $sigValidator or $cli"
)
}

Expand Down
48 changes: 48 additions & 0 deletions src/main/scala/io/iohk/ethereum/crypto/SignatureValidator.scala
@@ -0,0 +1,48 @@
package io.iohk.ethereum.crypto

import akka.util.ByteString
import io.iohk.ethereum.crypto
import io.iohk.ethereum.jsonrpc.JsonMethodsImplicits
import io.iohk.ethereum.nodebuilder.SecureRandomBuilder
import io.iohk.ethereum.utils.ByteStringUtils

import scala.util.{Failure, Success, Try}

object SignatureValidator extends App with SecureRandomBuilder with JsonMethodsImplicits {

args match {
case Array(pk, sig, msgHash) =>
Try {
val signature = ECDSASignature.fromBytes(ByteString(decode(sig)))
val msg = ByteStringUtils.string2hash(msgHash)

signature.flatMap(_.publicKey(msg))
} match {
case Failure(exception) =>
println(s"Can't recover public key from signature [$sig] and msg [$msgHash]: ERROR: ${exception.getMessage}")
sys.exit(1)
case Success(recoveredPk) =>
val publicKey = ByteStringUtils.string2hash(pk)
recoveredPk match {
case Some(pk) =>
if(pk == publicKey) {
println(s"Recovered public key [${ByteStringUtils.hash2string(pk)}] is the same as given one")
} else {
println(s"Recovered public key [${ByteStringUtils.hash2string(pk)}] is different than given [${ByteStringUtils.hash2string(publicKey)}]")
sys.exit(1)
}
case None =>
println(s"Can't recover public key from signature [$sig] and msg [$msgHash]")
sys.exit(1)
}
}
case _ =>
val keyPair = crypto.generateKeyPair(secureRandom)
val pkStr = ByteStringUtils.hash2string(ByteString(crypto.pubKeyFromKeyPair(keyPair)))
val hash = kec256(Array(1.toByte))
val hashStr = ByteStringUtils.hash2string(ByteString(hash))
val signature = ECDSASignature.sign(hash, keyPair)
val sigStr = ByteStringUtils.hash2string(signature.toBytes)
println(s"Bad Input. Example usage: [signature-validator publicKey signature message_hash]. Example: [signature-validator $pkStr $sigStr $hashStr]")
}
}
5 changes: 5 additions & 0 deletions src/universal/bin/signatureValidator
@@ -0,0 +1,5 @@
#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR/..
./bin/mantis -- signature-validator "$@"

0 comments on commit dbba322

Please sign in to comment.