utils.go 4.5 KB

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