utils.go 3.7 KB

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