use single account manager instead of creating new ones

This commit is contained in:
Adrian Tiberius 2016-07-04 15:28:49 +03:00
parent 0b34f85a43
commit 032cc27901
2 changed files with 88 additions and 34 deletions

View File

@ -9,6 +9,10 @@ import (
"github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/les"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
errextra "github.com/pkg/errors" errextra "github.com/pkg/errors"
) )
@ -24,11 +28,24 @@ func createAccount(password string) (string, string, error) {
if currentNode != nil { if currentNode != nil {
w := true w := true
keydir := datadir + "/testnet/keystore"
accman := accounts.NewManager(keydir, scryptN, scryptP, accountSync)
// Retrieve the AccountManager
var ethereum *eth.FullNodeService
var accountManager *accounts.Manager
if err := currentNode.Service(&ethereum); err == nil {
accountManager = ethereum.ApiBackend.AccountManager()
} else {
var ethereum *les.LightNodeService
if err := currentNode.Service(&ethereum); err == nil {
accountManager = ethereum.ApiBackend.AccountManager()
} else {
glog.V(logger.Warn).Infoln("cannot get account manager:", err)
}
}
if accountManager != nil {
// generate the account // generate the account
account, err := accman.NewAccount(password, w) account, err := accountManager.NewAccount(password, w)
if err != nil { if err != nil {
return "", "", errextra.Wrap(err, "Account manager could not create the account") return "", "", errextra.Wrap(err, "Account manager could not create the account")
} }
@ -46,6 +63,8 @@ func createAccount(password string) (string, string, error) {
pubKey := common.ToHex(crypto.FromECDSAPub(&key.PrivateKey.PublicKey)) pubKey := common.ToHex(crypto.FromECDSAPub(&key.PrivateKey.PublicKey))
return address, pubKey, nil return address, pubKey, nil
}
return "", "", errors.New("Could not retrieve account manager")
} }
@ -60,19 +79,33 @@ func unlockAccount(address, password string, seconds int) error {
if currentNode != nil { if currentNode != nil {
accman := utils.MakeAccountManager(c, accountSync) // Retrieve the AccountManager
account, err := utils.MakeAddress(accman, address) var ethereum *eth.FullNodeService
var accountManager *accounts.Manager
if err := currentNode.Service(&ethereum); err == nil {
accountManager = ethereum.ApiBackend.AccountManager()
} else {
var ethereum *les.LightNodeService
if err := currentNode.Service(&ethereum); err == nil {
accountManager = ethereum.ApiBackend.AccountManager()
} else {
glog.V(logger.Warn).Infoln("cannot get account manager:", err)
}
}
if accountManager != nil {
account, err := utils.MakeAddress(accountManager, address)
if err != nil { if err != nil {
return errextra.Wrap(err, "Could not retrieve account from address") return errextra.Wrap(err, "Could not retrieve account from address")
} }
err = accman.TimedUnlock(account, password, time.Duration(seconds)*time.Second) err = accountManager.TimedUnlock(account, password, time.Duration(seconds)*time.Second)
if err != nil { if err != nil {
return errextra.Wrap(err, "Could not decrypt account") return errextra.Wrap(err, "Could not decrypt account")
} }
return nil return nil
}
return errors.New("Could not retrieve account manager")
} }
return errors.New("No running node detected for account unlock") return errors.New("No running node detected for account unlock")

View File

@ -7,6 +7,9 @@ import (
"github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/les"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/node"
@ -32,6 +35,7 @@ var (
currentNode *node.Node // currently running geth node currentNode *node.Node // currently running geth node
c *cli.Context // the CLI context used to start the geth node c *cli.Context // the CLI context used to start the geth node
accountSync *[]node.Service // the object used to sync accounts between geth services accountSync *[]node.Service // the object used to sync accounts between geth services
accountManager *accounts.Manager // the account manager attached to the currentNode
datadir string // data directory for geth datadir string // data directory for geth
) )
@ -49,6 +53,7 @@ func MakeNode(inputDir string) *node.Node {
// TODO remove admin rpcapi flag // TODO remove admin rpcapi flag
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.Bool("lightkdf", true, "Reduce key-derivation RAM & CPU usage at some expense of KDF strength")
set.Bool("shh", true, "whisper") set.Bool("shh", true, "whisper")
set.Bool("light", true, "disable eth") set.Bool("light", true, "disable eth")
set.Bool("testnet", true, "light test network") set.Bool("testnet", true, "light test network")
@ -72,6 +77,22 @@ func MakeNode(inputDir string) *node.Node {
utils.DebugSetup(c) utils.DebugSetup(c)
currentNode, accountSync = utils.MakeSystemNode(clientIdentifier, vString, rConfig, makeDefaultExtra(), c) currentNode, accountSync = utils.MakeSystemNode(clientIdentifier, vString, rConfig, makeDefaultExtra(), c)
// Retrieve the AccountManager
// doesn't work because node not started yet ... maybe use some kind of event when node started
// and then get account managet and also signal the event to the app
var ethereum *eth.FullNodeService
if err := currentNode.Service(&ethereum); err == nil {
accountManager = ethereum.ApiBackend.AccountManager()
} else {
var ethereum *les.LightNodeService
if err := currentNode.Service(&ethereum); err == nil {
accountManager = ethereum.ApiBackend.AccountManager()
} else {
glog.V(logger.Warn).Infoln("cannot get account manager:", err)
}
}
return currentNode return currentNode
} }