solidity.go 4.7 KB

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