utils.go 4.9 KB

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