ui_lib.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/ethutil"
  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(ethutil.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 := ethutil.Compile(code, false)
  117. if err != nil {
  118. return err.Error(), err
  119. }
  120. return ethutil.Bytes2Hex(bcode), err
  121. }
  122. func (self *UiLib) Call(params map[string]interface{}) (string, error) {
  123. object := mapToTxParams(params)
  124. return self.XEth.Execute(
  125. object["to"],
  126. object["value"],
  127. object["gas"],
  128. object["gasPrice"],
  129. object["data"],
  130. )
  131. }
  132. func (self *UiLib) AddLocalTransaction(to, data, gas, gasPrice, value string) int {
  133. return 0
  134. /*
  135. return self.miner.AddLocalTx(&miner.LocalTx{
  136. To: ethutil.Hex2Bytes(to),
  137. Data: ethutil.Hex2Bytes(data),
  138. Gas: gas,
  139. GasPrice: gasPrice,
  140. Value: value,
  141. }) - 1
  142. */
  143. }
  144. func (self *UiLib) RemoveLocalTransaction(id int) {
  145. //self.miner.RemoveLocalTx(id)
  146. }
  147. func (self *UiLib) SetGasPrice(price string) {
  148. self.Miner().MinAcceptedGasPrice = ethutil.Big(price)
  149. }
  150. func (self *UiLib) SetExtra(extra string) {
  151. self.Miner().Extra = extra
  152. }
  153. func (self *UiLib) ToggleMining() bool {
  154. if !self.Miner().Mining() {
  155. self.Miner().Start()
  156. return true
  157. } else {
  158. self.Miner().Stop()
  159. return false
  160. }
  161. }
  162. func (self *UiLib) ToHex(data string) string {
  163. return "0x" + ethutil.Bytes2Hex([]byte(data))
  164. }
  165. func (self *UiLib) ToAscii(data string) string {
  166. start := 0
  167. if len(data) > 1 && data[0:2] == "0x" {
  168. start = 2
  169. }
  170. return string(ethutil.Hex2Bytes(data[start:]))
  171. }
  172. /// Ethereum filter methods
  173. func (self *UiLib) NewFilter(object map[string]interface{}, view *qml.Common) (id int) {
  174. /* TODO remove me
  175. filter := qt.NewFilterFromMap(object, self.eth)
  176. filter.MessageCallback = func(messages state.Messages) {
  177. view.Call("messages", xeth.ToMessages(messages), id)
  178. }
  179. id = self.filterManager.InstallFilter(filter)
  180. return id
  181. */
  182. return 0
  183. }
  184. func (self *UiLib) NewFilterString(typ string, view *qml.Common) (id int) {
  185. /* TODO remove me
  186. filter := core.NewFilter(self.eth)
  187. filter.BlockCallback = func(block *types.Block) {
  188. view.Call("messages", "{}", id)
  189. }
  190. id = self.filterManager.InstallFilter(filter)
  191. return id
  192. */
  193. return 0
  194. }
  195. func (self *UiLib) Messages(id int) *ethutil.List {
  196. /* TODO remove me
  197. filter := self.filterManager.GetFilter(id)
  198. if filter != nil {
  199. messages := xeth.ToMessages(filter.Find())
  200. return messages
  201. }
  202. */
  203. return ethutil.EmptyList()
  204. }
  205. func (self *UiLib) ReadFile(p string) string {
  206. content, err := ioutil.ReadFile(self.AssetPath(path.Join("ext", p)))
  207. if err != nil {
  208. guilogger.Infoln("error reading file", p, ":", err)
  209. }
  210. return string(content)
  211. }
  212. func (self *UiLib) UninstallFilter(id int) {
  213. self.filterManager.UninstallFilter(id)
  214. }
  215. func mapToTxParams(object map[string]interface{}) map[string]string {
  216. // Default values
  217. if object["from"] == nil {
  218. object["from"] = ""
  219. }
  220. if object["to"] == nil {
  221. object["to"] = ""
  222. }
  223. if object["value"] == nil {
  224. object["value"] = ""
  225. }
  226. if object["gas"] == nil {
  227. object["gas"] = ""
  228. }
  229. if object["gasPrice"] == nil {
  230. object["gasPrice"] = ""
  231. }
  232. var dataStr string
  233. var data []string
  234. if list, ok := object["data"].(*qml.List); ok {
  235. list.Convert(&data)
  236. } else if str, ok := object["data"].(string); ok {
  237. data = []string{str}
  238. }
  239. for _, str := range data {
  240. if ethutil.IsHex(str) {
  241. str = str[2:]
  242. if len(str) != 64 {
  243. str = ethutil.LeftPadString(str, 64)
  244. }
  245. } else {
  246. str = ethutil.Bytes2Hex(ethutil.LeftPadBytes(ethutil.Big(str).Bytes(), 32))
  247. }
  248. dataStr += str
  249. }
  250. object["data"] = dataStr
  251. conv := make(map[string]string)
  252. for key, value := range object {
  253. if v, ok := value.(string); ok {
  254. conv[key] = v
  255. }
  256. }
  257. return conv
  258. }