Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
57 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/main/scala/io/iohk/ethereum/crypto/SignatureValidator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/bin/bash | ||
|
||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
cd $DIR/.. | ||
./bin/mantis -- signature-validator "$@" |