utils.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library 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. // The go-ethereum library 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 the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "strings"
  19. "fmt"
  20. "github.com/ethereum/go-ethereum/eth"
  21. "github.com/ethereum/go-ethereum/rpc/codec"
  22. "github.com/ethereum/go-ethereum/rpc/shared"
  23. "github.com/ethereum/go-ethereum/xeth"
  24. )
  25. var (
  26. // Mapping between the different methods each api supports
  27. AutoCompletion = map[string][]string{
  28. "admin": []string{
  29. "addPeer",
  30. "datadir",
  31. "enableUserAgent",
  32. "exportChain",
  33. "getContractInfo",
  34. "httpGet",
  35. "importChain",
  36. "nodeInfo",
  37. "peers",
  38. "register",
  39. "registerUrl",
  40. "saveInfo",
  41. "setGlobalRegistrar",
  42. "setHashReg",
  43. "setUrlHint",
  44. "setSolc",
  45. "sleep",
  46. "sleepBlocks",
  47. "startNatSpec",
  48. "startRPC",
  49. "stopNatSpec",
  50. "stopRPC",
  51. "verbosity",
  52. },
  53. "db": []string{
  54. "getString",
  55. "putString",
  56. "getHex",
  57. "putHex",
  58. },
  59. "debug": []string{
  60. "dumpBlock",
  61. "getBlockRlp",
  62. "metrics",
  63. "printBlock",
  64. "processBlock",
  65. "seedHash",
  66. "setHead",
  67. },
  68. "eth": []string{
  69. "accounts",
  70. "blockNumber",
  71. "call",
  72. "contract",
  73. "coinbase",
  74. "compile.lll",
  75. "compile.serpent",
  76. "compile.solidity",
  77. "contract",
  78. "defaultAccount",
  79. "defaultBlock",
  80. "estimateGas",
  81. "filter",
  82. "getBalance",
  83. "getBlock",
  84. "getBlockTransactionCount",
  85. "getBlockUncleCount",
  86. "getCode",
  87. "getNatSpec",
  88. "getCompilers",
  89. "gasPrice",
  90. "getStorageAt",
  91. "getTransaction",
  92. "getTransactionCount",
  93. "getTransactionFromBlock",
  94. "getTransactionReceipt",
  95. "getUncle",
  96. "hashrate",
  97. "mining",
  98. "namereg",
  99. "pendingTransactions",
  100. "resend",
  101. "sendRawTransaction",
  102. "sendTransaction",
  103. "sign",
  104. "syncing",
  105. },
  106. "miner": []string{
  107. "hashrate",
  108. "makeDAG",
  109. "setEtherbase",
  110. "setExtra",
  111. "setGasPrice",
  112. "startAutoDAG",
  113. "start",
  114. "stopAutoDAG",
  115. "stop",
  116. },
  117. "net": []string{
  118. "peerCount",
  119. "listening",
  120. },
  121. "personal": []string{
  122. "listAccounts",
  123. "newAccount",
  124. "unlockAccount",
  125. },
  126. "shh": []string{
  127. "post",
  128. "newIdentify",
  129. "hasIdentity",
  130. "newGroup",
  131. "addToGroup",
  132. "filter",
  133. },
  134. "txpool": []string{
  135. "status",
  136. },
  137. "web3": []string{
  138. "sha3",
  139. "version",
  140. "fromWei",
  141. "toWei",
  142. "toHex",
  143. "toAscii",
  144. "fromAscii",
  145. "toBigNumber",
  146. "isAddress",
  147. },
  148. }
  149. )
  150. // Parse a comma separated API string to individual api's
  151. func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth.Ethereum) ([]shared.EthereumApi, error) {
  152. if len(strings.TrimSpace(apistr)) == 0 {
  153. return nil, fmt.Errorf("Empty apistr provided")
  154. }
  155. names := strings.Split(apistr, ",")
  156. apis := make([]shared.EthereumApi, len(names))
  157. for i, name := range names {
  158. switch strings.ToLower(strings.TrimSpace(name)) {
  159. case shared.AdminApiName:
  160. apis[i] = NewAdminApi(xeth, eth.Network(), eth, codec)
  161. case shared.DebugApiName:
  162. apis[i] = NewDebugApi(xeth, eth, codec)
  163. case shared.DbApiName:
  164. apis[i] = NewDbApi(xeth, eth, codec)
  165. case shared.EthApiName:
  166. apis[i] = NewEthApi(xeth, eth, codec)
  167. case shared.MinerApiName:
  168. apis[i] = NewMinerApi(eth, codec)
  169. case shared.NetApiName:
  170. apis[i] = NewNetApi(xeth, eth, codec)
  171. case shared.ShhApiName:
  172. apis[i] = NewShhApi(xeth, eth, codec)
  173. case shared.TxPoolApiName:
  174. apis[i] = NewTxPoolApi(xeth, eth, codec)
  175. case shared.PersonalApiName:
  176. apis[i] = NewPersonalApi(xeth, eth, codec)
  177. case shared.Web3ApiName:
  178. apis[i] = NewWeb3Api(xeth, codec)
  179. default:
  180. return nil, fmt.Errorf("Unknown API '%s'", name)
  181. }
  182. }
  183. return apis, nil
  184. }
  185. func Javascript(name string) string {
  186. switch strings.ToLower(strings.TrimSpace(name)) {
  187. case shared.AdminApiName:
  188. return Admin_JS
  189. case shared.DebugApiName:
  190. return Debug_JS
  191. case shared.DbApiName:
  192. return Db_JS
  193. case shared.EthApiName:
  194. return Eth_JS
  195. case shared.MinerApiName:
  196. return Miner_JS
  197. case shared.NetApiName:
  198. return Net_JS
  199. case shared.ShhApiName:
  200. return Shh_JS
  201. case shared.TxPoolApiName:
  202. return TxPool_JS
  203. case shared.PersonalApiName:
  204. return Personal_JS
  205. }
  206. return ""
  207. }