/
wallet_import.go
202 lines (169 loc) · 6.07 KB
/
wallet_import.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package cmd
import (
"context"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/elysiumstation/fury/cmd/furywallet/commands/cli"
"github.com/elysiumstation/fury/cmd/furywallet/commands/flags"
"github.com/elysiumstation/fury/cmd/furywallet/commands/printer"
vgfs "github.com/elysiumstation/fury/libs/fs"
vgzap "github.com/elysiumstation/fury/libs/zap"
"github.com/elysiumstation/fury/wallet/api"
"github.com/elysiumstation/fury/wallet/wallet"
"github.com/elysiumstation/fury/wallet/wallets"
"github.com/spf13/cobra"
)
var (
importWalletLong = cli.LongDesc(`
Import a wallet using the recovery phrase and generate the first Ed25519 key pair.
You will be asked to create a passphrase. The passphrase is used to protect
the file in which the keys are stored. Hence, it can be different from the
original passphrase, used during the wallet creation. This doesn't affect the
key generation process in any way.
`)
importWalletExample = cli.Examples(`
# Import a wallet using the recovery phrase
{{.Software}} import --wallet WALLET --recovery-phrase-file PATH_TO_RECOVERY_PHRASE
# Import an older version of the wallet using the recovery phrase
{{.Software}} import --wallet WALLET --recovery-phrase-file PATH_TO_RECOVERY_PHRASE --version VERSION
`)
)
type ImportWalletHandler func(api.AdminImportWalletParams) (importWalletResult, error)
type importedWallet struct {
Name string `json:"name"`
KeyDerivationVersion uint32 `json:"keyDerivationVersion"`
FilePath string `json:"filePath"`
}
type importWalletResult struct {
Wallet importedWallet `json:"wallet"`
Key firstPublicKey `json:"key"`
}
func NewCmdImportWallet(w io.Writer, rf *RootFlags) *cobra.Command {
h := func(params api.AdminImportWalletParams) (importWalletResult, error) {
walletStore, err := wallets.InitialiseStore(rf.Home, false)
if err != nil {
return importWalletResult{}, fmt.Errorf("couldn't initialise wallets store: %w", err)
}
defer walletStore.Close()
importWallet := api.NewAdminImportWallet(walletStore)
rawResult, errDetails := importWallet.Handle(context.Background(), params)
if errDetails != nil {
return importWalletResult{}, errors.New(errDetails.Data)
}
result := rawResult.(api.AdminImportWalletResult)
return importWalletResult{
Wallet: importedWallet{
Name: result.Wallet.Name,
KeyDerivationVersion: result.Wallet.KeyDerivationVersion,
FilePath: walletStore.GetWalletPath(result.Wallet.Name),
},
Key: firstPublicKey{
PublicKey: result.Key.PublicKey,
Algorithm: result.Key.Algorithm,
Meta: result.Key.Metadata,
},
}, nil
}
return BuildCmdImportWallet(w, h, rf)
}
func BuildCmdImportWallet(w io.Writer, handler ImportWalletHandler, rf *RootFlags) *cobra.Command {
f := &ImportWalletFlags{}
cmd := &cobra.Command{
Use: "import",
Short: "Import a wallet using the recovery phrase",
Long: importWalletLong,
Example: importWalletExample,
RunE: func(_ *cobra.Command, _ []string) error {
req, err := f.Validate()
if err != nil {
return err
}
resp, err := handler(req)
if err != nil {
return err
}
switch rf.Output {
case flags.InteractiveOutput:
PrintImportWalletResponse(w, resp)
case flags.JSONOutput:
return printer.FprintJSON(w, resp)
}
return nil
},
}
cmd.Flags().StringVarP(&f.Wallet,
"wallet", "w",
"",
"Name of the wallet to use",
)
cmd.Flags().StringVarP(&f.PassphraseFile,
"passphrase-file", "p",
"",
"Path to the file containing the passphrase to access the wallet",
)
cmd.Flags().StringVar(&f.RecoveryPhraseFile,
"recovery-phrase-file",
"",
"Path to the file containing the recovery phrase of the wallet",
)
cmd.Flags().Uint32Var(&f.Version,
"version",
wallet.LatestVersion,
fmt.Sprintf("Version of the wallet to import: %v", wallet.SupportedKeyDerivationVersions),
)
_ = cmd.RegisterFlagCompletionFunc("version", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
vs := make([]string, 0, len(wallet.SupportedKeyDerivationVersions))
for i, v := range wallet.SupportedKeyDerivationVersions {
vs[i] = strconv.FormatUint(uint64(v), 10) //nolint:gomnd
}
return vgzap.SupportedLogLevels, cobra.ShellCompDirectiveDefault
})
return cmd
}
type ImportWalletFlags struct {
Wallet string
PassphraseFile string
RecoveryPhraseFile string
Version uint32
}
func (f *ImportWalletFlags) Validate() (api.AdminImportWalletParams, error) {
params := api.AdminImportWalletParams{
KeyDerivationVersion: f.Version,
}
if len(f.Wallet) == 0 {
return api.AdminImportWalletParams{}, flags.MustBeSpecifiedError("wallet")
}
params.Wallet = f.Wallet
if len(f.RecoveryPhraseFile) == 0 {
return api.AdminImportWalletParams{}, flags.MustBeSpecifiedError("recovery-phrase-file")
}
recoveryPhrase, err := vgfs.ReadFile(f.RecoveryPhraseFile)
if err != nil {
return api.AdminImportWalletParams{}, fmt.Errorf("couldn't read recovery phrase file: %w", err)
}
params.RecoveryPhrase = strings.Trim(string(recoveryPhrase), "\n")
passphrase, err := flags.GetConfirmedPassphrase(f.PassphraseFile)
if err != nil {
return api.AdminImportWalletParams{}, err
}
params.Passphrase = passphrase
return params, nil
}
func PrintImportWalletResponse(w io.Writer, resp importWalletResult) {
p := printer.NewInteractivePrinter(w)
str := p.String()
defer p.Print(str)
str.CheckMark().Text("Wallet ").Bold(resp.Wallet.Name).Text(" has been imported at: ").SuccessText(resp.Wallet.FilePath).NextLine()
str.CheckMark().Text("First key pair has been generated for the wallet ").Bold(resp.Wallet.Name).Text(" at: ").SuccessText(resp.Wallet.FilePath).NextLine()
str.CheckMark().SuccessText("Importing the wallet succeeded").NextSection()
str.Text("First public key:").NextLine()
str.WarningText(resp.Key.PublicKey).NextLine()
str.NextSection()
str.BlueArrow().InfoText("Run the service").NextLine()
str.Text("Now, you can run the service. See the following command:").NextSection()
str.Code(fmt.Sprintf("%s service run --help", os.Args[0])).NextLine()
}