go-ethereum/xeth/types.go

268 lines
6.8 KiB
Go
Raw Normal View History

2014-10-31 08:30:08 -05:00
package xeth
import (
"bytes"
"fmt"
"strings"
2014-12-04 03:28:02 -06:00
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
2014-10-31 06:37:43 -05:00
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp"
2014-10-31 08:43:14 -05:00
"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
}
2015-01-28 11:35:49 -06:00
type Object struct {
*state.StateObject
}
func NewObject(state *state.StateObject) *Object {
return &Object{state}
}
func (self *Object) StorageString(str string) *ethutil.Value {
if ethutil.IsHex(str) {
return self.storage(ethutil.Hex2Bytes(str[2:]))
2015-01-28 11:35:49 -06:00
} else {
return self.storage(ethutil.RightPadBytes([]byte(str), 32))
2015-01-28 11:35:49 -06:00
}
}
func (self *Object) StorageValue(addr *ethutil.Value) *ethutil.Value {
return self.storage(addr.Bytes())
2015-01-28 11:35:49 -06:00
}
func (self *Object) storage(addr []byte) *ethutil.Value {
2015-01-28 11:35:49 -06:00
return self.StateObject.GetStorage(ethutil.BigD(addr))
}
func (self *Object) Storage() (storage map[string]string) {
storage = make(map[string]string)
it := self.StateObject.Trie().Iterator()
for it.Next() {
var data []byte
rlp.Decode(bytes.NewReader(it.Value), &data)
storage[toHex(it.Key)] = toHex(data)
}
self.StateObject.Trie().PrintRoot()
return
}
// Block interface exposed to QML
2015-01-28 11:35:49 -06:00
type Block struct {
//Transactions string `json:"transactions"`
ref *types.Block
Size string `json:"size"`
Number int `json:"number"`
Hash string `json:"hash"`
Transactions *ethutil.List `json:"transactions"`
2014-10-31 04:50:16 -05:00
Uncles *ethutil.List `json:"uncles"`
Time int64 `json:"time"`
Coinbase string `json:"coinbase"`
Name string `json:"name"`
GasLimit string `json:"gasLimit"`
GasUsed string `json:"gasUsed"`
2014-09-25 09:57:49 -05:00
PrevHash string `json:"prevHash"`
Bloom string `json:"bloom"`
Raw string `json:"raw"`
}
// Creates a new QML Block from a chain block
2015-01-28 11:35:49 -06:00
func NewBlock(block *types.Block) *Block {
if block == nil {
2015-01-28 11:35:49 -06:00
return &Block{}
}
2015-01-28 11:35:49 -06:00
ptxs := make([]*Transaction, len(block.Transactions()))
2014-10-31 04:50:16 -05:00
for i, tx := range block.Transactions() {
2015-01-28 11:35:49 -06:00
ptxs[i] = NewTx(tx)
}
2014-10-31 04:50:16 -05:00
txlist := ethutil.NewList(ptxs)
2015-01-28 11:35:49 -06:00
puncles := make([]*Block, len(block.Uncles()))
for i, uncle := range block.Uncles() {
2015-01-28 11:35:49 -06:00
puncles[i] = NewBlock(types.NewBlockWithHeader(uncle))
2014-10-31 04:50:16 -05:00
}
ulist := ethutil.NewList(puncles)
2015-01-28 11:35:49 -06:00
return &Block{
2014-09-25 09:57:49 -05:00
ref: block, Size: block.Size().String(),
Number: int(block.NumberU64()), GasUsed: block.GasUsed().String(),
GasLimit: block.GasLimit().String(), Hash: toHex(block.Hash()),
2014-10-31 04:50:16 -05:00
Transactions: txlist, Uncles: ulist,
Time: block.Time(),
Coinbase: toHex(block.Coinbase()),
PrevHash: toHex(block.ParentHash()),
Bloom: toHex(block.Bloom()),
Raw: block.String(),
2014-09-25 09:57:49 -05:00
}
}
2015-01-28 11:35:49 -06:00
func (self *Block) ToString() string {
if self.ref != nil {
return self.ref.String()
}
return ""
}
2015-01-28 11:35:49 -06:00
func (self *Block) GetTransaction(hash string) *Transaction {
tx := self.ref.Transaction(fromHex(hash))
if tx == nil {
return nil
}
2015-01-28 11:35:49 -06:00
return NewTx(tx)
}
2015-01-28 11:35:49 -06:00
type Transaction struct {
ref *types.Transaction
Value string `json:"value"`
Gas string `json:"gas"`
GasPrice string `json:"gasPrice"`
Hash string `json:"hash"`
Address string `json:"address"`
Sender string `json:"sender"`
RawData string `json:"rawData"`
Data string `json:"data"`
Contract bool `json:"isContract"`
CreatesContract bool `json:"createsContract"`
Confirmations int `json:"confirmations"`
}
2015-01-28 11:35:49 -06:00
func NewTx(tx *types.Transaction) *Transaction {
hash := toHex(tx.Hash())
receiver := toHex(tx.To())
if receiver == "0000000000000000000000000000000000000000" {
receiver = toHex(core.AddressFromMessage(tx))
}
sender := toHex(tx.From())
2014-12-18 08:33:22 -06:00
createsContract := core.MessageCreatesContract(tx)
var data string
2014-12-18 08:33:22 -06:00
if createsContract {
data = strings.Join(core.Disassemble(tx.Data()), "\n")
} else {
data = toHex(tx.Data())
}
2015-01-28 11:35:49 -06:00
return &Transaction{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())}
}
2015-01-28 11:35:49 -06:00
func (self *Transaction) ToString() string {
return self.ref.String()
}
2015-01-28 11:35:49 -06:00
type Key struct {
Address string `json:"address"`
PrivateKey string `json:"privateKey"`
PublicKey string `json:"publicKey"`
}
2015-01-28 11:35:49 -06:00
func NewKey(key *crypto.KeyPair) *Key {
return &Key{toHex(key.Address()), toHex(key.PrivateKey), toHex(key.PublicKey)}
}
type PReceipt struct {
CreatedContract bool `json:"createdContract"`
Address string `json:"address"`
Hash string `json:"hash"`
Sender string `json:"sender"`
}
func NewPReciept(contractCreation bool, creationAddress, hash, address []byte) *PReceipt {
return &PReceipt{
contractCreation,
toHex(creationAddress),
toHex(hash),
toHex(address),
}
}
// Peer interface exposed to QML
2015-01-28 11:35:49 -06:00
type Peer struct {
ref *p2p.Peer
Ip string `json:"ip"`
Version string `json:"version"`
Caps string `json:"caps"`
}
2015-01-28 11:35:49 -06:00
func NewPeer(peer *p2p.Peer) *Peer {
var caps []string
for _, cap := range peer.Caps() {
caps = append(caps, fmt.Sprintf("%s/%d", cap.Name, cap.Version))
}
2015-01-28 11:35:49 -06:00
return &Peer{
ref: peer,
2015-01-06 06:18:09 -06:00
Ip: fmt.Sprintf("%v", peer.RemoteAddr()),
Version: fmt.Sprintf("%v", peer.Identity()),
Caps: fmt.Sprintf("%v", caps),
}
}
2015-01-28 11:35:49 -06:00
type Receipt struct {
CreatedContract bool `json:"createdContract"`
Address string `json:"address"`
Hash string `json:"hash"`
Sender string `json:"sender"`
}
2015-01-28 11:35:49 -06:00
func NewReciept(contractCreation bool, creationAddress, hash, address []byte) *Receipt {
return &Receipt{
contractCreation,
toHex(creationAddress),
toHex(hash),
toHex(address),
}
}
2014-08-18 03:17:45 -05:00
2015-01-28 11:35:49 -06:00
type Message struct {
2014-08-18 03:17:45 -05:00
To string `json:"to"`
From string `json:"from"`
Input string `json:"input"`
Output string `json:"output"`
Path int32 `json:"path"`
Origin string `json:"origin"`
Timestamp int32 `json:"timestamp"`
Coinbase string `json:"coinbase"`
Block string `json:"block"`
Number int32 `json:"number"`
Value string `json:"value"`
}
2015-01-28 11:35:49 -06:00
func NewMessage(message *state.Message) Message {
return Message{
To: toHex(message.To),
From: toHex(message.From),
Input: toHex(message.Input),
Output: toHex(message.Output),
2014-08-18 03:17:45 -05:00
Path: int32(message.Path),
Origin: toHex(message.Origin),
2014-08-18 03:17:45 -05:00
Timestamp: int32(message.Timestamp),
Coinbase: toHex(message.Origin),
Block: toHex(message.Block),
2014-08-18 03:17:45 -05:00
Number: int32(message.Number.Int64()),
Value: message.Value.String(),
}
}