diff options
author | Gustav Simonsson <gustav.simonsson@gmail.com> | 2015-02-26 20:22:09 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2015-03-06 21:10:42 +0800 |
commit | bc45e5c6de3052a4c853387dea0af5cd9207f1f7 (patch) | |
tree | 3230b89b3bf25eaf53e1b3b164cb72e89d8c4398 /xeth/xeth.go | |
parent | e64f727529287b7414af6d1f482ea5f318cbd2eb (diff) | |
download | dexon-bc45e5c6de3052a4c853387dea0af5cd9207f1f7.tar dexon-bc45e5c6de3052a4c853387dea0af5cd9207f1f7.tar.gz dexon-bc45e5c6de3052a4c853387dea0af5cd9207f1f7.tar.bz2 dexon-bc45e5c6de3052a4c853387dea0af5cd9207f1f7.tar.lz dexon-bc45e5c6de3052a4c853387dea0af5cd9207f1f7.tar.xz dexon-bc45e5c6de3052a4c853387dea0af5cd9207f1f7.tar.zst dexon-bc45e5c6de3052a4c853387dea0af5cd9207f1f7.zip |
Integrate eth_accounts and eth_transact to use new account manager
* Add from to eth_transact / xeth.Transact and add static pass in lieu
of integrating with native Mist window for user passphrase entry
* Make eth_accounts return AccountManager.Accounts()
* Add a Generate Key menu item in Mist
Diffstat (limited to 'xeth/xeth.go')
-rw-r--r-- | xeth/xeth.go | 40 |
1 files changed, 28 insertions, 12 deletions
diff --git a/xeth/xeth.go b/xeth/xeth.go index 677d40fd5..91bd35f8e 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -7,8 +7,8 @@ package xeth import ( "bytes" "encoding/json" - "fmt" + "github.com/ethereum/go-ethereum/accounts" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" @@ -27,6 +27,7 @@ var pipelogger = logger.NewLogger("XETH") type Backend interface { BlockProcessor() *core.BlockProcessor ChainManager() *core.ChainManager + AccountManager() *accounts.AccountManager TxPool() *core.TxPool PeerCount() int IsListening() bool @@ -42,6 +43,7 @@ type XEth struct { eth Backend blockProcessor *core.BlockProcessor chainManager *core.ChainManager + accountManager *accounts.AccountManager state *State whisper *Whisper miner *miner.Miner @@ -52,6 +54,7 @@ func New(eth Backend) *XEth { eth: eth, blockProcessor: eth.BlockProcessor(), chainManager: eth.ChainManager(), + accountManager: eth.AccountManager(), whisper: NewWhisper(eth.Whisper()), miner: eth.Miner(), } @@ -106,7 +109,13 @@ func (self *XEth) Block(v interface{}) *Block { } func (self *XEth) Accounts() []string { - return []string{toHex(self.eth.KeyManager().Address())} + // TODO: check err? + accounts, _ := self.eth.AccountManager().Accounts() + accountAddresses := make([]string, len(accounts)) + for i, ac := range accounts { + accountAddresses[i] = toHex(ac.Address) + } + return accountAddresses } func (self *XEth) PeerCount() int { @@ -266,17 +275,19 @@ func (self *XEth) Call(toStr, valueStr, gasStr, gasPriceStr, dataStr string) (st return toHex(res), nil } -func (self *XEth) Transact(toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { +func (self *XEth) Transact(fromStr, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) { + var ( + from []byte to []byte value = ethutil.NewValue(valueStr) gas = ethutil.NewValue(gasStr) price = ethutil.NewValue(gasPriceStr) data []byte - key = self.eth.KeyManager().KeyPair() contractCreation bool ) + from = fromHex(fromStr) data = fromHex(codeStr) to = fromHex(toStr) if len(to) == 0 { @@ -290,21 +301,26 @@ func (self *XEth) Transact(toStr, valueStr, gasStr, gasPriceStr, codeStr string) tx = types.NewTransactionMessage(to, value.BigInt(), gas.BigInt(), price.BigInt(), data) } - var err error - state := self.eth.ChainManager().TxState() - if balance := state.GetBalance(key.Address()); balance.Cmp(tx.Value()) < 0 { - return "", fmt.Errorf("insufficient balance. balance=%v tx=%v", balance, tx.Value()) - } - nonce := state.GetNonce(key.Address()) + state := self.chainManager.TransState() + nonce := state.GetNonce(from) tx.SetNonce(nonce) - tx.Sign(key.PrivateKey) + sig, err := self.accountManager.Sign(&accounts.Account{Address: from}, tx.Hash()) + if err != nil { + return "", err + } + tx.SetSignatureValues(sig) err = self.eth.TxPool().Add(tx) if err != nil { return "", err } - state.SetNonce(key.Address(), nonce+1) + state.SetNonce(from, nonce+1) + + if contractCreation { + addr := core.AddressFromMessage(tx) + pipelogger.Infof("Contract addr %x\n", addr) + } if types.IsContractAddr(to) { return toHex(core.AddressFromMessage(tx)), nil |