Skip to content

Commit c75dc4b

Browse files
authored
Merge pull request #824 from input-output-hk/million-dollars-tool-felix-pays
Add signature validation tool + fix checkpointing padding.
2 parents 7031c7f + 81efecd commit c75dc4b

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
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

src/main/scala/io/iohk/ethereum/crypto/ECDSASignature.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ case class ECDSASignature(r: BigInt, s: BigInt, v: Byte) {
174174
import ECDSASignature.RLength
175175

176176
def bigInt2Bytes(b: BigInt) =
177-
ByteString(ByteUtils.bigIntToBytes(b, RLength))
177+
ByteUtils.padLeft(ByteString(b.toByteArray).takeRight(RLength), RLength, 0)
178178

179179
bigInt2Bytes(r) ++ bigInt2Bytes(s) :+ v
180180
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.security.SecureRandomBuilder
7+
import io.iohk.ethereum.utils.ByteStringUtils
8+
9+
import scala.util.{Failure, Success, Try}
10+
11+
// scalastyle:off regex
12+
object SignatureValidator extends App with SecureRandomBuilder with JsonMethodsImplicits {
13+
14+
args match {
15+
case Array(pk, sig, msgHash) =>
16+
Try {
17+
val signature = ECDSASignature.fromBytes(ByteStringUtils.string2hash(sig))
18+
val msg = ByteStringUtils.string2hash(msgHash)
19+
20+
signature.flatMap(_.publicKey(msg))
21+
} match {
22+
case Failure(exception) =>
23+
System.err.println(
24+
s"Can't recover public key from signature [$sig] and msg [$msgHash]: ERROR: ${exception.getMessage}"
25+
)
26+
sys.exit(1)
27+
case Success(recoveredPk) =>
28+
val publicKey = ByteStringUtils.string2hash(pk)
29+
recoveredPk match {
30+
case Some(pk) =>
31+
if (pk == publicKey) {
32+
System.err.println(
33+
s"Recovered public key [${ByteStringUtils.hash2string(pk)}] is the same as given one"
34+
)
35+
} else {
36+
System.err.println(s"Recovered public key [${ByteStringUtils
37+
.hash2string(pk)}] is different than given [${ByteStringUtils.hash2string(publicKey)}]")
38+
sys.exit(1)
39+
}
40+
case None =>
41+
System.err.println(s"Can't recover public key from signature [$sig] and msg [$msgHash]")
42+
sys.exit(1)
43+
}
44+
}
45+
case _ =>
46+
val keyPair = crypto.generateKeyPair(secureRandom)
47+
val pkStr = ByteStringUtils.hash2string(ByteString(crypto.pubKeyFromKeyPair(keyPair)))
48+
val hash = kec256(Array(1.toByte))
49+
val hashStr = ByteStringUtils.hash2string(ByteString(hash))
50+
val signature = ECDSASignature.sign(hash, keyPair)
51+
val sigStr = ByteStringUtils.hash2string(signature.toBytes)
52+
System.err.println(
53+
s"Bad Input. Example usage: [signature-validator publicKey signature message_hash]. Example: [signature-validator $pkStr $sigStr $hashStr]"
54+
)
55+
sys.exit(1)
56+
}
57+
}

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)