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 pathdecrypt.go
110 lines (105 loc) · 3.02 KB
/
decrypt.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
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 DecryptDatabase struct {
DataDir string `short:"d" long:"datadir" description:"specify the data directory to be used"`
}
func (x *DecryptDatabase) 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("Decrypt 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 decrypt 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 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 decrypt 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 {
fmt.Println("No comprende")
}
}
fmt.Print("Enter your password: ")
// nolint:unconvert
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
fmt.Println("")
pw := string(bytePassword)
pw = strings.Replace(pw, "'", "''", -1)
sqlliteDB, err := db.Create(repoPath, pw, testnet, wallet.Bitcoin)
if err != nil || sqlliteDB.Config().IsEncrypted() {
fmt.Println("Invalid password")
return err
}
if err := os.MkdirAll(path.Join(repoPath, "tmp", "datastore"), os.ModePerm); err != nil {
return err
}
tmpDB, err := db.Create(path.Join(repoPath, "tmp"), "", testnet, wallet.Bitcoin)
if err != nil {
fmt.Println(err)
return err
}
err = tmpDB.InitTables("")
if err != nil {
fmt.Println(err)
return err
}
if err := sqlliteDB.Copy(path.Join(repoPath, "tmp", "datastore", filename), ""); err != nil {
fmt.Println(err)
return err
}
err = os.Rename(path.Join(repoPath, "tmp", "datastore", filename), path.Join(repoPath, "datastore", filename))
if err != nil {
fmt.Println(err)
return err
}
os.RemoveAll(path.Join(repoPath, "tmp"))
fmt.Println("Success!")
return nil
}