solidity.go 4.6 KB

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