forked from hyperledger-archives/fabric-chaincode-evm
/
types.go
285 lines (250 loc) · 9.05 KB
/
types.go
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
/*
Package types contains the types used to interact with the json-rpc
interface. It exists for users of fab3 types to use them without importing the
fabric protobuf definitions.
*/
package types
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
)
/*
Input types used as arguments to ethservice methods.
*/
const (
// HexEncodedAddressLegnth is 20 bytes, which is 40 chars when hex-encoded
HexEncodedAddressLegnth = 40
// HexEncodedTopicLegnth is 32 bytes, which is 64 hex chars when hex-encoded
HexEncodedTopicLegnth = 64
)
type EthArgs struct {
To string `json:"to"`
From string `json:"from"`
Gas string `json:"gas"`
GasPrice string `json:"gasPrice"`
Value string `json:"value"`
Data string `json:"data"`
Nonce string `json:"nonce"`
}
type GetLogsArgs struct {
FromBlock string `json:"fromBlock,omitempty"`
ToBlock string `json:"toBlock,omitempty"`
Address AddressFilter `json:"address,omitempty"`
Topics TopicsFilter `json:"topics,omitempty"`
BlockHash string `json:"blockHash,omitempty"`
}
type AddressFilter []string // 20 Byte Addresses, OR'd together
type TopicFilter []string // 32 Byte Topics, OR'd together
type TopicsFilter []TopicFilter // TopicFilter, AND'd together
func (gla *GetLogsArgs) UnmarshalJSON(data []byte) error {
type inputGetLogsArgs struct {
FromBlock string `json:"fromBlock"`
ToBlock string `json:"toBlock"`
Address interface{} `json:"address"` // string or array of strings.
Topics interface{} `json:"topics"` // array of strings, or array of array of strings
BlockHash string `json:"blockHash"`
}
var input inputGetLogsArgs
if err := json.Unmarshal(data, &input); err != nil {
return err
}
gla.FromBlock = strip0x(input.FromBlock)
gla.ToBlock = strip0x(input.ToBlock)
gla.BlockHash = strip0x(input.BlockHash)
if gla.BlockHash != "" && (gla.FromBlock != "" || gla.ToBlock != "") {
return errors.New("cannot provide BlockHash and (FromBlock or ToBlock), they are exclusive options")
}
if input.Address != nil {
var af AddressFilter
// DATA|Array, 20 Bytes - (optional) Contract address or a list of
// addresses from which logs should originate.
switch address := input.Address.(type) {
case string:
a, err := NewAddressFilter(address)
if err != nil {
return err
}
af = append(af, a...)
case []interface{}:
for i, address := range address {
if singleAddress, ok := address.(string); ok {
a, err := NewAddressFilter(singleAddress)
if err != nil {
return errors.Wrapf(err, "invalid address at position %d", i)
}
af = append(af, a...)
}
}
default:
return fmt.Errorf("badly formatted address field")
}
gla.Address = af
}
if input.Topics != nil {
var tf TopicsFilter
// handle the topics parsing
if topics, ok := input.Topics.([]interface{}); !ok {
return fmt.Errorf("topics must be slice")
} else {
for i, topic := range topics {
if singleTopic, ok := topic.(string); ok {
f, err := NewTopicFilter(singleTopic)
if err != nil {
return errors.Wrapf(err, "invalid topic at position %d", i)
}
tf = append(tf, f)
} else if multipleTopic, ok := topic.([]interface{}); ok {
var mtf TopicFilter
for _, singleTopic := range multipleTopic {
if stringTopic, ok := singleTopic.(string); ok {
f, err := NewTopicFilter(stringTopic)
if err != nil {
return errors.Wrapf(err, "invalid topic at position %d", i)
}
mtf = append(mtf, f...)
} else if singleTopic == nil {
f := TopicFilter{""}
mtf = append(mtf, f...)
} else {
return fmt.Errorf("all topics must be strings")
}
}
tf = append(tf, mtf)
} else if topic == nil {
f := TopicFilter{""}
tf = append(tf, f)
} else {
return fmt.Errorf("incorrect topics format %q", topic)
}
}
}
gla.Topics = tf
}
return nil
}
// NewAddressFilter takes a string and checks that is the correct length to
// represent a topic and strips the 0x
func NewAddressFilter(s string) (AddressFilter, error) {
s = strip0x(s)
if len(s) != HexEncodedAddressLegnth {
return nil, fmt.Errorf("address in wrong format, need 40 chars prefixed with '0x', got %d chars for %q", len(s), s)
}
return AddressFilter{s}, nil
}
// NewTopicFilter takes a string and checks that is the correct length to
// represent a topic and strips the 0x
func NewTopicFilter(s string) (TopicFilter, error) {
s = strip0x(s)
if len(s) != HexEncodedTopicLegnth {
return nil, fmt.Errorf("topic in wrong format, need 64 chars prefixed with '0x', got %d for %q", len(s), s)
}
return TopicFilter{s}, nil
}
func NewTopicsFilter(tf ...TopicFilter) TopicsFilter {
return tf
}
/*
Output types used as return values from ethservice methods.
*/
type TxReceipt struct {
TransactionHash string `json:"transactionHash"`
TransactionIndex string `json:"transactionIndex"`
BlockHash string `json:"blockHash"`
BlockNumber string `json:"blockNumber"`
ContractAddress string `json:"contractAddress,omitempty"`
GasUsed int `json:"gasUsed"`
CumulativeGasUsed int `json:"cumulativeGasUsed"`
To string `json:"to"`
Logs []Log `json:"logs"`
Status string `json:"status"`
From string `json:"from"`
}
type Log struct {
Address string `json:"address"`
Topics []string `json:"topics"`
Data string `json:"data,omitempty"`
BlockNumber string `json:"blockNumber"`
TxHash string `json:"transactionHash"`
TxIndex string `json:"transactionIndex"`
BlockHash string `json:"blockHash"`
Index string `json:"logIndex"`
}
// Transaction represents an ethereum evm transaction.
//
// https://github.com/ethereum/wiki/wiki/JSON-RPC#returns-28
type Transaction struct { // object, or null when no transaction was found:
BlockHash string `json:"blockHash"` // DATA, 32 Bytes - hash of the block where this transaction was in. null when its pending.
BlockNumber string `json:"blockNumber"` // QUANTITY - block number where this transaction was in. null when its pending.
To string `json:"to"` // DATA, 20 Bytes - address of the receiver. null when its a contract creation transaction.
// From is generated by EVM Chaincode. Until account generation
// stabilizes, we are not returning a value.
//
// From can be gotten from the Signature on the Transaction Envelope
//
From string `json:"from"` // DATA, 20 Bytes - address of the sender.
Input string `json:"input"` // DATA - the data send along with the transaction.
TransactionIndex string `json:"transactionIndex"` // QUANTITY - integer of the transactions index position in the block. null when its pending.
Hash string `json:"hash"` // DATA, 32 Bytes - hash of the transaction.
GasPrice string `json:"gasPrice"` // QUANTITY - gas price provided by the sender in Wei.
Value string `json:"value"` // QUANTITY - value transferred in Wei.
}
// MarshalJSON will json marshal the tx object as well as setting the Gas Price and Value fields as Ox0
func (tx *Transaction) MarshalJSON() ([]byte, error) {
type Alias Transaction
temp := (Alias)(*tx)
temp.GasPrice = "0x0"
temp.Value = "0x0"
return json.Marshal(temp)
}
// Block is an eth return struct
// defined https://github.com/ethereum/wiki/wiki/JSON-RPC#returns-26
type Block struct {
BlockData
// size: QUANTITY - integer the size of this block in bytes.
// timestamp: QUANTITY - the unix timestamp for when the block was collated.
Transactions []interface{} `json:"transactions"` // transactions: Array - Array of transaction objects, or 32 Bytes transaction hashes depending on the last given parameter.
}
// BlockData contains the fields that are constant in a Block object
type BlockData struct {
Number string `json:"number"` // number: QUANTITY - the block number. null when its pending block.
Hash string `json:"hash"` // hash: DATA, 32 Bytes - hash of the block. null when its pending block.
ParentHash string `json:"parentHash"` // parentHash: DATA, 32 Bytes - hash of the parent block.
}
// MarshalJSON marshals the data differently based on whether
// transactions are full or just tx hashes
func (blk *Block) MarshalJSON() ([]byte, error) {
if len(blk.Transactions) == 0 {
return json.Marshal(*blk)
}
if _, ok := blk.Transactions[0].(Transaction); !ok {
return json.Marshal(*blk)
}
txns := make([]Transaction, len(blk.Transactions))
for i, txn := range blk.Transactions {
txns[i] = txn.(Transaction)
}
temp := struct {
BlockData
Transactions []Transaction `json:"transactions"`
}{
BlockData: BlockData{
Number: blk.Number,
Hash: blk.Hash,
ParentHash: blk.ParentHash,
},
Transactions: txns,
}
return json.Marshal(temp)
}
func strip0x(s string) string {
//Not checking for malformed addresses just stripping `0x` prefix where applicable
if len(s) > 2 && s[0:2] == "0x" {
return s[2:]
}
return s
}