utils.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package api
  2. import (
  3. "strings"
  4. "fmt"
  5. "github.com/ethereum/go-ethereum/eth"
  6. "github.com/ethereum/go-ethereum/rpc/codec"
  7. "github.com/ethereum/go-ethereum/rpc/shared"
  8. "github.com/ethereum/go-ethereum/xeth"
  9. )
  10. var (
  11. // Mapping between the different methods each api supports
  12. AutoCompletion = map[string][]string{
  13. "admin": []string{
  14. "addPeer",
  15. "peers",
  16. "nodeInfo",
  17. "exportChain",
  18. "importChain",
  19. "verbosity",
  20. "chainSyncStatus",
  21. "setSolc",
  22. "datadir",
  23. "startRPC",
  24. "stopRPC",
  25. },
  26. "db": []string{
  27. "getString",
  28. "putString",
  29. "getHex",
  30. "putHex",
  31. },
  32. "debug": []string{
  33. "dumpBlock",
  34. "getBlockRlp",
  35. "printBlock",
  36. "processBlock",
  37. "seedHash",
  38. "setHead",
  39. },
  40. "eth": []string{
  41. "accounts",
  42. "blockNumber",
  43. "getBalance",
  44. "protocolVersion",
  45. "coinbase",
  46. "mining",
  47. "gasPrice",
  48. "getStorage",
  49. "storageAt",
  50. "getStorageAt",
  51. "getTransactionCount",
  52. "getBlockTransactionCountByHash",
  53. "getBlockTransactionCountByNumber",
  54. "getUncleCountByBlockHash",
  55. "getUncleCountByBlockNumber",
  56. "getData",
  57. "getCode",
  58. "sign",
  59. "sendRawTransaction",
  60. "sendTransaction",
  61. "transact",
  62. "estimateGas",
  63. "call",
  64. "flush",
  65. "getBlockByHash",
  66. "getBlockByNumber",
  67. "getTransactionByHash",
  68. "getTransactionByBlockHashAndIndex",
  69. "getUncleByBlockHashAndIndex",
  70. "getUncleByBlockNumberAndIndex",
  71. "getCompilers",
  72. "compileSolidity",
  73. "newFilter",
  74. "newBlockFilter",
  75. "newPendingTransactionFilter",
  76. "uninstallFilter",
  77. "getFilterChanges",
  78. "getFilterLogs",
  79. "getLogs",
  80. "hashrate",
  81. "getWork",
  82. "submitWork",
  83. "pendingTransactions",
  84. },
  85. "miner": []string{
  86. "hashrate",
  87. "makeDAG",
  88. "setExtra",
  89. "setGasPrice",
  90. "startAutoDAG",
  91. "start",
  92. "stopAutoDAG",
  93. "stop",
  94. },
  95. "net": []string{
  96. "peerCount",
  97. "listening",
  98. },
  99. "personal": []string{
  100. "listAccounts",
  101. "newAccount",
  102. "deleteAccount",
  103. "unlockAccount",
  104. },
  105. "shh": []string{
  106. "version",
  107. "post",
  108. "hasIdentity",
  109. "newIdentity",
  110. "newFilter",
  111. "uninstallFilter",
  112. "getFilterChanges",
  113. },
  114. "txpool": []string{
  115. "status",
  116. },
  117. "web3": []string{
  118. "sha3",
  119. "version",
  120. "fromWei",
  121. "toWei",
  122. "toHex",
  123. "toAscii",
  124. "fromAscii",
  125. "toBigNumber",
  126. "isAddress",
  127. },
  128. }
  129. )
  130. // Parse a comma separated API string to individual api's
  131. func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth.Ethereum) ([]shared.EthereumApi, error) {
  132. if len(strings.TrimSpace(apistr)) == 0 {
  133. return nil, fmt.Errorf("Empty apistr provided")
  134. }
  135. names := strings.Split(apistr, ",")
  136. apis := make([]shared.EthereumApi, len(names))
  137. for i, name := range names {
  138. switch strings.ToLower(strings.TrimSpace(name)) {
  139. case shared.AdminApiName:
  140. apis[i] = NewAdminApi(xeth, eth, codec)
  141. case shared.DebugApiName:
  142. apis[i] = NewDebugApi(xeth, eth, codec)
  143. case shared.DbApiName:
  144. apis[i] = NewDbApi(xeth, eth, codec)
  145. case shared.EthApiName:
  146. apis[i] = NewEthApi(xeth, eth, codec)
  147. case shared.MinerApiName:
  148. apis[i] = NewMinerApi(eth, codec)
  149. case shared.NetApiName:
  150. apis[i] = NewNetApi(xeth, eth, codec)
  151. case shared.ShhApiName:
  152. apis[i] = NewShhApi(xeth, eth, codec)
  153. case shared.TxPoolApiName:
  154. apis[i] = NewTxPoolApi(xeth, eth, codec)
  155. case shared.PersonalApiName:
  156. apis[i] = NewPersonalApi(xeth, eth, codec)
  157. case shared.Web3ApiName:
  158. apis[i] = NewWeb3Api(xeth, codec)
  159. default:
  160. return nil, fmt.Errorf("Unknown API '%s'", name)
  161. }
  162. }
  163. return apis, nil
  164. }
  165. func Javascript(name string) string {
  166. switch strings.ToLower(strings.TrimSpace(name)) {
  167. case shared.AdminApiName:
  168. return Admin_JS
  169. case shared.DebugApiName:
  170. return Debug_JS
  171. case shared.DbApiName:
  172. return Db_JS
  173. case shared.EthApiName:
  174. return Eth_JS
  175. case shared.MinerApiName:
  176. return Miner_JS
  177. case shared.NetApiName:
  178. return Net_JS
  179. case shared.ShhApiName:
  180. return Shh_JS
  181. case shared.TxPoolApiName:
  182. return TxPool_JS
  183. case shared.PersonalApiName:
  184. return Personal_JS
  185. }
  186. return ""
  187. }