-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathkeystore.go
85 lines (68 loc) · 2 KB
/
keystore.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
//go:build debug
package debug
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/dgraph-io/badger/v3"
"github.com/pokt-network/pocket/app/client/keybase"
"github.com/pokt-network/pocket/build"
"github.com/pokt-network/pocket/logger"
)
const debugKeybaseSuffix = "/.pocket/keys"
var (
// TODO: Allow users to override this value via `datadir` flag or env var or config file
debugKeybasePath string
)
// Initialise the debug keybase with the 999 validator keys from the private-keys manifest file
func init() {
homeDir, err := os.UserHomeDir()
if err != nil {
logger.Global.Fatal().Err(err).Msg("Cannot find user home directory")
}
debugKeybasePath = homeDir + debugKeybaseSuffix
// Initialise the debug keybase with the 999 validators
if err := initializeDebugKeybase(); err != nil {
logger.Global.Fatal().Err(err).Msg("Cannot initialise the keybase with the validator keys")
}
}
func initializeDebugKeybase() error {
// Create/Open the keybase at `$HOME/.pocket/keys`
kb, err := keybase.NewKeybase(debugKeybasePath)
if err != nil {
return err
}
db := kb.GetBadgerDB()
if err := restoreBadgerDB(build.DebugKeybaseBackup, db); err != nil {
return err
}
// Close DB connection
if err := kb.Stop(); err != nil {
return err
}
return nil
}
func restoreBadgerDB(backupData []byte, db *badger.DB) error {
logger.Global.Debug().Msgf(fmt.Sprintf("Debug keybase initializing... Restoring from the embedded backup file..."))
// Create a temporary directory to store the backup data
tempDir, err := ioutil.TempDir("", "badgerdb-restore")
if err != nil {
return err
}
defer os.RemoveAll(tempDir)
// Write the backup data to a file in the temporary directory
backupFilePath := filepath.Join(tempDir, "backup")
if err := ioutil.WriteFile(backupFilePath, backupData, 0644); err != nil {
return err
}
backupFile, err := os.Open(backupFilePath)
if err != nil {
return err
}
defer backupFile.Close()
if err := db.Load(backupFile, 4); err != nil {
return err
}
return nil
}