solidity.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package compiler
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "os"
  8. "os/exec"
  9. "path"
  10. "path/filepath"
  11. "regexp"
  12. "strings"
  13. "github.com/ethereum/go-ethereum/common"
  14. "github.com/ethereum/go-ethereum/crypto"
  15. "github.com/ethereum/go-ethereum/logger"
  16. "github.com/ethereum/go-ethereum/logger/glog"
  17. )
  18. const (
  19. flair = "Christian <c@ethdev.com> and Lefteris <lefteris@ethdev.com> (c) 2014-2015"
  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) Compile(source string) (contract *Contract, err error) {
  80. if len(source) == 0 {
  81. err = fmt.Errorf("empty source")
  82. return
  83. }
  84. wd, err := ioutil.TempDir("", "solc")
  85. if err != nil {
  86. return
  87. }
  88. defer os.RemoveAll(wd)
  89. in := strings.NewReader(source)
  90. var out bytes.Buffer
  91. // cwd set to temp dir
  92. cmd := exec.Command(sol.solcPath, params...)
  93. cmd.Dir = wd
  94. cmd.Stdin = in
  95. cmd.Stdout = &out
  96. err = cmd.Run()
  97. if err != nil {
  98. err = fmt.Errorf("solc error: %v", err)
  99. return
  100. }
  101. matches, _ := filepath.Glob(wd + "/*.binary")
  102. if len(matches) < 1 {
  103. err = fmt.Errorf("solc error: missing code output")
  104. return
  105. }
  106. if len(matches) > 1 {
  107. err = fmt.Errorf("multi-contract sources are not supported")
  108. return
  109. }
  110. _, file := filepath.Split(matches[0])
  111. base := strings.Split(file, ".")[0]
  112. codeFile := path.Join(wd, base+".binary")
  113. abiDefinitionFile := path.Join(wd, base+".abi")
  114. userDocFile := path.Join(wd, base+".docuser")
  115. developerDocFile := path.Join(wd, base+".docdev")
  116. code, err := ioutil.ReadFile(codeFile)
  117. if err != nil {
  118. err = fmt.Errorf("error reading compiler output for code: %v", err)
  119. return
  120. }
  121. abiDefinitionJson, err := ioutil.ReadFile(abiDefinitionFile)
  122. if err != nil {
  123. err = fmt.Errorf("error reading compiler output for abiDefinition: %v", err)
  124. return
  125. }
  126. var abiDefinition interface{}
  127. err = json.Unmarshal(abiDefinitionJson, &abiDefinition)
  128. userDocJson, err := ioutil.ReadFile(userDocFile)
  129. if err != nil {
  130. err = fmt.Errorf("error reading compiler output for userDoc: %v", err)
  131. return
  132. }
  133. var userDoc interface{}
  134. err = json.Unmarshal(userDocJson, &userDoc)
  135. developerDocJson, err := ioutil.ReadFile(developerDocFile)
  136. if err != nil {
  137. err = fmt.Errorf("error reading compiler output for developerDoc: %v", err)
  138. return
  139. }
  140. var developerDoc interface{}
  141. err = json.Unmarshal(developerDocJson, &developerDoc)
  142. contract = &Contract{
  143. Code: string(code),
  144. Info: ContractInfo{
  145. Source: source,
  146. Language: "Solidity",
  147. LanguageVersion: languageVersion,
  148. CompilerVersion: sol.version,
  149. AbiDefinition: abiDefinition,
  150. UserDoc: userDoc,
  151. DeveloperDoc: developerDoc,
  152. },
  153. }
  154. return
  155. }
  156. func ExtractInfo(contract *Contract, filename string) (contenthash common.Hash, err error) {
  157. contractInfo, err := json.Marshal(contract.Info)
  158. if err != nil {
  159. return
  160. }
  161. contenthash = common.BytesToHash(crypto.Sha3(contractInfo))
  162. err = ioutil.WriteFile(filename, contractInfo, 0600)
  163. return
  164. }