added utility functions to set up db, keymanager, ethereum init and simplify key tasks

This commit is contained in:
zelig 2014-06-29 18:39:13 +01:00
parent 9bd67de671
commit cf7fcadeca

View File

@ -3,6 +3,8 @@ package utils
import ( import (
"fmt" "fmt"
"github.com/ethereum/eth-go" "github.com/ethereum/eth-go"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethdb"
"github.com/ethereum/eth-go/ethlog" "github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethminer" "github.com/ethereum/eth-go/ethminer"
"github.com/ethereum/eth-go/ethpub" "github.com/ethereum/eth-go/ethpub"
@ -13,7 +15,6 @@ import (
"os" "os"
"os/signal" "os/signal"
"path" "path"
"strings"
"time" "time"
) )
@ -100,16 +101,28 @@ func InitLogging(Datadir string, LogFile string, LogLevel int, DebugFile string)
func InitConfig(ConfigFile string, Datadir string, Identifier string, EnvPrefix string) { func InitConfig(ConfigFile string, Datadir string, Identifier string, EnvPrefix string) {
InitDataDir(Datadir) InitDataDir(Datadir)
ethutil.ReadConfig(ConfigFile, Datadir, Identifier, EnvPrefix) ethutil.ReadConfig(ConfigFile, Datadir, Identifier, EnvPrefix)
ethutil.Config.Set("rpcport", "700")
} }
func exit(status int) { func exit(err error) {
status := 0
if err != nil {
logger.Errorln("Fatal: ", err)
status = 1
}
ethlog.Flush() ethlog.Flush()
os.Exit(status) os.Exit(status)
} }
func NewEthereum(UseUPnP bool, OutboundPort string, MaxPeer int) *eth.Ethereum { func NewDatabase() ethutil.Database {
ethereum, err := eth.New(eth.CapDefault, UseUPnP) db, err := ethdb.NewLDBDatabase("database")
if err != nil {
exit(err)
}
return db
}
func NewEthereum(db ethutil.Database, keyManager *ethcrypto.KeyManager, usePnp bool, OutboundPort string, MaxPeer int) *eth.Ethereum {
ethereum, err := eth.New(db, keyManager, eth.CapDefault, usePnp)
if err != nil { if err != nil {
logger.Fatalln("eth start err:", err) logger.Fatalln("eth start err:", err)
} }
@ -129,50 +142,47 @@ func StartEthereum(ethereum *eth.Ethereum, UseSeed bool) {
func ShowGenesis(ethereum *eth.Ethereum) { func ShowGenesis(ethereum *eth.Ethereum) {
logger.Infoln(ethereum.BlockChain().Genesis()) logger.Infoln(ethereum.BlockChain().Genesis())
exit(0) exit(nil)
} }
func KeyTasks(GenAddr bool, ImportKey string, ExportKey bool, NonInteractive bool) { func NewKeyManager(KeyStore string, Datadir string, db ethutil.Database) *ethcrypto.KeyManager {
var keyManager *ethcrypto.KeyManager
switch {
case KeyStore == "db":
keyManager = ethcrypto.NewDBKeyManager(db)
case KeyStore == "file":
keyManager = ethcrypto.NewFileKeyManager(Datadir)
default:
exit(fmt.Errorf("unknown keystore type: %s", KeyStore))
}
return keyManager
}
func KeyTasks(keyManager *ethcrypto.KeyManager, KeyRing string, GenAddr bool, SecretFile string, ExportDir string, NonInteractive bool) {
var err error
switch { switch {
case GenAddr: case GenAddr:
if NonInteractive || confirm("This action overwrites your old private key.") { if NonInteractive || confirm("This action overwrites your old private key.") {
CreateKeyPair(true) err = keyManager.Init(KeyRing, 0, true)
} }
exit(0) exit(err)
case len(ImportKey) > 0: case len(SecretFile) > 0:
if NonInteractive || confirm("This action overwrites your old private key.") { if NonInteractive || confirm("This action overwrites your old private key.") {
// import should be from file err = keyManager.InitFromSecretsFile(KeyRing, 0, SecretFile)
mnemonic := strings.Split(ImportKey, " ")
if len(mnemonic) == 24 {
logger.Infoln("Got mnemonic key, importing.")
key := ethutil.MnemonicDecode(mnemonic)
ImportPrivateKey(key)
} else if len(mnemonic) == 1 {
logger.Infoln("Got hex key, importing.")
ImportPrivateKey(ImportKey)
} else {
logger.Errorln("Did not recognise format, exiting.")
}
} }
exit(0) exit(err)
case ExportKey: // this should be exporting to a filename case len(ExportDir) > 0:
keyPair := ethutil.GetKeyRing().Get(0) err = keyManager.Init(KeyRing, 0, false)
fmt.Printf(` if err == nil {
Generating new address and keypair. err = keyManager.Export(ExportDir)
Please keep your keys somewhere save. }
exit(err)
++++++++++++++++ KeyRing +++++++++++++++++++
addr: %x
prvk: %x
pubk: %x
++++++++++++++++++++++++++++++++++++++++++++
save these words so you can restore your account later: %s
`, keyPair.Address(), keyPair.PrivateKey, keyPair.PublicKey)
exit(0)
default: default:
// Creates a keypair if none exists // Creates a keypair if none exists
CreateKeyPair(false) err = keyManager.Init(KeyRing, 0, false)
if err != nil {
exit(err)
}
} }
} }
@ -192,13 +202,7 @@ func StartMining(ethereum *eth.Ethereum) bool {
if !ethereum.Mining { if !ethereum.Mining {
ethereum.Mining = true ethereum.Mining = true
if ethutil.GetKeyRing().Len() == 0 { addr := ethereum.KeyManager().Address()
logger.Errorln("No address found, can't start mining")
ethereum.Mining = false
return true //????
}
keyPair := ethutil.GetKeyRing().Get(0)
addr := keyPair.Address()
go func() { go func() {
miner = ethminer.NewDefaultMiner(addr, ethereum) miner = ethminer.NewDefaultMiner(addr, ethereum)