-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanagement_key.go
81 lines (61 loc) · 1.88 KB
/
management_key.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
package factomdid
import (
"encoding/pem"
"strings"
"github.com/FactomProject/btcutil/base58"
)
// ManagementKey is a key used to sign updates for an existing DID
type ManagementKey struct {
AbstractKey
Priority int `json:"priority" form:"priority" query:"priority" validate:"min=0"`
}
// NewManagementKey generates new ManagementKey with alias and keyType
func NewManagementKey(alias string, keyType string, priority int) (*ManagementKey, error) {
key := &ManagementKey{}
key.Alias = alias
key.KeyType = keyType
key.Priority = priority
// validate Alias and KeyType
err := validate.StructPartial(key.AbstractKey, "Alias", "KeyType")
if err != nil {
return nil, err
}
// validate Priority
err = validate.StructPartial(key, "Priority")
if err != nil {
return nil, err
}
key.generateRandomKeys()
return key, nil
}
// helper function to convert ManagementKey into ManagementKeySchema
func (mgmtkey *ManagementKey) toSchema(DID string) (*ManagementKeySchema, error) {
// validate ManagementKey
err := validate.Struct(mgmtkey)
if err != nil {
return nil, err
}
s := &ManagementKeySchema{}
s.Controller = mgmtkey.Controller
s.ID = strings.Join([]string{DID, mgmtkey.Alias}, "#")
s.Priority = mgmtkey.Priority
s.PriorityRequirement = mgmtkey.PriorityRequirement
s.Type = mgmtkey.KeyType
if mgmtkey.KeyType == KeyTypeRSA {
s.PublicKeyPem = string(pem.EncodeToMemory(&pem.Block{Type: "RSA PUBLIC KEY", Bytes: mgmtkey.PublicKey}))
} else {
s.PublicKeyBase58 = base58.Encode(mgmtkey.PublicKey)
}
return s, nil
}
// helper function to convert ManagementKey into RevokeIDSchema
func (mgmtkey *ManagementKey) toRevokeIDSchema(DID string) (*RevokeIDSchema, error) {
// validate Management Key
err := validate.StructPartial(mgmtkey, "Alias")
if err != nil {
return nil, err
}
s := &RevokeIDSchema{}
s.ID = strings.Join([]string{DID, mgmtkey.Alias}, "#")
return s, nil
}