-
Notifications
You must be signed in to change notification settings - Fork 7
/
import_wallet.go
84 lines (80 loc) · 2.77 KB
/
import_wallet.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
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package sigsvr
import (
"fmt"
"github.com/ontio/layer2/node/account"
"github.com/ontio/layer2/node/cmd"
"github.com/ontio/layer2/node/cmd/sigsvr/store"
"github.com/ontio/layer2/node/cmd/utils"
"github.com/ontio/layer2/node/common"
"github.com/urfave/cli"
)
var ImportWalletCommand = cli.Command{
Name: "import",
Usage: "Import accounts from a wallet file",
ArgsUsage: "",
Action: importWallet,
Flags: []cli.Flag{
utils.CliWalletDirFlag,
utils.WalletFileFlag,
},
Description: "",
}
func importWallet(ctx *cli.Context) error {
walletDirPath := ctx.String(utils.GetFlagName(utils.CliWalletDirFlag))
walletFilePath := ctx.String(utils.GetFlagName(utils.WalletFileFlag))
if walletDirPath == "" || walletFilePath == "" {
cmd.PrintErrorMsg("Missing %s or %s flag.", utils.CliWalletDirFlag.Name, utils.WalletFileFlag.Name)
cli.ShowSubcommandHelp(ctx)
return nil
}
if !common.FileExisted(walletFilePath) {
return fmt.Errorf("wallet file:%s does not exist", walletFilePath)
}
walletStore, err := store.NewWalletStore(walletDirPath)
if err != nil {
return fmt.Errorf("NewWalletStore dir path:%s error:%s", walletDirPath, err)
}
wallet, err := account.Open(walletFilePath)
if err != nil {
return fmt.Errorf("open wallet:%s error:%s", walletFilePath, err)
}
walletData := wallet.GetWalletData()
if *walletStore.WalletScrypt != *walletData.Scrypt {
return fmt.Errorf("import account failed, wallet scrypt:%+v != %+v", walletData.Scrypt, walletStore.WalletScrypt)
}
addNum := 0
updateNum := 0
for i := 0; i < len(walletData.Accounts); i++ {
ok, err := walletStore.AddAccountData(walletData.Accounts[i])
if err != nil {
return fmt.Errorf("import account address:%s error:%s", walletData.Accounts[i].Address, err)
}
if ok {
addNum++
} else {
updateNum++
}
}
cmd.PrintInfoMsg("Import account success.")
cmd.PrintInfoMsg("Total account number:%d", len(walletData.Accounts))
cmd.PrintInfoMsg("Add account number:%d", addNum)
cmd.PrintInfoMsg("Update account number:%d", updateNum)
return nil
}