2014-10-31 08:30:08 -05:00
|
|
|
package xeth
|
2014-08-15 06:05:13 -05:00
|
|
|
|
|
|
|
import (
|
2015-01-29 16:58:43 -06:00
|
|
|
"bytes"
|
2015-01-05 12:53:53 -06:00
|
|
|
"fmt"
|
2014-08-15 06:05:13 -05:00
|
|
|
"strings"
|
|
|
|
|
2015-03-18 05:44:25 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2014-12-04 03:28:02 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2015-03-25 18:42:35 -05:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
2014-12-04 03:28:02 -06:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2014-10-31 06:37:43 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2014-12-14 12:09:33 -06:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2015-01-29 16:58:43 -06:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2014-08-15 06:05:13 -05:00
|
|
|
)
|
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
type Object struct {
|
|
|
|
*state.StateObject
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewObject(state *state.StateObject) *Object {
|
|
|
|
return &Object{state}
|
|
|
|
}
|
|
|
|
|
2015-03-16 05:27:38 -05:00
|
|
|
func (self *Object) StorageString(str string) *common.Value {
|
|
|
|
if common.IsHex(str) {
|
|
|
|
return self.storage(common.Hex2Bytes(str[2:]))
|
2015-01-28 11:35:49 -06:00
|
|
|
} else {
|
2015-03-16 05:27:38 -05:00
|
|
|
return self.storage(common.RightPadBytes([]byte(str), 32))
|
2015-01-28 11:35:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-16 05:27:38 -05:00
|
|
|
func (self *Object) StorageValue(addr *common.Value) *common.Value {
|
2015-01-29 13:39:26 -06:00
|
|
|
return self.storage(addr.Bytes())
|
2015-01-28 11:35:49 -06:00
|
|
|
}
|
|
|
|
|
2015-03-16 05:27:38 -05:00
|
|
|
func (self *Object) storage(addr []byte) *common.Value {
|
|
|
|
return self.StateObject.GetStorage(common.BigD(addr))
|
2015-01-28 11:35:49 -06:00
|
|
|
}
|
|
|
|
|
2015-01-29 13:39:26 -06:00
|
|
|
func (self *Object) Storage() (storage map[string]string) {
|
|
|
|
storage = make(map[string]string)
|
|
|
|
|
|
|
|
it := self.StateObject.Trie().Iterator()
|
|
|
|
for it.Next() {
|
2015-01-29 16:58:43 -06:00
|
|
|
var data []byte
|
|
|
|
rlp.Decode(bytes.NewReader(it.Value), &data)
|
2015-03-25 18:42:35 -05:00
|
|
|
storage[common.ToHex(self.Trie().GetKey(it.Key))] = common.ToHex(data)
|
2015-01-29 13:39:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-15 06:05:13 -05:00
|
|
|
// Block interface exposed to QML
|
2015-01-28 11:35:49 -06:00
|
|
|
type Block struct {
|
2014-09-07 17:49:47 -05:00
|
|
|
//Transactions string `json:"transactions"`
|
2014-11-18 09:58:22 -06:00
|
|
|
ref *types.Block
|
2015-03-18 05:44:25 -05:00
|
|
|
Size string `json:"size"`
|
|
|
|
Number int `json:"number"`
|
|
|
|
Hash string `json:"hash"`
|
2015-03-16 05:27:38 -05:00
|
|
|
Transactions *common.List `json:"transactions"`
|
|
|
|
Uncles *common.List `json:"uncles"`
|
2015-03-18 05:44:25 -05:00
|
|
|
Time int64 `json:"time"`
|
|
|
|
Coinbase string `json:"coinbase"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
GasLimit string `json:"gasLimit"`
|
|
|
|
GasUsed string `json:"gasUsed"`
|
|
|
|
PrevHash string `json:"prevHash"`
|
|
|
|
Bloom string `json:"bloom"`
|
|
|
|
Raw string `json:"raw"`
|
2014-08-15 06:05:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a new QML Block from a chain block
|
2015-01-28 11:35:49 -06:00
|
|
|
func NewBlock(block *types.Block) *Block {
|
2014-08-15 06:05:13 -05:00
|
|
|
if block == nil {
|
2015-01-28 11:35:49 -06:00
|
|
|
return &Block{}
|
2014-08-15 06:05:13 -05:00
|
|
|
}
|
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
ptxs := make([]*Transaction, len(block.Transactions()))
|
2015-04-08 15:17:49 -05:00
|
|
|
/*
|
|
|
|
for i, tx := range block.Transactions() {
|
|
|
|
ptxs[i] = NewTx(tx)
|
|
|
|
}
|
|
|
|
*/
|
2015-03-16 05:27:38 -05:00
|
|
|
txlist := common.NewList(ptxs)
|
2014-08-15 06:05:13 -05:00
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
puncles := make([]*Block, len(block.Uncles()))
|
2015-04-08 15:17:49 -05:00
|
|
|
/*
|
|
|
|
for i, uncle := range block.Uncles() {
|
|
|
|
puncles[i] = NewBlock(types.NewBlockWithHeader(uncle))
|
|
|
|
}
|
|
|
|
*/
|
2015-03-16 05:27:38 -05:00
|
|
|
ulist := common.NewList(puncles)
|
2014-08-15 06:05:13 -05:00
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
return &Block{
|
2014-09-25 09:57:49 -05:00
|
|
|
ref: block, Size: block.Size().String(),
|
2014-12-23 06:48:44 -06:00
|
|
|
Number: int(block.NumberU64()), GasUsed: block.GasUsed().String(),
|
2015-03-18 12:23:37 -05:00
|
|
|
GasLimit: block.GasLimit().String(), Hash: block.Hash().Hex(),
|
2014-10-31 04:50:16 -05:00
|
|
|
Transactions: txlist, Uncles: ulist,
|
2014-12-23 06:48:44 -06:00
|
|
|
Time: block.Time(),
|
2015-03-18 12:23:37 -05:00
|
|
|
Coinbase: block.Coinbase().Hex(),
|
|
|
|
PrevHash: block.ParentHash().Hex(),
|
|
|
|
Bloom: common.ToHex(block.Bloom().Bytes()),
|
2014-11-09 18:17:31 -06:00
|
|
|
Raw: block.String(),
|
2014-09-25 09:57:49 -05:00
|
|
|
}
|
2014-08-15 06:05:13 -05:00
|
|
|
}
|
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
func (self *Block) ToString() string {
|
2014-08-15 06:05:13 -05:00
|
|
|
if self.ref != nil {
|
|
|
|
return self.ref.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
func (self *Block) GetTransaction(hash string) *Transaction {
|
2015-03-18 05:44:25 -05:00
|
|
|
tx := self.ref.Transaction(common.HexToHash(hash))
|
2014-08-15 06:05:13 -05:00
|
|
|
if tx == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
return NewTx(tx)
|
2014-08-15 06:05:13 -05:00
|
|
|
}
|
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
type Transaction struct {
|
2014-11-18 09:58:22 -06:00
|
|
|
ref *types.Transaction
|
2014-08-15 06:05:13 -05:00
|
|
|
|
|
|
|
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 {
|
2015-06-01 15:00:48 -05:00
|
|
|
sender, err := tx.From()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-18 05:44:25 -05:00
|
|
|
hash := tx.Hash().Hex()
|
2015-04-10 03:20:04 -05:00
|
|
|
|
|
|
|
var receiver string
|
|
|
|
if to := tx.To(); to != nil {
|
|
|
|
receiver = to.Hex()
|
|
|
|
} else {
|
2015-03-18 07:00:01 -05:00
|
|
|
receiver = core.AddressFromMessage(tx).Hex()
|
2014-08-15 06:05:13 -05:00
|
|
|
}
|
2014-12-18 08:33:22 -06:00
|
|
|
createsContract := core.MessageCreatesContract(tx)
|
2014-08-15 06:05:13 -05:00
|
|
|
|
|
|
|
var data string
|
2014-12-18 08:33:22 -06:00
|
|
|
if createsContract {
|
2014-12-18 08:18:13 -06:00
|
|
|
data = strings.Join(core.Disassemble(tx.Data()), "\n")
|
2014-08-15 06:05:13 -05:00
|
|
|
} else {
|
2015-03-16 09:17:19 -05:00
|
|
|
data = common.ToHex(tx.Data())
|
2014-08-15 06:05:13 -05:00
|
|
|
}
|
|
|
|
|
2015-03-18 12:23:37 -05:00
|
|
|
return &Transaction{ref: tx, Hash: hash, Value: common.CurrencyToString(tx.Value()), Address: receiver, Contract: createsContract, Gas: tx.Gas().String(), GasPrice: tx.GasPrice().String(), Data: data, Sender: sender.Hex(), CreatesContract: createsContract, RawData: common.ToHex(tx.Data())}
|
2014-08-15 06:05:13 -05:00
|
|
|
}
|
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
func (self *Transaction) ToString() string {
|
2014-08-15 06:05:13 -05:00
|
|
|
return self.ref.String()
|
|
|
|
}
|
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
type Key struct {
|
2014-08-15 06:05:13 -05:00
|
|
|
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 {
|
2015-03-16 09:17:19 -05:00
|
|
|
return &Key{common.ToHex(key.Address()), common.ToHex(key.PrivateKey), common.ToHex(key.PublicKey)}
|
2014-08-15 06:05:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2015-03-16 09:17:19 -05:00
|
|
|
common.ToHex(creationAddress),
|
|
|
|
common.ToHex(hash),
|
|
|
|
common.ToHex(address),
|
2014-08-15 06:05:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Peer interface exposed to QML
|
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
type Peer struct {
|
2015-01-05 12:53:53 -06:00
|
|
|
ref *p2p.Peer
|
|
|
|
Ip string `json:"ip"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
Caps string `json:"caps"`
|
2014-12-14 12:09:33 -06:00
|
|
|
}
|
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
func NewPeer(peer *p2p.Peer) *Peer {
|
2015-01-05 12:53:53 -06:00
|
|
|
var caps []string
|
|
|
|
for _, cap := range peer.Caps() {
|
|
|
|
caps = append(caps, fmt.Sprintf("%s/%d", cap.Name, cap.Version))
|
|
|
|
}
|
2014-12-14 12:09:33 -06:00
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
return &Peer{
|
2015-01-05 12:53:53 -06:00
|
|
|
ref: peer,
|
2015-01-06 06:18:09 -06:00
|
|
|
Ip: fmt.Sprintf("%v", peer.RemoteAddr()),
|
2015-02-04 20:16:16 -06:00
|
|
|
Version: fmt.Sprintf("%v", peer.ID()),
|
2015-01-05 12:53:53 -06:00
|
|
|
Caps: fmt.Sprintf("%v", caps),
|
|
|
|
}
|
2014-08-15 06:05:13 -05:00
|
|
|
}
|
|
|
|
|
2015-01-28 11:35:49 -06:00
|
|
|
type Receipt struct {
|
2014-08-15 06:05:13 -05:00
|
|
|
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{
|
2014-08-15 06:05:13 -05:00
|
|
|
contractCreation,
|
2015-03-16 09:17:19 -05:00
|
|
|
common.ToHex(creationAddress),
|
|
|
|
common.ToHex(hash),
|
|
|
|
common.ToHex(address),
|
2014-08-15 06:05:13 -05:00
|
|
|
}
|
|
|
|
}
|