Skip to content

Commit 82dba84

Browse files
committed
Merge remote-tracking branch 'origin/feature/eckeygen' into phase/iele_testnet
2 parents 022e261 + 5cb2602 commit 82dba84

File tree

8 files changed

+76
-40
lines changed

8 files changed

+76
-40
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ project/boot/
55
project/plugins/project/
66
.ensime
77
.ensime_cache/
8-
nodeId.keys
98

109
# intellij scala worksheet
1110
*.sc

src/main/resources/application.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mantis {
99
datadir = ${user.home}"/.mantis"
1010

1111
# The unencrypted private key of this node
12-
node-key-file = ${mantis.datadir}"/nodeId.keys"
12+
node-key-file = ${mantis.datadir}"/node.key"
1313

1414
# Keystore directory: stores encrypted private keys of accounts managed by this node
1515
keystore-dir = ${mantis.datadir}"/keystore"

src/main/scala/io/iohk/ethereum/App.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.iohk.ethereum
22

3+
import io.iohk.ethereum.crypto.EcKeyGen
34
import io.iohk.ethereum.extvm.VmServerApp
45
import io.iohk.ethereum.faucet.Faucet
56
import io.iohk.ethereum.mallet.main.Mallet
@@ -16,6 +17,7 @@ object App extends Logger {
1617
val vmServer = "vm-server"
1718
val mallet = "mallet"
1819
val faucet = "faucet"
20+
val ecKeyGen = "eckeygen"
1921

2022
args.headOption match {
2123
case None => Mantis.main(args)
@@ -25,9 +27,11 @@ object App extends Logger {
2527
case Some(`vmServer`) => VmServerApp.main(args.tail)
2628
case Some(`mallet`) => Mallet.main(args.tail)
2729
case Some(`faucet`) => Faucet.main(args.tail)
30+
case Some(`ecKeyGen`) => EcKeyGen.main(args.tail)
2831
case Some(unknown) =>
2932
log.error(s"Unrecognised launcher option, " +
30-
s"first parameter must be $launchKeytool, $downloadBootstrap, $launchMantis, $mallet, $faucet or $vmServer")
33+
s"first parameter must be $launchKeytool, $downloadBootstrap, $launchMantis, " +
34+
s"$mallet, $faucet, $vmServer or $ecKeyGen")
3135
}
3236

3337

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.iohk.ethereum.crypto
2+
3+
import io.iohk.ethereum.nodebuilder.SecureRandomBuilder
4+
5+
/**
6+
* A simple tool to generate and ECDSA key pair. The key pair will be printed in the format:
7+
* priv-key-hex (32 bytes)
8+
* pub-key-hex (64 bytes)
9+
*
10+
* Run:
11+
* ./eckeygen > mantis-datadir/node.key
12+
*
13+
* to generate the private key for the node. Note that only the private key will be read upon Mantis boot,
14+
* and the second line is equivalent to node ID.
15+
* The tool can also be used to generate keys for an Ethereum account.
16+
*/
17+
object EcKeyGen extends App with SecureRandomBuilder {
18+
val (prv, pub) = newRandomKeyPairAsStrings(secureRandom)
19+
//scalastyle:off
20+
println(prv + "\n" + pub)
21+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import org.spongycastle.crypto.AsymmetricCipherKeyPair
1111
import org.spongycastle.crypto.digests.{RIPEMD160Digest, SHA256Digest}
1212
import org.spongycastle.crypto.generators.{ECKeyPairGenerator, PKCS5S2ParametersGenerator, SCrypt}
1313
import org.spongycastle.crypto.params._
14+
import org.spongycastle.util.encoders.Hex
1415

1516
package object crypto {
1617

@@ -82,6 +83,12 @@ package object crypto {
8283
def pubKeyFromPrvKey(prvKey: ByteString): ByteString =
8384
ByteString(pubKeyFromPrvKey(prvKey.toArray))
8485

86+
def newRandomKeyPairAsStrings(secureRandom: SecureRandom = new SecureRandom): (String, String) = {
87+
val keyPair = generateKeyPair(secureRandom)
88+
val (prv, pub) = keyPairToByteArrays(keyPair)
89+
(Hex.toHexString(prv), Hex.toHexString(pub))
90+
}
91+
8592
def ripemd160(input: Array[Byte]): Array[Byte] = {
8693
val digest = new RIPEMD160Digest
8794
digest.update(input, 0, input.length)

src/main/scala/io/iohk/ethereum/network/package.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ package object network {
3434
val keysValuePair = generateKeyPair(secureRandom)
3535

3636
//Write keys to file
37-
val (priv, _) = keyPairToByteArrays(keysValuePair)
37+
val (priv, pub) = keyPairToByteArrays(keysValuePair)
3838
require(file.getParentFile.exists() || file.getParentFile.mkdirs(), "Key's file parent directory creation failed")
3939
val writer = new PrintWriter(filePath)
4040
try {
41-
writer.write(Hex.toHexString(priv))
41+
writer.write(Hex.toHexString(priv) + "\n" + Hex.toHexString(pub))
4242
} finally {
4343
writer.close()
4444
}
@@ -47,7 +47,7 @@ package object network {
4747
} else {
4848
val reader = Source.fromFile(filePath)
4949
try {
50-
val privHex = reader.mkString
50+
val privHex = reader.getLines().next()
5151
keyPairFromPrvKey(Hex.decode(privHex))
5252
} finally {
5353
reader.close()

src/universal/bin/eckeygen

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 -- eckeygen "$@"

src/universal/conf/storage.conf

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
mantis {
2-
3-
# Base directory where all the data used by the node is stored, including blockchain data and private keys
4-
# datadir = ${user.home}"/.mantis"
5-
6-
# The unencrypted private key of this node
7-
# node-key-file = ${mantis.datadir}"/nodeId.key"
8-
9-
# Keystore directory: stores encrypted private keys of accounts managed by this node
10-
# keystore-dir = ${mantis.datadir}"/keystore"
11-
12-
db {
13-
leveldb {
14-
# LevelDB data directory
15-
# path = ${mantis.datadir}"/leveldb/"
16-
}
17-
}
18-
19-
pruning {
20-
# Pruning mode that the application will use.
21-
#
22-
# - archive: No pruning is performed
23-
# - basic: reference count based pruning
24-
#
25-
# After changing, please delete previous db before starting the client:
26-
#
27-
# mode = "basic"
28-
29-
# The amount of block history kept before pruning
30-
# Note: if fast-sync clients choose target block offset greater than this value, mantis may not be able to
31-
# correctly act as a fast-sync server
32-
# history = 1000
33-
}
34-
}
1+
mantis {
2+
3+
# Base directory where all the data used by the node is stored, including blockchain data and private keys
4+
# datadir = ${user.home}"/.mantis"
5+
6+
# The unencrypted private key of this node
7+
# node-key-file = ${mantis.datadir}"/node.key"
8+
9+
# Keystore directory: stores encrypted private keys of accounts managed by this node
10+
# keystore-dir = ${mantis.datadir}"/keystore"
11+
12+
db {
13+
leveldb {
14+
# LevelDB data directory
15+
# path = ${mantis.datadir}"/leveldb/"
16+
}
17+
}
18+
19+
pruning {
20+
# Pruning mode that the application will use.
21+
#
22+
# - archive: No pruning is performed
23+
# - basic: reference count based pruning
24+
#
25+
# After changing, please delete previous db before starting the client:
26+
#
27+
# mode = "basic"
28+
29+
# The amount of block history kept before pruning
30+
# Note: if fast-sync clients choose target block offset greater than this value, mantis may not be able to
31+
# correctly act as a fast-sync server
32+
# history = 1000
33+
}
34+
}

0 commit comments

Comments
 (0)