-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
executable file
·182 lines (153 loc) · 3.7 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
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
package main
import (
"encoding/hex"
"fmt"
"os"
"strings"
"github.com/multivactech/Offline-Tools/account"
"github.com/multivactech/Offline-Tools/keystore"
"github.com/multivactech/Offline-Tools/mnemonic"
"github.com/multivactech/Offline-Tools/signature"
"github.com/spf13/cobra"
)
const v = "version 1.0"
func generate(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println("args error")
return
}
mtvAccount, _ := mnemonic.GenerateMnemonicByLength(24)
fileName, _ := keystore.MakeKeyStore([]byte(args[0]), []byte(mtvAccount.PrivateKey))
fmt.Println("generate success!")
fmt.Println("public key:", mtvAccount.PublicKey)
fmt.Println("private key:", mtvAccount.PrivateKey)
fmt.Println("mnemonic:", mtvAccount.Mnemonic)
fmt.Println("keystore file:", fileName)
}
func recoverByMnemonic(cmd *cobra.Command, args []string) {
if len(args) != 24 {
fmt.Println("args error")
return
}
mne := ""
for i := range args {
if i != 0 {
mne += " "
}
mne += args[i]
}
fmt.Println(len(mne), mne)
pub, prv, err := mnemonic.ToAccount(mne)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("public key:", pub)
fmt.Println("private key:", prv)
}
func recoverByKeystore(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println("args error")
return
}
data, err := keystore.ReadJSON(args[0])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
prv, err := keystore.GetPrivatekeyFromKeystore(args[1], data)
if err != nil {
fmt.Println("出现未知错误:", err)
return
}
fmt.Println("private key:", prv)
}
//args[0]: priv key args[1]: txhex
func sign(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println("args error")
return
}
signInfo := unzipBox(args[1])
fmt.Println(signInfo)
signData, _ := hex.DecodeString(signInfo[1])
fmt.Println("a", signData)
sig, err := signature.Sign(args[0], signData)
fmt.Println("b", sig)
if err != nil {
fmt.Println(err)
return
}
pubKey, err := account.PrivatekeyToPublickey(args[0])
if err != nil {
fmt.Println(err)
return
}
signReturn := hex.EncodeToString(sig)
pubKeyReturn := hex.EncodeToString(pubKey)
ans := signInfo[0] + "." + signReturn + "." + pubKeyReturn
fmt.Println("sign success! message is:", ans)
}
func init() {
rootCmd.AddCommand(cmdGenerate)
rootCmd.AddCommand(cmdSign)
cmdCover.AddCommand(cmdCoverByKeystore)
cmdCover.AddCommand(cmdCoverByMnemonic)
rootCmd.AddCommand(cmdCover)
rootCmd.AddCommand(cmdVersion)
}
//execute used to execute cobra command
func execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func main() {
execute()
}
var rootCmd = &cobra.Command{
Use: "./tool []",
Long: `
Welcome to the MultiVAC offline tool,which provides functions for generating accounts, restoring accounts, and signing transactions.
`,
}
var cmdGenerate = &cobra.Command{
Use: "generate [password]",
Short: "generate account",
Run: generate,
}
var cmdCover = &cobra.Command{
Use: "recover [sub]",
Short: "cover account",
}
var cmdSign = &cobra.Command{
Use: "sign [private key] [sign message]",
Short: "sign a message",
Run: sign,
}
var cmdVersion = &cobra.Command{
Use: "version",
Short: "print tool version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(v)
},
}
var cmdCoverByMnemonic = &cobra.Command{
Use: "bymnemonic [mneonic]...",
Short: "recover by mnemonic",
Run: recoverByMnemonic,
}
var cmdCoverByKeystore = &cobra.Command{
Use: "bykeystore [keystore path] [password]",
Short: "recover by keystore",
Run: recoverByKeystore,
}
//unzipBox used to unzip input string
func unzipBox(box string) []string {
ans := strings.Split(box, ".")
if len(ans) != 3 {
return []string{}
}
return ans
}