You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
6.9 KiB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. package main
  17. import "os"
  18. import "io"
  19. import "fmt"
  20. import "bufio"
  21. import "strings"
  22. import "strconv"
  23. import "compress/gzip"
  24. import "github.com/chzyer/readline"
  25. import "github.com/arnaucode/derosuite/globals"
  26. import "github.com/arnaucode/derosuite/walletapi"
  27. // handle all commands while in prompt mode
  28. func handle_prompt_command(l *readline.Instance, line string) {
  29. var err error
  30. line = strings.TrimSpace(line)
  31. line_parts := strings.Fields(line)
  32. if len(line_parts) < 1 { // if no command return
  33. return
  34. }
  35. _ = err
  36. command := ""
  37. if len(line_parts) >= 1 {
  38. command = strings.ToLower(line_parts[0])
  39. }
  40. switch command {
  41. case "help":
  42. usage(l.Stderr())
  43. case "address": // give user his account address
  44. if account_valid {
  45. fmt.Fprintf(l.Stderr(), "%s\n", account.GetAddress())
  46. }
  47. case "rescan_bc": // rescan from 0
  48. fallthrough
  49. case "rescan_spent": // rescan from 0
  50. if offline_mode {
  51. go trigger_offline_data_scan()
  52. } else {
  53. globals.Logger.Warnf("This command is NOT yet implemented")
  54. }
  55. case "seed": // give user his seed
  56. display_seed(l)
  57. case "spendkey": // give user his spend key
  58. display_spend_key(l)
  59. case "viewkey": // give user his viewkey
  60. display_view_key(l)
  61. case "walletviewkey":
  62. display_viewwallet_key(l)
  63. case "set": // set different settings
  64. case "close": // close the account
  65. account_valid = false
  66. tmp := &walletapi.Account{}
  67. address = ""
  68. account = tmp // overwrite previous instance
  69. case "menu": // enable menu mode
  70. menu_mode = true
  71. globals.Logger.Infof("Menu mode enabled")
  72. case "bye", "exit", "quit":
  73. globals.Exit_In_Progress = true
  74. case "": // blank enter key just loop
  75. default:
  76. fmt.Fprintf(l.Stderr(), "you said: %s", strconv.Quote(line))
  77. }
  78. }
  79. // if we are in offline, scan default or user provided file
  80. // this function will replay the blockchain data in offline mode
  81. func trigger_offline_data_scan() {
  82. filename := default_offline_datafile
  83. if globals.Arguments["--offline_datafile"] != nil {
  84. filename = globals.Arguments["--offline_datafile"].(string)
  85. }
  86. f, err := os.Open(filename)
  87. if err != nil {
  88. globals.Logger.Warnf("Cannot read offline data file=\"%s\" err: %s ", filename, err)
  89. return
  90. }
  91. w := bufio.NewReader(f)
  92. gzipreader, err := gzip.NewReader(w)
  93. if err != nil {
  94. globals.Logger.Warnf("Error while decompressing offline data file=\"%s\" err: %s ", filename, err)
  95. return
  96. }
  97. defer gzipreader.Close()
  98. io.Copy(pipe_writer, gzipreader)
  99. }
  100. // this completer is used to complete the commands at the prompt
  101. // BUG, this needs to be disabled in menu mode
  102. var completer = readline.NewPrefixCompleter(
  103. readline.PcItem("help"),
  104. readline.PcItem("address"),
  105. readline.PcItem("rescan_bc"),
  106. readline.PcItem("rescan_spent"),
  107. readline.PcItem("print_height"),
  108. readline.PcItem("seed"),
  109. readline.PcItem("menu"),
  110. readline.PcItem("set",
  111. readline.PcItem("priority",
  112. readline.PcItem("lowest x1"),
  113. readline.PcItem("low x4"),
  114. readline.PcItem("normal x8"),
  115. readline.PcItem("high x13"),
  116. readline.PcItem("veryhigh x20"),
  117. ),
  118. readline.PcItem("default-ring-size"),
  119. readline.PcItem("store-tx-info"),
  120. readline.PcItem("ask-password"),
  121. ),
  122. readline.PcItem("spendkey"),
  123. readline.PcItem("viewkey"),
  124. readline.PcItem("walletviewkey"),
  125. readline.PcItem("bye"),
  126. readline.PcItem("exit"),
  127. readline.PcItem("quit"),
  128. )
  129. // help command screen
  130. func usage(w io.Writer) {
  131. io.WriteString(w, "commands:\n")
  132. io.WriteString(w, "\t\033[1mhelp\033[0m\t\tthis help\n")
  133. io.WriteString(w, "\t\033[1maddress\033[0m\t\tDisplay user address\n")
  134. io.WriteString(w, "\t\033[1mmenu\033[0m\t\tEnable menu mode\n")
  135. io.WriteString(w, "\t\033[1mrescan_bc\033[0m\tRescan blockchain again from 0 height\n")
  136. io.WriteString(w, "\t\033[1mprint_block\033[0m\tPrint block, print_block <block_hash> or <block_height>\n")
  137. io.WriteString(w, "\t\033[1mseed\033[0m\tDisplay seed\n")
  138. io.WriteString(w, "\t\033[1mset\033[0m\tSet various settings\n")
  139. io.WriteString(w, "\t\033[1mstatus\033[0m\t\tShow genereal information\n")
  140. io.WriteString(w, "\t\033[1mspendkey\033[0m\tView secret key\n")
  141. io.WriteString(w, "\t\033[1mviewkey\033[0m\tView view key\n")
  142. io.WriteString(w, "\t\033[1mwalletviewkey\033[0m\tWallet view key, used to create watchable view only wallet\n")
  143. io.WriteString(w, "\t\033[1mbye\033[0m\t\tQuit wallet\n")
  144. io.WriteString(w, "\t\033[1mexit\033[0m\t\tQuit wallet\n")
  145. io.WriteString(w, "\t\033[1mquit\033[0m\t\tQuit wallet\n")
  146. }
  147. // display seed to the user in his preferred language
  148. func display_seed(l *readline.Instance) {
  149. if account_valid {
  150. seed := account.GetSeed()
  151. fmt.Fprintf(l.Stderr(), color_green+"PLEASE NOTE: the following 25 words can be used to recover access to your wallet. Please write them down and store them somewhere safe and secure. Please do not store them in your email or on file storage services outside of your immediate control."+color_white+"\n")
  152. fmt.Fprintf(os.Stderr, color_red+"%s"+color_white+"\n", seed)
  153. }
  154. }
  155. // display spend key
  156. // viewable wallet do not have spend secret key
  157. // TODO wee need to give user a warning if we are printing secret
  158. func display_spend_key(l *readline.Instance) {
  159. if account_valid {
  160. if !account.ViewOnly {
  161. fmt.Fprintf(os.Stderr, "spend key secret : "+color_red+"%s"+color_white+"\n", account.Keys.Spendkey_Secret)
  162. }
  163. fmt.Fprintf(os.Stderr, "spend key public : %s\n", account.Keys.Spendkey_Public)
  164. }
  165. }
  166. //display view key
  167. func display_view_key(l *readline.Instance) {
  168. if account_valid {
  169. fmt.Fprintf(os.Stderr, "view key secret : "+color_yellow+"%s"+color_white+"\n", account.Keys.Viewkey_Secret)
  170. fmt.Fprintf(os.Stderr, "view key public : %s\n", account.Keys.Viewkey_Public)
  171. }
  172. }
  173. // display wallet view only Keys
  174. // this will create a watchable view only mode
  175. func display_viewwallet_key(l *readline.Instance) {
  176. if account_valid {
  177. io.WriteString(l.Stderr(), fmt.Sprintf("This Key can used to create a watch only wallet. This wallet can only see incoming funds but cannot spend them.\nThe key is below.\n%s\n\n", account.GetViewWalletKey()))
  178. }
  179. }