Merge pull request #14 from status-im/account-sync

Fix account inject/create functionality, remove datadir from create
This commit is contained in:
Jarrad 2016-07-12 15:39:13 +02:00 committed by GitHub
commit 00d153b05a
6 changed files with 51 additions and 22 deletions

View File

@ -4,13 +4,13 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"path/filepath"
"time" "time"
"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/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/node"
errextra "github.com/pkg/errors" errextra "github.com/pkg/errors"
) )
@ -20,13 +20,13 @@ var (
) )
// createAccount creates an internal geth account // createAccount creates an internal geth account
func createAccount(password, keydir string) (string, string, error) { func createAccount(password string) (string, string, error) {
if currentNode != nil { if currentNode != nil {
var sync *[]node.Service
w := true w := true
accman := accounts.NewManager(keydir, scryptN, scryptP, sync) keydir := filepath.Join(datadir, "/keystore")
accman := accounts.NewManager(keydir, scryptN, scryptP, accountSync)
// generate the account // generate the account
account, err := accman.NewAccount(password, w) account, err := accman.NewAccount(password, w)
@ -61,7 +61,7 @@ func unlockAccount(address, password string, seconds int) error {
if currentNode != nil { if currentNode != nil {
accman := utils.MakeAccountManager(c, &accountSync) accman := utils.MakeAccountManager(c, accountSync)
account, err := utils.MakeAddress(accman, address) account, err := utils.MakeAddress(accman, 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")
@ -82,9 +82,9 @@ func unlockAccount(address, password string, seconds int) error {
// createAndStartNode creates a node entity and starts the // createAndStartNode creates a node entity and starts the
// node running locally // node running locally
func createAndStartNode(datadir string) error { func createAndStartNode(inputDir string) error {
currentNode = MakeNode(datadir) currentNode = MakeNode(inputDir)
if currentNode != nil { if currentNode != nil {
RunNode(currentNode) RunNode(currentNode)
return nil return nil

View File

@ -5,6 +5,10 @@ import (
"os" "os"
"testing" "testing"
"time" "time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/whisper"
) )
// TestAccountBindings makes sure we can create an account and subsequently // TestAccountBindings makes sure we can create an account and subsequently
@ -19,12 +23,33 @@ func TestAccountBindings(t *testing.T) {
} }
// create an account // create an account
address, _, err := createAccount("badpassword", ".ethereumtest/keystore") address, pubkey, err := createAccount("badpassword")
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
t.Error("Test failed: could not create account") t.Error("Test failed: could not create account")
} }
// test to see if the account was injected in whisper
whisperInstance := (*accountSync)[0].(*whisper.Whisper)
identitySucess := whisperInstance.HasIdentity(crypto.ToECDSAPub(common.FromHex(pubkey)))
if !identitySucess || err != nil {
t.Error("Test failed: identity not injected into whisper")
}
// test to see if we can post with the injected whisper identity
postArgs := whisper.PostArgs{
From: pubkey,
To: pubkey,
TTL: 100,
Topics: [][]byte{[]byte("test topic")},
Payload: "test message",
}
whisperAPI := whisper.NewPublicWhisperAPI(whisperInstance)
postSucess, err := whisperAPI.Post(postArgs)
if !postSucess || err != nil {
t.Error("Test failed: Could not post to whisper")
}
// unlock the created account // unlock the created account
err = unlockAccount(address, "badpassword", 10) err = unlockAccount(address, "badpassword", 10)
if err != nil { if err != nil {

View File

@ -10,11 +10,11 @@ import (
var emptyError = "" var emptyError = ""
//export CreateAccount //export CreateAccount
func CreateAccount(password, keydir *C.char) *C.char { func CreateAccount(password *C.char) *C.char {
// This is equivalent to creating an account from the command line, // This is equivalent to creating an account from the command line,
// just modified to handle the function arg passing // just modified to handle the function arg passing
address, pubKey, err := createAccount(C.GoString(password), C.GoString(keydir)) address, pubKey, err := createAccount(C.GoString(password))
errString := emptyError errString := emptyError
if err != nil { if err != nil {

View File

@ -27,11 +27,12 @@ const (
) )
var ( var (
vString string // Combined textual representation of the version vString string // Combined textual representation of the version
rConfig release.Config // Structured version information and release oracle config rConfig release.Config // Structured version information and release oracle config
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
datadir string // data directory for geth
) )
func main() { func main() {
@ -39,17 +40,19 @@ func main() {
// Placeholder for anything we want to run by default // Placeholder for anything we want to run by default
fmt.Println("You are running statusgo!") fmt.Println("You are running statusgo!")
createAndStartNode(".ethereum")
} }
// MakeNode create a geth node entity // MakeNode create a geth node entity
func MakeNode(datadir string) *node.Node { func MakeNode(inputDir string) *node.Node {
datadir = inputDir
// TODO remove admin rpcapi flag // TODO remove admin rpcapi flag
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.Bool("shh", true, "whisper") set.Bool("shh", true, "whisper")
set.Bool("noeth", true, "disable eth") // set.Bool("noeth", true, "disable eth")
set.Bool("light", true, "enable light client")
set.Bool("testnet", true, "enable test network")
set.Bool("rpc", true, "enable rpc") set.Bool("rpc", true, "enable rpc")
set.String("rpcaddr", "localhost", "host for RPC") set.String("rpcaddr", "localhost", "host for RPC")
set.String("rpcport", "8545", "rpc port") set.String("rpcport", "8545", "rpc port")

View File

@ -670,7 +670,7 @@ func MakePasswordList(ctx *cli.Context) []string {
// MakeSystemNode sets up a local node, configures the services to launch and // MakeSystemNode sets up a local node, configures the services to launch and
// assembles the P2P protocol stack. // assembles the P2P protocol stack.
func MakeSystemNode(name, version string, relconf release.Config, extra []byte, ctx *cli.Context) (*node.Node, []node.Service) { func MakeSystemNode(name, version string, relconf release.Config, extra []byte, ctx *cli.Context) (*node.Node, *[]node.Service) {
// Avoid conflicting network flags // Avoid conflicting network flags
networks, netFlags := 0, []cli.BoolFlag{DevModeFlag, TestNetFlag, OlympicFlag} networks, netFlags := 0, []cli.BoolFlag{DevModeFlag, TestNetFlag, OlympicFlag}
for _, flag := range netFlags { for _, flag := range netFlags {
@ -845,7 +845,7 @@ func MakeSystemNode(name, version string, relconf release.Config, extra []byte,
} }
} }
return stack, accountSync return stack, &accountSync
} }
// SetupNetwork configures the system for either the main net or some test network. // SetupNetwork configures the system for either the main net or some test network.

View File

@ -150,7 +150,8 @@ func (self *Whisper) GetIdentity(key *ecdsa.PublicKey) *ecdsa.PrivateKey {
func (self *Whisper) InjectIdentity(key *ecdsa.PrivateKey) error { func (self *Whisper) InjectIdentity(key *ecdsa.PrivateKey) error {
identity := string(crypto.FromECDSAPub(&key.PublicKey)) identity := string(crypto.FromECDSAPub(&key.PublicKey))
self.keys[identity] = key keyCopy := *key
self.keys[identity] = &keyCopy
if _, ok := self.keys[identity]; !ok { if _, ok := self.keys[identity]; !ok {
return fmt.Errorf("key insert into keys map failed") return fmt.Errorf("key insert into keys map failed")
} }