utils.go 3.5 KB

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