2019-07-22 12:17:27 +03:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
2016-04-14 19:18:24 +03:00
|
|
|
// This file is part of go-ethereum.
|
2016-03-16 12:48:33 +02:00
|
|
|
//
|
2016-04-14 19:18:24 +03:00
|
|
|
// 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
|
2016-03-16 12:48:33 +02:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2016-04-14 19:18:24 +03:00
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
2016-03-16 12:48:33 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-04-14 19:18:24 +03:00
|
|
|
// GNU General Public License for more details.
|
2016-03-16 12:48:33 +02:00
|
|
|
//
|
2016-04-14 19:18:24 +03:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-03-16 12:48:33 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-03-21 14:34:49 +02:00
|
|
|
"encoding/json"
|
2016-03-16 12:48:33 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-12-16 15:37:15 +03:00
|
|
|
"path/filepath"
|
2019-11-14 10:26:10 +03:00
|
|
|
"regexp"
|
2016-03-26 11:43:09 +02:00
|
|
|
"strings"
|
2016-03-16 12:48:33 +02:00
|
|
|
|
2019-12-16 15:37:15 +03:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
2016-03-16 12:48:33 +02:00
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
2019-07-08 15:59:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/cmd/utils"
|
2016-03-21 14:34:49 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common/compiler"
|
2019-07-08 11:27:05 +03:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2020-07-14 11:35:32 +03:00
|
|
|
"github.com/ethereum/go-ethereum/internal/flags"
|
2019-07-08 15:59:07 +03:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"gopkg.in/urfave/cli.v1"
|
2016-03-16 12:48:33 +02:00
|
|
|
)
|
|
|
|
|
2019-07-08 15:59:07 +03:00
|
|
|
var (
|
|
|
|
// Git SHA1 commit hash of the release (set via linker flags)
|
|
|
|
gitCommit = ""
|
|
|
|
gitDate = ""
|
2016-03-21 14:34:49 +02:00
|
|
|
|
2019-07-08 15:59:07 +03:00
|
|
|
app *cli.App
|
2019-03-18 15:29:26 +03:00
|
|
|
|
2019-07-08 15:59:07 +03:00
|
|
|
// 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",
|
|
|
|
}
|
2019-11-14 10:26:10 +03:00
|
|
|
aliasFlag = cli.StringFlag{
|
|
|
|
Name: "alias",
|
2020-12-12 19:36:32 +03:00
|
|
|
Usage: "Comma separated aliases for function and event renaming, e.g. original1=alias1, original2=alias2",
|
2019-11-14 10:26:10 +03:00
|
|
|
}
|
2016-03-16 12:48:33 +02:00
|
|
|
)
|
|
|
|
|
2019-07-08 15:59:07 +03:00
|
|
|
func init() {
|
2020-07-14 11:35:32 +03:00
|
|
|
app = flags.NewApp(gitCommit, gitDate, "ethereum checkpoint helper tool")
|
2019-07-08 15:59:07 +03:00
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
abiFlag,
|
|
|
|
binFlag,
|
|
|
|
typeFlag,
|
|
|
|
jsonFlag,
|
|
|
|
solFlag,
|
|
|
|
solcFlag,
|
|
|
|
vyFlag,
|
|
|
|
vyperFlag,
|
|
|
|
excFlag,
|
|
|
|
pkgFlag,
|
|
|
|
outFlag,
|
|
|
|
langFlag,
|
2019-11-14 10:26:10 +03:00
|
|
|
aliasFlag,
|
2019-07-08 15:59:07 +03:00
|
|
|
}
|
|
|
|
app.Action = utils.MigrateFlags(abigen)
|
2020-07-14 11:35:32 +03:00
|
|
|
cli.CommandHelpTemplate = flags.OriginCommandHelpTemplate
|
2019-07-08 15:59:07 +03:00
|
|
|
}
|
2016-03-16 12:48:33 +02:00
|
|
|
|
2019-07-08 15:59:07 +03:00
|
|
|
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)")
|
2016-09-05 19:07:57 +03:00
|
|
|
}
|
|
|
|
var lang bind.Lang
|
2019-07-08 15:59:07 +03:00
|
|
|
switch c.GlobalString(langFlag.Name) {
|
2016-09-05 19:07:57 +03:00
|
|
|
case "go":
|
|
|
|
lang = bind.LangGo
|
|
|
|
case "java":
|
|
|
|
lang = bind.LangJava
|
2019-07-03 13:17:43 +03:00
|
|
|
case "objc":
|
|
|
|
lang = bind.LangObjC
|
2019-07-08 15:59:07 +03:00
|
|
|
utils.Fatalf("Objc binding generation is uncompleted")
|
2016-09-05 19:07:57 +03:00
|
|
|
default:
|
2019-07-08 15:59:07 +03:00
|
|
|
utils.Fatalf("Unsupported destination language \"%s\" (--lang)", c.GlobalString(langFlag.Name))
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
2016-03-21 14:34:49 +02:00
|
|
|
// If the entire solidity code was specified, build and bind based on that
|
|
|
|
var (
|
2019-11-14 10:26:10 +03:00
|
|
|
abis []string
|
|
|
|
bins []string
|
|
|
|
types []string
|
|
|
|
sigs []map[string]string
|
|
|
|
libs = make(map[string]string)
|
|
|
|
aliases = make(map[string]string)
|
2016-03-21 14:34:49 +02:00
|
|
|
)
|
2019-07-08 15:59:07 +03:00
|
|
|
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 {
|
2016-03-26 11:43:09 +02:00
|
|
|
// Generate the list of types to exclude from binding
|
|
|
|
exclude := make(map[string]bool)
|
2019-07-08 15:59:07 +03:00
|
|
|
for _, kind := range strings.Split(c.GlobalString(excFlag.Name), ",") {
|
2016-03-26 11:43:09 +02:00
|
|
|
exclude[strings.ToLower(kind)] = true
|
|
|
|
}
|
cmd/abigen: support for reading solc output from stdin (#16683)
Allow the --abi flag to be given - to indicate that it should read the
ABI information from standard input. It expects to read the solc output
with the --combined-json flag providing bin, abi, userdoc, devdoc, and
metadata, and works very similarly to the internal invocation of solc,
except it allows external invocation of solc.
This facilitates integration with more complex solc invocations, such
as invocations that require path remapping or --allow-paths tweaks.
Simple usage example:
solc --combined-json bin,abi,userdoc,devdoc,metadata *.sol | abigen --abi -
2018-06-05 13:22:02 +03:00
|
|
|
var err error
|
2019-07-08 15:59:07 +03:00
|
|
|
var contracts map[string]*compiler.Contract
|
2019-03-18 15:29:26 +03:00
|
|
|
|
|
|
|
switch {
|
2019-07-08 15:59:07 +03:00
|
|
|
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):
|
2019-12-16 15:37:15 +03:00
|
|
|
output, err := compiler.CompileVyper(c.GlobalString(vyperFlag.Name), c.GlobalString(vyFlag.Name))
|
cmd/abigen: support for reading solc output from stdin (#16683)
Allow the --abi flag to be given - to indicate that it should read the
ABI information from standard input. It expects to read the solc output
with the --combined-json flag providing bin, abi, userdoc, devdoc, and
metadata, and works very similarly to the internal invocation of solc,
except it allows external invocation of solc.
This facilitates integration with more complex solc invocations, such
as invocations that require path remapping or --allow-paths tweaks.
Simple usage example:
solc --combined-json bin,abi,userdoc,devdoc,metadata *.sol | abigen --abi -
2018-06-05 13:22:02 +03:00
|
|
|
if err != nil {
|
2019-07-08 15:59:07 +03:00
|
|
|
utils.Fatalf("Failed to build Vyper contract: %v", err)
|
cmd/abigen: support for reading solc output from stdin (#16683)
Allow the --abi flag to be given - to indicate that it should read the
ABI information from standard input. It expects to read the solc output
with the --combined-json flag providing bin, abi, userdoc, devdoc, and
metadata, and works very similarly to the internal invocation of solc,
except it allows external invocation of solc.
This facilitates integration with more complex solc invocations, such
as invocations that require path remapping or --allow-paths tweaks.
Simple usage example:
solc --combined-json bin,abi,userdoc,devdoc,metadata *.sol | abigen --abi -
2018-06-05 13:22:02 +03:00
|
|
|
}
|
2019-12-16 15:37:15 +03:00
|
|
|
contracts = make(map[string]*compiler.Contract)
|
|
|
|
for n, contract := range output {
|
|
|
|
name := n
|
|
|
|
// Sanitize the combined json names to match the
|
|
|
|
// format expected by solidity.
|
|
|
|
if !strings.Contains(n, ":") {
|
|
|
|
// Remove extra path components
|
|
|
|
name = abi.ToCamelCase(strings.TrimSuffix(filepath.Base(name), ".vy"))
|
|
|
|
}
|
|
|
|
contracts[name] = contract
|
|
|
|
}
|
|
|
|
|
2019-07-08 15:59:07 +03:00
|
|
|
case c.GlobalIsSet(jsonFlag.Name):
|
|
|
|
jsonOutput, err := ioutil.ReadFile(c.GlobalString(jsonFlag.Name))
|
2019-03-18 15:29:26 +03:00
|
|
|
if err != nil {
|
2019-07-08 15:59:07 +03:00
|
|
|
utils.Fatalf("Failed to read combined-json from compiler: %v", err)
|
2019-03-18 15:29:26 +03:00
|
|
|
}
|
2019-07-08 15:59:07 +03:00
|
|
|
contracts, err = compiler.ParseCombinedJSON(jsonOutput, "", "", "", "")
|
cmd/abigen: support for reading solc output from stdin (#16683)
Allow the --abi flag to be given - to indicate that it should read the
ABI information from standard input. It expects to read the solc output
with the --combined-json flag providing bin, abi, userdoc, devdoc, and
metadata, and works very similarly to the internal invocation of solc,
except it allows external invocation of solc.
This facilitates integration with more complex solc invocations, such
as invocations that require path remapping or --allow-paths tweaks.
Simple usage example:
solc --combined-json bin,abi,userdoc,devdoc,metadata *.sol | abigen --abi -
2018-06-05 13:22:02 +03:00
|
|
|
if err != nil {
|
2019-07-08 15:59:07 +03:00
|
|
|
utils.Fatalf("Failed to read contract information from json output: %v", err)
|
cmd/abigen: support for reading solc output from stdin (#16683)
Allow the --abi flag to be given - to indicate that it should read the
ABI information from standard input. It expects to read the solc output
with the --combined-json flag providing bin, abi, userdoc, devdoc, and
metadata, and works very similarly to the internal invocation of solc,
except it allows external invocation of solc.
This facilitates integration with more complex solc invocations, such
as invocations that require path remapping or --allow-paths tweaks.
Simple usage example:
solc --combined-json bin,abi,userdoc,devdoc,metadata *.sol | abigen --abi -
2018-06-05 13:22:02 +03:00
|
|
|
}
|
2016-03-21 14:34:49 +02:00
|
|
|
}
|
2016-03-26 11:43:09 +02:00
|
|
|
// Gather all non-excluded contract for binding
|
2016-03-21 14:34:49 +02:00
|
|
|
for name, contract := range contracts {
|
2016-03-26 11:43:09 +02:00
|
|
|
if exclude[strings.ToLower(name)] {
|
|
|
|
continue
|
|
|
|
}
|
2019-03-18 15:29:26 +03:00
|
|
|
abi, err := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse
|
|
|
|
if err != nil {
|
2019-07-08 15:59:07 +03:00
|
|
|
utils.Fatalf("Failed to parse ABIs from compiler output: %v", err)
|
2019-03-18 15:29:26 +03:00
|
|
|
}
|
2016-03-21 14:34:49 +02:00
|
|
|
abis = append(abis, string(abi))
|
|
|
|
bins = append(bins, contract.Code)
|
2019-07-02 10:52:58 +03:00
|
|
|
sigs = append(sigs, contract.Hashes)
|
2017-02-06 20:16:56 +03:00
|
|
|
nameParts := strings.Split(name, ":")
|
|
|
|
types = append(types, nameParts[len(nameParts)-1])
|
2019-07-08 11:27:05 +03:00
|
|
|
|
|
|
|
libPattern := crypto.Keccak256Hash([]byte(name)).String()[2:36]
|
|
|
|
libs[libPattern] = nameParts[len(nameParts)-1]
|
2016-03-21 14:34:49 +02:00
|
|
|
}
|
2016-03-17 19:27:37 +02:00
|
|
|
}
|
2019-11-14 10:26:10 +03:00
|
|
|
// Extract all aliases from the flags
|
|
|
|
if c.GlobalIsSet(aliasFlag.Name) {
|
|
|
|
// We support multi-versions for aliasing
|
|
|
|
// e.g.
|
|
|
|
// foo=bar,foo2=bar2
|
|
|
|
// foo:bar,foo2:bar2
|
|
|
|
re := regexp.MustCompile(`(?:(\w+)[:=](\w+))`)
|
|
|
|
submatches := re.FindAllStringSubmatch(c.GlobalString(aliasFlag.Name), -1)
|
|
|
|
for _, match := range submatches {
|
|
|
|
aliases[match[1]] = match[2]
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 19:27:37 +02:00
|
|
|
// Generate the contract binding
|
2019-11-14 10:26:10 +03:00
|
|
|
code, err := bind.Bind(types, abis, bins, sigs, c.GlobalString(pkgFlag.Name), lang, libs, aliases)
|
2016-03-16 12:48:33 +02:00
|
|
|
if err != nil {
|
2019-07-08 15:59:07 +03:00
|
|
|
utils.Fatalf("Failed to generate ABI binding: %v", err)
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
|
|
|
// Either flush it out to a file or display on the standard output
|
2019-07-08 15:59:07 +03:00
|
|
|
if !c.GlobalIsSet(outFlag.Name) {
|
2016-03-16 12:48:33 +02:00
|
|
|
fmt.Printf("%s\n", code)
|
2019-07-08 15:59:07 +03:00
|
|
|
return nil
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
2019-07-08 15:59:07 +03:00
|
|
|
if err := ioutil.WriteFile(c.GlobalString(outFlag.Name), []byte(code), 0600); err != nil {
|
|
|
|
utils.Fatalf("Failed to write ABI binding: %v", err)
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
2019-07-08 15:59:07 +03:00
|
|
|
return nil
|
2016-03-16 12:48:33 +02:00
|
|
|
}
|
cmd/abigen: support for reading solc output from stdin (#16683)
Allow the --abi flag to be given - to indicate that it should read the
ABI information from standard input. It expects to read the solc output
with the --combined-json flag providing bin, abi, userdoc, devdoc, and
metadata, and works very similarly to the internal invocation of solc,
except it allows external invocation of solc.
This facilitates integration with more complex solc invocations, such
as invocations that require path remapping or --allow-paths tweaks.
Simple usage example:
solc --combined-json bin,abi,userdoc,devdoc,metadata *.sol | abigen --abi -
2018-06-05 13:22:02 +03:00
|
|
|
|
2019-07-08 15:59:07 +03:00
|
|
|
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)
|
cmd/abigen: support for reading solc output from stdin (#16683)
Allow the --abi flag to be given - to indicate that it should read the
ABI information from standard input. It expects to read the solc output
with the --combined-json flag providing bin, abi, userdoc, devdoc, and
metadata, and works very similarly to the internal invocation of solc,
except it allows external invocation of solc.
This facilitates integration with more complex solc invocations, such
as invocations that require path remapping or --allow-paths tweaks.
Simple usage example:
solc --combined-json bin,abi,userdoc,devdoc,metadata *.sol | abigen --abi -
2018-06-05 13:22:02 +03:00
|
|
|
}
|
|
|
|
}
|