github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/xeth/types.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package xeth 18 19 import ( 20 "bytes" 21 "fmt" 22 "math/big" 23 "strings" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/core" 27 "github.com/ethereum/go-ethereum/core/state" 28 "github.com/ethereum/go-ethereum/core/types" 29 "github.com/ethereum/go-ethereum/crypto" 30 "github.com/ethereum/go-ethereum/p2p" 31 "github.com/ethereum/go-ethereum/rlp" 32 ) 33 34 type Object struct { 35 *state.StateObject 36 } 37 38 func NewObject(state *state.StateObject) *Object { 39 return &Object{state} 40 } 41 42 func (self *Object) StorageString(str string) []byte { 43 if common.IsHex(str) { 44 return self.storage(common.Hex2Bytes(str[2:])) 45 } else { 46 return self.storage(common.RightPadBytes([]byte(str), 32)) 47 } 48 } 49 50 func (self *Object) StorageValue(addr *common.Value) []byte { 51 return self.storage(addr.Bytes()) 52 } 53 54 func (self *Object) storage(addr []byte) []byte { 55 return self.StateObject.GetState(common.BytesToHash(addr)).Bytes() 56 } 57 58 func (self *Object) Storage() (storage map[string]string) { 59 storage = make(map[string]string) 60 61 it := self.StateObject.Trie().Iterator() 62 for it.Next() { 63 var data []byte 64 rlp.Decode(bytes.NewReader(it.Value), &data) 65 storage[common.ToHex(self.Trie().GetKey(it.Key))] = common.ToHex(data) 66 } 67 68 return 69 } 70 71 // Block interface exposed to QML 72 type Block struct { 73 //Transactions string `json:"transactions"` 74 ref *types.Block 75 Size string `json:"size"` 76 Number int `json:"number"` 77 Hash string `json:"hash"` 78 Transactions *common.List `json:"transactions"` 79 Uncles *common.List `json:"uncles"` 80 Time *big.Int `json:"time"` 81 Coinbase string `json:"coinbase"` 82 Name string `json:"name"` 83 GasLimit string `json:"gasLimit"` 84 GasUsed string `json:"gasUsed"` 85 PrevHash string `json:"prevHash"` 86 Bloom string `json:"bloom"` 87 Raw string `json:"raw"` 88 } 89 90 // Creates a new QML Block from a chain block 91 func NewBlock(block *types.Block) *Block { 92 if block == nil { 93 return &Block{} 94 } 95 96 ptxs := make([]*Transaction, len(block.Transactions())) 97 /* 98 for i, tx := range block.Transactions() { 99 ptxs[i] = NewTx(tx) 100 } 101 */ 102 txlist := common.NewList(ptxs) 103 104 puncles := make([]*Block, len(block.Uncles())) 105 /* 106 for i, uncle := range block.Uncles() { 107 puncles[i] = NewBlock(types.NewBlockWithHeader(uncle)) 108 } 109 */ 110 ulist := common.NewList(puncles) 111 112 return &Block{ 113 ref: block, Size: block.Size().String(), 114 Number: int(block.NumberU64()), GasUsed: block.GasUsed().String(), 115 GasLimit: block.GasLimit().String(), Hash: block.Hash().Hex(), 116 Transactions: txlist, Uncles: ulist, 117 Time: block.Time(), 118 Coinbase: block.Coinbase().Hex(), 119 PrevHash: block.ParentHash().Hex(), 120 Bloom: common.ToHex(block.Bloom().Bytes()), 121 Raw: block.String(), 122 } 123 } 124 125 func (self *Block) ToString() string { 126 if self.ref != nil { 127 return self.ref.String() 128 } 129 130 return "" 131 } 132 133 func (self *Block) GetTransaction(hash string) *Transaction { 134 tx := self.ref.Transaction(common.HexToHash(hash)) 135 if tx == nil { 136 return nil 137 } 138 139 return NewTx(tx) 140 } 141 142 type Transaction struct { 143 ref *types.Transaction 144 145 Value string `json:"value"` 146 Gas string `json:"gas"` 147 GasPrice string `json:"gasPrice"` 148 Hash string `json:"hash"` 149 Address string `json:"address"` 150 Sender string `json:"sender"` 151 RawData string `json:"rawData"` 152 Data string `json:"data"` 153 Contract bool `json:"isContract"` 154 CreatesContract bool `json:"createsContract"` 155 Confirmations int `json:"confirmations"` 156 } 157 158 func NewTx(tx *types.Transaction) *Transaction { 159 sender, err := tx.From() 160 if err != nil { 161 return nil 162 } 163 hash := tx.Hash().Hex() 164 165 var receiver string 166 if to := tx.To(); to != nil { 167 receiver = to.Hex() 168 } else { 169 from, _ := tx.From() 170 receiver = crypto.CreateAddress(from, tx.Nonce()).Hex() 171 } 172 createsContract := core.MessageCreatesContract(tx) 173 174 var data string 175 if createsContract { 176 data = strings.Join(core.Disassemble(tx.Data()), "\n") 177 } else { 178 data = common.ToHex(tx.Data()) 179 } 180 181 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())} 182 } 183 184 func (self *Transaction) ToString() string { 185 return self.ref.String() 186 } 187 188 type PReceipt struct { 189 CreatedContract bool `json:"createdContract"` 190 Address string `json:"address"` 191 Hash string `json:"hash"` 192 Sender string `json:"sender"` 193 } 194 195 func NewPReciept(contractCreation bool, creationAddress, hash, address []byte) *PReceipt { 196 return &PReceipt{ 197 contractCreation, 198 common.ToHex(creationAddress), 199 common.ToHex(hash), 200 common.ToHex(address), 201 } 202 } 203 204 // Peer interface exposed to QML 205 206 type Peer struct { 207 ref *p2p.Peer 208 Ip string `json:"ip"` 209 Version string `json:"version"` 210 Caps string `json:"caps"` 211 } 212 213 func NewPeer(peer *p2p.Peer) *Peer { 214 var caps []string 215 for _, cap := range peer.Caps() { 216 caps = append(caps, fmt.Sprintf("%s/%d", cap.Name, cap.Version)) 217 } 218 219 return &Peer{ 220 ref: peer, 221 Ip: fmt.Sprintf("%v", peer.RemoteAddr()), 222 Version: fmt.Sprintf("%v", peer.ID()), 223 Caps: fmt.Sprintf("%v", caps), 224 } 225 } 226 227 type Receipt struct { 228 CreatedContract bool `json:"createdContract"` 229 Address string `json:"address"` 230 Hash string `json:"hash"` 231 Sender string `json:"sender"` 232 } 233 234 func NewReciept(contractCreation bool, creationAddress, hash, address []byte) *Receipt { 235 return &Receipt{ 236 contractCreation, 237 common.ToHex(creationAddress), 238 common.ToHex(hash), 239 common.ToHex(address), 240 } 241 }