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/cmd/utils"
"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"
errextra "github.com/pkg/errors"
)
@ -24,28 +28,43 @@ func createAccount(password string) (string, string, error) {
if currentNode != nil {
w := true
keydir := datadir + "/testnet/keystore"
accman := accounts.NewManager(keydir, scryptN, scryptP, accountSync)
// generate the account
account, err := accman.NewAccount(password, w)
if err != nil {
return "", "", errextra.Wrap(err, "Account manager could not create the account")
// 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)
}
}
address := fmt.Sprintf("%x", account.Address)
// recover the public key to return
keyContents, err := ioutil.ReadFile(account.File)
if err != nil {
return address, "", errextra.Wrap(err, "Could not load the key contents")
}
key, err := accounts.DecryptKey(keyContents, password)
if err != nil {
return address, "", errextra.Wrap(err, "Could not recover the key")
}
pubKey := common.ToHex(crypto.FromECDSAPub(&key.PrivateKey.PublicKey))
if accountManager != nil {
// generate the account
account, err := accountManager.NewAccount(password, w)
if err != nil {
return "", "", errextra.Wrap(err, "Account manager could not create the account")
}
address := fmt.Sprintf("%x", account.Address)
return address, pubKey, nil
// recover the public key to return
keyContents, err := ioutil.ReadFile(account.File)
if err != nil {
return address, "", errextra.Wrap(err, "Could not load the key contents")
}
key, err := accounts.DecryptKey(keyContents, password)
if err != nil {
return address, "", errextra.Wrap(err, "Could not recover the key")
}
pubKey := common.ToHex(crypto.FromECDSAPub(&key.PrivateKey.PublicKey))
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 {
accman := utils.MakeAccountManager(c, accountSync)
account, err := utils.MakeAddress(accman, address)
if err != nil {
return errextra.Wrap(err, "Could not retrieve account from address")
// 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)
}
}
err = accman.TimedUnlock(account, password, time.Duration(seconds)*time.Second)
if err != nil {
return errextra.Wrap(err, "Could not decrypt account")
if accountManager != nil {
account, err := utils.MakeAddress(accountManager, address)
if err != nil {
return errextra.Wrap(err, "Could not retrieve account from address")
}
err = accountManager.TimedUnlock(account, password, time.Duration(seconds)*time.Second)
if err != nil {
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")

View File

@ -7,6 +7,9 @@ import (
"github.com/ethereum/go-ethereum/cmd/utils"
"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/glog"
"github.com/ethereum/go-ethereum/node"
@ -27,12 +30,13 @@ const (
)
var (
vString string // Combined textual representation of the version
rConfig release.Config // Structured version information and release oracle config
currentNode *node.Node // currently running 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
datadir string // data directory for geth
vString string // Combined textual representation of the version
rConfig release.Config // Structured version information and release oracle config
currentNode *node.Node // currently running 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
accountManager *accounts.Manager // the account manager attached to the currentNode
datadir string // data directory for geth
)
func main() {
@ -49,6 +53,7 @@ func MakeNode(inputDir string) *node.Node {
// TODO remove admin rpcapi flag
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("light", true, "disable eth")
set.Bool("testnet", true, "light test network")
@ -72,6 +77,22 @@ func MakeNode(inputDir string) *node.Node {
utils.DebugSetup(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
}