ui_lib.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /**
  15. * @authors
  16. * Jeffrey Wilcke <i@jev.io>
  17. */
  18. package main
  19. import (
  20. "fmt"
  21. "io/ioutil"
  22. "path"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/eth"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/event/filter"
  27. "github.com/ethereum/go-ethereum/javascript"
  28. "github.com/ethereum/go-ethereum/xeth"
  29. "github.com/obscuren/qml"
  30. )
  31. type memAddr struct {
  32. Num string
  33. Value string
  34. }
  35. // UI Library that has some basic functionality exposed
  36. type UiLib struct {
  37. *xeth.XEth
  38. engine *qml.Engine
  39. eth *eth.Ethereum
  40. connected bool
  41. assetPath string
  42. // The main application window
  43. win *qml.Window
  44. jsEngine *javascript.JSRE
  45. filterCallbacks map[int][]int
  46. filterManager *filter.FilterManager
  47. }
  48. func NewUiLib(engine *qml.Engine, eth *eth.Ethereum, assetPath string) *UiLib {
  49. x := xeth.New(eth, nil)
  50. lib := &UiLib{XEth: x, engine: engine, eth: eth, assetPath: assetPath, jsEngine: javascript.NewJSRE(x), filterCallbacks: make(map[int][]int)} //, filters: make(map[int]*xeth.JSFilter)}
  51. lib.filterManager = filter.NewFilterManager(eth.EventMux())
  52. go lib.filterManager.Start()
  53. return lib
  54. }
  55. func (self *UiLib) Notef(args []interface{}) {
  56. guilogger.Infoln(args...)
  57. }
  58. func (self *UiLib) ImportTx(rlpTx string) {
  59. tx := types.NewTransactionFromBytes(common.Hex2Bytes(rlpTx))
  60. err := self.eth.TxPool().Add(tx)
  61. if err != nil {
  62. guilogger.Infoln("import tx failed ", err)
  63. }
  64. }
  65. func (self *UiLib) EvalJavascriptFile(path string) {
  66. self.jsEngine.LoadExtFile(path[7:])
  67. }
  68. func (self *UiLib) EvalJavascriptString(str string) string {
  69. value, err := self.jsEngine.Run(str)
  70. if err != nil {
  71. return err.Error()
  72. }
  73. return fmt.Sprintf("%v", value)
  74. }
  75. func (ui *UiLib) Muted(content string) {
  76. component, err := ui.engine.LoadFile(ui.AssetPath("qml/muted.qml"))
  77. if err != nil {
  78. guilogger.Debugln(err)
  79. return
  80. }
  81. win := component.CreateWindow(nil)
  82. go func() {
  83. path := "file://" + ui.AssetPath("muted/index.html")
  84. win.Set("url", path)
  85. win.Show()
  86. win.Wait()
  87. }()
  88. }
  89. func (ui *UiLib) Connect(button qml.Object) {
  90. if !ui.connected {
  91. ui.eth.Start()
  92. ui.connected = true
  93. button.Set("enabled", false)
  94. }
  95. }
  96. func (ui *UiLib) ConnectToPeer(nodeURL string) {
  97. if err := ui.eth.SuggestPeer(nodeURL); err != nil {
  98. guilogger.Infoln("SuggestPeer error: " + err.Error())
  99. }
  100. }
  101. func (ui *UiLib) AssetPath(p string) string {
  102. return path.Join(ui.assetPath, p)
  103. }
  104. func (self *UiLib) Transact(params map[string]interface{}) (string, error) {
  105. object := mapToTxParams(params)
  106. return self.XEth.Transact(
  107. object["from"],
  108. object["to"],
  109. object["value"],
  110. object["gas"],
  111. object["gasPrice"],
  112. object["data"],
  113. )
  114. }
  115. func (self *UiLib) Compile(code string) (string, error) {
  116. bcode, err := common.Compile(code, false)
  117. if err != nil {
  118. return err.Error(), err
  119. }
  120. return common.Bytes2Hex(bcode), err
  121. }
  122. func (self *UiLib) Call(params map[string]interface{}) (string, error) {
  123. object := mapToTxParams(params)
  124. return self.XEth.Call(
  125. object["from"],
  126. object["to"],
  127. object["value"],
  128. object["gas"],
  129. object["gasPrice"],
  130. object["data"],
  131. )
  132. }
  133. func (self *UiLib) AddLocalTransaction(to, data, gas, gasPrice, value string) int {
  134. return 0
  135. /*
  136. return self.miner.AddLocalTx(&miner.LocalTx{
  137. To: common.Hex2Bytes(to),
  138. Data: common.Hex2Bytes(data),
  139. Gas: gas,
  140. GasPrice: gasPrice,
  141. Value: value,
  142. }) - 1
  143. */
  144. }
  145. func (self *UiLib) RemoveLocalTransaction(id int) {
  146. //self.miner.RemoveLocalTx(id)
  147. }
  148. func (self *UiLib) ToggleMining() bool {
  149. if !self.eth.IsMining() {
  150. err := self.eth.StartMining()
  151. return err == nil
  152. } else {
  153. self.eth.StopMining()
  154. return false
  155. }
  156. }
  157. func (self *UiLib) ToHex(data string) string {
  158. return "0x" + common.Bytes2Hex([]byte(data))
  159. }
  160. func (self *UiLib) ToAscii(data string) string {
  161. start := 0
  162. if len(data) > 1 && data[0:2] == "0x" {
  163. start = 2
  164. }
  165. return string(common.Hex2Bytes(data[start:]))
  166. }
  167. /// Ethereum filter methods
  168. func (self *UiLib) NewFilter(object map[string]interface{}, view *qml.Common) (id int) {
  169. /* TODO remove me
  170. filter := qt.NewFilterFromMap(object, self.eth)
  171. filter.MessageCallback = func(messages state.Messages) {
  172. view.Call("messages", xeth.ToMessages(messages), id)
  173. }
  174. id = self.filterManager.InstallFilter(filter)
  175. return id
  176. */
  177. return 0
  178. }
  179. func (self *UiLib) NewFilterString(typ string, view *qml.Common) (id int) {
  180. /* TODO remove me
  181. filter := core.NewFilter(self.eth)
  182. filter.BlockCallback = func(block *types.Block) {
  183. view.Call("messages", "{}", id)
  184. }
  185. id = self.filterManager.InstallFilter(filter)
  186. return id
  187. */
  188. return 0
  189. }
  190. func (self *UiLib) Messages(id int) *common.List {
  191. /* TODO remove me
  192. filter := self.filterManager.GetFilter(id)
  193. if filter != nil {
  194. messages := xeth.ToMessages(filter.Find())
  195. return messages
  196. }
  197. */
  198. return common.EmptyList()
  199. }
  200. func (self *UiLib) ReadFile(p string) string {
  201. content, err := ioutil.ReadFile(self.AssetPath(path.Join("ext", p)))
  202. if err != nil {
  203. guilogger.Infoln("error reading file", p, ":", err)
  204. }
  205. return string(content)
  206. }
  207. func (self *UiLib) UninstallFilter(id int) {
  208. self.filterManager.UninstallFilter(id)
  209. }
  210. func mapToTxParams(object map[string]interface{}) map[string]string {
  211. // Default values
  212. if object["from"] == nil {
  213. object["from"] = ""
  214. }
  215. if object["to"] == nil {
  216. object["to"] = ""
  217. }
  218. if object["value"] == nil {
  219. object["value"] = ""
  220. }
  221. if object["gas"] == nil {
  222. object["gas"] = ""
  223. }
  224. if object["gasPrice"] == nil {
  225. object["gasPrice"] = ""
  226. }
  227. var dataStr string
  228. var data []string
  229. if list, ok := object["data"].(*qml.List); ok {
  230. list.Convert(&data)
  231. } else if str, ok := object["data"].(string); ok {
  232. data = []string{str}
  233. }
  234. for _, str := range data {
  235. if common.IsHex(str) {
  236. str = str[2:]
  237. if len(str) != 64 {
  238. str = common.LeftPadString(str, 64)
  239. }
  240. } else {
  241. str = common.Bytes2Hex(common.LeftPadBytes(common.Big(str).Bytes(), 32))
  242. }
  243. dataStr += str
  244. }
  245. object["data"] = dataStr
  246. conv := make(map[string]string)
  247. for key, value := range object {
  248. if v, ok := value.(string); ok {
  249. conv[key] = v
  250. }
  251. }
  252. return conv
  253. }