-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiledContract.go
149 lines (118 loc) · 3.31 KB
/
compiledContract.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
package contract
import (
"encoding/hex"
"encoding/json"
"github.com/ethereum/go-ethereum/crypto"
"github.com/pkg/errors"
"github.com/fantasygold/solar/abi"
)
type ABIDefinition struct {
Name string `json:"name"`
Type string `json:"type"`
Payable bool `json:"payable"`
Inputs []ABIType `json:"inputs"`
Outputs []ABIType `json:"outputs"`
Constant bool `json:"constant"`
// Event
Anonymous bool `json:"anonymous"`
}
type ABIType struct {
Name string `json:"name"`
Type string `json:"type"`
// Event
Indexed bool `json:"indexed"`
}
type CompiledContract struct {
// Where the contract is defined
Source string `json:"source"`
Name string `json:"name"`
ABI []ABIDefinition `json:"abi"`
Bin Bytes `json:"bin"`
// KECAAK256 of bytecode without auxdata
BinKeccak256 Bytes `json:"binhash"`
}
func (c *CompiledContract) EncodingABI() (*abi.ABI, error) {
jsonABI, err := json.Marshal(c.ABI)
if err != nil {
return nil, err
}
var encodingABI abi.ABI
err = json.Unmarshal(jsonABI, &encodingABI)
if err != nil {
return nil, err
}
return &encodingABI, nil
}
func (c *CompiledContract) ToBytes(jsonParams []byte) (Bytes, error) {
calldata := c.Bin
abi, err := c.EncodingABI()
if err != nil {
return nil, errors.Wrap(err, "abi")
}
constructor := abi.Constructor
if len(constructor.Inputs) == 0 && len(jsonParams) != 0 {
return nil, errors.New("does not expect constructor params")
}
if len(constructor.Inputs) != 0 {
var params []interface{}
err = json.Unmarshal(jsonParams, ¶ms)
if err != nil {
return nil, errors.Errorf("expected constructor params in JSON, got: %#v", string(jsonParams))
}
packedParams, err := abi.Constructor.Pack(params...)
if err != nil {
return nil, errors.Wrap(err, "constructor")
}
calldata = append(calldata, packedParams...)
}
return calldata, nil
}
type RawCompiledContract struct {
RawMetadata string `json:"metadata"`
Bin []byte
Metadata struct {
Output struct {
Version int64
Language string
ABI []ABIDefinition `json:"abi"`
}
}
}
func (c *RawCompiledContract) BinHash256() []byte {
bin := c.BinWithoutAuxData()
return crypto.Keccak256(bin)
}
func (c *RawCompiledContract) BinWithoutAuxData() []byte {
// https://solidity.readthedocs.io/en/develop/miscellaneous.html#encoding-of-the-metadata-hash-in-the-bytecode
// 0xa1 0x65 'b' 'z' 'z' 'r' '0' 0x58 0x20 <32 bytes swarm hash> 0x00 0x29
// a1 65 62 7a 7a 72 30 58 20 [32 bytes] 0x00 0x29
// 11 + 32 bytes
return c.Bin[0 : len(c.Bin)-11-32-1]
}
func (c *RawCompiledContract) UnmarshalJSON(data []byte) error {
type dataStruct struct {
RawMetadata string `json:"metadata"`
BinStr string `json:"bin"`
}
var dest dataStruct
err := json.Unmarshal(data, &dest)
if err != nil {
return errors.Wrap(err, "parse contract raw metadata")
}
if dest.RawMetadata == "" && dest.BinStr == "" {
// this contract is interface only
return nil
}
// Recursively parse Metadata, which is a json string.
err = json.Unmarshal([]byte(dest.RawMetadata), &c.Metadata)
if err != nil {
return errors.Wrap(err, "parse contract metadata")
}
bin, err := hex.DecodeString(dest.BinStr)
if err != nil {
return errors.Wrap(err, "decode byte string")
}
c.RawMetadata = dest.RawMetadata
c.Bin = bin
return nil
}