main.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. package main
  17. import (
  18. "encoding/json"
  19. "flag"
  20. "fmt"
  21. "io/ioutil"
  22. "os"
  23. "strings"
  24. "github.com/ethereum/go-ethereum/accounts/abi/bind"
  25. "github.com/ethereum/go-ethereum/common/compiler"
  26. )
  27. var (
  28. abiFlag = flag.String("abi", "", "Path to the Ethereum contract ABI json to bind")
  29. binFlag = flag.String("bin", "", "Path to the Ethereum contract bytecode (generate deploy method)")
  30. typFlag = flag.String("type", "", "Struct name for the binding (default = package name)")
  31. solFlag = flag.String("sol", "", "Path to the Ethereum contract Solidity source to build and bind")
  32. solcFlag = flag.String("solc", "solc", "Solidity compiler to use if source builds are requested")
  33. excFlag = flag.String("exc", "", "Comma separated types to exclude from binding")
  34. pkgFlag = flag.String("pkg", "", "Package name to generate the binding into")
  35. outFlag = flag.String("out", "", "Output file for the generated binding (default = stdout)")
  36. langFlag = flag.String("lang", "go", "Destination language for the bindings (go, java, objc)")
  37. )
  38. func main() {
  39. // Parse and ensure all needed inputs are specified
  40. flag.Parse()
  41. if *abiFlag == "" && *solFlag == "" {
  42. fmt.Printf("No contract ABI (--abi) or Solidity source (--sol) specified\n")
  43. os.Exit(-1)
  44. } else if (*abiFlag != "" || *binFlag != "" || *typFlag != "") && *solFlag != "" {
  45. fmt.Printf("Contract ABI (--abi), bytecode (--bin) and type (--type) flags are mutually exclusive with the Solidity source (--sol) flag\n")
  46. os.Exit(-1)
  47. }
  48. if *pkgFlag == "" {
  49. fmt.Printf("No destination package specified (--pkg)\n")
  50. os.Exit(-1)
  51. }
  52. var lang bind.Lang
  53. switch *langFlag {
  54. case "go":
  55. lang = bind.LangGo
  56. case "java":
  57. lang = bind.LangJava
  58. case "objc":
  59. lang = bind.LangObjC
  60. default:
  61. fmt.Printf("Unsupported destination language \"%s\" (--lang)\n", *langFlag)
  62. os.Exit(-1)
  63. }
  64. // If the entire solidity code was specified, build and bind based on that
  65. var (
  66. abis []string
  67. bins []string
  68. types []string
  69. )
  70. if *solFlag != "" {
  71. // Generate the list of types to exclude from binding
  72. exclude := make(map[string]bool)
  73. for _, kind := range strings.Split(*excFlag, ",") {
  74. exclude[strings.ToLower(kind)] = true
  75. }
  76. contracts, err := compiler.CompileSolidity(*solcFlag, *solFlag)
  77. if err != nil {
  78. fmt.Printf("Failed to build Solidity contract: %v\n", err)
  79. os.Exit(-1)
  80. }
  81. // Gather all non-excluded contract for binding
  82. for name, contract := range contracts {
  83. if exclude[strings.ToLower(name)] {
  84. continue
  85. }
  86. abi, _ := json.Marshal(contract.Info.AbiDefinition) // Flatten the compiler parse
  87. abis = append(abis, string(abi))
  88. bins = append(bins, contract.Code)
  89. nameParts := strings.Split(name, ":")
  90. types = append(types, nameParts[len(nameParts)-1])
  91. }
  92. } else {
  93. // Otherwise load up the ABI, optional bytecode and type name from the parameters
  94. abi, err := ioutil.ReadFile(*abiFlag)
  95. if err != nil {
  96. fmt.Printf("Failed to read input ABI: %v\n", err)
  97. os.Exit(-1)
  98. }
  99. abis = append(abis, string(abi))
  100. bin := []byte{}
  101. if *binFlag != "" {
  102. if bin, err = ioutil.ReadFile(*binFlag); err != nil {
  103. fmt.Printf("Failed to read input bytecode: %v\n", err)
  104. os.Exit(-1)
  105. }
  106. }
  107. bins = append(bins, string(bin))
  108. kind := *typFlag
  109. if kind == "" {
  110. kind = *pkgFlag
  111. }
  112. types = append(types, kind)
  113. }
  114. // Generate the contract binding
  115. code, err := bind.Bind(types, abis, bins, *pkgFlag, lang)
  116. if err != nil {
  117. fmt.Printf("Failed to generate ABI binding: %v\n", err)
  118. os.Exit(-1)
  119. }
  120. // Either flush it out to a file or display on the standard output
  121. if *outFlag == "" {
  122. fmt.Printf("%s\n", code)
  123. return
  124. }
  125. if err := ioutil.WriteFile(*outFlag, []byte(code), 0600); err != nil {
  126. fmt.Printf("Failed to write ABI binding: %v\n", err)
  127. os.Exit(-1)
  128. }
  129. }