forked from btcsuite/btcwallet
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
156 lines (133 loc) · 3.2 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2015-2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"github.com/ltcsuite/ltcutil"
"github.com/ltcsuite/ltcwallet/waddrmgr"
"github.com/ltcsuite/ltcwallet/walletdb"
_ "github.com/ltcsuite/ltcwallet/walletdb/bdb"
"github.com/ltcsuite/ltcwallet/wtxmgr"
"github.com/jessevdk/go-flags"
)
const defaultNet = "mainnet"
var datadir = ltcutil.AppDataDir("ltcwallet", false)
// Flags.
var opts = struct {
Force bool `short:"f" description:"Force removal without prompt"`
DbPath string `long:"db" description:"Path to wallet database"`
}{
Force: false,
DbPath: filepath.Join(datadir, defaultNet, "wallet.db"),
}
func init() {
_, err := flags.Parse(&opts)
if err != nil {
os.Exit(1)
}
}
var (
// Namespace keys.
waddrmgrNamespace = []byte("waddrmgr")
wtxmgrNamespace = []byte("wtxmgr")
)
func yes(s string) bool {
switch s {
case "y", "Y", "yes", "Yes":
return true
default:
return false
}
}
func no(s string) bool {
switch s {
case "n", "N", "no", "No":
return true
default:
return false
}
}
func main() {
os.Exit(mainInt())
}
func mainInt() int {
fmt.Println("Database path:", opts.DbPath)
_, err := os.Stat(opts.DbPath)
if os.IsNotExist(err) {
fmt.Println("Database file does not exist")
return 1
}
for !opts.Force {
fmt.Print("Drop all ltcwallet transaction history? [y/N] ")
scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
if !scanner.Scan() {
// Exit on EOF.
return 0
}
err := scanner.Err()
if err != nil {
fmt.Println()
fmt.Println(err)
return 1
}
resp := scanner.Text()
if yes(resp) {
break
}
if no(resp) || resp == "" {
return 0
}
fmt.Println("Enter yes or no.")
}
db, err := walletdb.Open("bdb", opts.DbPath, true)
if err != nil {
fmt.Println("Failed to open database:", err)
return 1
}
defer db.Close()
fmt.Println("Dropping btcwallet transaction history")
err = walletdb.Update(db, func(tx walletdb.ReadWriteTx) error {
err := tx.DeleteTopLevelBucket(wtxmgrNamespace)
if err != nil && err != walletdb.ErrBucketNotFound {
return err
}
ns, err := tx.CreateTopLevelBucket(wtxmgrNamespace)
if err != nil {
return err
}
err = wtxmgr.Create(ns)
if err != nil {
return err
}
ns = tx.ReadWriteBucket(waddrmgrNamespace)
birthdayBlock, err := waddrmgr.FetchBirthdayBlock(ns)
if err != nil {
fmt.Println("Wallet does not have a birthday block " +
"set, falling back to rescan from genesis")
startBlock, err := waddrmgr.FetchStartBlock(ns)
if err != nil {
return err
}
return waddrmgr.PutSyncedTo(ns, startBlock)
}
// We'll need to remove our birthday block first because it
// serves as a barrier when updating our state to detect reorgs
// due to the wallet not storing all block hashes of the chain.
if err := waddrmgr.DeleteBirthdayBlock(ns); err != nil {
return err
}
if err := waddrmgr.PutSyncedTo(ns, &birthdayBlock); err != nil {
return err
}
return waddrmgr.PutBirthdayBlock(ns, birthdayBlock)
})
if err != nil {
fmt.Println("Failed to drop and re-create namespace:", err)
return 1
}
return 0
}