ui_lib.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. "io/ioutil"
  21. "path/filepath"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/eth"
  25. "github.com/ethereum/go-ethereum/event/filter"
  26. "github.com/ethereum/go-ethereum/xeth"
  27. "github.com/obscuren/qml"
  28. )
  29. type memAddr struct {
  30. Num string
  31. Value string
  32. }
  33. // UI Library that has some basic functionality exposed
  34. type UiLib struct {
  35. *xeth.XEth
  36. engine *qml.Engine
  37. eth *eth.Ethereum
  38. connected bool
  39. assetPath string
  40. // The main application window
  41. win *qml.Window
  42. filterCallbacks map[int][]int
  43. filterManager *filter.FilterManager
  44. }
  45. func NewUiLib(engine *qml.Engine, eth *eth.Ethereum, assetPath, libPath string) *UiLib {
  46. x := xeth.New(eth, nil)
  47. lib := &UiLib{
  48. XEth: x,
  49. engine: engine,
  50. eth: eth,
  51. assetPath: assetPath,
  52. filterCallbacks: make(map[int][]int),
  53. }
  54. lib.filterManager = filter.NewFilterManager(eth.EventMux())
  55. go lib.filterManager.Start()
  56. return lib
  57. }
  58. func (self *UiLib) Notef(args []interface{}) {
  59. guilogger.Infoln(args...)
  60. }
  61. func (self *UiLib) ImportTx(rlpTx string) {
  62. tx := types.NewTransactionFromBytes(common.Hex2Bytes(rlpTx))
  63. err := self.eth.TxPool().Add(tx)
  64. if err != nil {
  65. guilogger.Infoln("import tx failed ", err)
  66. }
  67. }
  68. func (ui *UiLib) Muted(content string) {
  69. component, err := ui.engine.LoadFile(ui.AssetPath("qml/muted.qml"))
  70. if err != nil {
  71. guilogger.Debugln(err)
  72. return
  73. }
  74. win := component.CreateWindow(nil)
  75. go func() {
  76. path := "file://" + ui.AssetPath("muted/index.html")
  77. win.Set("url", path)
  78. win.Show()
  79. win.Wait()
  80. }()
  81. }
  82. func (ui *UiLib) Connect(button qml.Object) {
  83. if !ui.connected {
  84. ui.eth.Start()
  85. ui.connected = true
  86. button.Set("enabled", false)
  87. }
  88. }
  89. func (ui *UiLib) ConnectToPeer(nodeURL string) {
  90. if err := ui.eth.AddPeer(nodeURL); err != nil {
  91. guilogger.Infoln("AddPeer error: " + err.Error())
  92. }
  93. }
  94. func (ui *UiLib) AssetPath(p string) string {
  95. return filepath.Join(ui.assetPath, p)
  96. }
  97. func (self *UiLib) Transact(params map[string]interface{}) (string, error) {
  98. object := mapToTxParams(params)
  99. return self.XEth.Transact(
  100. object["from"],
  101. object["to"],
  102. "",
  103. object["value"],
  104. object["gas"],
  105. object["gasPrice"],
  106. object["data"],
  107. )
  108. }
  109. func (self *UiLib) Call(params map[string]interface{}) (string, string, error) {
  110. object := mapToTxParams(params)
  111. return self.XEth.Call(
  112. object["from"],
  113. object["to"],
  114. object["value"],
  115. object["gas"],
  116. object["gasPrice"],
  117. object["data"],
  118. )
  119. }
  120. func (self *UiLib) AddLocalTransaction(to, data, gas, gasPrice, value string) int {
  121. return 0
  122. /*
  123. return self.miner.AddLocalTx(&miner.LocalTx{
  124. To: common.Hex2Bytes(to),
  125. Data: common.Hex2Bytes(data),
  126. Gas: gas,
  127. GasPrice: gasPrice,
  128. Value: value,
  129. }) - 1
  130. */
  131. }
  132. func (self *UiLib) RemoveLocalTransaction(id int) {
  133. //self.miner.RemoveLocalTx(id)
  134. }
  135. func (self *UiLib) ToggleMining() bool {
  136. if !self.eth.IsMining() {
  137. err := self.eth.StartMining(4)
  138. return err == nil
  139. } else {
  140. self.eth.StopMining()
  141. return false
  142. }
  143. }
  144. func (self *UiLib) ToHex(data string) string {
  145. return "0x" + common.Bytes2Hex([]byte(data))
  146. }
  147. func (self *UiLib) ToAscii(data string) string {
  148. start := 0
  149. if len(data) > 1 && data[0:2] == "0x" {
  150. start = 2
  151. }
  152. return string(common.Hex2Bytes(data[start:]))
  153. }
  154. /// Ethereum filter methods
  155. func (self *UiLib) NewFilter(object map[string]interface{}, view *qml.Common) (id int) {
  156. /* TODO remove me
  157. filter := qt.NewFilterFromMap(object, self.eth)
  158. filter.MessageCallback = func(messages state.Messages) {
  159. view.Call("messages", xeth.ToMessages(messages), id)
  160. }
  161. id = self.filterManager.InstallFilter(filter)
  162. return id
  163. */
  164. return 0
  165. }
  166. func (self *UiLib) NewFilterString(typ string, view *qml.Common) (id int) {
  167. /* TODO remove me
  168. filter := core.NewFilter(self.eth)
  169. filter.BlockCallback = func(block *types.Block) {
  170. view.Call("messages", "{}", id)
  171. }
  172. id = self.filterManager.InstallFilter(filter)
  173. return id
  174. */
  175. return 0
  176. }
  177. func (self *UiLib) Messages(id int) *common.List {
  178. /* TODO remove me
  179. filter := self.filterManager.GetFilter(id)
  180. if filter != nil {
  181. messages := xeth.ToMessages(filter.Find())
  182. return messages
  183. }
  184. */
  185. return common.EmptyList()
  186. }
  187. func (self *UiLib) ReadFile(p string) string {
  188. content, err := ioutil.ReadFile(self.AssetPath(filepath.Join("ext", p)))
  189. if err != nil {
  190. guilogger.Infoln("error reading file", p, ":", err)
  191. }
  192. return string(content)
  193. }
  194. func (self *UiLib) UninstallFilter(id int) {
  195. self.filterManager.UninstallFilter(id)
  196. }
  197. func mapToTxParams(object map[string]interface{}) map[string]string {
  198. // Default values
  199. if object["from"] == nil {
  200. object["from"] = ""
  201. }
  202. if object["to"] == nil {
  203. object["to"] = ""
  204. }
  205. if object["value"] == nil {
  206. object["value"] = ""
  207. }
  208. if object["gas"] == nil {
  209. object["gas"] = ""
  210. }
  211. if object["gasPrice"] == nil {
  212. object["gasPrice"] = ""
  213. }
  214. var dataStr string
  215. var data []string
  216. if list, ok := object["data"].(*qml.List); ok {
  217. list.Convert(&data)
  218. } else if str, ok := object["data"].(string); ok {
  219. data = []string{str}
  220. }
  221. for _, str := range data {
  222. if common.IsHex(str) {
  223. str = str[2:]
  224. if len(str) != 64 {
  225. str = common.LeftPadString(str, 64)
  226. }
  227. } else {
  228. str = common.Bytes2Hex(common.LeftPadBytes(common.Big(str).Bytes(), 32))
  229. }
  230. dataStr += str
  231. }
  232. object["data"] = dataStr
  233. conv := make(map[string]string)
  234. for key, value := range object {
  235. if v, ok := value.(string); ok {
  236. conv[key] = v
  237. }
  238. }
  239. return conv
  240. }