aboutsummaryrefslogtreecommitdiffstats
path: root/xeth
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-01-10 07:51:56 +0800
committerobscuren <geffobscura@gmail.com>2015-01-10 07:51:56 +0800
commite3da85faedf21a3ddb73a0fa29decf65364e6c39 (patch)
tree516faf70a5346bec9016bbdd308b6bcc23d5bccb /xeth
parent491c23a728e4f5cdedfa040aded6a6b136f6bee0 (diff)
downloadgo-tangerine-e3da85faedf21a3ddb73a0fa29decf65364e6c39.tar.gz
go-tangerine-e3da85faedf21a3ddb73a0fa29decf65364e6c39.tar.zst
go-tangerine-e3da85faedf21a3ddb73a0fa29decf65364e6c39.zip
Implemented filter for ws + fixes
* proper 0xhex * filters fixed * start of filter manager * accounts for ws. Closes #246
Diffstat (limited to 'xeth')
-rw-r--r--xeth/hexface.go73
-rw-r--r--xeth/js_types.go63
2 files changed, 69 insertions, 67 deletions
diff --git a/xeth/hexface.go b/xeth/hexface.go
index c3d8cef86..808ae7d39 100644
--- a/xeth/hexface.go
+++ b/xeth/hexface.go
@@ -20,7 +20,7 @@ func NewJSXEth(eth core.EthManager) *JSXEth {
}
func (self *JSXEth) BlockByHash(strHash string) *JSBlock {
- hash := ethutil.Hex2Bytes(strHash)
+ hash := fromHex(strHash)
block := self.obj.ChainManager().GetBlock(hash)
return NewJSBlock(block)
@@ -50,8 +50,12 @@ func (self *JSXEth) Key() *JSKey {
return NewJSKey(self.obj.KeyManager().KeyPair())
}
+func (self *JSXEth) Accounts() []string {
+ return []string{toHex(self.obj.KeyManager().Address())}
+}
+
func (self *JSXEth) StateObject(addr string) *JSObject {
- object := &Object{self.World().safeGet(ethutil.Hex2Bytes(addr))}
+ object := &Object{self.World().safeGet(fromHex(addr))}
return NewJSObject(object)
}
@@ -78,7 +82,7 @@ func (self *JSXEth) IsListening() bool {
}
func (self *JSXEth) CoinBase() string {
- return ethutil.Bytes2Hex(self.obj.KeyManager().Address())
+ return toHex(self.obj.KeyManager().Address())
}
func (self *JSXEth) NumberToHuman(balance string) string {
@@ -88,46 +92,46 @@ func (self *JSXEth) NumberToHuman(balance string) string {
}
func (self *JSXEth) StorageAt(addr, storageAddr string) string {
- storage := self.World().SafeGet(ethutil.Hex2Bytes(addr)).Storage(ethutil.Hex2Bytes(storageAddr))
+ storage := self.World().SafeGet(fromHex(addr)).Storage(fromHex(storageAddr))
- return ethutil.Bytes2Hex(storage.Bytes())
+ return toHex(storage.Bytes())
}
func (self *JSXEth) BalanceAt(addr string) string {
- return self.World().SafeGet(ethutil.Hex2Bytes(addr)).Balance().String()
+ return self.World().SafeGet(fromHex(addr)).Balance().String()
}
func (self *JSXEth) TxCountAt(address string) int {
- return int(self.World().SafeGet(ethutil.Hex2Bytes(address)).Nonce)
+ return int(self.World().SafeGet(fromHex(address)).Nonce)
}
func (self *JSXEth) CodeAt(address string) string {
- return ethutil.Bytes2Hex(self.World().SafeGet(ethutil.Hex2Bytes(address)).Code)
+ return toHex(self.World().SafeGet(fromHex(address)).Code)
}
func (self *JSXEth) IsContract(address string) bool {
- return len(self.World().SafeGet(ethutil.Hex2Bytes(address)).Code) > 0
+ return len(self.World().SafeGet(fromHex(address)).Code) > 0
}
func (self *JSXEth) SecretToAddress(key string) string {
- pair, err := crypto.NewKeyPairFromSec(ethutil.Hex2Bytes(key))
+ pair, err := crypto.NewKeyPairFromSec(fromHex(key))
if err != nil {
return ""
}
- return ethutil.Bytes2Hex(pair.Address())
+ return toHex(pair.Address())
}
func (self *JSXEth) Execute(addr, value, gas, price, data string) (string, error) {
ret, err := self.ExecuteObject(&Object{
- self.World().safeGet(ethutil.Hex2Bytes(addr))},
- ethutil.Hex2Bytes(data),
+ self.World().safeGet(fromHex(addr))},
+ fromHex(data),
ethutil.NewValue(value),
ethutil.NewValue(gas),
ethutil.NewValue(price),
)
- return ethutil.Bytes2Hex(ret), err
+ return toHex(ret), err
}
type KeyVal struct {
@@ -137,10 +141,10 @@ type KeyVal struct {
func (self *JSXEth) EachStorage(addr string) string {
var values []KeyVal
- object := self.World().SafeGet(ethutil.Hex2Bytes(addr))
+ object := self.World().SafeGet(fromHex(addr))
it := object.Trie().Iterator()
for it.Next() {
- values = append(values, KeyVal{ethutil.Bytes2Hex(it.Key), ethutil.Bytes2Hex(it.Value)})
+ values = append(values, KeyVal{toHex(it.Key), toHex(it.Value)})
}
valuesJson, err := json.Marshal(values)
@@ -154,7 +158,7 @@ func (self *JSXEth) EachStorage(addr string) string {
func (self *JSXEth) ToAscii(str string) string {
padded := ethutil.RightPadBytes([]byte(str), 32)
- return "0x" + ethutil.Bytes2Hex(padded)
+ return "0x" + toHex(padded)
}
func (self *JSXEth) FromAscii(str string) string {
@@ -162,7 +166,7 @@ func (self *JSXEth) FromAscii(str string) string {
str = str[2:]
}
- return string(bytes.Trim(ethutil.Hex2Bytes(str), "\x00"))
+ return string(bytes.Trim(fromHex(str), "\x00"))
}
func (self *JSXEth) FromNumber(str string) string {
@@ -170,7 +174,7 @@ func (self *JSXEth) FromNumber(str string) string {
str = str[2:]
}
- return ethutil.BigD(ethutil.Hex2Bytes(str)).String()
+ return ethutil.BigD(fromHex(str)).String()
}
func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr string) (string, error) {
@@ -182,26 +186,11 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr
data []byte
)
- if ethutil.IsHex(codeStr) {
- data = ethutil.Hex2Bytes(codeStr[2:])
- } else {
- data = ethutil.Hex2Bytes(codeStr)
- }
-
- if ethutil.IsHex(toStr) {
- to = ethutil.Hex2Bytes(toStr[2:])
- } else {
- to = ethutil.Hex2Bytes(toStr)
- }
+ data = fromHex(codeStr)
- var keyPair *crypto.KeyPair
- var err error
- if ethutil.IsHex(key) {
- keyPair, err = crypto.NewKeyPairFromSec([]byte(ethutil.Hex2Bytes(key[2:])))
- } else {
- keyPair, err = crypto.NewKeyPairFromSec([]byte(ethutil.Hex2Bytes(key)))
- }
+ to = fromHex(toStr)
+ keyPair, err := crypto.NewKeyPairFromSec([]byte(fromHex(key)))
if err != nil {
return "", err
}
@@ -211,14 +200,14 @@ func (self *JSXEth) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr
return "", err
}
if types.IsContractAddr(to) {
- return ethutil.Bytes2Hex(core.AddressFromMessage(tx)), nil
+ return toHex(core.AddressFromMessage(tx)), nil
}
- return ethutil.Bytes2Hex(tx.Hash()), nil
+ return toHex(tx.Hash()), nil
}
func (self *JSXEth) PushTx(txStr string) (*JSReceipt, error) {
- tx := types.NewTransactionFromBytes(ethutil.Hex2Bytes(txStr))
+ tx := types.NewTransactionFromBytes(fromHex(txStr))
err := self.obj.TxPool().Add(tx)
if err != nil {
return nil, err
@@ -233,11 +222,11 @@ func (self *JSXEth) CompileMutan(code string) string {
return err.Error()
}
- return ethutil.Bytes2Hex(data)
+ return toHex(data)
}
func (self *JSXEth) FindInConfig(str string) string {
- return ethutil.Bytes2Hex(self.World().Config().Get(str).Address())
+ return toHex(self.World().Config().Get(str).Address())
}
func ToJSMessages(messages state.Messages) *ethutil.List {
diff --git a/xeth/js_types.go b/xeth/js_types.go
index 5fe5946b1..a0be996ff 100644
--- a/xeth/js_types.go
+++ b/xeth/js_types.go
@@ -12,6 +12,19 @@ import (
"github.com/ethereum/go-ethereum/state"
)
+func toHex(b []byte) string {
+ return "0x" + ethutil.Bytes2Hex(b)
+}
+func fromHex(s string) []byte {
+ if len(s) > 1 {
+ if s[0:2] == "0x" {
+ s = s[2:]
+ }
+ return ethutil.Hex2Bytes(s)
+ }
+ return nil
+}
+
// Block interface exposed to QML
type JSBlock struct {
//Transactions string `json:"transactions"`
@@ -52,12 +65,12 @@ func NewJSBlock(block *types.Block) *JSBlock {
return &JSBlock{
ref: block, Size: block.Size().String(),
Number: int(block.NumberU64()), GasUsed: block.GasUsed().String(),
- GasLimit: block.GasLimit().String(), Hash: ethutil.Bytes2Hex(block.Hash()),
+ GasLimit: block.GasLimit().String(), Hash: toHex(block.Hash()),
Transactions: txlist, Uncles: ulist,
Time: block.Time(),
- Coinbase: ethutil.Bytes2Hex(block.Coinbase()),
- PrevHash: ethutil.Bytes2Hex(block.ParentHash()),
- Bloom: ethutil.Bytes2Hex(block.Bloom()),
+ Coinbase: toHex(block.Coinbase()),
+ PrevHash: toHex(block.ParentHash()),
+ Bloom: toHex(block.Bloom()),
Raw: block.String(),
}
}
@@ -71,7 +84,7 @@ func (self *JSBlock) ToString() string {
}
func (self *JSBlock) GetTransaction(hash string) *JSTransaction {
- tx := self.ref.Transaction(ethutil.Hex2Bytes(hash))
+ tx := self.ref.Transaction(fromHex(hash))
if tx == nil {
return nil
}
@@ -96,22 +109,22 @@ type JSTransaction struct {
}
func NewJSTx(tx *types.Transaction) *JSTransaction {
- hash := ethutil.Bytes2Hex(tx.Hash())
- receiver := ethutil.Bytes2Hex(tx.To())
+ hash := toHex(tx.Hash())
+ receiver := toHex(tx.To())
if receiver == "0000000000000000000000000000000000000000" {
- receiver = ethutil.Bytes2Hex(core.AddressFromMessage(tx))
+ receiver = toHex(core.AddressFromMessage(tx))
}
- sender := ethutil.Bytes2Hex(tx.From())
+ sender := toHex(tx.From())
createsContract := core.MessageCreatesContract(tx)
var data string
if createsContract {
data = strings.Join(core.Disassemble(tx.Data()), "\n")
} else {
- data = ethutil.Bytes2Hex(tx.Data())
+ data = toHex(tx.Data())
}
- return &JSTransaction{ref: tx, Hash: hash, Value: ethutil.CurrencyToString(tx.Value()), Address: receiver, Contract: createsContract, Gas: tx.Gas().String(), GasPrice: tx.GasPrice().String(), Data: data, Sender: sender, CreatesContract: createsContract, RawData: ethutil.Bytes2Hex(tx.Data())}
+ return &JSTransaction{ref: tx, Hash: hash, Value: ethutil.CurrencyToString(tx.Value()), Address: receiver, Contract: createsContract, Gas: tx.Gas().String(), GasPrice: tx.GasPrice().String(), Data: data, Sender: sender, CreatesContract: createsContract, RawData: toHex(tx.Data())}
}
func (self *JSTransaction) ToString() string {
@@ -125,7 +138,7 @@ type JSKey struct {
}
func NewJSKey(key *crypto.KeyPair) *JSKey {
- return &JSKey{ethutil.Bytes2Hex(key.Address()), ethutil.Bytes2Hex(key.PrivateKey), ethutil.Bytes2Hex(key.PublicKey)}
+ return &JSKey{toHex(key.Address()), toHex(key.PrivateKey), toHex(key.PublicKey)}
}
type JSObject struct {
@@ -146,9 +159,9 @@ type PReceipt struct {
func NewPReciept(contractCreation bool, creationAddress, hash, address []byte) *PReceipt {
return &PReceipt{
contractCreation,
- ethutil.Bytes2Hex(creationAddress),
- ethutil.Bytes2Hex(hash),
- ethutil.Bytes2Hex(address),
+ toHex(creationAddress),
+ toHex(hash),
+ toHex(address),
}
}
@@ -185,9 +198,9 @@ type JSReceipt struct {
func NewJSReciept(contractCreation bool, creationAddress, hash, address []byte) *JSReceipt {
return &JSReceipt{
contractCreation,
- ethutil.Bytes2Hex(creationAddress),
- ethutil.Bytes2Hex(hash),
- ethutil.Bytes2Hex(address),
+ toHex(creationAddress),
+ toHex(hash),
+ toHex(address),
}
}
@@ -207,15 +220,15 @@ type JSMessage struct {
func NewJSMessage(message *state.Message) JSMessage {
return JSMessage{
- To: ethutil.Bytes2Hex(message.To),
- From: ethutil.Bytes2Hex(message.From),
- Input: ethutil.Bytes2Hex(message.Input),
- Output: ethutil.Bytes2Hex(message.Output),
+ To: toHex(message.To),
+ From: toHex(message.From),
+ Input: toHex(message.Input),
+ Output: toHex(message.Output),
Path: int32(message.Path),
- Origin: ethutil.Bytes2Hex(message.Origin),
+ Origin: toHex(message.Origin),
Timestamp: int32(message.Timestamp),
- Coinbase: ethutil.Bytes2Hex(message.Origin),
- Block: ethutil.Bytes2Hex(message.Block),
+ Coinbase: toHex(message.Origin),
+ Block: toHex(message.Block),
Number: int32(message.Number.Int64()),
Value: message.Value.String(),
}