github.com/bearnetworkchain/go-bearnetwork@v1.10.19-0.20220604150648-d63890c2e42b/cmd/abigen/main.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "io" 23 "os" 24 "regexp" 25 "strings" 26 27 "github.com/bearnetworkchain/go-bearnetwork/accounts/abi/bind" 28 "github.com/bearnetworkchain/go-bearnetwork/cmd/utils" 29 "github.com/bearnetworkchain/go-bearnetwork/common/compiler" 30 "github.com/bearnetworkchain/go-bearnetwork/crypto" 31 "github.com/bearnetworkchain/go-bearnetwork/internal/flags" 32 "github.com/bearnetworkchain/go-bearnetwork/log" 33 "gopkg.in/urfave/cli.v1" 34 ) 35 36 var ( 37 // Git SHA1 commit hash of the release (set via linker flags) 38 gitCommit = "" 39 gitDate = "" 40 41 app *cli.App 42 43 // Flags needed by abigen 44 abiFlag = cli.StringFlag{ 45 Name: "abi", 46 Usage: "Path to the Ethereum contract ABI json to bind, - for STDIN", 47 } 48 binFlag = cli.StringFlag{ 49 Name: "bin", 50 Usage: "Path to the Ethereum contract bytecode (generate deploy method)", 51 } 52 typeFlag = cli.StringFlag{ 53 Name: "type", 54 Usage: "Struct name for the binding (default = package name)", 55 } 56 jsonFlag = cli.StringFlag{ 57 Name: "combined-json", 58 Usage: "Path to the combined-json file generated by compiler, - for STDIN", 59 } 60 excFlag = cli.StringFlag{ 61 Name: "exc", 62 Usage: "Comma separated types to exclude from binding", 63 } 64 pkgFlag = cli.StringFlag{ 65 Name: "pkg", 66 Usage: "Package name to generate the binding into", 67 } 68 outFlag = cli.StringFlag{ 69 Name: "out", 70 Usage: "Output file for the generated binding (default = stdout)", 71 } 72 langFlag = cli.StringFlag{ 73 Name: "lang", 74 Usage: "Destination language for the bindings (go, java, objc)", 75 Value: "go", 76 } 77 aliasFlag = cli.StringFlag{ 78 Name: "alias", 79 Usage: "Comma separated aliases for function and event renaming, e.g. original1=alias1, original2=alias2", 80 } 81 ) 82 83 func init() { 84 app = flags.NewApp(gitCommit, gitDate, "ethereum checkpoint helper tool") 85 app.Flags = []cli.Flag{ 86 abiFlag, 87 binFlag, 88 typeFlag, 89 jsonFlag, 90 excFlag, 91 pkgFlag, 92 outFlag, 93 langFlag, 94 aliasFlag, 95 } 96 app.Action = utils.MigrateFlags(abigen) 97 cli.CommandHelpTemplate = flags.OriginCommandHelpTemplate 98 } 99 100 func abigen(c *cli.Context) error { 101 utils.CheckExclusive(c, abiFlag, jsonFlag) // Only one source can be selected. 102 if c.GlobalString(pkgFlag.Name) == "" { 103 utils.Fatalf("No destination package specified (--pkg)") 104 } 105 var lang bind.Lang 106 switch c.GlobalString(langFlag.Name) { 107 case "go": 108 lang = bind.LangGo 109 case "java": 110 lang = bind.LangJava 111 case "objc": 112 lang = bind.LangObjC 113 utils.Fatalf("Objc binding generation is uncompleted") 114 default: 115 utils.Fatalf("Unsupported destination language \"%s\" (--lang)", c.GlobalString(langFlag.Name)) 116 } 117 // If the entire solidity code was specified, build and bind based on that 118 var ( 119 abis []string 120 bins []string 121 types []string 122 sigs []map[string]string 123 libs = make(map[string]string) 124 aliases = make(map[string]string) 125 ) 126 if c.GlobalString(abiFlag.Name) != "" { 127 // Load up the ABI, optional bytecode and type name from the parameters 128 var ( 129 abi []byte 130 err error 131 ) 132 input := c.GlobalString(abiFlag.Name) 133 if input == "-" { 134 abi, err = io.ReadAll(os.Stdin) 135 } else { 136 abi, err = os.ReadFile(input) 137 } 138 if err != nil { 139 utils.Fatalf("Failed to read input ABI: %v", err) 140 } 141 abis = append(abis, string(abi)) 142 143 var bin []byte 144 if binFile := c.GlobalString(binFlag.Name); binFile != "" { 145 if bin, err = os.ReadFile(binFile); err != nil { 146 utils.Fatalf("Failed to read input bytecode: %v", err) 147 } 148 if strings.Contains(string(bin), "//") { 149 utils.Fatalf("Contract has additional library references, please use other mode(e.g. --combined-json) to catch library infos") 150 } 151 } 152 bins = append(bins, string(bin)) 153 154 kind := c.GlobalString(typeFlag.Name) 155 if kind == "" { 156 kind = c.GlobalString(pkgFlag.Name) 157 } 158 types = append(types, kind) 159 } else { 160 // Generate the list of types to exclude from binding 161 exclude := make(map[string]bool) 162 for _, kind := range strings.Split(c.GlobalString(excFlag.Name), ",") { 163 exclude[strings.ToLower(kind)] = true 164 } 165 var contracts map[string]*compiler.Contract 166 167 if c.GlobalIsSet(jsonFlag.Name) { 168 var ( 169 input = c.GlobalString(jsonFlag.Name) 170 jsonOutput []byte 171 err error 172 ) 173 if input == "-" { 174 jsonOutput, err = io.ReadAll(os.Stdin) 175 } else { 176 jsonOutput, err = os.ReadFile(input) 177 } 178 if err != nil { 179 utils.Fatalf("Failed to read combined-json: %v", err) 180 } 181 contracts, err = compiler.ParseCombinedJSON(jsonOutput, "", "", "", "") 182 if err != nil { 183 utils.Fatalf("Failed to read contract information from json output: %v", err) 184 } 185 } 186 // Gather all non-excluded contract for binding 187 for name, contract := range contracts { 188 if exclude[strings.ToLower(name)] { 189 continue 190 } 191 abi, err := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse 192 if err != nil { 193 utils.Fatalf("Failed to parse ABIs from compiler output: %v", err) 194 } 195 abis = append(abis, string(abi)) 196 bins = append(bins, contract.Code) 197 sigs = append(sigs, contract.Hashes) 198 nameParts := strings.Split(name, ":") 199 types = append(types, nameParts[len(nameParts)-1]) 200 201 libPattern := crypto.Keccak256Hash([]byte(name)).String()[2:36] 202 libs[libPattern] = nameParts[len(nameParts)-1] 203 } 204 } 205 // Extract all aliases from the flags 206 if c.GlobalIsSet(aliasFlag.Name) { 207 // We support multi-versions for aliasing 208 // e.g. 209 // foo=bar,foo2=bar2 210 // foo:bar,foo2:bar2 211 re := regexp.MustCompile(`(?:(\w+)[:=](\w+))`) 212 submatches := re.FindAllStringSubmatch(c.GlobalString(aliasFlag.Name), -1) 213 for _, match := range submatches { 214 aliases[match[1]] = match[2] 215 } 216 } 217 // Generate the contract binding 218 code, err := bind.Bind(types, abis, bins, sigs, c.GlobalString(pkgFlag.Name), lang, libs, aliases) 219 if err != nil { 220 utils.Fatalf("Failed to generate ABI binding: %v", err) 221 } 222 // Either flush it out to a file or display on the standard output 223 if !c.GlobalIsSet(outFlag.Name) { 224 fmt.Printf("%s\n", code) 225 return nil 226 } 227 if err := os.WriteFile(c.GlobalString(outFlag.Name), []byte(code), 0600); err != nil { 228 utils.Fatalf("Failed to write ABI binding: %v", err) 229 } 230 return nil 231 } 232 233 func main() { 234 log.Root().SetHandler(log.LvlFilterHandler(log.LvlInfo, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))) 235 236 if err := app.Run(os.Args); err != nil { 237 fmt.Fprintln(os.Stderr, err) 238 os.Exit(1) 239 } 240 }