forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encrypt.go
130 lines (125 loc) · 3.41 KB
/
encrypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package cmd
import (
"bufio"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"syscall"
"github.com/OpenBazaar/openbazaar-go/repo"
"github.com/OpenBazaar/openbazaar-go/repo/db"
lockfile "github.com/ipfs/go-ipfs/repo/fsrepo/lock"
"golang.org/x/crypto/ssh/terminal"
)
type EncryptDatabase struct{}
func (x *EncryptDatabase) Execute(args []string) error {
reader := bufio.NewReader(os.Stdin)
var repoPath string
var dbPath string
var filename string
var testnet bool
var err error
for {
fmt.Print("Encrypt the mainnet or testnet db?: ")
resp, _ := reader.ReadString('\n')
if strings.Contains(strings.ToLower(resp), "mainnet") {
repoPath, err = repo.GetRepoPath(false)
if err != nil {
fmt.Println(err)
return nil
}
filename = "mainnet.db"
dbPath = path.Join(repoPath, "datastore", filename)
repoLockFile := filepath.Join(repoPath, lockfile.LockFile)
if _, err := os.Stat(repoLockFile); !os.IsNotExist(err) {
fmt.Println("Cannot encrypt while the daemon is running.")
return nil
}
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
fmt.Println("Database does not exist. You may need to run the node at least once to initialize it.")
return nil
}
break
} else if strings.Contains(strings.ToLower(resp), "testnet") {
repoPath, err = repo.GetRepoPath(true)
if err != nil {
fmt.Println(err)
return nil
}
testnet = true
filename = "testnet.db"
dbPath = path.Join(repoPath, "datastore", filename)
repoLockFile := filepath.Join(repoPath, lockfile.LockFile)
if _, err := os.Stat(repoLockFile); !os.IsNotExist(err) {
fmt.Println("Cannot encrypt while the daemon is running.")
return nil
}
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
fmt.Println("Database does not exist. You may need to run the daemon at least once to initialize it.")
return nil
}
break
} else {
fmt.Println("No comprende")
}
}
var pw string
for {
fmt.Print("Enter a veerrrry strong password: ")
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
fmt.Println("")
resp := string(bytePassword)
if len(resp) < 8 {
fmt.Println("You call that a password? Try again.")
} else if resp != "" {
pw = resp
break
} else {
fmt.Println("Seriously, enter a password.")
}
}
for {
fmt.Print("Confirm your password: ")
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
fmt.Println("")
resp := string(bytePassword)
if resp == pw {
break
} else {
fmt.Println("Quit effin around. Try again.")
}
}
pw = strings.Replace(pw, "'", "''", -1)
tmpPath := path.Join(repoPath, "tmp")
sqlliteDB, err := db.Create(repoPath, "", testnet)
if err != nil {
fmt.Println(err)
return err
}
if sqlliteDB.Config().IsEncrypted() {
fmt.Println("The database is alredy encrypted")
return nil
}
if err := os.MkdirAll(path.Join(repoPath, "tmp", "datastore"), os.ModePerm); err != nil {
return err
}
tmpDB, err := db.Create(tmpPath, pw, testnet)
if err != nil {
fmt.Println(err)
return err
}
tmpDB.InitTables(pw)
if err := sqlliteDB.Copy(path.Join(tmpPath, "datastore", filename), pw); err != nil {
fmt.Println(err)
return err
}
err = os.Rename(path.Join(tmpPath, "datastore", filename), path.Join(repoPath, "datastore", filename))
if err != nil {
fmt.Println(err)
return err
}
os.RemoveAll(path.Join(tmpPath))
fmt.Println("Success! You must now run openbazaard start with the --password flag.")
return nil
}