aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/abigen/main.go
blob: 6af34c5fe8e37efa374268d07ba09736054b1312 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright 2019 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"
    "strings"

    "github.com/ethereum/go-ethereum/accounts/abi/bind"
    "github.com/ethereum/go-ethereum/cmd/utils"
    "github.com/ethereum/go-ethereum/common/compiler"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/log"
    "gopkg.in/urfave/cli.v1"
)

const (
    commandHelperTemplate = `{{.Name}}{{if .Subcommands}} command{{end}}{{if .Flags}} [command options]{{end}} [arguments...]
{{if .Description}}{{.Description}}
{{end}}{{if .Subcommands}}
SUBCOMMANDS:
    {{range .Subcommands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
    {{end}}{{end}}{{if .Flags}}
OPTIONS:
{{range $.Flags}}{{"\t"}}{{.}}
{{end}}
{{end}}`
)

var (
    // Git SHA1 commit hash of the release (set via linker flags)
    gitCommit = ""
    gitDate   = ""

    app *cli.App

    // Flags needed by abigen
    abiFlag = cli.StringFlag{
        Name:  "abi",
        Usage: "Path to the Ethereum contract ABI json to bind, - for STDIN",
    }
    binFlag = cli.StringFlag{
        Name:  "bin",
        Usage: "Path to the Ethereum contract bytecode (generate deploy method)",
    }
    typeFlag = cli.StringFlag{
        Name:  "type",
        Usage: "Struct name for the binding (default = package name)",
    }
    jsonFlag = cli.StringFlag{
        Name:  "combined-json",
        Usage: "Path to the combined-json file generated by compiler",
    }
    solFlag = cli.StringFlag{
        Name:  "sol",
        Usage: "Path to the Ethereum contract Solidity source to build and bind",
    }
    solcFlag = cli.StringFlag{
        Name:  "solc",
        Usage: "Solidity compiler to use if source builds are requested",
        Value: "solc",
    }
    vyFlag = cli.StringFlag{
        Name:  "vy",
        Usage: "Path to the Ethereum contract Vyper source to build and bind",
    }
    vyperFlag = cli.StringFlag{
        Name:  "vyper",
        Usage: "Vyper compiler to use if source builds are requested",
        Value: "vyper",
    }
    excFlag = cli.StringFlag{
        Name:  "exc",
        Usage: "Comma separated types to exclude from binding",
    }
    pkgFlag = cli.StringFlag{
        Name:  "pkg",
        Usage: "Package name to generate the binding into",
    }
    outFlag = cli.StringFlag{
        Name:  "out",
        Usage: "Output file for the generated binding (default = stdout)",
    }
    langFlag = cli.StringFlag{
        Name:  "lang",
        Usage: "Destination language for the bindings (go, java, objc)",
        Value: "go",
    }
)

func init() {
    app = utils.NewApp(gitCommit, gitDate, "ethereum checkpoint helper tool")
    app.Flags = []cli.Flag{
        abiFlag,
        binFlag,
        typeFlag,
        jsonFlag,
        solFlag,
        solcFlag,
        vyFlag,
        vyperFlag,
        excFlag,
        pkgFlag,
        outFlag,
        langFlag,
    }
    app.Action = utils.MigrateFlags(abigen)
    cli.CommandHelpTemplate = commandHelperTemplate
}

func abigen(c *cli.Context) error {
    utils.CheckExclusive(c, abiFlag, jsonFlag, solFlag, vyFlag) // Only one source can be selected.
    if c.GlobalString(pkgFlag.Name) == "" {
        utils.Fatalf("No destination package specified (--pkg)")
    }
    var lang bind.Lang
    switch c.GlobalString(langFlag.Name) {
    case "go":
        lang = bind.LangGo
    case "java":
        lang = bind.LangJava
    case "objc":
        lang = bind.LangObjC
        utils.Fatalf("Objc binding generation is uncompleted")
    default:
        utils.Fatalf("Unsupported destination language \"%s\" (--lang)", c.GlobalString(langFlag.Name))
    }
    // If the entire solidity code was specified, build and bind based on that
    var (
        abis  []string
        bins  []string
        types []string
        sigs  []map[string]string
        libs  = make(map[string]string)
    )
    if c.GlobalString(abiFlag.Name) != "" {
        // Load up the ABI, optional bytecode and type name from the parameters
        var (
            abi []byte
            err error
        )
        input := c.GlobalString(abiFlag.Name)
        if input == "-" {
            abi, err = ioutil.ReadAll(os.Stdin)
        } else {
            abi, err = ioutil.ReadFile(input)
        }
        if err != nil {
            utils.Fatalf("Failed to read input ABI: %v", err)
        }
        abis = append(abis, string(abi))

        var bin []byte
        if binFile := c.GlobalString(binFlag.Name); binFile != "" {
            if bin, err = ioutil.ReadFile(binFile); err != nil {
                utils.Fatalf("Failed to read input bytecode: %v", err)
            }
            if strings.Contains(string(bin), "//") {
                utils.Fatalf("Contract has additional library references, please use other mode(e.g. --combined-json) to catch library infos")
            }
        }
        bins = append(bins, string(bin))

        kind := c.GlobalString(typeFlag.Name)
        if kind == "" {
            kind = c.GlobalString(pkgFlag.Name)
        }
        types = append(types, kind)
    } else {
        // Generate the list of types to exclude from binding
        exclude := make(map[string]bool)
        for _, kind := range strings.Split(c.GlobalString(excFlag.Name), ",") {
            exclude[strings.ToLower(kind)] = true
        }
        var err error
        var contracts map[string]*compiler.Contract

        switch {
        case c.GlobalIsSet(solFlag.Name):
            contracts, err = compiler.CompileSolidity(c.GlobalString(solcFlag.Name), c.GlobalString(solFlag.Name))
            if err != nil {
                utils.Fatalf("Failed to build Solidity contract: %v", err)
            }
        case c.GlobalIsSet(vyFlag.Name):
            contracts, err = compiler.CompileVyper(c.GlobalString(vyperFlag.Name), c.GlobalString(vyFlag.Name))
            if err != nil {
                utils.Fatalf("Failed to build Vyper contract: %v", err)
            }
        case c.GlobalIsSet(jsonFlag.Name):
            jsonOutput, err := ioutil.ReadFile(c.GlobalString(jsonFlag.Name))
            if err != nil {
                utils.Fatalf("Failed to read combined-json from compiler: %v", err)
            }
            contracts, err = compiler.ParseCombinedJSON(jsonOutput, "", "", "", "")
            if err != nil {
                utils.Fatalf("Failed to read contract information from json output: %v", err)
            }
        }
        // Gather all non-excluded contract for binding
        for name, contract := range contracts {
            if exclude[strings.ToLower(name)] {
                continue
            }
            abi, err := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse
            if err != nil {
                utils.Fatalf("Failed to parse ABIs from compiler output: %v", err)
            }
            abis = append(abis, string(abi))
            bins = append(bins, contract.Code)
            sigs = append(sigs, contract.Hashes)
            nameParts := strings.Split(name, ":")
            types = append(types, nameParts[len(nameParts)-1])

            libPattern := crypto.Keccak256Hash([]byte(name)).String()[2:36]
            libs[libPattern] = nameParts[len(nameParts)-1]
        }
    }
    // Generate the contract binding
    code, err := bind.Bind(types, abis, bins, sigs, c.GlobalString(pkgFlag.Name), lang, libs)
    if err != nil {
        utils.Fatalf("Failed to generate ABI binding: %v", err)
    }
    // Either flush it out to a file or display on the standard output
    if !c.GlobalIsSet(outFlag.Name) {
        fmt.Printf("%s\n", code)
        return nil
    }
    if err := ioutil.WriteFile(c.GlobalString(outFlag.Name), []byte(code), 0600); err != nil {
        utils.Fatalf("Failed to write ABI binding: %v", err)
    }
    return nil
}

func main() {
    log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true))))

    if err := app.Run(os.Args); err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
}