/
exportkey.go
75 lines (62 loc) · 1.92 KB
/
exportkey.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
// Copyright 2015 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"encoding/hex"
"fmt"
fct "github.com/FactomProject/factoid"
"github.com/FactomProject/factoid/wallet"
)
/************************************************************
* ExportKey
************************************************************/
type ExportKey struct {
}
var _ ICommand = (*ExportKey)(nil)
// ExportKey <name>
func (ExportKey) Execute(state IState, args []string) error {
if len(args) != 2 {
return fmt.Errorf("Invalid Parameters")
}
name := args[1]
weblk := state.GetFS().GetDB().GetRaw([]byte(fct.W_NAME), []byte(name))
if weblk == nil {
return fmt.Errorf("Unknown address. Check that you spelled the name correctly")
}
we := weblk.(wallet.IWalletEntry)
public, err := we.GetAddress()
if err != nil {
return err
}
private := we.GetPrivKey(0)
adrtype := we.GetType()
binPublic := hex.EncodeToString(public.Bytes())
binPrivate := hex.EncodeToString(private[:32])
var usrPublic, usrPrivate string
if adrtype == "fct" {
usrPublic = fct.ConvertFctAddressToUserStr(fct.NewAddress(public.Bytes()))
usrPrivate = fct.ConvertFctPrivateToUserStr(fct.NewAddress(private[:32]))
} else {
usrPublic = fct.ConvertECAddressToUserStr(fct.NewAddress(public.Bytes()))
usrPrivate = fct.ConvertECPrivateToUserStr(fct.NewAddress(private[:32]))
}
fmt.Println("Private Key:")
fmt.Println(" ", usrPrivate)
fmt.Println(" ", binPrivate)
fmt.Println("Public Key:")
fmt.Println(" ", usrPublic)
fmt.Println(" ", binPublic)
return nil
}
func (ExportKey) Name() string {
return "exportkey"
}
func (ExportKey) ShortHelp() string {
return "ExportKey <name> -- Prints the private and public keys tied to this <name>"
}
func (ExportKey) LongHelp() string {
return `
ExportKey <name> Prints the public and private keys tied to this <name>.
`
}