forked from mirror/go-ethereum
Merge branch 'accounts-integration' of https://github.com/fjl/go-ethereum into fjl-accounts-integration
commit
05c9351659
@ -1,41 +0,0 @@ |
||||
/* |
||||
This file is part of go-ethereum |
||||
|
||||
go-ethereum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
go-ethereum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
/** |
||||
* @authors |
||||
* Gustav Simonsson <gustav.simonsson@gmail.com> |
||||
*/ |
||||
package main |
||||
|
||||
import ( |
||||
"flag" |
||||
"fmt" |
||||
"os" |
||||
) |
||||
|
||||
var ( |
||||
TestFile string |
||||
) |
||||
|
||||
func Init() { |
||||
flag.Usage = func() { |
||||
fmt.Fprintf(os.Stderr, "%s <testfile>\n", os.Args[0]) |
||||
flag.PrintDefaults() |
||||
} |
||||
flag.Parse() |
||||
|
||||
TestFile = flag.Arg(0) |
||||
} |
@ -1,130 +0,0 @@ |
||||
/* |
||||
This file is part of go-ethereum |
||||
|
||||
go-ethereum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
go-ethereum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
/** |
||||
* @authors |
||||
* Jeffrey Wilcke <i@jev.io> |
||||
*/ |
||||
package main |
||||
|
||||
import ( |
||||
"crypto/ecdsa" |
||||
"flag" |
||||
"fmt" |
||||
"log" |
||||
"os" |
||||
"path" |
||||
"runtime" |
||||
|
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/ethutil" |
||||
"github.com/ethereum/go-ethereum/logger" |
||||
"github.com/ethereum/go-ethereum/p2p/nat" |
||||
"github.com/ethereum/go-ethereum/vm" |
||||
) |
||||
|
||||
var ( |
||||
Identifier string |
||||
KeyRing string |
||||
KeyStore string |
||||
StartRpc bool |
||||
RpcListenAddress string |
||||
RpcPort int |
||||
OutboundPort string |
||||
ShowGenesis bool |
||||
AddPeer string |
||||
MaxPeer int |
||||
GenAddr bool |
||||
BootNodes string |
||||
NodeKey *ecdsa.PrivateKey |
||||
NAT nat.Interface |
||||
SecretFile string |
||||
ExportDir string |
||||
NonInteractive bool |
||||
Datadir string |
||||
LogFile string |
||||
ConfigFile string |
||||
DebugFile string |
||||
LogLevel int |
||||
VmType int |
||||
MinerThreads int |
||||
) |
||||
|
||||
// flags specific to gui client
|
||||
var AssetPath string |
||||
var defaultConfigFile = path.Join(ethutil.DefaultDataDir(), "conf.ini") |
||||
|
||||
func Init() { |
||||
// TODO: move common flag processing to cmd/utils
|
||||
flag.Usage = func() { |
||||
fmt.Fprintf(os.Stderr, "%s [options] [filename]:\noptions precedence: default < config file < environment variables < command line\n", os.Args[0]) |
||||
flag.PrintDefaults() |
||||
} |
||||
|
||||
flag.IntVar(&VmType, "vm", 0, "Virtual Machine type: 0-1: standard, debug") |
||||
flag.StringVar(&Identifier, "id", "", "Custom client identifier") |
||||
flag.StringVar(&KeyRing, "keyring", "", "identifier for keyring to use") |
||||
flag.StringVar(&KeyStore, "keystore", "db", "system to store keyrings: db|file") |
||||
flag.StringVar(&RpcListenAddress, "rpcaddr", "127.0.0.1", "address for json-rpc server to listen on") |
||||
flag.IntVar(&RpcPort, "rpcport", 8545, "port to start json-rpc server on") |
||||
flag.BoolVar(&StartRpc, "rpc", true, "start rpc server") |
||||
flag.BoolVar(&NonInteractive, "y", false, "non-interactive mode (say yes to confirmations)") |
||||
flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key") |
||||
flag.StringVar(&SecretFile, "import", "", "imports the file given (hex or mnemonic formats)") |
||||
flag.StringVar(&ExportDir, "export", "", "exports the session keyring to files in the directory given") |
||||
flag.StringVar(&LogFile, "logfile", "", "log file (defaults to standard output)") |
||||
flag.StringVar(&Datadir, "datadir", ethutil.DefaultDataDir(), "specifies the datadir to use") |
||||
flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file") |
||||
flag.StringVar(&DebugFile, "debug", "", "debug file (no debugging if not set)") |
||||
flag.IntVar(&LogLevel, "loglevel", int(logger.InfoLevel), "loglevel: 0-5 (= silent,error,warn,info,debug,debug detail)") |
||||
|
||||
flag.StringVar(&AssetPath, "asset_path", ethutil.DefaultAssetPath(), "absolute path to GUI assets directory") |
||||
|
||||
// Network stuff
|
||||
var ( |
||||
nodeKeyFile = flag.String("nodekey", "", "network private key file") |
||||
nodeKeyHex = flag.String("nodekeyhex", "", "network private key (for testing)") |
||||
natstr = flag.String("nat", "any", "port mapping mechanism (any|none|upnp|pmp|extip:<IP>)") |
||||
) |
||||
flag.StringVar(&OutboundPort, "port", "30303", "listening port") |
||||
flag.StringVar(&BootNodes, "bootnodes", "", "space-separated node URLs for discovery bootstrap") |
||||
flag.IntVar(&MaxPeer, "maxpeer", 30, "maximum desired peers") |
||||
|
||||
flag.IntVar(&MinerThreads, "minerthreads", runtime.NumCPU(), "number of miner threads") |
||||
|
||||
flag.Parse() |
||||
|
||||
var err error |
||||
if NAT, err = nat.Parse(*natstr); err != nil { |
||||
log.Fatalf("-nat: %v", err) |
||||
} |
||||
switch { |
||||
case *nodeKeyFile != "" && *nodeKeyHex != "": |
||||
log.Fatal("Options -nodekey and -nodekeyhex are mutually exclusive") |
||||
case *nodeKeyFile != "": |
||||
if NodeKey, err = crypto.LoadECDSA(*nodeKeyFile); err != nil { |
||||
log.Fatalf("-nodekey: %v", err) |
||||
} |
||||
case *nodeKeyHex != "": |
||||
if NodeKey, err = crypto.HexToECDSA(*nodeKeyHex); err != nil { |
||||
log.Fatalf("-nodekeyhex: %v", err) |
||||
} |
||||
} |
||||
|
||||
if VmType >= int(vm.MaxVmTy) { |
||||
log.Fatal("Invalid VM type ", VmType) |
||||
} |
||||
} |
@ -1,134 +0,0 @@ |
||||
package crypto |
||||
|
||||
import ( |
||||
"fmt" |
||||
"sync" |
||||
|
||||
"github.com/ethereum/go-ethereum/ethutil" |
||||
"github.com/ethereum/go-ethereum/logger" |
||||
) |
||||
|
||||
var keylogger = logger.NewLogger("KEY") |
||||
|
||||
type KeyManager struct { |
||||
keyRing *KeyRing |
||||
session string |
||||
keyStore KeyStore // interface
|
||||
keyRings map[string]*KeyRing // cache
|
||||
keyPair *KeyPair |
||||
} |
||||
|
||||
func NewDBKeyManager(db ethutil.Database) *KeyManager { |
||||
return &KeyManager{keyStore: &DBKeyStore{db: db}, keyRings: make(map[string]*KeyRing)} |
||||
} |
||||
|
||||
func NewFileKeyManager(basedir string) *KeyManager { |
||||
return &KeyManager{keyStore: &FileKeyStore{basedir: basedir}, keyRings: make(map[string]*KeyRing)} |
||||
} |
||||
|
||||
func (k *KeyManager) KeyPair() *KeyPair { |
||||
return k.keyPair |
||||
} |
||||
|
||||
func (k *KeyManager) KeyRing() *KeyPair { |
||||
return k.keyPair |
||||
} |
||||
|
||||
func (k *KeyManager) PrivateKey() []byte { |
||||
return k.keyPair.PrivateKey |
||||
} |
||||
|
||||
func (k *KeyManager) PublicKey() []byte { |
||||
return k.keyPair.PublicKey |
||||
} |
||||
|
||||
func (k *KeyManager) Address() []byte { |
||||
return k.keyPair.Address() |
||||
} |
||||
|
||||
func (k *KeyManager) save(session string, keyRing *KeyRing) error { |
||||
err := k.keyStore.Save(session, keyRing) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
k.keyRings[session] = keyRing |
||||
return nil |
||||
} |
||||
|
||||
func (k *KeyManager) load(session string) (*KeyRing, error) { |
||||
keyRing, found := k.keyRings[session] |
||||
if !found { |
||||
var err error |
||||
keyRing, err = k.keyStore.Load(session) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
} |
||||
return keyRing, nil |
||||
} |
||||
|
||||
func cursorError(cursor int, len int) error { |
||||
return fmt.Errorf("cursor %d out of range (0..%d)", cursor, len) |
||||
} |
||||
|
||||
func (k *KeyManager) reset(session string, cursor int, keyRing *KeyRing) error { |
||||
if cursor >= keyRing.Len() { |
||||
return cursorError(cursor, keyRing.Len()) |
||||
} |
||||
lock := &sync.Mutex{} |
||||
lock.Lock() |
||||
defer lock.Unlock() |
||||
err := k.save(session, keyRing) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
k.session = session |
||||
k.keyRing = keyRing |
||||
k.keyPair = keyRing.GetKeyPair(cursor) |
||||
return nil |
||||
} |
||||
|
||||
func (k *KeyManager) SetCursor(cursor int) error { |
||||
if cursor >= k.keyRing.Len() { |
||||
return cursorError(cursor, k.keyRing.Len()) |
||||
} |
||||
k.keyPair = k.keyRing.GetKeyPair(cursor) |
||||
return nil |
||||
} |
||||
|
||||
func (k *KeyManager) Init(session string, cursor int, force bool) error { |
||||
var keyRing *KeyRing |
||||
if !force { |
||||
var err error |
||||
keyRing, err = k.load(session) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
} |
||||
if keyRing == nil { |
||||
keyRing = NewGeneratedKeyRing(1) |
||||
keylogger.Infof("Created keypair. Private key: %x\n", keyRing.keys[0].PrivateKey) |
||||
} |
||||
return k.reset(session, cursor, keyRing) |
||||
} |
||||
|
||||
func (k *KeyManager) InitFromSecretsFile(session string, cursor int, secretsfile string) error { |
||||
keyRing, err := NewKeyRingFromFile(secretsfile) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return k.reset(session, cursor, keyRing) |
||||
} |
||||
|
||||
func (k *KeyManager) InitFromString(session string, cursor int, secrets string) error { |
||||
keyRing, err := NewKeyRingFromString(secrets) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return k.reset(session, cursor, keyRing) |
||||
} |
||||
|
||||
func (k *KeyManager) Export(dir string) error { |
||||
fileKeyStore := FileKeyStore{dir} |
||||
return fileKeyStore.Save(k.session, k.keyRing) |
||||
} |
@ -1,113 +0,0 @@ |
||||
package crypto |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io/ioutil" |
||||
"os" |
||||
"path" |
||||
"strings" |
||||
|
||||
"github.com/ethereum/go-ethereum/ethutil" |
||||
) |
||||
|
||||
type KeyStore interface { |
||||
Load(string) (*KeyRing, error) |
||||
Save(string, *KeyRing) error |
||||
} |
||||
|
||||
type DBKeyStore struct { |
||||
db ethutil.Database |
||||
} |
||||
|
||||
const dbKeyPrefix = "KeyRing" |
||||
|
||||
func (k *DBKeyStore) dbKey(session string) []byte { |
||||
return []byte(fmt.Sprintf("%s%s", dbKeyPrefix, session)) |
||||
} |
||||
|
||||
func (k *DBKeyStore) Save(session string, keyRing *KeyRing) error { |
||||
k.db.Put(k.dbKey(session), keyRing.RlpEncode()) |
||||
return nil |
||||
} |
||||
|
||||
func (k *DBKeyStore) Load(session string) (*KeyRing, error) { |
||||
data, err := k.db.Get(k.dbKey(session)) |
||||
if err != nil { |
||||
return nil, nil |
||||
} |
||||
var keyRing *KeyRing |
||||
keyRing, err = NewKeyRingFromBytes(data) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
// if empty keyRing is found we return nil, no error
|
||||
if keyRing.Len() == 0 { |
||||
return nil, nil |
||||
} |
||||
return keyRing, nil |
||||
} |
||||
|
||||
type FileKeyStore struct { |
||||
basedir string |
||||
} |
||||
|
||||
func (k *FileKeyStore) Save(session string, keyRing *KeyRing) error { |
||||
var content []byte |
||||
var err error |
||||
var privateKeys []string |
||||
var publicKeys []string |
||||
var mnemonics []string |
||||
var addresses []string |
||||
keyRing.Each(func(keyPair *KeyPair) { |
||||
privateKeys = append(privateKeys, ethutil.Bytes2Hex(keyPair.PrivateKey)) |
||||
publicKeys = append(publicKeys, ethutil.Bytes2Hex(keyPair.PublicKey)) |
||||
addresses = append(addresses, ethutil.Bytes2Hex(keyPair.Address())) |
||||
mnemonics = append(mnemonics, keyPair.Mnemonic()) |
||||
}) |
||||
|
||||
basename := session |
||||
if session == "" { |
||||
basename = "default" |
||||
} |
||||
|
||||
path := path.Join(k.basedir, basename) |
||||
content = []byte(strings.Join(privateKeys, "\n")) |
||||
err = ioutil.WriteFile(path+".prv", content, 0600) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
content = []byte(strings.Join(publicKeys, "\n")) |
||||
err = ioutil.WriteFile(path+".pub", content, 0644) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
content = []byte(strings.Join(addresses, "\n")) |
||||
err = ioutil.WriteFile(path+".addr", content, 0644) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
content = []byte(strings.Join(mnemonics, "\n")) |
||||
err = ioutil.WriteFile(path+".mne", content, 0600) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func (k *FileKeyStore) Load(session string) (*KeyRing, error) { |
||||
basename := session |
||||
if session == "" { |
||||
basename = "default" |
||||
} |
||||
secfile := path.Join(k.basedir, basename+".prv") |
||||
_, err := os.Stat(secfile) |
||||
// if file is not found then we return nil, no error
|
||||
if err != nil { |
||||
return nil, nil |
||||
} |
||||
return NewKeyRingFromFile(secfile) |
||||
} |
@ -1,123 +0,0 @@ |
||||
package crypto |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io/ioutil" |
||||
"strings" |
||||
|
||||
"github.com/ethereum/go-ethereum/ethutil" |
||||
) |
||||
|
||||
type KeyRing struct { |
||||
keys []*KeyPair |
||||
} |
||||
|
||||
func NewKeyRing() *KeyRing { |
||||
return &KeyRing{} |
||||
} |
||||
|
||||
func (k *KeyRing) AddKeyPair(keyPair *KeyPair) { |
||||
k.keys = append(k.keys, keyPair) |
||||
} |
||||
|
||||
func (k *KeyRing) GetKeyPair(i int) *KeyPair { |
||||
if len(k.keys) > i { |
||||
return k.keys[i] |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
|
||||
func (k *KeyRing) Empty() bool { |
||||
return k.Len() == 0 |
||||
} |
||||
|
||||
func (k *KeyRing) Len() int { |
||||
return len(k.keys) |
||||
} |
||||
|
||||
func (k *KeyRing) Each(f func(*KeyPair)) { |
||||
for _, keyPair := range k.keys { |
||||
f(keyPair) |
||||
} |
||||
} |
||||
|
||||
func NewGeneratedKeyRing(len int) *KeyRing { |
||||
keyRing := NewKeyRing() |
||||
for i := 0; i < len; i++ { |
||||
keyRing.AddKeyPair(GenerateNewKeyPair()) |
||||
} |
||||
return keyRing |
||||
} |
||||
|
||||
func NewKeyRingFromFile(secfile string) (*KeyRing, error) { |
||||
var content []byte |
||||
var err error |
||||
content, err = ioutil.ReadFile(secfile) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
keyRing, err := NewKeyRingFromString(string(content)) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return keyRing, nil |
||||
} |
||||
|
||||
func NewKeyRingFromString(content string) (*KeyRing, error) { |
||||
secretStrings := strings.Split(content, "\n") |
||||
var secrets [][]byte |
||||
for _, secretString := range secretStrings { |
||||
secret := secretString |
||||
words := strings.Split(secretString, " ") |
||||
if len(words) == 24 { |
||||
secret = MnemonicDecode(words) |
||||
} else if len(words) != 1 { |
||||
return nil, fmt.Errorf("Unrecognised key format") |
||||
} |
||||
|
||||
if len(secret) != 0 { |
||||
secrets = append(secrets, ethutil.Hex2Bytes(secret)) |
||||
} |
||||
} |
||||
|
||||
return NewKeyRingFromSecrets(secrets) |
||||
} |
||||
|
||||
func NewKeyRingFromSecrets(secs [][]byte) (*KeyRing, error) { |
||||
keyRing := NewKeyRing() |
||||
for _, sec := range secs { |
||||
keyPair, err := NewKeyPairFromSec(sec) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
keyRing.AddKeyPair(keyPair) |
||||
} |
||||
return keyRing, nil |
||||
} |
||||
|
||||
func NewKeyRingFromBytes(data []byte) (*KeyRing, error) { |
||||
var secrets [][]byte |
||||
it := ethutil.NewValueFromBytes(data).NewIterator() |
||||
for it.Next() { |
||||
secret := it.Value().Bytes() |
||||
secrets = append(secrets, secret) |
||||
} |
||||
keyRing, err := NewKeyRingFromSecrets(secrets) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return keyRing, nil |
||||
} |
||||
|
||||
func (k *KeyRing) RlpEncode() []byte { |
||||
return k.RlpValue().Encode() |
||||
} |
||||
|
||||
func (k *KeyRing) RlpValue() *ethutil.Value { |
||||
v := ethutil.EmptyValue() |
||||
k.Each(func(keyPair *KeyPair) { |
||||
v.Append(keyPair.RlpValue()) |
||||
}) |
||||
return v |
||||
} |
@ -1,122 +0,0 @@ |
||||
package crypto |
||||
|
||||
// import (
|
||||
// "github.com/ethereum/go-ethereum/ethdb"
|
||||
// // "io/ioutil"
|
||||
// "fmt"
|
||||
// "os"
|
||||
// "path"
|
||||
// "testing"
|
||||
// )
|
||||
|
||||
// // test if persistence layer works
|
||||
// func TestDBKeyManager(t *testing.T) {
|
||||
// memdb, _ := ethdb.NewMemDatabase()
|
||||
// keyManager0 := NewDBKeyManager(memdb)
|
||||
// err := keyManager0.Init("", 0, false)
|
||||
// if err != nil {
|
||||
// t.Error("Unexpected error: ", err)
|
||||
// }
|
||||
// keyManager1 := NewDBKeyManager(memdb)
|
||||
// err = keyManager1.Init("", 0, false)
|
||||
// if err != nil {
|
||||
// t.Error("Unexpected error: ", err)
|
||||
// }
|
||||
// if string(keyManager0.PrivateKey()) != string(keyManager1.PrivateKey()) {
|
||||
// t.Error("Expected private keys %x, %x, to be identical via db persistence", keyManager0.PrivateKey(), keyManager1.PrivateKey())
|
||||
// }
|
||||
// err = keyManager1.Init("", 0, true)
|
||||
// if err != nil {
|
||||
// t.Error("Unexpected error: ", err)
|
||||
// }
|
||||
// if string(keyManager0.PrivateKey()) == string(keyManager1.PrivateKey()) {
|
||||
// t.Error("Expected private keys %x, %x, to be be different despite db persistence if force generate", keyManager0.PrivateKey(), keyManager1.PrivateKey())
|
||||
// }
|
||||
// }
|
||||
|
||||
// func TestFileKeyManager(t *testing.T) {
|
||||
// basedir0 := "/tmp/ethtest0"
|
||||
// os.RemoveAll(basedir0)
|
||||
// os.Mkdir(basedir0, 0777)
|
||||
|
||||
// keyManager0 := NewFileKeyManager(basedir0)
|
||||
// err := keyManager0.Init("", 0, false)
|
||||
// if err != nil {
|
||||
// t.Error("Unexpected error: ", err)
|
||||
// }
|
||||
|
||||
// keyManager1 := NewFileKeyManager(basedir0)
|
||||
|
||||
// err = keyManager1.Init("", 0, false)
|
||||
// if err != nil {
|
||||
// t.Error("Unexpected error: ", err)
|
||||
// }
|
||||
// if string(keyManager0.PrivateKey()) != string(keyManager1.PrivateKey()) {
|
||||
// t.Error("Expected private keys %x, %x, to be identical via db persistence", keyManager0.PrivateKey(), keyManager1.PrivateKey())
|
||||
// }
|
||||
|
||||
// err = keyManager1.Init("", 0, true)
|
||||
// if err != nil {
|
||||
// t.Error("Unexpected error: ", err)
|
||||
// }
|
||||
// if string(keyManager0.PrivateKey()) == string(keyManager1.PrivateKey()) {
|
||||
// t.Error("Expected private keys %x, %x, to be be different despite db persistence if force generate", keyManager0.PrivateKey(), keyManager1.PrivateKey())
|
||||
// }
|
||||
// }
|
||||
|
||||
// // cursor errors
|
||||
// func TestCursorErrors(t *testing.T) {
|
||||
// memdb, _ := ethdb.NewMemDatabase()
|
||||
// keyManager0 := NewDBKeyManager(memdb)
|
||||
// err := keyManager0.Init("", 0, false)
|
||||
// err = keyManager0.Init("", 1, false)
|
||||
// if err == nil {
|
||||
// t.Error("Expected cursor error")
|
||||
// }
|
||||
// err = keyManager0.SetCursor(1)
|
||||
// if err == nil {
|
||||
// t.Error("Expected cursor error")
|
||||
// }
|
||||
// }
|
||||
|
||||
// func TestExportImport(t *testing.T) {
|
||||
// memdb, _ := ethdb.NewMemDatabase()
|
||||
// keyManager0 := NewDBKeyManager(memdb)
|
||||
// err := keyManager0.Init("", 0, false)
|
||||
// basedir0 := "/tmp/ethtest0"
|
||||
// os.RemoveAll(basedir0)
|
||||
// os.Mkdir(basedir0, 0777)
|
||||
// keyManager0.Export(basedir0)
|
||||
|
||||
// keyManager1 := NewFileKeyManager(basedir0)
|
||||
// err = keyManager1.Init("", 0, false)
|
||||
// if err != nil {
|
||||
// t.Error("Unexpected error: ", err)
|
||||
// }
|
||||
// fmt.Printf("keyRing: %v\n", keyManager0.KeyPair())
|
||||
// fmt.Printf("keyRing: %v\n", keyManager1.KeyPair())
|
||||
// if string(keyManager0.PrivateKey()) != string(keyManager1.PrivateKey()) {
|
||||
// t.Error("Expected private keys %x, %x, to be identical via export to filestore basedir", keyManager0.PrivateKey(), keyManager1.PrivateKey())
|
||||
// }
|
||||
// path.Join("")
|
||||
|
||||
// // memdb, _ = ethdb.NewMemDatabase()
|
||||
// // keyManager2 := NewDBKeyManager(memdb)
|
||||
// // err = keyManager2.InitFromSecretsFile("", 0, path.Join(basedir0, "default.prv"))
|
||||
// // if err != nil {
|
||||
// // t.Error("Unexpected error: ", err)
|
||||
// // }
|
||||
// // if string(keyManager0.PrivateKey()) != string(keyManager2.PrivateKey()) {
|
||||
// // t.Error("Expected private keys %s, %s, to be identical via export/import prv", keyManager0.PrivateKey(), keyManager1.PrivateKey())
|
||||
// // }
|
||||
|
||||
// // memdb, _ = ethdb.NewMemDatabase()
|
||||
// // keyManager3 := NewDBKeyManager(memdb)
|
||||
// // err = keyManager3.InitFromSecretsFile("", 0, path.Join(basedir0, "default.mne"))
|
||||
// // if err != nil {
|
||||
// // t.Error("Unexpected error: ", err)
|
||||
// // }
|
||||
// // if string(keyManager0.PrivateKey()) != string(keyManager3.PrivateKey()) {
|
||||
// // t.Error("Expected private keys %s, %s, to be identical via export/import mnemonic file", keyManager0.PrivateKey(), keyManager1.PrivateKey())
|
||||
// // }
|
||||
// }
|
@ -0,0 +1,52 @@ |
||||
package rpc |
||||
|
||||
import ( |
||||
"net/http" |
||||
|
||||
"github.com/ethereum/go-ethereum/logger" |
||||
"github.com/ethereum/go-ethereum/xeth" |
||||
) |
||||
|
||||
var rpchttplogger = logger.NewLogger("RPC-HTTP") |
||||
|
||||
const ( |
||||
jsonrpcver = "2.0" |
||||
maxSizeReqLength = 1024 * 1024 // 1MB
|
||||
) |
||||
|
||||
// JSONRPC returns a handler that implements the Ethereum JSON-RPC API.
|
||||
func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler { |
||||
var json JsonWrapper |
||||
api := NewEthereumApi(pipe, dataDir) |
||||
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
||||
w.Header().Set("Access-Control-Allow-Origin", "*") |
||||
|
||||
rpchttplogger.DebugDetailln("Handling request") |
||||
|
||||
if req.ContentLength > maxSizeReqLength { |
||||
jsonerr := &RpcErrorObject{-32700, "Error: Request too large"} |
||||
json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr}) |
||||
return |
||||
} |
||||
|
||||
reqParsed, reqerr := json.ParseRequestBody(req) |
||||
if reqerr != nil { |
||||
jsonerr := &RpcErrorObject{-32700, "Error: Could not parse request"} |
||||
json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr}) |
||||
return |
||||
} |
||||
|
||||
var response interface{} |
||||
reserr := api.GetRequestReply(&reqParsed, &response) |
||||
if reserr != nil { |
||||
rpchttplogger.Warnln(reserr) |
||||
jsonerr := &RpcErrorObject{-32603, reserr.Error()} |
||||
json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr}) |
||||
return |
||||
} |
||||
|
||||
rpchttplogger.DebugDetailf("Generated response: %T %s", response, response) |
||||
json.Send(w, &RpcSuccessResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Result: response}) |
||||
}) |
||||
} |
@ -1,124 +0,0 @@ |
||||
/* |
||||
This file is part of go-ethereum |
||||
|
||||
go-ethereum is free software: you can redistribute it and/or modify |
||||
it under the terms of the GNU General Public License as published by |
||||
the Free Software Foundation, either version 3 of the License, or |
||||
(at your option) any later version. |
||||
|
||||
go-ethereum is distributed in the hope that it will be useful, |
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
GNU General Public License for more details. |
||||
|
||||
You should have received a copy of the GNU General Public License |
||||
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/ |
||||
package rpchttp |
||||
|
||||
import ( |
||||
"fmt" |
||||
"net" |
||||
"net/http" |
||||
|
||||
"github.com/ethereum/go-ethereum/logger" |
||||
"github.com/ethereum/go-ethereum/rpc" |
||||
"github.com/ethereum/go-ethereum/xeth" |
||||
) |
||||
|
||||
var rpchttplogger = logger.NewLogger("RPC-HTTP") |
||||
var JSON rpc.JsonWrapper |
||||
|
||||
const maxSizeReqLength = 1024 * 1024 // 1MB
|
||||
|
||||
func NewRpcHttpServer(pipe *xeth.XEth, address string, port int) (*RpcHttpServer, error) { |
||||
sport := fmt.Sprintf("%s:%d", address, port) |
||||
l, err := net.Listen("tcp", sport) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return &RpcHttpServer{ |
||||
listener: l, |
||||
quit: make(chan bool), |
||||
pipe: pipe, |
||||
port: port, |
||||
addr: address, |
||||
}, nil |
||||
} |
||||
|
||||
type RpcHttpServer struct { |
||||
quit chan bool |
||||
listener net.Listener |
||||
pipe *xeth.XEth |
||||
port int |
||||
addr string |
||||
} |
||||
|
||||
func (s *RpcHttpServer) exitHandler() { |
||||
out: |
||||
for { |
||||
select { |
||||
case <-s.quit: |
||||
s.listener.Close() |
||||
break out |
||||
} |
||||
} |
||||
|
||||
rpchttplogger.Infoln("Shutdown RPC-HTTP server") |
||||
} |
||||
|
||||
func (s *RpcHttpServer) Stop() { |
||||
close(s.quit) |
||||
} |
||||
|
||||
func (s *RpcHttpServer) Start() { |
||||
rpchttplogger.Infof("Starting RPC-HTTP server on %s:%d", s.addr, s.port) |
||||
go s.exitHandler() |
||||
|
||||
api := rpc.NewEthereumApi(s.pipe) |
||||
h := s.apiHandler(api) |
||||
http.Handle("/", h) |
||||
|
||||
err := http.Serve(s.listener, nil) |
||||
// FIX Complains on shutdown due to listner already being closed
|
||||
if err != nil { |
||||
rpchttplogger.Errorln("Error on RPC-HTTP interface:", err) |
||||
} |
||||
} |
||||
|
||||
func (s *RpcHttpServer) apiHandler(api *rpc.EthereumApi) http.Handler { |
||||
var jsonrpcver string = "2.0" |
||||
fn := func(w http.ResponseWriter, req *http.Request) { |
||||
w.Header().Set("Access-Control-Allow-Origin", "*") |
||||
|
||||
rpchttplogger.DebugDetailln("Handling request") |
||||
|
||||
if req.ContentLength > maxSizeReqLength { |
||||
jsonerr := &rpc.RpcErrorObject{-32700, "Error: Request too large"} |
||||
JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr}) |
||||
return |
||||
} |
||||
|
||||
reqParsed, reqerr := JSON.ParseRequestBody(req) |
||||
if reqerr != nil { |
||||
jsonerr := &rpc.RpcErrorObject{-32700, "Error: Could not parse request"} |
||||
JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr}) |
||||
return |
||||
} |
||||
|
||||
var response interface{} |
||||
reserr := api.GetRequestReply(&reqParsed, &response) |
||||
if reserr != nil { |
||||
rpchttplogger.Warnln(reserr) |
||||
jsonerr := &rpc.RpcErrorObject{-32603, reserr.Error()} |
||||
JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr}) |
||||
return |
||||
} |
||||
|
||||
rpchttplogger.DebugDetailf("Generated response: %T %s", response, response) |
||||
JSON.Send(w, &rpc.RpcSuccessResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Result: response}) |
||||
} |
||||
|
||||
return http.HandlerFunc(fn) |
||||
} |
@ -1,8 +0,0 @@ |
||||
package ui |
||||
|
||||
import "github.com/ethereum/go-ethereum/core/types" |
||||
|
||||
type Interface interface { |
||||
UnlockAccount(address []byte) bool |
||||
ConfirmTransaction(tx *types.Transaction) bool |
||||
} |
Loading…
Reference in new issue