forked from FactomProject/walletapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportkey.go
83 lines (69 loc) · 1.91 KB
/
importkey.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
// 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"
"github.com/FactomProject/ed25519"
fct "github.com/FactomProject/factoid"
)
/************************************************************
* Import
************************************************************/
type ImportKey struct {
}
var _ ICommand = (*ImportKey)(nil)
// ImportKey <name> <private key>
func (ImportKey) Execute(state IState, args []string) error {
if len(args) != 3 {
return fmt.Errorf("Invalid Parameters")
}
name := args[1]
adr := args[2]
if err := ValidName(name); err != nil {
return err
}
a := state.GetFS().GetDB().GetRaw([]byte(fct.W_NAME), []byte(name))
if a != nil {
return fmt.Errorf("That address name is in use. Specify a different name.")
}
fa := fct.ValidateFPrivateUserStr(adr)
ec := fct.ValidateECPrivateUserStr(adr)
b, err := hex.DecodeString(adr)
if err == nil && len(b) != 32 {
err = fmt.Errorf("wrong length")
}
if fa || ec || err == nil {
var privateKey []byte
if err != nil {
privateKey = fct.ConvertUserStrToAddress(adr)
} else {
privateKey = b
}
var fixed [64]byte
copy(fixed[:], privateKey)
publicKey := ed25519.GetPublicKey(&fixed)
adrtype := "ec"
if fa {
adrtype = "fct"
}
_, err := state.GetFS().GetWallet().AddKeyPair(adrtype, []byte(name), publicKey[:], privateKey, false)
if err != nil {
return err
}
return nil
}
return fmt.Errorf("Not a valid Private Key; Check that your key is typed correctly")
}
func (ImportKey) Name() string {
return "importkey"
}
func (ImportKey) ShortHelp() string {
return "ImportKey <name> <private key> -- Create a new address <key> with the given <private key>"
}
func (ImportKey) LongHelp() string {
return `
ImportKey <name> <private key> Create a new address name <addr> with the given <private key>
`
}