@@ -26,15 +26,15 @@ func usage() {
26
26
fmt .Println (ver ())
27
27
fmt .Println ()
28
28
fmt .Println ("Usage" )
29
- fmt .Printf (" %s <command> [flags] args...\n " , name )
29
+ fmt .Printf (" %s <command> [flags] args...\n " , name )
30
30
fmt .Println ("" )
31
31
fmt .Printf ("See usage: %s help <command>\n " , name )
32
32
fmt .Println ("" )
33
33
fmt .Println ("Commands:" )
34
34
fmt .Println (" version" )
35
- fmt .Println (" gen [name.wif]" )
36
- fmt .Println (" sign <key> <msg>" )
37
- fmt .Println (" inspect <key | address | signature>" )
35
+ fmt .Println (" gen [--cointype '0xcc'] [ name.wif]" )
36
+ fmt .Println (" sign [--cointype '0x4c'] <key> <msg>" )
37
+ fmt .Println (" inspect [--cointype '0x4c'] <key | address | signature>" )
38
38
fmt .Println (" decode (alias of inspect)" )
39
39
fmt .Println (" verify <payment address> <msg> <signature>" )
40
40
fmt .Println ("" )
@@ -99,26 +99,42 @@ func main() {
99
99
}
100
100
101
101
func gen (args []string ) {
102
- wif := dashmsg . GenerateWIF ()
102
+ var cointype string
103
103
104
- if len (args ) == 1 {
104
+ flags := flag .NewFlagSet ("gen" , flag .ExitOnError )
105
+ flags .StringVar (& cointype , "cointype" , "" , "the magic version (hex) string of the private key" )
106
+ flags .Parse (args )
107
+
108
+ cointype = strings .TrimPrefix (cointype , "0x" )
109
+
110
+ wif := dashmsg .GenerateWIF (cointype )
111
+
112
+ if len (flags .Args ()) == 1 {
105
113
b := []byte (wif )
106
114
b = append (b , '\n' )
107
- ioutil .WriteFile (args [0 ], b , 0644 )
108
- fmt .Printf ("wrote Private Key (as WIF) to %q\n " , args [0 ])
115
+ ioutil .WriteFile (flags . Args () [0 ], b , 0644 )
116
+ fmt .Printf ("wrote Private Key (as WIF) to %q\n " , flags . Args () [0 ])
109
117
return
110
118
}
111
119
112
120
fmt .Println (wif )
113
121
}
114
122
115
123
func inspect (args []string ) {
116
- if len (args ) != 1 {
124
+ var cointype string
125
+
126
+ flags := flag .NewFlagSet ("inspect" , flag .ExitOnError )
127
+ flags .StringVar (& cointype , "cointype" , "" , "the magic version (hex) string of the private key" )
128
+ flags .Parse (args )
129
+
130
+ cointype = strings .TrimPrefix (cointype , "0x" )
131
+
132
+ if len (flags .Args ()) != 1 {
117
133
fmt .Fprintf (os .Stderr , "usage: %s inspect <addr-or-key>\n " , os .Args [0 ])
118
134
os .Exit (1 )
119
135
return
120
136
}
121
- input := args [0 ]
137
+ input := flags . Args () [0 ]
122
138
123
139
var usererr error
124
140
inputlen := len (input )
@@ -144,16 +160,19 @@ func inspect(args []string) {
144
160
break
145
161
}
146
162
147
- priv , err := dashmsg .WIFToPrivateKey (wif )
163
+ privCointype , priv , err := dashmsg .WIFToPrivateKey (wif )
148
164
if nil != err {
149
165
usererr = err
150
166
break
151
167
}
168
+ if 0 == len (cointype ) {
169
+ cointype = privCointype
170
+ }
152
171
153
172
pubBytes := dashmsg .MarshalPublicKey (priv .PublicKey )
154
- pkh := dashmsg .PublicKeyToAddress (priv .PublicKey )
173
+ pkh := dashmsg .PublicKeyToAddress (cointype , priv .PublicKey )
155
174
156
- fmt .Printf ("PrivateKey (hex): %s (coin type)\n " , hexstr [: 2 ] )
175
+ fmt .Printf ("PrivateKey (hex): %s (coin type)\n " , privCointype )
157
176
fmt .Printf (" : %s\n " , hexstr [2 :66 ])
158
177
fmt .Printf (" : %s (compressed)\n " , hexstr [66 :])
159
178
fmt .Println ()
@@ -184,9 +203,14 @@ func inspect(args []string) {
184
203
}
185
204
186
205
func sign (args []string ) {
206
+ var cointype string
207
+
187
208
flags := flag .NewFlagSet ("sign" , flag .ExitOnError )
209
+ flags .StringVar (& cointype , "cointype" , "" , "the magic version (hex) string of the private key" )
188
210
flags .Parse (args )
189
211
212
+ cointype = strings .TrimPrefix (cointype , "0x" )
213
+
190
214
if len (flags .Args ()) <= 1 {
191
215
fmt .Fprintf (os .Stderr , "usage: %s sign <addr-or-key> <msg>\n " , os .Args [0 ])
192
216
os .Exit (1 )
@@ -195,7 +219,7 @@ func sign(args []string) {
195
219
wifname := flags .Args ()[0 ]
196
220
payload := flags .Args ()[1 ]
197
221
198
- priv , err := readWif (wifname )
222
+ _ , priv , err := readWif (wifname )
199
223
if nil != err {
200
224
fmt .Fprintf (os .Stderr , "error: could not decode private key: %v\n " , err )
201
225
os .Exit (1 )
@@ -252,7 +276,15 @@ func verify(args []string) {
252
276
return
253
277
}
254
278
255
- if dashmsg .PublicKeyToAddress (* pub ) == addr {
279
+ cointype , err := dashmsg .AddressToCointype (addr )
280
+ if nil != err {
281
+ // Neither a valid file nor string. Blast!
282
+ fmt .Printf ("can't detect coin type of %q: %v\n " , addr , err )
283
+ os .Exit (1 )
284
+ return
285
+ }
286
+
287
+ if dashmsg .PublicKeyToAddress (cointype , * pub ) == addr {
256
288
fmt .Println ("Verified: true" )
257
289
return
258
290
}
@@ -270,18 +302,18 @@ func readFileOrString(str string) []byte {
270
302
return b
271
303
}
272
304
273
- func readWif (wifname string ) (* ecdsa.PrivateKey , error ) {
305
+ func readWif (wifname string ) (string , * ecdsa.PrivateKey , error ) {
274
306
// Read as file
275
307
wif := readFileOrString (wifname )
276
308
277
- priv , err := dashmsg .WIFToPrivateKey (string (wif ))
309
+ cointype , priv , err := dashmsg .WIFToPrivateKey (string (wif ))
278
310
if nil != err {
279
311
// Neither a valid file nor string. Blast!
280
- return nil , fmt .Errorf (
312
+ return "" , nil , fmt .Errorf (
281
313
"could not read private key as file (or parse as string) %q:\n %s" ,
282
314
wifname , err ,
283
315
)
284
316
}
285
317
286
- return priv , nil
318
+ return cointype , priv , nil
287
319
}
0 commit comments