solidity.go 4.6 KB

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