/
private_receipt.go
154 lines (145 loc) · 4.67 KB
/
private_receipt.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
package types
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/bsostech/go-besu/privacy"
)
// PrivateReceipt represents the results of a transaction.
type PrivateReceipt struct {
// Consensus fields: These fields are defined by the Yellow Paper
PostState []byte `json:"root"`
Status uint64 `json:"status"`
// CumulativeGasUsed uint64 `json:"cumulativeGasUsed" gencodec:"required"`
Bloom types.Bloom `json:"logsBloom" gencodec:"required"`
Logs []*types.Log `json:"logs" gencodec:"required"`
// Implementation fields: These fields are added by geth when processing a transaction.
// They are stored in the chain database.
TxHash common.Hash `json:"transactionHash" gencodec:"required"`
ContractAddress common.Address `json:"contractAddress"`
// GasUsed uint64 `json:"gasUsed" gencodec:"required"`
// Inclusion information: These fields provide information about the inclusion of the
// transaction corresponding to this receipt.
BlockHash common.Hash `json:"blockHash,omitempty"`
BlockNumber *big.Int `json:"blockNumber,omitempty"`
TransactionIndex uint `json:"transactionIndex"`
// Privacy
PrivateFrom privacy.PublicKey `json:"privateFrom" gencodec:"required"`
PrivateFor []privacy.PublicKey `json:"privateFor" gencodec:"required"`
Restriction string
// Private
CommitmentHash common.Hash `json:"commitmentHash" gencodec:"required"`
Output []byte `json:"output"`
}
// MarshalPrivateReceipt .
func MarshalPrivateReceipt(r map[string]interface{}) (*PrivateReceipt, error) {
// contractAddress not required
var contractAddress common.Address
if v, ok := r["contractAddress"]; ok {
contractAddress = common.HexToAddress(v.(string))
}
// output not required
var output []byte
if v, ok := r["output"]; ok {
output, _ = hexutil.Decode(v.(string))
}
// commitmentHash required
if _, ok := r["commitmentHash"]; !ok {
return nil, fmt.Errorf("commitmentHash not found")
}
commitmentHash := common.HexToHash(r["commitmentHash"].(string))
// transactionHash required
if _, ok := r["transactionHash"]; !ok {
return nil, fmt.Errorf("transactionHash not found")
}
transactionHash := common.HexToHash(r["transactionHash"].(string))
// privateFrom required
if _, ok := r["privateFrom"]; !ok {
return nil, fmt.Errorf("privateFrom not found")
}
privateFrom, err := privacy.ToPublicKey(r["privateFrom"].(string))
if err != nil {
return nil, err
}
// privateFor required
if _, ok := r["privateFor"]; !ok {
return nil, fmt.Errorf("privateFor not found")
}
var privateFor []privacy.PublicKey
for _, v := range r["privateFor"].([]interface{}) {
key, err := privacy.ToPublicKey(v.(string))
if err != nil {
continue
}
privateFor = append(privateFor, key)
}
// status not required
status := uint64(0)
if v, ok := r["status"]; ok {
if v.(string) == "0x1" {
status = uint64(1)
}
}
// logs required
if _, ok := r["logs"]; !ok {
return nil, fmt.Errorf("logs not found")
}
var logs []*types.Log
for _, v := range r["logs"].([]interface{}) {
var log *types.Log
err := log.UnmarshalJSON(v.([]byte))
if err != nil {
continue
}
logs = append(logs, log)
}
// logsBloom required
if _, ok := r["logsBloom"]; !ok {
return nil, fmt.Errorf("logsBloom not found")
}
logsBloomString := r["logsBloom"].(string)
logsBloomBytes, err := hexutil.Decode(logsBloomString)
if err != nil {
return nil, fmt.Errorf("failed to Decode %v, err: %v", logsBloomString, err)
}
logsBloom := types.BytesToBloom(logsBloomBytes)
if err != nil {
return nil, fmt.Errorf("failed to UnmarshalText %v, err: %v", logsBloomString, err)
}
// blockHash not required
var blockHash common.Hash
if v, ok := r["blockHash"]; ok {
blockHash = common.HexToHash(v.(string))
}
// blockNumber not required
var blockNumber *big.Int
if v, ok := r["blockNumber"]; ok {
i := new(big.Int)
i.SetString(v.(string), 16)
blockNumber = i
}
// transactionIndex not required
var transactionIndex uint
if v, ok := r["transactionIndex"]; ok {
i := new(big.Int)
i.SetString(v.(string), 16)
transactionIndex = uint(i.Uint64())
}
return &PrivateReceipt{
Status: status,
Bloom: logsBloom,
Logs: logs,
TxHash: transactionHash,
ContractAddress: contractAddress,
BlockHash: blockHash,
BlockNumber: blockNumber,
TransactionIndex: transactionIndex,
PrivateFrom: privateFrom,
PrivateFor: privateFor,
Restriction: "restricted",
CommitmentHash: commitmentHash,
Output: output,
}, nil
}