From d57b7e8d5fd59007e7fc42132de5cc9459a2bc71 Mon Sep 17 00:00:00 2001
From: obscuren <geffobscura@gmail.com>
Date: Thu, 26 Jun 2014 23:25:17 +0200
Subject: [PATCH 01/14] Fix for installer

---
 ethereal/main.go | 2 +-
 install.sh       | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/ethereal/main.go b/ethereal/main.go
index 1674b59d9..316f2285f 100644
--- a/ethereal/main.go
+++ b/ethereal/main.go
@@ -10,7 +10,7 @@ import (
 )
 
 func main() {
-	// Leave QT on top at ALL times.
+	// Leave QT on top at ALL times. Qt Needs to be initialized from the main thread
 	qml.Init(nil)
 
 	runtime.GOMAXPROCS(runtime.NumCPU())
diff --git a/install.sh b/install.sh
index fe80a6956..fc4724217 100644
--- a/install.sh
+++ b/install.sh
@@ -27,7 +27,7 @@ if [ $? != 0 ]; then
 fi
 
 echo "serpent-go"
-cd $GOPATH/src/github.com/obscuren/mutan
+cd $GOPATH/src/github.com/obscuren/serpent-go
 
 echo "init submodule"
 git submodule init

From 1b74b98f90d6641b2cd18c60dc9b5829a9870d86 Mon Sep 17 00:00:00 2001
From: obscuren <geffobscura@gmail.com>
Date: Thu, 26 Jun 2014 23:31:07 +0200
Subject: [PATCH 02/14] Check if go install succeed

---
 install.sh | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/install.sh b/install.sh
index fc4724217..9719a1afc 100644
--- a/install.sh
+++ b/install.sh
@@ -49,5 +49,9 @@ else
 fi
 
 go install
+if [ $? == 0 ]; then
+	echo "go install failed"
+	exit
+fi
 
 echo "done. Please run $exe :-)"

From 2d48fc11130d5aba8ed9f37ecdf7f8b5115753cf Mon Sep 17 00:00:00 2001
From: zelig <viktor.tron@gmail.com>
Date: Sun, 29 Jun 2014 13:43:27 +0100
Subject: [PATCH 03/14] fix logmessage if data directory doesn't exist

---
 utils/cmd.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/utils/cmd.go b/utils/cmd.go
index c084542b4..99a1f2628 100644
--- a/utils/cmd.go
+++ b/utils/cmd.go
@@ -77,7 +77,7 @@ func InitDataDir(Datadir string) {
 	_, err := os.Stat(Datadir)
 	if err != nil {
 		if os.IsNotExist(err) {
-			fmt.Printf("Debug logging directory '%s' doesn't exist, creating it\n", Datadir)
+			fmt.Printf("Data directory '%s' doesn't exist, creating it\n", Datadir)
 			os.Mkdir(Datadir, 0777)
 		}
 	}

From e38b016547e98a33daf013c98c951254f95aeabf Mon Sep 17 00:00:00 2001
From: zelig <viktor.tron@gmail.com>
Date: Sun, 29 Jun 2014 18:32:48 +0100
Subject: [PATCH 04/14] changed name for ethutil hex functions

---
 ethereum/javascript_runtime.go | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/ethereum/javascript_runtime.go b/ethereum/javascript_runtime.go
index 0dfe07a54..852a50487 100644
--- a/ethereum/javascript_runtime.go
+++ b/ethereum/javascript_runtime.go
@@ -122,12 +122,12 @@ out:
 			}
 		case object := <-self.changeChan:
 			if stateObject, ok := object.Resource.(*ethchain.StateObject); ok {
-				for _, cb := range self.objectCb[ethutil.Hex(stateObject.Address())] {
+				for _, cb := range self.objectCb[ethutil.Bytes2Hex(stateObject.Address())] {
 					val, _ := self.vm.ToValue(ethpub.NewPStateObject(stateObject))
 					cb.Call(cb, val)
 				}
 			} else if storageObject, ok := object.Resource.(*ethchain.StorageState); ok {
-				for _, cb := range self.objectCb[ethutil.Hex(storageObject.StateAddress)+ethutil.Hex(storageObject.Address)] {
+				for _, cb := range self.objectCb[ethutil.Bytes2Hex(storageObject.StateAddress)+ethutil.Bytes2Hex(storageObject.Address)] {
 					val, _ := self.vm.ToValue(ethpub.NewPStorageState(storageObject))
 					cb.Call(cb, val)
 				}
@@ -178,12 +178,12 @@ func (self *JSRE) watch(call otto.FunctionCall) otto.Value {
 	if storageCallback {
 		self.objectCb[addr+storageAddr] = append(self.objectCb[addr+storageAddr], cb)
 
-		event := "storage:" + string(ethutil.FromHex(addr)) + ":" + string(ethutil.FromHex(storageAddr))
+		event := "storage:" + string(ethutil.Hex2Bytes(addr)) + ":" + string(ethutil.Hex2Bytes(storageAddr))
 		self.ethereum.Reactor().Subscribe(event, self.changeChan)
 	} else {
 		self.objectCb[addr] = append(self.objectCb[addr], cb)
 
-		event := "object:" + string(ethutil.FromHex(addr))
+		event := "object:" + string(ethutil.Hex2Bytes(addr))
 		self.ethereum.Reactor().Subscribe(event, self.changeChan)
 	}
 
@@ -221,7 +221,7 @@ func (self *JSRE) execBlock(call otto.FunctionCall) otto.Value {
 		return otto.UndefinedValue()
 	}
 
-	err = utils.BlockDo(self.ethereum, ethutil.FromHex(hash))
+	err = utils.BlockDo(self.ethereum, ethutil.Hex2Bytes(hash))
 	if err != nil {
 		fmt.Println(err)
 		return otto.FalseValue()

From 0ea9595d41c59d763237f7a3dd415f23dad34c6f Mon Sep 17 00:00:00 2001
From: zelig <viktor.tron@gmail.com>
Date: Sun, 29 Jun 2014 18:33:22 +0100
Subject: [PATCH 05/14] new command line options - keyring: keyring/session
 identifier used by key manager - keystore: choice of db/file key storage -
 import message updated - export: name of directory to export keys to (was
 bool)

---
 ethereum/flags.go | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/ethereum/flags.go b/ethereum/flags.go
index 8fb87cf34..d5a9c3a8a 100644
--- a/ethereum/flags.go
+++ b/ethereum/flags.go
@@ -10,6 +10,8 @@ import (
 )
 
 var Identifier string
+var KeyRing string
+var KeyStore string
 var StartRpc bool
 var RpcPort int
 var UseUPnP bool
@@ -19,8 +21,8 @@ var AddPeer string
 var MaxPeer int
 var GenAddr bool
 var UseSeed bool
-var ImportKey string
-var ExportKey bool
+var SecretFile string
+var ExportDir string
 var NonInteractive bool
 var Datadir string
 var LogFile string
@@ -47,6 +49,8 @@ func Init() {
 	}
 
 	flag.StringVar(&Identifier, "id", "", "Custom client identifier")
+	flag.StringVar(&KeyRing, "keyring", "", "identifier for keyring to use")
+	flag.StringVar(&KeyStore, "keystore", "db", "system to store keyrings: db|file (db)")
 	flag.StringVar(&OutboundPort, "port", "30303", "listening port")
 	flag.BoolVar(&UseUPnP, "upnp", false, "enable UPnP support")
 	flag.IntVar(&MaxPeer, "maxpeer", 10, "maximum desired peers")
@@ -55,9 +59,9 @@ func Init() {
 	flag.BoolVar(&NonInteractive, "y", false, "non-interactive mode (say yes to confirmations)")
 	flag.BoolVar(&UseSeed, "seed", true, "seed peers")
 	flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
-	flag.BoolVar(&ExportKey, "export", false, "export private key")
+	flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)")
+	flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given")
 	flag.StringVar(&LogFile, "logfile", "", "log file (defaults to standard output)")
-	flag.StringVar(&ImportKey, "import", "", "imports the given private key (hex)")
 	flag.StringVar(&Datadir, "datadir", defaultDataDir(), "specifies the datadir to use")
 	flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file")
 	flag.StringVar(&DebugFile, "debug", "", "debug file (no debugging if not set)")

From 27e1352c85e9b73fdccc83b559a104c68cc00975 Mon Sep 17 00:00:00 2001
From: zelig <viktor.tron@gmail.com>
Date: Sun, 29 Jun 2014 18:37:22 +0100
Subject: [PATCH 06/14] main loop uses new flags and common methods in util:
 db, keymanager set up

---
 ethereum/main.go | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/ethereum/main.go b/ethereum/main.go
index 6b1995eec..1531871cb 100644
--- a/ethereum/main.go
+++ b/ethereum/main.go
@@ -21,10 +21,14 @@ func main() {
 
 	utils.InitLogging(Datadir, LogFile, LogLevel, DebugFile)
 
-	ethereum := utils.NewEthereum(UseUPnP, OutboundPort, MaxPeer)
+	db := utils.NewDatabase()
+
+	keyManager := utils.NewKeyManager(KeyStore, Datadir, db)
 
 	// create, import, export keys
-	utils.KeyTasks(GenAddr, ImportKey, ExportKey, NonInteractive)
+	utils.KeyTasks(keyManager, KeyRing, GenAddr, SecretFile, ExportDir, NonInteractive)
+
+	ethereum := utils.NewEthereum(db, keyManager, UseUPnP, OutboundPort, MaxPeer)
 
 	if ShowGenesis {
 		utils.ShowGenesis(ethereum)

From 9bd67de671bd0f70e85dcc518fd91fa080a849fc Mon Sep 17 00:00:00 2001
From: zelig <viktor.tron@gmail.com>
Date: Sun, 29 Jun 2014 18:38:17 +0100
Subject: [PATCH 07/14] remove keys file, now subsumed under
 ethcrypto/key_manager

---
 utils/keys.go | 108 --------------------------------------------------
 1 file changed, 108 deletions(-)
 delete mode 100644 utils/keys.go

diff --git a/utils/keys.go b/utils/keys.go
deleted file mode 100644
index 1c0f6a9d6..000000000
--- a/utils/keys.go
+++ /dev/null
@@ -1,108 +0,0 @@
-package utils
-
-import (
-	"fmt"
-	"github.com/ethereum/eth-go/ethutil"
-	"github.com/obscuren/secp256k1-go"
-)
-
-func CreateKeyPair(force bool) {
-	if force {
-		ethutil.GetKeyRing().Reset()
-		fmt.Println("resetting")
-	}
-
-	if ethutil.GetKeyRing().Get(0) == nil {
-		_, prv := secp256k1.GenerateKeyPair()
-
-		keyPair, err := ethutil.GetKeyRing().NewKeyPair(prv)
-		if err != nil {
-			panic(err)
-		}
-
-		mne := ethutil.MnemonicEncode(ethutil.Hex(keyPair.PrivateKey))
-
-		fmt.Printf(`
-Generating new address and keypair.
-Please keep your keys somewhere save.
-
-++++++++++++++++ KeyRing +++++++++++++++++++
-addr: %x
-prvk: %x
-pubk: %x
-++++++++++++++++++++++++++++++++++++++++++++
-save these words so you can restore your account later: %s
-`, keyPair.Address(), keyPair.PrivateKey, keyPair.PublicKey, mne)
-	}
-}
-
-func ImportPrivateKey(sec string) {
-	ethutil.GetKeyRing().Reset()
-
-	keyPair, err := ethutil.GetKeyRing().NewKeyPair(ethutil.FromHex(sec))
-	if err != nil {
-		panic(err)
-	}
-
-	mne := ethutil.MnemonicEncode(ethutil.Hex(keyPair.PrivateKey))
-
-	fmt.Printf(`
-Generating new address and keypair.
-Please keep your keys somewhere save.
-
-++++++++++++++++ KeyRing +++++++++++++++++++
-addr: %x
-prvk: %x
-pubk: %x
-++++++++++++++++++++++++++++++++++++++++++++
-save these words so you can restore your account later: %s
-`, keyPair.Address(), keyPair.PrivateKey, keyPair.PublicKey, mne)
-}
-
-/*
-func CreateKeyPair(force bool) {
-	data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
-	if len(data) == 0 || force {
-		pub, prv := secp256k1.GenerateKeyPair()
-		pair := &ethutil.Key{PrivateKey: prv, PublicKey: pub}
-		ethutil.Config.Db.Put([]byte("KeyRing"), pair.RlpEncode())
-		mne := ethutil.MnemonicEncode(ethutil.Hex(prv))
-
-		fmt.Printf(`
-Generating new address and keypair.
-Please keep your keys somewhere save.
-
-++++++++++++++++ KeyRing +++++++++++++++++++
-addr: %x
-prvk: %x
-pubk: %x
-++++++++++++++++++++++++++++++++++++++++++++
-save these words so you can restore your account later: %s
-`, pair.Address(), prv, pub, mne)
-
-	}
-}
-*/
-
-/*
-func ImportPrivateKey(prvKey string) {
-	key := ethutil.FromHex(prvKey)
-	msg := []byte("tmp")
-	// Couldn't think of a better way to get the pub key
-	sig, _ := secp256k1.Sign(msg, key)
-	pub, _ := secp256k1.RecoverPubkey(msg, sig)
-	pair := &ethutil.Key{PrivateKey: key, PublicKey: pub}
-	ethutil.Config.Db.Put([]byte("KeyRing"), pair.RlpEncode())
-
-	fmt.Printf(`
-Importing private key
-
-++++++++++++++++ KeyRing +++++++++++++++++++
-addr: %x
-prvk: %x
-pubk: %x
-++++++++++++++++++++++++++++++++++++++++++++
-
-`, pair.Address(), key, pub)
-}
-*/

From cf7fcadeca356adf780d00583a16f0b722cc7987 Mon Sep 17 00:00:00 2001
From: zelig <viktor.tron@gmail.com>
Date: Sun, 29 Jun 2014 18:39:13 +0100
Subject: [PATCH 08/14] added utility functions to set up db, keymanager,
 ethereum init and simplify key tasks

---
 utils/cmd.go | 96 +++++++++++++++++++++++++++-------------------------
 1 file changed, 50 insertions(+), 46 deletions(-)

diff --git a/utils/cmd.go b/utils/cmd.go
index 99a1f2628..d20af98cc 100644
--- a/utils/cmd.go
+++ b/utils/cmd.go
@@ -3,6 +3,8 @@ package utils
 import (
 	"fmt"
 	"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/ethminer"
 	"github.com/ethereum/eth-go/ethpub"
@@ -13,7 +15,6 @@ import (
 	"os"
 	"os/signal"
 	"path"
-	"strings"
 	"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) {
 	InitDataDir(Datadir)
 	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()
 	os.Exit(status)
 }
 
-func NewEthereum(UseUPnP bool, OutboundPort string, MaxPeer int) *eth.Ethereum {
-	ethereum, err := eth.New(eth.CapDefault, UseUPnP)
+func NewDatabase() ethutil.Database {
+	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 {
 		logger.Fatalln("eth start err:", err)
 	}
@@ -129,50 +142,47 @@ func StartEthereum(ethereum *eth.Ethereum, UseSeed bool) {
 
 func ShowGenesis(ethereum *eth.Ethereum) {
 	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 {
 	case GenAddr:
 		if NonInteractive || confirm("This action overwrites your old private key.") {
-			CreateKeyPair(true)
+			err = keyManager.Init(KeyRing, 0, true)
 		}
-		exit(0)
-	case len(ImportKey) > 0:
+		exit(err)
+	case len(SecretFile) > 0:
 		if NonInteractive || confirm("This action overwrites your old private key.") {
-			// import should be from file
-			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.")
-			}
+			err = keyManager.InitFromSecretsFile(KeyRing, 0, SecretFile)
 		}
-		exit(0)
-	case ExportKey: // this should be exporting to a filename
-		keyPair := ethutil.GetKeyRing().Get(0)
-		fmt.Printf(`
-Generating new address and keypair.
-Please keep your keys somewhere save.
-
-++++++++++++++++ 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)
+		exit(err)
+	case len(ExportDir) > 0:
+		err = keyManager.Init(KeyRing, 0, false)
+		if err == nil {
+			err = keyManager.Export(ExportDir)
+		}
+		exit(err)
 	default:
 		// 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 {
 		ethereum.Mining = true
 
-		if ethutil.GetKeyRing().Len() == 0 {
-			logger.Errorln("No address found, can't start mining")
-			ethereum.Mining = false
-			return true //????
-		}
-		keyPair := ethutil.GetKeyRing().Get(0)
-		addr := keyPair.Address()
+		addr := ethereum.KeyManager().Address()
 
 		go func() {
 			miner = ethminer.NewDefaultMiner(addr, ethereum)

From 12fbb7ae5ceb3c06fb7c4611e82d7cb10cea06be Mon Sep 17 00:00:00 2001
From: zelig <viktor.tron@gmail.com>
Date: Sun, 29 Jun 2014 20:31:00 +0100
Subject: [PATCH 09/14] new command line options - keyring: keyring/session
 identifier used by key manager - keystore: choice of db/file key storage -
 import message updated - export: name of directory to export keys to (was
 bool)

---
 ethereal/flags.go | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/ethereal/flags.go b/ethereal/flags.go
index 9bed38d9f..2c9b3af5f 100644
--- a/ethereal/flags.go
+++ b/ethereal/flags.go
@@ -13,6 +13,8 @@ import (
 )
 
 var Identifier string
+var KeyRing string
+var KeyStore string
 var StartRpc bool
 var RpcPort int
 var UseUPnP bool
@@ -22,8 +24,8 @@ var AddPeer string
 var MaxPeer int
 var GenAddr bool
 var UseSeed bool
-var ImportKey string
-var ExportKey bool
+var SecretFile string
+var ExportDir string
 var NonInteractive bool
 var Datadir string
 var LogFile string
@@ -73,6 +75,8 @@ func Init() {
 	}
 
 	flag.StringVar(&Identifier, "id", "", "Custom client identifier")
+	flag.StringVar(&KeyRing, "keyring", "", "identifier for keyring to use")
+	flag.StringVar(&KeyStore, "keystore", "db", "system to store keyrings: db|file (db)")
 	flag.StringVar(&OutboundPort, "port", "30303", "listening port")
 	flag.BoolVar(&UseUPnP, "upnp", false, "enable UPnP support")
 	flag.IntVar(&MaxPeer, "maxpeer", 10, "maximum desired peers")
@@ -81,9 +85,9 @@ func Init() {
 	flag.BoolVar(&NonInteractive, "y", false, "non-interactive mode (say yes to confirmations)")
 	flag.BoolVar(&UseSeed, "seed", true, "seed peers")
 	flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
-	flag.BoolVar(&ExportKey, "export", false, "export private key")
+	flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)")
+	flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given")
 	flag.StringVar(&LogFile, "logfile", "", "log file (defaults to standard output)")
-	flag.StringVar(&ImportKey, "import", "", "imports the given private key (hex)")
 	flag.StringVar(&Datadir, "datadir", defaultDataDir(), "specifies the datadir to use")
 	flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file")
 	flag.StringVar(&DebugFile, "debug", "", "debug file (no debugging if not set)")

From e43e4ff2c1d882ec96424ced1cf8980e14636afd Mon Sep 17 00:00:00 2001
From: zelig <viktor.tron@gmail.com>
Date: Sun, 29 Jun 2014 20:32:31 +0100
Subject: [PATCH 10/14] main loop uses new flags and common methods in util:
 db, keymanager set up; passes KeyRing/Session string to gui

---
 ethereal/main.go | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/ethereal/main.go b/ethereal/main.go
index 316f2285f..82427f088 100644
--- a/ethereal/main.go
+++ b/ethereal/main.go
@@ -30,10 +30,14 @@ func main() {
 
 	utils.InitLogging(Datadir, LogFile, LogLevel, DebugFile)
 
-	ethereum := utils.NewEthereum(UseUPnP, OutboundPort, MaxPeer)
+	db := utils.NewDatabase()
+
+	keyManager := utils.NewKeyManager(KeyStore, Datadir, db)
 
 	// create, import, export keys
-	utils.KeyTasks(GenAddr, ImportKey, ExportKey, NonInteractive)
+	utils.KeyTasks(keyManager, KeyRing, GenAddr, SecretFile, ExportDir, NonInteractive)
+
+	ethereum := utils.NewEthereum(db, keyManager, UseUPnP, OutboundPort, MaxPeer)
 
 	if ShowGenesis {
 		utils.ShowGenesis(ethereum)
@@ -43,7 +47,7 @@ func main() {
 		utils.StartRpc(ethereum, RpcPort)
 	}
 
-	gui := ethui.New(ethereum, LogLevel)
+	gui := ethui.New(ethereum, KeyRing, LogLevel)
 
 	utils.RegisterInterrupt(func(os.Signal) {
 		gui.Stop()

From 29cc1af2bc8d4a516e30abba9155584d7df8dd83 Mon Sep 17 00:00:00 2001
From: zelig <viktor.tron@gmail.com>
Date: Sun, 29 Jun 2014 20:34:07 +0100
Subject: [PATCH 11/14] remove ui/library; instead expose gui itself for
 initial window

---
 ethereal/ui/library.go | 46 ------------------------------------------
 1 file changed, 46 deletions(-)
 delete mode 100644 ethereal/ui/library.go

diff --git a/ethereal/ui/library.go b/ethereal/ui/library.go
deleted file mode 100644
index 267108195..000000000
--- a/ethereal/ui/library.go
+++ /dev/null
@@ -1,46 +0,0 @@
-package ethui
-
-import (
-	"fmt"
-	"github.com/ethereum/eth-go/ethchain"
-	"github.com/ethereum/eth-go/ethutil"
-	"github.com/ethereum/go-ethereum/utils"
-	"github.com/obscuren/secp256k1-go"
-	"strings"
-)
-
-type EthLib struct {
-	stateManager *ethchain.StateManager
-	blockChain   *ethchain.BlockChain
-	txPool       *ethchain.TxPool
-	Db           *Debugger
-}
-
-func (lib *EthLib) ImportAndSetPrivKey(privKey string) bool {
-	fmt.Println(privKey)
-	mnemonic := strings.Split(privKey, " ")
-	if len(mnemonic) == 24 {
-		fmt.Println("Got mnemonic key, importing.")
-		key := ethutil.MnemonicDecode(mnemonic)
-		utils.ImportPrivateKey(key)
-	} else if len(mnemonic) == 1 {
-		fmt.Println("Got hex key, importing.")
-		utils.ImportPrivateKey(privKey)
-	} else {
-		fmt.Println("Did not recognise format, exiting.")
-		return false
-	}
-	return true
-}
-
-func (lib *EthLib) CreateAndSetPrivKey() (string, string, string, string) {
-	_, prv := secp256k1.GenerateKeyPair()
-	keyPair, err := ethutil.GetKeyRing().NewKeyPair(prv)
-	if err != nil {
-		panic(err)
-	}
-
-	mne := ethutil.MnemonicEncode(ethutil.Hex(keyPair.PrivateKey))
-	mnemonicString := strings.Join(mne, " ")
-	return mnemonicString, fmt.Sprintf("%x", keyPair.Address()), ethutil.Hex(keyPair.PrivateKey), ethutil.Hex(keyPair.PublicKey)
-}

From 8aea468744e223f310da98f7478fb5b468d99563 Mon Sep 17 00:00:00 2001
From: zelig <viktor.tron@gmail.com>
Date: Sun, 29 Jun 2014 20:38:26 +0100
Subject: [PATCH 12/14] gui changes - remove lib   *EthLib, expose gui itself
 to initial import window - remove addr []byte instead use dynamic adress() -
 use ethereum.KeyManager to retrieve address and privateKey - add Session
 string (keyRing identifier) - add and reimplement ImportAndSetPrivKey and
 CreateAndSetPrivKey

---
 ethereal/ui/gui.go | 88 +++++++++++++++++++++++++++-------------------
 1 file changed, 52 insertions(+), 36 deletions(-)

diff --git a/ethereal/ui/gui.go b/ethereal/ui/gui.go
index f861236aa..d8c39e837 100644
--- a/ethereal/ui/gui.go
+++ b/ethereal/ui/gui.go
@@ -28,36 +28,28 @@ type Gui struct {
 	eth *eth.Ethereum
 
 	// The public Ethereum library
-	lib   *EthLib
 	uiLib *UiLib
 
 	txDb *ethdb.LDBDatabase
 
-	addr []byte
-
 	pub      *ethpub.PEthereum
 	logLevel ethlog.LogLevel
 	open     bool
+
+	Session string
 }
 
 // Create GUI, but doesn't start it
-func New(ethereum *eth.Ethereum, logLevel int) *Gui {
-	lib := &EthLib{stateManager: ethereum.StateManager(), blockChain: ethereum.BlockChain(), txPool: ethereum.TxPool()}
+func New(ethereum *eth.Ethereum, session string, logLevel int) *Gui {
+
 	db, err := ethdb.NewLDBDatabase("tx_database")
 	if err != nil {
 		panic(err)
 	}
 
-	// On first run we won't have any keys yet, so this would crash.
-	// Therefor we check if we are ready to actually start this process
-	var addr []byte
-	if ethutil.GetKeyRing().Len() != 0 {
-		addr = ethutil.GetKeyRing().Get(0).Address()
-	}
-
 	pub := ethpub.NewPEthereum(ethereum)
 
-	return &Gui{eth: ethereum, lib: lib, txDb: db, addr: addr, pub: pub, logLevel: ethlog.LogLevel(logLevel), open: false}
+	return &Gui{eth: ethereum, txDb: db, pub: pub, logLevel: ethlog.LogLevel(logLevel), Session: session, open: false}
 }
 
 func (gui *Gui) Start(assetPath string) {
@@ -158,12 +150,11 @@ func (gui *Gui) showWallet(context *qml.Context) (*qml.Window, error) {
 }
 
 func (gui *Gui) showKeyImport(context *qml.Context) (*qml.Window, error) {
-	context.SetVar("lib", gui.lib)
+	context.SetVar("lib", gui)
 	component, err := gui.engine.LoadFile(gui.uiLib.AssetPath("qml/first_run.qml"))
 	if err != nil {
 		return nil, err
 	}
-
 	return gui.createWindow(component), nil
 }
 
@@ -175,15 +166,36 @@ func (gui *Gui) createWindow(comp qml.Object) *qml.Window {
 
 	return gui.win
 }
+
+func (gui *Gui) ImportAndSetPrivKey(secret string) bool {
+	err := gui.eth.KeyManager().InitFromString(gui.Session, 0, secret)
+	if err != nil {
+		logger.Errorln("unable to import: ", err)
+		return false
+	}
+	logger.Errorln("successfully imported: ", err)
+	return true
+}
+
+func (gui *Gui) CreateAndSetPrivKey() (string, string, string, string) {
+	err := gui.eth.KeyManager().Init(gui.Session, 0, true)
+	if err != nil {
+		logger.Errorln("unable to create key: ", err)
+		return "", "", "", ""
+	}
+	return gui.eth.KeyManager().KeyPair().AsStrings()
+}
+
 func (gui *Gui) setInitialBlockChain() {
 	sBlk := gui.eth.BlockChain().LastBlockHash
 	blk := gui.eth.BlockChain().GetBlock(sBlk)
 	for ; blk != nil; blk = gui.eth.BlockChain().GetBlock(sBlk) {
 		sBlk = blk.PrevHash
+		addr := gui.address()
 
 		// Loop through all transactions to see if we missed any while being offline
 		for _, tx := range blk.Transactions() {
-			if bytes.Compare(tx.Sender(), gui.addr) == 0 || bytes.Compare(tx.Recipient, gui.addr) == 0 {
+			if bytes.Compare(tx.Sender(), addr) == 0 || bytes.Compare(tx.Recipient, addr) == 0 {
 				if ok, _ := gui.txDb.Get(tx.Hash()); ok == nil {
 					gui.txDb.Put(tx.Hash(), tx.RlpEncode())
 				}
@@ -199,25 +211,26 @@ type address struct {
 	Name, Address string
 }
 
-var namereg = ethutil.FromHex("bb5f186604d057c1c5240ca2ae0f6430138ac010")
+var namereg = ethutil.Hex2Bytes("bb5f186604d057c1c5240ca2ae0f6430138ac010")
 
 func (gui *Gui) loadAddressBook() {
 	gui.win.Root().Call("clearAddress")
 	stateObject := gui.eth.StateManager().CurrentState().GetStateObject(namereg)
 	if stateObject != nil {
 		stateObject.State().EachStorage(func(name string, value *ethutil.Value) {
-			gui.win.Root().Call("addAddress", struct{ Name, Address string }{name, ethutil.Hex(value.Bytes())})
+			gui.win.Root().Call("addAddress", struct{ Name, Address string }{name, ethutil.Bytes2Hex(value.Bytes())})
 		})
 	}
 }
 
 func (gui *Gui) readPreviousTransactions() {
 	it := gui.txDb.Db().NewIterator(nil, nil)
+	addr := gui.address()
 	for it.Next() {
 		tx := ethchain.NewTransactionFromBytes(it.Value())
 
 		var inout string
-		if bytes.Compare(tx.Sender(), gui.addr) == 0 {
+		if bytes.Compare(tx.Sender(), addr) == 0 {
 			inout = "send"
 		} else {
 			inout = "recv"
@@ -269,29 +282,29 @@ func (gui *Gui) update() {
 	state := gui.eth.StateManager().TransState()
 
 	unconfirmedFunds := new(big.Int)
-	gui.win.Root().Call("setWalletValue", fmt.Sprintf("%v", ethutil.CurrencyToString(state.GetAccount(gui.addr).Amount)))
+	gui.win.Root().Call("setWalletValue", fmt.Sprintf("%v", ethutil.CurrencyToString(state.GetAccount(gui.address()).Amount)))
 
 	for {
 		select {
 		case b := <-blockChan:
 			block := b.Resource.(*ethchain.Block)
 			gui.processBlock(block, false)
-			if bytes.Compare(block.Coinbase, gui.addr) == 0 {
-				gui.setWalletValue(gui.eth.StateManager().CurrentState().GetAccount(gui.addr).Amount, nil)
+			if bytes.Compare(block.Coinbase, gui.address()) == 0 {
+				gui.setWalletValue(gui.eth.StateManager().CurrentState().GetAccount(gui.address()).Amount, nil)
 			}
 
 		case txMsg := <-txChan:
 			tx := txMsg.Resource.(*ethchain.Transaction)
 
 			if txMsg.Event == "newTx:pre" {
-				object := state.GetAccount(gui.addr)
+				object := state.GetAccount(gui.address())
 
-				if bytes.Compare(tx.Sender(), gui.addr) == 0 {
+				if bytes.Compare(tx.Sender(), gui.address()) == 0 {
 					gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "send")
 					gui.txDb.Put(tx.Hash(), tx.RlpEncode())
 
 					unconfirmedFunds.Sub(unconfirmedFunds, tx.Value)
-				} else if bytes.Compare(tx.Recipient, gui.addr) == 0 {
+				} else if bytes.Compare(tx.Recipient, gui.address()) == 0 {
 					gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "recv")
 					gui.txDb.Put(tx.Hash(), tx.RlpEncode())
 
@@ -300,10 +313,10 @@ func (gui *Gui) update() {
 
 				gui.setWalletValue(object.Amount, unconfirmedFunds)
 			} else {
-				object := state.GetAccount(gui.addr)
-				if bytes.Compare(tx.Sender(), gui.addr) == 0 {
+				object := state.GetAccount(gui.address())
+				if bytes.Compare(tx.Sender(), gui.address()) == 0 {
 					object.SubAmount(tx.Value)
-				} else if bytes.Compare(tx.Recipient, gui.addr) == 0 {
+				} else if bytes.Compare(tx.Recipient, gui.address()) == 0 {
 					object.AddAmount(tx.Value)
 				}
 
@@ -330,22 +343,25 @@ func (gui *Gui) setPeerInfo() {
 	}
 }
 
+func (gui *Gui) privateKey() string {
+	return ethutil.Bytes2Hex(gui.eth.KeyManager().PrivateKey())
+}
+
+func (gui *Gui) address() []byte {
+	return gui.eth.KeyManager().Address()
+}
+
 func (gui *Gui) RegisterName(name string) {
-	keyPair := ethutil.GetKeyRing().Get(0)
 	name = fmt.Sprintf("\"%s\"\n1", name)
-	gui.pub.Transact(ethutil.Hex(keyPair.PrivateKey), "namereg", "1000", "1000000", "150", name)
+	gui.pub.Transact(gui.privateKey(), "namereg", "1000", "1000000", "150", name)
 }
 
 func (gui *Gui) Transact(recipient, value, gas, gasPrice, data string) (*ethpub.PReceipt, error) {
-	keyPair := ethutil.GetKeyRing().Get(0)
-
-	return gui.pub.Transact(ethutil.Hex(keyPair.PrivateKey), recipient, value, gas, gasPrice, data)
+	return gui.pub.Transact(gui.privateKey(), recipient, value, gas, gasPrice, data)
 }
 
 func (gui *Gui) Create(recipient, value, gas, gasPrice, data string) (*ethpub.PReceipt, error) {
-	keyPair := ethutil.GetKeyRing().Get(0)
-
-	return gui.pub.Transact(ethutil.Hex(keyPair.PrivateKey), recipient, value, gas, gasPrice, data)
+	return gui.pub.Transact(gui.privateKey(), recipient, value, gas, gasPrice, data)
 }
 
 func (gui *Gui) ChangeClientId(id string) {

From 098f7f23ce62d3f0c60d30d325576de93795cc4b Mon Sep 17 00:00:00 2001
From: zelig <viktor.tron@gmail.com>
Date: Sun, 29 Jun 2014 20:39:45 +0100
Subject: [PATCH 13/14] changed name for ethutil hex functions; and access to
 keyring via keyManager

---
 ethereal/ui/debugger.go       | 4 ++--
 ethereal/ui/ext_app.go        | 4 ++--
 ethereal/ui/html_container.go | 2 +-
 ethereal/ui/qml_app.go        | 2 +-
 ethereal/ui/ui_lib.go         | 4 ++--
 5 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/ethereal/ui/debugger.go b/ethereal/ui/debugger.go
index 85dd45563..5ad1b4a2c 100644
--- a/ethereal/ui/debugger.go
+++ b/ethereal/ui/debugger.go
@@ -49,7 +49,7 @@ func (self *DebuggerWindow) SetData(data string) {
 	self.win.Set("dataText", data)
 }
 func (self *DebuggerWindow) SetAsm(data string) {
-	dis := ethchain.Disassemble(ethutil.FromHex(data))
+	dis := ethchain.Disassemble(ethutil.Hex2Bytes(data))
 	for _, str := range dis {
 		self.win.Root().Call("setAsm", str)
 	}
@@ -101,7 +101,7 @@ func (self *DebuggerWindow) Debug(valueStr, gasStr, gasPriceStr, scriptStr, data
 		gasPrice = ethutil.Big(gasPriceStr)
 		value    = ethutil.Big(valueStr)
 		// Contract addr as test address
-		keyPair  = ethutil.GetKeyRing().Get(0)
+		keyPair  = self.lib.eth.KeyManager().KeyPair()
 		callerTx = ethchain.NewContractCreationTx(ethutil.Big(valueStr), gas, gasPrice, script)
 	)
 	callerTx.Sign(keyPair.PrivateKey)
diff --git a/ethereal/ui/ext_app.go b/ethereal/ui/ext_app.go
index d1a256cdb..0230c46ab 100644
--- a/ethereal/ui/ext_app.go
+++ b/ethereal/ui/ext_app.go
@@ -121,10 +121,10 @@ out:
 func (app *ExtApplication) Watch(addr, storageAddr string) {
 	var event string
 	if len(storageAddr) == 0 {
-		event = "object:" + string(ethutil.FromHex(addr))
+		event = "object:" + string(ethutil.Hex2Bytes(addr))
 		app.lib.eth.Reactor().Subscribe(event, app.changeChan)
 	} else {
-		event = "storage:" + string(ethutil.FromHex(addr)) + ":" + string(ethutil.FromHex(storageAddr))
+		event = "storage:" + string(ethutil.Hex2Bytes(addr)) + ":" + string(ethutil.Hex2Bytes(storageAddr))
 		app.lib.eth.Reactor().Subscribe(event, app.changeChan)
 	}
 
diff --git a/ethereal/ui/html_container.go b/ethereal/ui/html_container.go
index d7dc80af7..f2ebd840c 100644
--- a/ethereal/ui/html_container.go
+++ b/ethereal/ui/html_container.go
@@ -116,7 +116,7 @@ func (app *HtmlApplication) Window() *qml.Window {
 }
 
 func (app *HtmlApplication) NewBlock(block *ethchain.Block) {
-	b := &ethpub.PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Hex(block.Hash())}
+	b := &ethpub.PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Bytes2Hex(block.Hash())}
 	app.webView.Call("onNewBlockCb", b)
 }
 
diff --git a/ethereal/ui/qml_app.go b/ethereal/ui/qml_app.go
index 39ab7f922..d23fdd110 100644
--- a/ethereal/ui/qml_app.go
+++ b/ethereal/ui/qml_app.go
@@ -38,7 +38,7 @@ func (app *QmlApplication) NewWatcher(quitChan chan bool) {
 
 // Events
 func (app *QmlApplication) NewBlock(block *ethchain.Block) {
-	pblock := &ethpub.PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Hex(block.Hash())}
+	pblock := &ethpub.PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Bytes2Hex(block.Hash())}
 	app.win.Call("onNewBlockCb", pblock)
 }
 
diff --git a/ethereal/ui/ui_lib.go b/ethereal/ui/ui_lib.go
index ddc955176..892c1f065 100644
--- a/ethereal/ui/ui_lib.go
+++ b/ethereal/ui/ui_lib.go
@@ -77,9 +77,9 @@ func (ui *UiLib) AssetPath(p string) string {
 
 func (self *UiLib) StartDbWithContractAndData(contractHash, data string) {
 	dbWindow := NewDebuggerWindow(self)
-	object := self.eth.StateManager().CurrentState().GetStateObject(ethutil.FromHex(contractHash))
+	object := self.eth.StateManager().CurrentState().GetStateObject(ethutil.Hex2Bytes(contractHash))
 	if len(object.Script()) > 0 {
-		dbWindow.SetCode("0x" + ethutil.Hex(object.Script()))
+		dbWindow.SetCode("0x" + ethutil.Bytes2Hex(object.Script()))
 	}
 	dbWindow.SetData("0x" + data)
 

From 0ed9528d760028acda6333269423eab775026685 Mon Sep 17 00:00:00 2001
From: Ethan Buchman <ethan@coinculture.info>
Date: Tue, 1 Jul 2014 04:25:42 -0400
Subject: [PATCH 14/14] fix: miner wait untill UpToDate

---
 utils/cmd.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/utils/cmd.go b/utils/cmd.go
index 1d97c9639..db5db0d2c 100644
--- a/utils/cmd.go
+++ b/utils/cmd.go
@@ -204,7 +204,7 @@ func StartMining(ethereum *eth.Ethereum) bool {
 			miner = ethminer.NewDefaultMiner(addr, ethereum)
 			// Give it some time to connect with peers
 			time.Sleep(3 * time.Second)
-			for !ethereum.IsUpToDate() == false {
+			for !ethereum.IsUpToDate() {
 				time.Sleep(5 * time.Second)
 			}