/
wallet_migrate.go
87 lines (75 loc) · 2.02 KB
/
wallet_migrate.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
/*
Copyright © 2023 Glif LTD
*/
package cmd
import (
"fmt"
"github.com/glifio/cli/util"
"github.com/spf13/cobra"
)
// migrateCmd represents the migrate command
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Migrates keys from keys.toml to the new encrypted keystore",
Run: func(cmd *cobra.Command, args []string) {
err := checkWalletMigrated()
if err == nil {
fmt.Println("Wallet already migrated to encrypted keystore.")
return
}
err = migrateLegacyKeys()
if err != nil {
logFatal(err)
}
fmt.Printf("Keys successfully migrated to the encrypted keystore!\n\n")
fmt.Printf("Please set a new passphrase to encrypt the owner key:\n\n")
as := util.AccountsStore()
ownerAddr, _, err := as.GetAddrs(string(util.OwnerKey))
if err != nil {
logFatal(err)
}
err = changePassphrase(ownerAddr)
if err != nil {
logFatal(err)
}
fmt.Printf("\nFor increased security, please delete the legacy %s/keys.toml file with the cleartext private keys.\n", cfgDir)
fmt.Println("The legacy keys.toml file is no longer needed, unless you are just testing and plan to downgrade.")
bs := util.BackupsStore()
bs.Invalidate()
},
}
func init() {
walletCmd.AddCommand(migrateCmd)
}
func migrateLegacyKeys() error {
keys := []util.KeyType{util.OwnerKey, util.OperatorKey, util.RequestKey}
for _, key := range keys {
if err := migrateLegacyKey(key); err != nil {
return err
}
}
return nil
}
func migrateLegacyKey(key util.KeyType) error {
ksLegacy := util.KeyStoreLegacy()
ks := util.KeyStore()
as := util.AccountsStore()
pkStr, err := ksLegacy.Get(string(key))
if err != nil {
return err
}
if pkStr == "" {
return fmt.Errorf("migration failed, missing private key for %s in keys.toml", string(key))
}
pk, err := ksLegacy.GetPrivate(key)
if err != nil {
return err
}
account, err := ks.ImportECDSA(pk, "")
if err != nil {
return err
}
as.Set(string(key), account.Address.String())
fmt.Printf("Migrated %s key to encrypted key store with empty passphrase.\n", key)
return nil
}