diff options
-rw-r--r-- | ethereum/flags.go | 12 | ||||
-rw-r--r-- | ethereum/javascript_runtime.go | 10 | ||||
-rw-r--r-- | ethereum/main.go | 8 | ||||
-rw-r--r-- | utils/cmd.go | 98 | ||||
-rw-r--r-- | utils/keys.go | 108 |
5 files changed, 70 insertions, 166 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)") 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() 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) diff --git a/utils/cmd.go b/utils/cmd.go index 1d97c9639..dc435a700 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" ) @@ -77,7 +78,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) } } @@ -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 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(GenAddr bool, ImportKey string, ExportKey bool, NonInteractive bool) { +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(err) + case len(ExportDir) > 0: + err = keyManager.Init(KeyRing, 0, false) + if err == nil { + err = keyManager.Export(ExportDir) } - 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) 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) 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 := ðutil.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 := ðutil.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) -} -*/ |