Skip to content

Commit daa32c8

Browse files
committed
[ETCM-411] Add key pair generation to cli
1 parent 5430caa commit daa32c8

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/main/scala/io/iohk/ethereum/cli/CliCommands.scala

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import io.iohk.ethereum.crypto._
77
import io.iohk.ethereum.domain.Address
88
import io.iohk.ethereum.utils.ByteStringUtils
99
import java.security.SecureRandom
10+
import io.iohk.ethereum.nodebuilder.SecureRandomBuilder
1011
import org.bouncycastle.util.encoders.Hex
1112

12-
object CliCommands {
13+
object CliCommands extends SecureRandomBuilder {
1314

1415
val generatePrivateKeyCommand = "generate-private-key"
16+
val generateKeyPairsCommand = "generate-key-pairs"
1517
val deriveAddressCommand = "derive-address"
1618
val generateAllocsCommand = "generate-allocs"
1719
val balanceOption = "balance"
@@ -27,6 +29,22 @@ object CliCommands {
2729
}
2830
}
2931

32+
private val GenerateKeyPairs: Command[String] =
33+
Command(name = generateKeyPairsCommand, header = "Generate key pairs private/public") {
34+
val keyNumberOpts = Opts.argument[Int]("number of keys to generate").withDefault(1)
35+
36+
keyNumberOpts.map { numOfKeys =>
37+
val keyPairs = for (_ <- 1 to numOfKeys) yield newRandomKeyPairAsStrings(secureRandom)
38+
39+
/**
40+
* The key pairs will be printed in the format:
41+
* priv-key-hex (32 bytes)
42+
* pub-key-hex (64 bytes)
43+
*/
44+
keyPairs.map { case (prv, pub) => s"$prv\n$pub\n" }.mkString("\n")
45+
}
46+
}
47+
3048
private val DeriveAddressFromPrivateKey: Command[String] =
3149
Command(name = deriveAddressCommand, header = "Derive address from private key") {
3250

@@ -69,6 +87,6 @@ object CliCommands {
6987
}
7088

7189
val api: Command[String] = Command.apply(name = "cli", header = "Mantis CLI") {
72-
Opts.subcommands(GeneratePrivateKeyCommand, DeriveAddressFromPrivateKey, GenerateAllocs)
90+
Opts.subcommands(GeneratePrivateKeyCommand, DeriveAddressFromPrivateKey, GenerateAllocs, GenerateKeyPairs)
7391
}
7492
}

src/test/scala/io/iohk/ethereum/cli/CliCommandsSpec.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ class CliCommandsSpec extends AnyFlatSpec with Matchers with EitherValues {
8080
.value shouldBe s""""alloc": {$address: { "balance": $requestedBalance }, $address3: { "balance": $requestedBalance }, $address2: { "balance": $requestedBalance }}"""
8181
}
8282

83+
behavior of generateKeyPairsCommand
84+
it should "generate one key pair when passed no args" in {
85+
val result = api.parse(Seq(generateKeyPairsCommand))
86+
result shouldBe a[Right[_, _]]
87+
val stringSplit = result.right.get.split("\\n\\n")
88+
stringSplit.length shouldEqual 1
89+
}
90+
91+
it should "generate multiple key-pair when passed correct args" in {
92+
val numOfKeys = "5"
93+
val numOfKeysAsInt = numOfKeys.toInt
94+
val result = api.parse(Seq(generateKeyPairsCommand, numOfKeys))
95+
result shouldBe a[Right[_, _]]
96+
val stringSplit = result.right.get.split("\\n\\n")
97+
stringSplit.length shouldEqual numOfKeysAsInt
98+
}
8399
}
84100

85101
object Fixture {

0 commit comments

Comments
 (0)