utils.go 3.7 KB

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