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"
|
2022-05-16 12:59:35 +03:00
|
|
|
"io"
|
2016-03-16 12:48:33 +02:00
|
|
|
"os"
|
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
|
|
|
|
|
|
|
"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"
|
2022-06-27 19:22:36 +03:00
|
|
|
"github.com/urfave/cli/v2"
|
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
|
2022-06-27 19:22:36 +03:00
|
|
|
)
|
2019-03-18 15:29:26 +03:00
|
|
|
|
2022-06-27 19:22:36 +03:00
|
|
|
var (
|
2019-07-08 15:59:07 +03:00
|
|
|
// Flags needed by abigen
|
2022-06-27 19:22:36 +03:00
|
|
|
abiFlag = &cli.StringFlag{
|
2019-07-08 15:59:07 +03:00
|
|
|
Name: "abi",
|
|
|
|
Usage: "Path to the Ethereum contract ABI json to bind, - for STDIN",
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
binFlag = &cli.StringFlag{
|
2019-07-08 15:59:07 +03:00
|
|
|
Name: "bin",
|
|
|
|
Usage: "Path to the Ethereum contract bytecode (generate deploy method)",
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
typeFlag = &cli.StringFlag{
|
2019-07-08 15:59:07 +03:00
|
|
|
Name: "type",
|
|
|
|
Usage: "Struct name for the binding (default = package name)",
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
jsonFlag = &cli.StringFlag{
|
2019-07-08 15:59:07 +03:00
|
|
|
Name: "combined-json",
|
2022-05-26 10:26:37 +03:00
|
|
|
Usage: "Path to the combined-json file generated by compiler, - for STDIN",
|
2019-07-08 15:59:07 +03:00
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
excFlag = &cli.StringFlag{
|
2019-07-08 15:59:07 +03:00
|
|
|
Name: "exc",
|
|
|
|
Usage: "Comma separated types to exclude from binding",
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
pkgFlag = &cli.StringFlag{
|
2019-07-08 15:59:07 +03:00
|
|
|
Name: "pkg",
|
|
|
|
Usage: "Package name to generate the binding into",
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
outFlag = &cli.StringFlag{
|
2019-07-08 15:59:07 +03:00
|
|
|
Name: "out",
|
|
|
|
Usage: "Output file for the generated binding (default = stdout)",
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
langFlag = &cli.StringFlag{
|
2019-07-08 15:59:07 +03:00
|
|
|
Name: "lang",
|
|
|
|
Usage: "Destination language for the bindings (go, java, objc)",
|
|
|
|
Value: "go",
|
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
aliasFlag = &cli.StringFlag{
|
2019-11-14 10:26:10 +03:00
|
|
|
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")
|
2022-06-27 19:22:36 +03:00
|
|
|
app.Name = "abigen"
|
2019-07-08 15:59:07 +03:00
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
abiFlag,
|
|
|
|
binFlag,
|
|
|
|
typeFlag,
|
|
|
|
jsonFlag,
|
|
|
|
excFlag,
|
|
|
|
pkgFlag,
|
|
|
|
outFlag,
|
|
|
|
langFlag,
|
2019-11-14 10:26:10 +03:00
|
|
|
aliasFlag,
|
2019-07-08 15:59:07 +03:00
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
app.Action = abigen
|
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 {
|
2022-05-23 15:29:23 +03:00
|
|
|
utils.CheckExclusive(c, abiFlag, jsonFlag) // Only one source can be selected.
|
2022-06-27 19:22:36 +03:00
|
|
|
|
|
|
|
if c.String(pkgFlag.Name) == "" {
|
2019-07-08 15:59:07 +03:00
|
|
|
utils.Fatalf("No destination package specified (--pkg)")
|
2016-09-05 19:07:57 +03:00
|
|
|
}
|
|
|
|
var lang bind.Lang
|
2022-06-27 19:22:36 +03:00
|
|
|
switch c.String(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:
|
2022-06-27 19:22:36 +03:00
|
|
|
utils.Fatalf("Unsupported destination language \"%s\" (--lang)", c.String(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
|
|
|
)
|
2022-06-27 19:22:36 +03:00
|
|
|
if c.String(abiFlag.Name) != "" {
|
2019-07-08 15:59:07 +03:00
|
|
|
// Load up the ABI, optional bytecode and type name from the parameters
|
|
|
|
var (
|
|
|
|
abi []byte
|
|
|
|
err error
|
|
|
|
)
|
2022-06-27 19:22:36 +03:00
|
|
|
input := c.String(abiFlag.Name)
|
2019-07-08 15:59:07 +03:00
|
|
|
if input == "-" {
|
2022-05-16 12:59:35 +03:00
|
|
|
abi, err = io.ReadAll(os.Stdin)
|
2019-07-08 15:59:07 +03:00
|
|
|
} else {
|
2022-05-16 12:59:35 +03:00
|
|
|
abi, err = os.ReadFile(input)
|
2019-07-08 15:59:07 +03:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
utils.Fatalf("Failed to read input ABI: %v", err)
|
|
|
|
}
|
|
|
|
abis = append(abis, string(abi))
|
|
|
|
|
|
|
|
var bin []byte
|
2022-06-27 19:22:36 +03:00
|
|
|
if binFile := c.String(binFlag.Name); binFile != "" {
|
2022-05-16 12:59:35 +03:00
|
|
|
if bin, err = os.ReadFile(binFile); err != nil {
|
2019-07-08 15:59:07 +03:00
|
|
|
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))
|
|
|
|
|
2022-06-27 19:22:36 +03:00
|
|
|
kind := c.String(typeFlag.Name)
|
2019-07-08 15:59:07 +03:00
|
|
|
if kind == "" {
|
2022-06-27 19:22:36 +03:00
|
|
|
kind = c.String(pkgFlag.Name)
|
2019-07-08 15:59:07 +03:00
|
|
|
}
|
|
|
|
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)
|
2022-06-27 19:22:36 +03:00
|
|
|
for _, kind := range strings.Split(c.String(excFlag.Name), ",") {
|
2016-03-26 11:43:09 +02:00
|
|
|
exclude[strings.ToLower(kind)] = true
|
|
|
|
}
|
2019-07-08 15:59:07 +03:00
|
|
|
var contracts map[string]*compiler.Contract
|
2019-03-18 15:29:26 +03:00
|
|
|
|
2022-06-27 19:22:36 +03:00
|
|
|
if c.IsSet(jsonFlag.Name) {
|
2022-05-26 10:26:37 +03:00
|
|
|
var (
|
2022-06-27 19:22:36 +03:00
|
|
|
input = c.String(jsonFlag.Name)
|
2022-05-26 10:26:37 +03:00
|
|
|
jsonOutput []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if input == "-" {
|
|
|
|
jsonOutput, err = io.ReadAll(os.Stdin)
|
|
|
|
} else {
|
|
|
|
jsonOutput, err = os.ReadFile(input)
|
|
|
|
}
|
2019-03-18 15:29:26 +03:00
|
|
|
if err != nil {
|
2022-05-26 10:26:37 +03:00
|
|
|
utils.Fatalf("Failed to read combined-json: %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
|
|
|
|
2022-06-07 09:38:54 +03:00
|
|
|
// Derive the library placeholder which is a 34 character prefix of the
|
|
|
|
// hex encoding of the keccak256 hash of the fully qualified library name.
|
|
|
|
// Note that the fully qualified library name is the path of its source
|
|
|
|
// file and the library name separated by ":".
|
|
|
|
libPattern := crypto.Keccak256Hash([]byte(name)).String()[2:36] // the first 2 chars are 0x
|
2019-07-08 11:27:05 +03:00
|
|
|
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
|
2022-06-27 19:22:36 +03:00
|
|
|
if c.IsSet(aliasFlag.Name) {
|
2019-11-14 10:26:10 +03:00
|
|
|
// We support multi-versions for aliasing
|
|
|
|
// e.g.
|
|
|
|
// foo=bar,foo2=bar2
|
|
|
|
// foo:bar,foo2:bar2
|
|
|
|
re := regexp.MustCompile(`(?:(\w+)[:=](\w+))`)
|
2022-06-27 19:22:36 +03:00
|
|
|
submatches := re.FindAllStringSubmatch(c.String(aliasFlag.Name), -1)
|
2019-11-14 10:26:10 +03:00
|
|
|
for _, match := range submatches {
|
|
|
|
aliases[match[1]] = match[2]
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 19:27:37 +02:00
|
|
|
// Generate the contract binding
|
2022-06-27 19:22:36 +03:00
|
|
|
code, err := bind.Bind(types, abis, bins, sigs, c.String(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
|
2022-06-27 19:22:36 +03:00
|
|
|
if !c.IsSet(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
|
|
|
}
|
2022-06-27 19:22:36 +03:00
|
|
|
if err := os.WriteFile(c.String(outFlag.Name), []byte(code), 0600); err != nil {
|
2019-07-08 15:59:07 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|