@@ -5,6 +5,7 @@ import com.monovore.decline.{Command, Opts}
5
5
import io .iohk .ethereum .crypto
6
6
import io .iohk .ethereum .crypto ._
7
7
import io .iohk .ethereum .domain .Address
8
+ import io .iohk .ethereum .keystore .{EncryptedKey , EncryptedKeyJsonCodec }
8
9
import io .iohk .ethereum .utils .ByteStringUtils
9
10
import java .security .SecureRandom
10
11
import io .iohk .ethereum .security .SecureRandomBuilder
@@ -16,9 +17,16 @@ object CliCommands extends SecureRandomBuilder {
16
17
val generateKeyPairsCommand = " generate-key-pairs"
17
18
val deriveAddressCommand = " derive-address"
18
19
val generateAllocsCommand = " generate-allocs"
20
+ val encryptKeyCommand = " encrypt-key"
21
+
19
22
val balanceOption = " balance"
20
23
val keyOption = " key"
21
24
val addressOption = " address"
25
+ val passphraseOption = " passphrase"
26
+
27
+ val privateKeyArgument = " private-key"
28
+ private val privateKeyOpt = Opts
29
+ .argument[String ](privateKeyArgument)
22
30
23
31
private val GeneratePrivateKeyCommand : Command [String ] =
24
32
Command (name = generatePrivateKeyCommand, header = " Generate private key" ) {
@@ -47,11 +55,7 @@ object CliCommands extends SecureRandomBuilder {
47
55
48
56
private val DeriveAddressFromPrivateKey : Command [String ] =
49
57
Command (name = deriveAddressCommand, header = " Derive address from private key" ) {
50
-
51
- Opts
52
- .argument[String ](" private-key" )
53
- .map(Hex .decode)
54
- .map(privKeyToAddress)
58
+ privateKeyOpt.map(Hex .decode).map(privKeyToAddress)
55
59
}
56
60
57
61
private val GenerateAllocs : Command [String ] =
@@ -74,6 +78,21 @@ object CliCommands extends SecureRandomBuilder {
74
78
}
75
79
}
76
80
81
+ private val EncryptKey : Command [String ] =
82
+ Command (name = encryptKeyCommand, header = " Encrypt private key" ) {
83
+
84
+ val privateKey = privateKeyOpt.map(ByteStringUtils .string2hash)
85
+
86
+ val passphrase = Opts
87
+ .option[String ](long = passphraseOption, short = " p" , help = " Passphrase" )
88
+ .withDefault(" " )
89
+
90
+ (privateKey, passphrase).mapN { (privateKey, passphrase) =>
91
+ val encKey = EncryptedKey (privateKey, passphrase, secureRandom)
92
+ EncryptedKeyJsonCodec .toJson(encKey)
93
+ }
94
+ }
95
+
77
96
private def allocs (addresses : List [String ], balance : BigInt ): String =
78
97
s """ "alloc": ${addresses
79
98
.map(address => s """ $address: { "balance": $balance } """ )
@@ -87,6 +106,12 @@ object CliCommands extends SecureRandomBuilder {
87
106
}
88
107
89
108
val api : Command [String ] = Command .apply(name = " cli" , header = " Mantis CLI" ) {
90
- Opts .subcommands(GeneratePrivateKeyCommand , DeriveAddressFromPrivateKey , GenerateAllocs , GenerateKeyPairs )
109
+ Opts .subcommands(
110
+ GeneratePrivateKeyCommand ,
111
+ DeriveAddressFromPrivateKey ,
112
+ GenerateAllocs ,
113
+ GenerateKeyPairs ,
114
+ EncryptKey
115
+ )
91
116
}
92
117
}
0 commit comments