aboutsummaryrefslogtreecommitdiffstats
path: root/xeth/types.go
blob: ad5101d61f725e670341e7a86ed2411969bc680b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2014 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package xeth

import (
    "bytes"
    "fmt"
    "strings"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core"
    "github.com/ethereum/go-ethereum/core/state"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/p2p"
    "github.com/ethereum/go-ethereum/rlp"
)

type Object struct {
    *state.StateObject
}

func NewObject(state *state.StateObject) *Object {
    return &Object{state}
}

func (self *Object) StorageString(str string) []byte {
    if common.IsHex(str) {
        return self.storage(common.Hex2Bytes(str[2:]))
    } else {
        return self.storage(common.RightPadBytes([]byte(str), 32))
    }
}

func (self *Object) StorageValue(addr *common.Value) []byte {
    return self.storage(addr.Bytes())
}

func (self *Object) storage(addr []byte) []byte {
    return self.StateObject.GetState(common.BytesToHash(addr)).Bytes()
}

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[common.ToHex(self.Trie().GetKey(it.Key))] = common.ToHex(data)
    }

    return
}

// Block interface exposed to QML
type Block struct {
    //Transactions string `json:"transactions"`
    ref          *types.Block
    Size         string       `json:"size"`
    Number       int          `json:"number"`
    Hash         string       `json:"hash"`
    Transactions *common.List `json:"transactions"`
    Uncles       *common.List `json:"uncles"`
    Time         uint64       `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"`
}

// Creates a new QML Block from a chain block
func NewBlock(block *types.Block) *Block {
    if block == nil {
        return &Block{}
    }

    ptxs := make([]*Transaction, len(block.Transactions()))
    /*
        for i, tx := range block.Transactions() {
            ptxs[i] = NewTx(tx)
        }
    */
    txlist := common.NewList(ptxs)

    puncles := make([]*Block, len(block.Uncles()))
    /*
        for i, uncle := range block.Uncles() {
            puncles[i] = NewBlock(types.NewBlockWithHeader(uncle))
        }
    */
    ulist := common.NewList(puncles)

    return &Block{
        ref: block, Size: block.Size().String(),
        Number: int(block.NumberU64()), GasUsed: block.GasUsed().String(),
        GasLimit: block.GasLimit().String(), Hash: block.Hash().Hex(),
        Transactions: txlist, Uncles: ulist,
        Time:     block.Time(),
        Coinbase: block.Coinbase().Hex(),
        PrevHash: block.ParentHash().Hex(),
        Bloom:    common.ToHex(block.Bloom().Bytes()),
        Raw:      block.String(),
    }
}

func (self *Block) ToString() string {
    if self.ref != nil {
        return self.ref.String()
    }

    return ""
}

func (self *Block) GetTransaction(hash string) *Transaction {
    tx := self.ref.Transaction(common.HexToHash(hash))
    if tx == nil {
        return nil
    }

    return NewTx(tx)
}

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"`
}

func NewTx(tx *types.Transaction) *Transaction {
    sender, err := tx.From()
    if err != nil {
        return nil
    }
    hash := tx.Hash().Hex()

    var receiver string
    if to := tx.To(); to != nil {
        receiver = to.Hex()
    } else {
        from, _ := tx.From()
        receiver = crypto.CreateAddress(from, tx.Nonce()).Hex()
    }
    createsContract := core.MessageCreatesContract(tx)

    var data string
    if createsContract {
        data = strings.Join(core.Disassemble(tx.Data()), "\n")
    } else {
        data = common.ToHex(tx.Data())
    }

    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())}
}

func (self *Transaction) ToString() string {
    return self.ref.String()
}

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,
        common.ToHex(creationAddress),
        common.ToHex(hash),
        common.ToHex(address),
    }
}

// Peer interface exposed to QML

type Peer struct {
    ref     *p2p.Peer
    Ip      string `json:"ip"`
    Version string `json:"version"`
    Caps    string `json:"caps"`
}

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))
    }

    return &Peer{
        ref:     peer,
        Ip:      fmt.Sprintf("%v", peer.RemoteAddr()),
        Version: fmt.Sprintf("%v", peer.ID()),
        Caps:    fmt.Sprintf("%v", caps),
    }
}

type Receipt struct {
    CreatedContract bool   `json:"createdContract"`
    Address         string `json:"address"`
    Hash            string `json:"hash"`
    Sender          string `json:"sender"`
}

func NewReciept(contractCreation bool, creationAddress, hash, address []byte) *Receipt {
    return &Receipt{
        contractCreation,
        common.ToHex(creationAddress),
        common.ToHex(hash),
        common.ToHex(address),
    }
}