status-go/src/gethdep.go

128 lines
3.5 KiB
Go
Raw Normal View History

package main
import (
2016-06-20 15:21:45 +00:00
"errors"
"fmt"
"io/ioutil"
"time"
"github.com/ethereum/go-ethereum/accounts"
2016-06-21 18:29:38 +00:00
"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"
2016-06-21 14:34:38 +00:00
"github.com/ethereum/go-ethereum/crypto"
errextra "github.com/pkg/errors"
)
var (
2016-06-30 12:53:17 +00:00
scryptN = 4096
scryptP = 6
)
// createAccount creates an internal geth account
func createAccount(password string) (string, string, error) {
2016-06-29 11:32:04 +00:00
if currentNode != nil {
w := true
// 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)
}
2016-06-29 11:32:04 +00:00
}
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)
// 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
2016-06-29 11:32:04 +00:00
}
return "", "", errors.New("Could not retrieve account manager")
2016-06-21 14:34:38 +00:00
}
2016-06-29 11:32:04 +00:00
return "", "", errors.New("No running node detected for account creation")
}
2016-06-20 15:21:45 +00:00
2016-06-21 18:29:38 +00:00
// unlockAccount unlocks an existing account for a certain duration and
// inject the account as a whisper identity if the account was created as
// a whisper enabled account
func unlockAccount(address, password string, seconds int) error {
2016-06-21 18:29:38 +00:00
if currentNode != nil {
// 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)
}
2016-06-21 18:29:38 +00:00
}
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
2016-06-21 18:29:38 +00:00
}
return errors.New("Could not retrieve account manager")
2016-06-21 18:29:38 +00:00
}
return errors.New("No running node detected for account unlock")
}
// createAndStartNode creates a node entity and starts the
// node running locally
func createAndStartNode(inputDir string) error {
2016-06-20 15:21:45 +00:00
currentNode = MakeNode(inputDir)
2016-06-20 15:21:45 +00:00
if currentNode != nil {
2016-06-29 11:32:04 +00:00
RunNode(currentNode)
2016-06-20 15:21:45 +00:00
return nil
}
return errors.New("Could not create the in-memory node object")
}