solidity.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2015 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 Lesser 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 Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package compiler
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "fmt"
  21. "io/ioutil"
  22. "os"
  23. "os/exec"
  24. "path/filepath"
  25. "regexp"
  26. "strings"
  27. "github.com/ethereum/go-ethereum/common"
  28. "github.com/ethereum/go-ethereum/crypto"
  29. "github.com/ethereum/go-ethereum/logger"
  30. "github.com/ethereum/go-ethereum/logger/glog"
  31. )
  32. const (
  33. // flair = "Christian <c@ethdev.com> and Lefteris <lefteris@ethdev.com> (c) 2014-2015"
  34. flair = ""
  35. languageVersion = "0"
  36. )
  37. var (
  38. versionRegExp = regexp.MustCompile("[0-9]+.[0-9]+.[0-9]+")
  39. params = []string{
  40. "--binary", // Request to output the contract in binary (hexadecimal).
  41. "file", //
  42. "--json-abi", // Request to output the contract's JSON ABI interface.
  43. "file", //
  44. "--natspec-user", // Request to output the contract's Natspec user documentation.
  45. "file", //
  46. "--natspec-dev", // Request to output the contract's Natspec developer documentation.
  47. "file",
  48. "--add-std",
  49. "1",
  50. }
  51. )
  52. type Contract struct {
  53. Code string `json:"code"`
  54. Info ContractInfo `json:"info"`
  55. }
  56. type ContractInfo struct {
  57. Source string `json:"source"`
  58. Language string `json:"language"`
  59. LanguageVersion string `json:"languageVersion"`
  60. CompilerVersion string `json:"compilerVersion"`
  61. AbiDefinition interface{} `json:"abiDefinition"`
  62. UserDoc interface{} `json:"userDoc"`
  63. DeveloperDoc interface{} `json:"developerDoc"`
  64. }
  65. type Solidity struct {
  66. solcPath string
  67. version string
  68. }
  69. func New(solcPath string) (sol *Solidity, err error) {
  70. // set default solc
  71. if len(solcPath) == 0 {
  72. solcPath = "solc"
  73. }
  74. solcPath, err = exec.LookPath(solcPath)
  75. if err != nil {
  76. return
  77. }
  78. cmd := exec.Command(solcPath, "--version")
  79. var out bytes.Buffer
  80. cmd.Stdout = &out
  81. err = cmd.Run()
  82. if err != nil {
  83. return
  84. }
  85. version := versionRegExp.FindString(out.String())
  86. sol = &Solidity{
  87. solcPath: solcPath,
  88. version: version,
  89. }
  90. glog.V(logger.Info).Infoln(sol.Info())
  91. return
  92. }
  93. func (sol *Solidity) Info() string {
  94. return fmt.Sprintf("solc v%s\nSolidity Compiler: %s\n%s", sol.version, sol.solcPath, flair)
  95. }
  96. func (sol *Solidity) Version() string {
  97. return sol.version
  98. }
  99. func (sol *Solidity) Compile(source string) (contracts map[string]*Contract, err error) {
  100. if len(source) == 0 {
  101. err = fmt.Errorf("empty source")
  102. return
  103. }
  104. wd, err := ioutil.TempDir("", "solc")
  105. if err != nil {
  106. return
  107. }
  108. defer os.RemoveAll(wd)
  109. in := strings.NewReader(source)
  110. var out bytes.Buffer
  111. // cwd set to temp dir
  112. cmd := exec.Command(sol.solcPath, params...)
  113. cmd.Dir = wd
  114. cmd.Stdin = in
  115. cmd.Stdout = &out
  116. err = cmd.Run()
  117. if err != nil {
  118. err = fmt.Errorf("solc error: %v", err)
  119. return
  120. }
  121. matches, _ := filepath.Glob(wd + "/*.binary")
  122. if len(matches) < 1 {
  123. err = fmt.Errorf("solc error: missing code output")
  124. return
  125. }
  126. contracts = make(map[string]*Contract)
  127. for _, path := range matches {
  128. _, file := filepath.Split(path)
  129. base := strings.Split(file, ".")[0]
  130. codeFile := filepath.Join(wd, base+".binary")
  131. abiDefinitionFile := filepath.Join(wd, base+".abi")
  132. userDocFile := filepath.Join(wd, base+".docuser")
  133. developerDocFile := filepath.Join(wd, base+".docdev")
  134. var code, abiDefinitionJson, userDocJson, developerDocJson []byte
  135. code, err = ioutil.ReadFile(codeFile)
  136. if err != nil {
  137. err = fmt.Errorf("error reading compiler output for code: %v", err)
  138. return
  139. }
  140. abiDefinitionJson, err = ioutil.ReadFile(abiDefinitionFile)
  141. if err != nil {
  142. err = fmt.Errorf("error reading compiler output for abiDefinition: %v", err)
  143. return
  144. }
  145. var abiDefinition interface{}
  146. err = json.Unmarshal(abiDefinitionJson, &abiDefinition)
  147. userDocJson, err = ioutil.ReadFile(userDocFile)
  148. if err != nil {
  149. err = fmt.Errorf("error reading compiler output for userDoc: %v", err)
  150. return
  151. }
  152. var userDoc interface{}
  153. err = json.Unmarshal(userDocJson, &userDoc)
  154. developerDocJson, err = ioutil.ReadFile(developerDocFile)
  155. if err != nil {
  156. err = fmt.Errorf("error reading compiler output for developerDoc: %v", err)
  157. return
  158. }
  159. var developerDoc interface{}
  160. err = json.Unmarshal(developerDocJson, &developerDoc)
  161. contract := &Contract{
  162. Code: "0x" + string(code),
  163. Info: ContractInfo{
  164. Source: source,
  165. Language: "Solidity",
  166. LanguageVersion: languageVersion,
  167. CompilerVersion: sol.version,
  168. AbiDefinition: abiDefinition,
  169. UserDoc: userDoc,
  170. DeveloperDoc: developerDoc,
  171. },
  172. }
  173. contracts[base] = contract
  174. }
  175. return
  176. }
  177. func SaveInfo(info *ContractInfo, filename string) (contenthash common.Hash, err error) {
  178. infojson, err := json.Marshal(info)
  179. if err != nil {
  180. return
  181. }
  182. contenthash = common.BytesToHash(crypto.Sha3(infojson))
  183. err = ioutil.WriteFile(filename, infojson, 0600)
  184. return
  185. }