status-go/src/gethdep.go

81 lines
1.9 KiB
Go
Raw Normal View History

package main
import (
2016-06-20 15:21:45 +00:00
"errors"
"fmt"
"github.com/ethereum/go-ethereum/accounts"
2016-06-21 18:29:38 +00:00
"github.com/ethereum/go-ethereum/cmd/utils"
2016-06-21 14:34:38 +00:00
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/node"
2016-06-21 14:34:38 +00:00
errextra "github.com/pkg/errors"
)
var (
scryptN = 262144
scryptP = 1
)
// createAccount creates an internal geth account
2016-06-21 14:34:38 +00:00
func createAccount(password, keydir string) (string, string, error) {
var sync *[]node.Service
w := true
2016-06-20 02:01:28 +00:00
accman := accounts.NewManager(keydir, scryptN, scryptP, sync)
2016-06-20 02:01:28 +00:00
account, err := accman.NewAccount(password, w)
if err != nil {
2016-06-21 14:34:38 +00:00
return "", "", errextra.Wrap(err, "Account manager could not create the account")
}
2016-06-21 18:29:38 +00:00
address := fmt.Sprintf("%x", account.Address)
2016-06-21 14:34:38 +00:00
key, err := crypto.LoadECDSA(account.File)
if err != nil {
return address, "", errextra.Wrap(err, "Could not load the key")
}
pubKey := string(crypto.FromECDSAPub(&key.PublicKey))
return address, pubKey, nil
}
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) 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")
}
err = accman.Unlock(account, password)
if err != nil {
return errextra.Wrap(err, "Could not decrypt account")
}
return nil
}
return errors.New("No running node detected for account unlock")
}
// createAndStartNode creates a node entity and starts the
// node running locally
2016-06-20 15:21:45 +00:00
func createAndStartNode(datadir string) error {
currentNode := MakeNode(datadir)
if currentNode != nil {
StartNode(currentNode)
return nil
}
return errors.New("Could not create the in-memory node object")
}