Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/keys #92

Merged
merged 12 commits into from
Jul 1, 2014
12 changes: 8 additions & 4 deletions ethereal/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
)

var Identifier string
var KeyRing string
var KeyStore string
var StartRpc bool
var RpcPort int
var UseUPnP bool
Expand All @@ -22,8 +24,8 @@ var AddPeer string
var MaxPeer int
var GenAddr bool
var UseSeed bool
var ImportKey string
var ExportKey bool
var SecretFile string
var ExportDir string
var NonInteractive bool
var Datadir string
var LogFile string
Expand Down Expand Up @@ -73,6 +75,8 @@ func Init() {
}

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 (db)")
flag.StringVar(&OutboundPort, "port", "30303", "listening port")
flag.BoolVar(&UseUPnP, "upnp", false, "enable UPnP support")
flag.IntVar(&MaxPeer, "maxpeer", 10, "maximum desired peers")
Expand All @@ -81,9 +85,9 @@ func Init() {
flag.BoolVar(&NonInteractive, "y", false, "non-interactive mode (say yes to confirmations)")
flag.BoolVar(&UseSeed, "seed", true, "seed peers")
flag.BoolVar(&GenAddr, "genaddr", false, "create a new priv/pub key")
flag.BoolVar(&ExportKey, "export", false, "export private 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(&ImportKey, "import", "", "imports the given private key (hex)")
flag.StringVar(&Datadir, "datadir", defaultDataDir(), "specifies the datadir to use")
flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file")
flag.StringVar(&DebugFile, "debug", "", "debug file (no debugging if not set)")
Expand Down
10 changes: 7 additions & 3 deletions ethereal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ func main() {

utils.InitLogging(Datadir, LogFile, LogLevel, DebugFile)

ethereum := utils.NewEthereum(UseUPnP, OutboundPort, MaxPeer)
db := utils.NewDatabase()

keyManager := utils.NewKeyManager(KeyStore, Datadir, db)

// create, import, export keys
utils.KeyTasks(GenAddr, ImportKey, ExportKey, NonInteractive)
utils.KeyTasks(keyManager, KeyRing, GenAddr, SecretFile, ExportDir, NonInteractive)

ethereum := utils.NewEthereum(db, keyManager, UseUPnP, OutboundPort, MaxPeer)

if ShowGenesis {
utils.ShowGenesis(ethereum)
Expand All @@ -43,7 +47,7 @@ func main() {
utils.StartRpc(ethereum, RpcPort)
}

gui := ethui.New(ethereum, LogLevel)
gui := ethui.New(ethereum, KeyRing, LogLevel)

utils.RegisterInterrupt(func(os.Signal) {
gui.Stop()
Expand Down
4 changes: 2 additions & 2 deletions ethereal/ui/debugger.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (self *DebuggerWindow) SetData(data string) {
self.win.Set("dataText", data)
}
func (self *DebuggerWindow) SetAsm(data string) {
dis := ethchain.Disassemble(ethutil.FromHex(data))
dis := ethchain.Disassemble(ethutil.Hex2Bytes(data))
for _, str := range dis {
self.win.Root().Call("setAsm", str)
}
Expand Down Expand Up @@ -101,7 +101,7 @@ func (self *DebuggerWindow) Debug(valueStr, gasStr, gasPriceStr, scriptStr, data
gasPrice = ethutil.Big(gasPriceStr)
value = ethutil.Big(valueStr)
// Contract addr as test address
keyPair = ethutil.GetKeyRing().Get(0)
keyPair = self.lib.eth.KeyManager().KeyPair()
callerTx = ethchain.NewContractCreationTx(ethutil.Big(valueStr), gas, gasPrice, script)
)
callerTx.Sign(keyPair.PrivateKey)
Expand Down
4 changes: 2 additions & 2 deletions ethereal/ui/ext_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ out:
func (app *ExtApplication) Watch(addr, storageAddr string) {
var event string
if len(storageAddr) == 0 {
event = "object:" + string(ethutil.FromHex(addr))
event = "object:" + string(ethutil.Hex2Bytes(addr))
app.lib.eth.Reactor().Subscribe(event, app.changeChan)
} else {
event = "storage:" + string(ethutil.FromHex(addr)) + ":" + string(ethutil.FromHex(storageAddr))
event = "storage:" + string(ethutil.Hex2Bytes(addr)) + ":" + string(ethutil.Hex2Bytes(storageAddr))
app.lib.eth.Reactor().Subscribe(event, app.changeChan)
}

Expand Down
88 changes: 52 additions & 36 deletions ethereal/ui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,28 @@ type Gui struct {
eth *eth.Ethereum

// The public Ethereum library
lib *EthLib
uiLib *UiLib

txDb *ethdb.LDBDatabase

addr []byte

pub *ethpub.PEthereum
logLevel ethlog.LogLevel
open bool

Session string
}

// Create GUI, but doesn't start it
func New(ethereum *eth.Ethereum, logLevel int) *Gui {
lib := &EthLib{stateManager: ethereum.StateManager(), blockChain: ethereum.BlockChain(), txPool: ethereum.TxPool()}
func New(ethereum *eth.Ethereum, session string, logLevel int) *Gui {

db, err := ethdb.NewLDBDatabase("tx_database")
if err != nil {
panic(err)
}

// On first run we won't have any keys yet, so this would crash.
// Therefor we check if we are ready to actually start this process
var addr []byte
if ethutil.GetKeyRing().Len() != 0 {
addr = ethutil.GetKeyRing().Get(0).Address()
}

pub := ethpub.NewPEthereum(ethereum)

return &Gui{eth: ethereum, lib: lib, txDb: db, addr: addr, pub: pub, logLevel: ethlog.LogLevel(logLevel), open: false}
return &Gui{eth: ethereum, txDb: db, pub: pub, logLevel: ethlog.LogLevel(logLevel), Session: session, open: false}
}

func (gui *Gui) Start(assetPath string) {
Expand Down Expand Up @@ -158,12 +150,11 @@ func (gui *Gui) showWallet(context *qml.Context) (*qml.Window, error) {
}

func (gui *Gui) showKeyImport(context *qml.Context) (*qml.Window, error) {
context.SetVar("lib", gui.lib)
context.SetVar("lib", gui)
component, err := gui.engine.LoadFile(gui.uiLib.AssetPath("qml/first_run.qml"))
if err != nil {
return nil, err
}

return gui.createWindow(component), nil
}

Expand All @@ -175,15 +166,36 @@ func (gui *Gui) createWindow(comp qml.Object) *qml.Window {

return gui.win
}

func (gui *Gui) ImportAndSetPrivKey(secret string) bool {
err := gui.eth.KeyManager().InitFromString(gui.Session, 0, secret)
if err != nil {
logger.Errorln("unable to import: ", err)
return false
}
logger.Errorln("successfully imported: ", err)
return true
}

func (gui *Gui) CreateAndSetPrivKey() (string, string, string, string) {
err := gui.eth.KeyManager().Init(gui.Session, 0, true)
if err != nil {
logger.Errorln("unable to create key: ", err)
return "", "", "", ""
}
return gui.eth.KeyManager().KeyPair().AsStrings()
}

func (gui *Gui) setInitialBlockChain() {
sBlk := gui.eth.BlockChain().LastBlockHash
blk := gui.eth.BlockChain().GetBlock(sBlk)
for ; blk != nil; blk = gui.eth.BlockChain().GetBlock(sBlk) {
sBlk = blk.PrevHash
addr := gui.address()

// Loop through all transactions to see if we missed any while being offline
for _, tx := range blk.Transactions() {
if bytes.Compare(tx.Sender(), gui.addr) == 0 || bytes.Compare(tx.Recipient, gui.addr) == 0 {
if bytes.Compare(tx.Sender(), addr) == 0 || bytes.Compare(tx.Recipient, addr) == 0 {
if ok, _ := gui.txDb.Get(tx.Hash()); ok == nil {
gui.txDb.Put(tx.Hash(), tx.RlpEncode())
}
Expand All @@ -199,25 +211,26 @@ type address struct {
Name, Address string
}

var namereg = ethutil.FromHex("bb5f186604d057c1c5240ca2ae0f6430138ac010")
var namereg = ethutil.Hex2Bytes("bb5f186604d057c1c5240ca2ae0f6430138ac010")

func (gui *Gui) loadAddressBook() {
gui.win.Root().Call("clearAddress")
stateObject := gui.eth.StateManager().CurrentState().GetStateObject(namereg)
if stateObject != nil {
stateObject.State().EachStorage(func(name string, value *ethutil.Value) {
gui.win.Root().Call("addAddress", struct{ Name, Address string }{name, ethutil.Hex(value.Bytes())})
gui.win.Root().Call("addAddress", struct{ Name, Address string }{name, ethutil.Bytes2Hex(value.Bytes())})
})
}
}

func (gui *Gui) readPreviousTransactions() {
it := gui.txDb.Db().NewIterator(nil, nil)
addr := gui.address()
for it.Next() {
tx := ethchain.NewTransactionFromBytes(it.Value())

var inout string
if bytes.Compare(tx.Sender(), gui.addr) == 0 {
if bytes.Compare(tx.Sender(), addr) == 0 {
inout = "send"
} else {
inout = "recv"
Expand Down Expand Up @@ -269,29 +282,29 @@ func (gui *Gui) update() {
state := gui.eth.StateManager().TransState()

unconfirmedFunds := new(big.Int)
gui.win.Root().Call("setWalletValue", fmt.Sprintf("%v", ethutil.CurrencyToString(state.GetAccount(gui.addr).Amount)))
gui.win.Root().Call("setWalletValue", fmt.Sprintf("%v", ethutil.CurrencyToString(state.GetAccount(gui.address()).Amount)))

for {
select {
case b := <-blockChan:
block := b.Resource.(*ethchain.Block)
gui.processBlock(block, false)
if bytes.Compare(block.Coinbase, gui.addr) == 0 {
gui.setWalletValue(gui.eth.StateManager().CurrentState().GetAccount(gui.addr).Amount, nil)
if bytes.Compare(block.Coinbase, gui.address()) == 0 {
gui.setWalletValue(gui.eth.StateManager().CurrentState().GetAccount(gui.address()).Amount, nil)
}

case txMsg := <-txChan:
tx := txMsg.Resource.(*ethchain.Transaction)

if txMsg.Event == "newTx:pre" {
object := state.GetAccount(gui.addr)
object := state.GetAccount(gui.address())

if bytes.Compare(tx.Sender(), gui.addr) == 0 {
if bytes.Compare(tx.Sender(), gui.address()) == 0 {
gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "send")
gui.txDb.Put(tx.Hash(), tx.RlpEncode())

unconfirmedFunds.Sub(unconfirmedFunds, tx.Value)
} else if bytes.Compare(tx.Recipient, gui.addr) == 0 {
} else if bytes.Compare(tx.Recipient, gui.address()) == 0 {
gui.win.Root().Call("addTx", ethpub.NewPTx(tx), "recv")
gui.txDb.Put(tx.Hash(), tx.RlpEncode())

Expand All @@ -300,10 +313,10 @@ func (gui *Gui) update() {

gui.setWalletValue(object.Amount, unconfirmedFunds)
} else {
object := state.GetAccount(gui.addr)
if bytes.Compare(tx.Sender(), gui.addr) == 0 {
object := state.GetAccount(gui.address())
if bytes.Compare(tx.Sender(), gui.address()) == 0 {
object.SubAmount(tx.Value)
} else if bytes.Compare(tx.Recipient, gui.addr) == 0 {
} else if bytes.Compare(tx.Recipient, gui.address()) == 0 {
object.AddAmount(tx.Value)
}

Expand All @@ -330,22 +343,25 @@ func (gui *Gui) setPeerInfo() {
}
}

func (gui *Gui) privateKey() string {
return ethutil.Bytes2Hex(gui.eth.KeyManager().PrivateKey())
}

func (gui *Gui) address() []byte {
return gui.eth.KeyManager().Address()
}

func (gui *Gui) RegisterName(name string) {
keyPair := ethutil.GetKeyRing().Get(0)
name = fmt.Sprintf("\"%s\"\n1", name)
gui.pub.Transact(ethutil.Hex(keyPair.PrivateKey), "namereg", "1000", "1000000", "150", name)
gui.pub.Transact(gui.privateKey(), "namereg", "1000", "1000000", "150", name)
}

func (gui *Gui) Transact(recipient, value, gas, gasPrice, data string) (*ethpub.PReceipt, error) {
keyPair := ethutil.GetKeyRing().Get(0)

return gui.pub.Transact(ethutil.Hex(keyPair.PrivateKey), recipient, value, gas, gasPrice, data)
return gui.pub.Transact(gui.privateKey(), recipient, value, gas, gasPrice, data)
}

func (gui *Gui) Create(recipient, value, gas, gasPrice, data string) (*ethpub.PReceipt, error) {
keyPair := ethutil.GetKeyRing().Get(0)

return gui.pub.Transact(ethutil.Hex(keyPair.PrivateKey), recipient, value, gas, gasPrice, data)
return gui.pub.Transact(gui.privateKey(), recipient, value, gas, gasPrice, data)
}

func (gui *Gui) ChangeClientId(id string) {
Expand Down
2 changes: 1 addition & 1 deletion ethereal/ui/html_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (app *HtmlApplication) Window() *qml.Window {
}

func (app *HtmlApplication) NewBlock(block *ethchain.Block) {
b := &ethpub.PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Hex(block.Hash())}
b := &ethpub.PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Bytes2Hex(block.Hash())}
app.webView.Call("onNewBlockCb", b)
}

Expand Down
46 changes: 0 additions & 46 deletions ethereal/ui/library.go

This file was deleted.

2 changes: 1 addition & 1 deletion ethereal/ui/qml_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (app *QmlApplication) NewWatcher(quitChan chan bool) {

// Events
func (app *QmlApplication) NewBlock(block *ethchain.Block) {
pblock := &ethpub.PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Hex(block.Hash())}
pblock := &ethpub.PBlock{Number: int(block.BlockInfo().Number), Hash: ethutil.Bytes2Hex(block.Hash())}
app.win.Call("onNewBlockCb", pblock)
}

Expand Down
4 changes: 2 additions & 2 deletions ethereal/ui/ui_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ func (ui *UiLib) AssetPath(p string) string {

func (self *UiLib) StartDbWithContractAndData(contractHash, data string) {
dbWindow := NewDebuggerWindow(self)
object := self.eth.StateManager().CurrentState().GetStateObject(ethutil.FromHex(contractHash))
object := self.eth.StateManager().CurrentState().GetStateObject(ethutil.Hex2Bytes(contractHash))
if len(object.Script()) > 0 {
dbWindow.SetCode("0x" + ethutil.Hex(object.Script()))
dbWindow.SetCode("0x" + ethutil.Bytes2Hex(object.Script()))
}
dbWindow.SetData("0x" + data)

Expand Down
Loading