utils.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. },
  84. "miner": []string{
  85. "hashrate",
  86. "makeDAG",
  87. "setExtra",
  88. "setGasPrice",
  89. "startAutoDAG",
  90. "start",
  91. "stopAutoDAG",
  92. "stop",
  93. },
  94. "net": []string{
  95. "peerCount",
  96. "listening",
  97. },
  98. "personal": []string{
  99. "listAccounts",
  100. "newAccount",
  101. "deleteAccount",
  102. "unlockAccount",
  103. },
  104. "shh": []string{
  105. "version",
  106. "post",
  107. "hasIdentity",
  108. "newIdentity",
  109. "newFilter",
  110. "uninstallFilter",
  111. "getFilterChanges",
  112. },
  113. "txpool": []string{
  114. "status",
  115. },
  116. "web3": []string{
  117. "sha3",
  118. "version",
  119. "fromWei",
  120. "toWei",
  121. "toHex",
  122. "toAscii",
  123. "fromAscii",
  124. "toBigNumber",
  125. "isAddress",
  126. },
  127. }
  128. )
  129. // Parse a comma separated API string to individual api's
  130. func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth.Ethereum) ([]shared.EthereumApi, error) {
  131. if len(strings.TrimSpace(apistr)) == 0 {
  132. return nil, fmt.Errorf("Empty apistr provided")
  133. }
  134. names := strings.Split(apistr, ",")
  135. apis := make([]shared.EthereumApi, len(names))
  136. for i, name := range names {
  137. switch strings.ToLower(strings.TrimSpace(name)) {
  138. case shared.AdminApiName:
  139. apis[i] = NewAdminApi(xeth, eth, codec)
  140. case shared.DebugApiName:
  141. apis[i] = NewDebugApi(xeth, eth, codec)
  142. case shared.DbApiName:
  143. apis[i] = NewDbApi(xeth, eth, codec)
  144. case shared.EthApiName:
  145. apis[i] = NewEthApi(xeth, codec)
  146. case shared.MinerApiName:
  147. apis[i] = NewMinerApi(eth, codec)
  148. case shared.NetApiName:
  149. apis[i] = NewNetApi(xeth, eth, codec)
  150. case shared.ShhApiName:
  151. apis[i] = NewShhApi(xeth, eth, codec)
  152. case shared.TxPoolApiName:
  153. apis[i] = NewTxPoolApi(xeth, eth, codec)
  154. case shared.PersonalApiName:
  155. apis[i] = NewPersonalApi(xeth, eth, codec)
  156. case shared.Web3ApiName:
  157. apis[i] = NewWeb3Api(xeth, codec)
  158. default:
  159. return nil, fmt.Errorf("Unknown API '%s'", name)
  160. }
  161. }
  162. return apis, nil
  163. }
  164. func Javascript(name string) string {
  165. switch strings.ToLower(strings.TrimSpace(name)) {
  166. case shared.AdminApiName:
  167. return Admin_JS
  168. case shared.DebugApiName:
  169. return Debug_JS
  170. case shared.DbApiName:
  171. return Db_JS
  172. case shared.MinerApiName:
  173. return Miner_JS
  174. case shared.NetApiName:
  175. return Net_JS
  176. case shared.ShhApiName:
  177. return Shh_JS
  178. case shared.TxPoolApiName:
  179. return TxPool_JS
  180. case shared.PersonalApiName:
  181. return Personal_JS
  182. }
  183. return ""
  184. }