github.com/karalabe/go-ethereum@v0.8.5/xeth/types.go (about) 1 package xeth 2 3 import ( 4 "bytes" 5 "fmt" 6 "strings" 7 8 "github.com/ethereum/go-ethereum/core" 9 "github.com/ethereum/go-ethereum/core/types" 10 "github.com/ethereum/go-ethereum/crypto" 11 "github.com/ethereum/go-ethereum/ethutil" 12 "github.com/ethereum/go-ethereum/p2p" 13 "github.com/ethereum/go-ethereum/rlp" 14 "github.com/ethereum/go-ethereum/state" 15 ) 16 17 func toHex(b []byte) string { 18 return "0x" + ethutil.Bytes2Hex(b) 19 } 20 func fromHex(s string) []byte { 21 if len(s) > 1 { 22 if s[0:2] == "0x" { 23 s = s[2:] 24 } 25 return ethutil.Hex2Bytes(s) 26 } 27 return nil 28 } 29 30 type Object struct { 31 *state.StateObject 32 } 33 34 func NewObject(state *state.StateObject) *Object { 35 return &Object{state} 36 } 37 38 func (self *Object) StorageString(str string) *ethutil.Value { 39 if ethutil.IsHex(str) { 40 return self.storage(ethutil.Hex2Bytes(str[2:])) 41 } else { 42 return self.storage(ethutil.RightPadBytes([]byte(str), 32)) 43 } 44 } 45 46 func (self *Object) StorageValue(addr *ethutil.Value) *ethutil.Value { 47 return self.storage(addr.Bytes()) 48 } 49 50 func (self *Object) storage(addr []byte) *ethutil.Value { 51 return self.StateObject.GetStorage(ethutil.BigD(addr)) 52 } 53 54 func (self *Object) Storage() (storage map[string]string) { 55 storage = make(map[string]string) 56 57 it := self.StateObject.Trie().Iterator() 58 for it.Next() { 59 var data []byte 60 rlp.Decode(bytes.NewReader(it.Value), &data) 61 storage[toHex(it.Key)] = toHex(data) 62 } 63 64 return 65 } 66 67 // Block interface exposed to QML 68 type Block struct { 69 //Transactions string `json:"transactions"` 70 ref *types.Block 71 Size string `json:"size"` 72 Number int `json:"number"` 73 Hash string `json:"hash"` 74 Transactions *ethutil.List `json:"transactions"` 75 Uncles *ethutil.List `json:"uncles"` 76 Time int64 `json:"time"` 77 Coinbase string `json:"coinbase"` 78 Name string `json:"name"` 79 GasLimit string `json:"gasLimit"` 80 GasUsed string `json:"gasUsed"` 81 PrevHash string `json:"prevHash"` 82 Bloom string `json:"bloom"` 83 Raw string `json:"raw"` 84 } 85 86 // Creates a new QML Block from a chain block 87 func NewBlock(block *types.Block) *Block { 88 if block == nil { 89 return &Block{} 90 } 91 92 ptxs := make([]*Transaction, len(block.Transactions())) 93 for i, tx := range block.Transactions() { 94 ptxs[i] = NewTx(tx) 95 } 96 txlist := ethutil.NewList(ptxs) 97 98 puncles := make([]*Block, len(block.Uncles())) 99 for i, uncle := range block.Uncles() { 100 puncles[i] = NewBlock(types.NewBlockWithHeader(uncle)) 101 } 102 ulist := ethutil.NewList(puncles) 103 104 return &Block{ 105 ref: block, Size: block.Size().String(), 106 Number: int(block.NumberU64()), GasUsed: block.GasUsed().String(), 107 GasLimit: block.GasLimit().String(), Hash: toHex(block.Hash()), 108 Transactions: txlist, Uncles: ulist, 109 Time: block.Time(), 110 Coinbase: toHex(block.Coinbase()), 111 PrevHash: toHex(block.ParentHash()), 112 Bloom: toHex(block.Bloom()), 113 Raw: block.String(), 114 } 115 } 116 117 func (self *Block) ToString() string { 118 if self.ref != nil { 119 return self.ref.String() 120 } 121 122 return "" 123 } 124 125 func (self *Block) GetTransaction(hash string) *Transaction { 126 tx := self.ref.Transaction(fromHex(hash)) 127 if tx == nil { 128 return nil 129 } 130 131 return NewTx(tx) 132 } 133 134 type Transaction struct { 135 ref *types.Transaction 136 137 Value string `json:"value"` 138 Gas string `json:"gas"` 139 GasPrice string `json:"gasPrice"` 140 Hash string `json:"hash"` 141 Address string `json:"address"` 142 Sender string `json:"sender"` 143 RawData string `json:"rawData"` 144 Data string `json:"data"` 145 Contract bool `json:"isContract"` 146 CreatesContract bool `json:"createsContract"` 147 Confirmations int `json:"confirmations"` 148 } 149 150 func NewTx(tx *types.Transaction) *Transaction { 151 hash := toHex(tx.Hash()) 152 receiver := toHex(tx.To()) 153 if len(receiver) == 0 { 154 receiver = toHex(core.AddressFromMessage(tx)) 155 } 156 sender := toHex(tx.From()) 157 createsContract := core.MessageCreatesContract(tx) 158 159 var data string 160 if createsContract { 161 data = strings.Join(core.Disassemble(tx.Data()), "\n") 162 } else { 163 data = toHex(tx.Data()) 164 } 165 166 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())} 167 } 168 169 func (self *Transaction) ToString() string { 170 return self.ref.String() 171 } 172 173 type Key struct { 174 Address string `json:"address"` 175 PrivateKey string `json:"privateKey"` 176 PublicKey string `json:"publicKey"` 177 } 178 179 func NewKey(key *crypto.KeyPair) *Key { 180 return &Key{toHex(key.Address()), toHex(key.PrivateKey), toHex(key.PublicKey)} 181 } 182 183 type PReceipt struct { 184 CreatedContract bool `json:"createdContract"` 185 Address string `json:"address"` 186 Hash string `json:"hash"` 187 Sender string `json:"sender"` 188 } 189 190 func NewPReciept(contractCreation bool, creationAddress, hash, address []byte) *PReceipt { 191 return &PReceipt{ 192 contractCreation, 193 toHex(creationAddress), 194 toHex(hash), 195 toHex(address), 196 } 197 } 198 199 // Peer interface exposed to QML 200 201 type Peer struct { 202 ref *p2p.Peer 203 Ip string `json:"ip"` 204 Version string `json:"version"` 205 Caps string `json:"caps"` 206 } 207 208 func NewPeer(peer *p2p.Peer) *Peer { 209 var caps []string 210 for _, cap := range peer.Caps() { 211 caps = append(caps, fmt.Sprintf("%s/%d", cap.Name, cap.Version)) 212 } 213 214 return &Peer{ 215 ref: peer, 216 Ip: fmt.Sprintf("%v", peer.RemoteAddr()), 217 Version: fmt.Sprintf("%v", peer.ID()), 218 Caps: fmt.Sprintf("%v", caps), 219 } 220 } 221 222 type Receipt struct { 223 CreatedContract bool `json:"createdContract"` 224 Address string `json:"address"` 225 Hash string `json:"hash"` 226 Sender string `json:"sender"` 227 } 228 229 func NewReciept(contractCreation bool, creationAddress, hash, address []byte) *Receipt { 230 return &Receipt{ 231 contractCreation, 232 toHex(creationAddress), 233 toHex(hash), 234 toHex(address), 235 } 236 }