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.

132 lines
4.3 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 "io"
  18. import "strings"
  19. import "github.com/chzyer/readline"
  20. import "github.com/arnaucode/derosuite/globals"
  21. import "github.com/arnaucode/derosuite/walletapi"
  22. // display menu before a wallet is opened
  23. func display_easymenu_pre_open_command(l *readline.Instance) {
  24. w := l.Stderr()
  25. io.WriteString(w, "Menu:\n")
  26. io.WriteString(w, "\t\033[1m1\033[0m\tCreate New Wallet\n")
  27. io.WriteString(w, "\t\033[1m2\033[0m\tRecover Wallet using recovery seed (25 words)\n")
  28. io.WriteString(w, "\t\033[1m3\033[0m\tRecover Wallet using recovery key (64 char private spend key hex)\n")
  29. io.WriteString(w, "\t\033[1m4\033[0m\tCreate Watch-able Wallet (view only) using wallet view key\n")
  30. io.WriteString(w, "\n\t\033[1m9\033[0m\tExit menu and start prompt\n")
  31. io.WriteString(w, "\t\033[1m0\033[0m\tExit Wallet\n")
  32. }
  33. // handle all commands
  34. func handle_easymenu_pre_open_command(l *readline.Instance, line string) {
  35. var err error
  36. line = strings.TrimSpace(line)
  37. line_parts := strings.Fields(line)
  38. if len(line_parts) < 1 { // if no command return
  39. return
  40. }
  41. command := ""
  42. if len(line_parts) >= 1 {
  43. command = strings.ToLower(line_parts[0])
  44. }
  45. account_state := account_valid
  46. switch command {
  47. case "1": // create a new random account
  48. if account_valid {
  49. globals.Logger.Warnf("Account already exists. Cannot create new account")
  50. break
  51. }
  52. account = Create_New_Account(l)
  53. account_valid = true
  54. globals.Logger.Debugf("Seed Language %s", account.SeedLanguage)
  55. address = account.GetAddress().String()
  56. display_seed(l)
  57. if offline_mode {
  58. go trigger_offline_data_scan()
  59. }
  60. case "2": // create wallet from recovery seed
  61. if account_valid {
  62. globals.Logger.Warnf("Account already exists. Cannot recover account")
  63. break
  64. }
  65. seed := read_line_with_prompt(l, "Enter your seed (25 words) : ")
  66. account, err = walletapi.Generate_Account_From_Recovery_Words(seed)
  67. if err != nil {
  68. globals.Logger.Warnf("Error while recovering seed err %s\n", err)
  69. break
  70. }
  71. account_valid = true
  72. globals.Logger.Debugf("Seed Language %s", account.SeedLanguage)
  73. globals.Logger.Infof("Successfully recovered wallet from seed")
  74. address = account.GetAddress().String()
  75. if offline_mode {
  76. go trigger_offline_data_scan()
  77. }
  78. case "3": // create wallet from hex seed
  79. account = Create_New_Account_from_seed(l)
  80. if account_valid {
  81. globals.Logger.Infof("Successfully recovered wallet from hex seed")
  82. display_seed(l)
  83. address = account.GetAddress().String()
  84. if offline_mode {
  85. go trigger_offline_data_scan()
  86. }
  87. }
  88. case "4": // create new view only wallet // TODO user providing wrong key is not being validated, do it ASAP
  89. account = Create_New_Account_from_viewable_key(l)
  90. if account_valid {
  91. globals.Logger.Infof("Successfully created view only wallet from viewable keys")
  92. address = account.GetAddress().String()
  93. account_valid = true
  94. if offline_mode {
  95. go trigger_offline_data_scan()
  96. }
  97. }
  98. case "9":
  99. menu_mode = false
  100. globals.Logger.Infof("Prompt mode enabled")
  101. case "0", "bye", "exit", "quit":
  102. globals.Exit_In_Progress = true
  103. default: // just loop
  104. }
  105. _ = account_state
  106. // NOTE: if we are in online mode, it is handled automatically
  107. // user opened or created a new account
  108. // rescan blockchain in offline mode
  109. //if account_state == false && account_valid && offline_mode {
  110. // go trigger_offline_data_scan()
  111. //}
  112. }