This repository was archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathencrypt.go
138 lines (133 loc) · 3.61 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
131
132
133
134
135
136
137
138
package cmd
import (
"bufio"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"syscall"
"github.com/OpenBazaar/openbazaar-go/repo"
"github.com/OpenBazaar/openbazaar-go/repo/db"
"github.com/OpenBazaar/wallet-interface"
"github.com/ipfs/go-ipfs/repo/fsrepo"
"golang.org/x/crypto/ssh/terminal"
)
type EncryptDatabase struct {
DataDir string `short:"d" long:"datadir" description:"specify the data directory to be used"`
}
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
if x.DataDir == "" {
repoPath, err = repo.GetRepoPath(false, "")
if err != nil {
fmt.Println(err)
return nil
}
} else {
repoPath = x.DataDir
}
for {
fmt.Print("Encrypt the mainnet or testnet db?: ")
resp, _ := reader.ReadString('\n')
if strings.Contains(strings.ToLower(resp), "mainnet") {
filename = "mainnet.db"
dbPath = path.Join(repoPath, "datastore", filename)
repoLockFile := filepath.Join(repoPath, fsrepo.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") {
testnet = true
filename = "testnet.db"
dbPath = path.Join(repoPath, "datastore", filename)
repoLockFile := filepath.Join(repoPath, fsrepo.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: ")
// nolint:unconvert
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: ")
// nolint:unconvert
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, wallet.Bitcoin)
if err != nil {
fmt.Println(err)
return err
}
if sqlliteDB.Config().IsEncrypted() {
fmt.Println("The database is already 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, wallet.Bitcoin)
if err != nil {
fmt.Println(err)
return err
}
err = tmpDB.InitTables(pw)
if err != nil {
fmt.Println(err)
return err
}
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 a password.")
return nil
}