/
identity.go
182 lines (150 loc) · 4 KB
/
identity.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 mc
import (
b58 "github.com/jbenet/go-base58"
p2p_crypto "github.com/libp2p/go-libp2p-crypto"
p2p_peer "github.com/libp2p/go-libp2p-peer"
"io/ioutil"
"log"
"os"
"path"
)
// Node identities: PeerIdentity and PublisherIdentity
// the structs are different because the semantics of id differ
// PeerIdentities use the raw public key hash as dictated by libp2p
// they use RSA keys for interop with js, which is lagging in libp2p-crypto
// PublisherIdentities use the base58 encoded public key as identifier
// they use ECC (Ed25519) keys and sign statements with them
type PeerIdentity struct {
ID p2p_peer.ID
PrivKey p2p_crypto.PrivKey
}
type PublisherIdentity struct {
ID58 string
PrivKey p2p_crypto.PrivKey
}
func (id PeerIdentity) Pretty() string {
return id.ID.Pretty()
}
func (id PublisherIdentity) Pretty() string {
return id.ID58
}
// Peer Identities
func MakePeerIdentity(home string) (empty PeerIdentity, err error) {
kpath := path.Join(home, "identity.node")
_, err = os.Stat(kpath)
if os.IsNotExist(err) {
return generatePeerIdentity(kpath)
}
if err != nil {
return
}
return loadPeerIdentity(kpath)
}
func generatePeerIdentity(kpath string) (empty PeerIdentity, err error) {
log.Printf("Generating new node identity")
// RSA keys for interop with js
privk, pubk, err := generateRSAKeyPair()
if err != nil {
return
}
id, err := p2p_peer.IDFromPublicKey(pubk)
if err != nil {
return
}
log.Printf("Saving key to %s", kpath)
err = saveKey(privk, kpath)
if err != nil {
return
}
log.Printf("Peer ID: %s", id.Pretty())
return PeerIdentity{ID: id, PrivKey: privk}, nil
}
func loadPeerIdentity(kpath string) (empty PeerIdentity, err error) {
log.Printf("Loading node identity from %s", kpath)
privk, err := loadKey(kpath)
if err != nil {
return
}
id, err := p2p_peer.IDFromPrivateKey(privk)
if err != nil {
return
}
log.Printf("Peer ID: %s", id.Pretty())
return PeerIdentity{id, privk}, nil
}
// Publisher Identities
func MakePublisherIdentity(home string) (empty PublisherIdentity, err error) {
kpath := path.Join(home, "identity.publisher") // .pub would be unfortunate
_, err = os.Stat(kpath)
if os.IsNotExist(err) {
return generatePublisherIdentity(kpath)
}
if err != nil {
return
}
return loadPublisherIdentity(kpath)
}
func generatePublisherIdentity(kpath string) (empty PublisherIdentity, err error) {
log.Printf("Generating new publisher identity")
privk, pubk, err := generateECCKeyPair()
if err != nil {
return
}
id58, err := PublisherID58(pubk)
if err != nil {
return
}
log.Printf("Saving key to %s", kpath)
err = saveKey(privk, kpath)
if err != nil {
return
}
log.Printf("Publisher ID: %s", id58)
return PublisherIdentity{id58, privk}, nil
}
func loadPublisherIdentity(kpath string) (empty PublisherIdentity, err error) {
log.Printf("Loading publisher identity from %s", kpath)
privk, err := loadKey(kpath)
if err != nil {
return
}
id58, err := PublisherID58(privk.GetPublic())
if err != nil {
return
}
log.Printf("Publisher ID: %s", id58)
return PublisherIdentity{id58, privk}, nil
}
func PublisherID58(pubk p2p_crypto.PubKey) (string, error) {
bytes, err := pubk.Bytes()
if err != nil {
return "", err
}
id58 := b58.Encode(bytes)
return id58, nil
}
func PublisherKey(id58 string) (p2p_crypto.PubKey, error) {
bytes := b58.Decode(id58)
return p2p_crypto.UnmarshalPublicKey(bytes)
}
// Key management
func loadKey(kpath string) (p2p_crypto.PrivKey, error) {
bytes, err := ioutil.ReadFile(kpath)
if err != nil {
return nil, err
}
return p2p_crypto.UnmarshalPrivateKey(bytes)
}
func saveKey(privk p2p_crypto.PrivKey, kpath string) error {
bytes, err := privk.Bytes()
if err != nil {
return err
}
return ioutil.WriteFile(kpath, bytes, 0600)
}
func generateRSAKeyPair() (p2p_crypto.PrivKey, p2p_crypto.PubKey, error) {
return p2p_crypto.GenerateKeyPair(p2p_crypto.RSA, 2048)
}
func generateECCKeyPair() (p2p_crypto.PrivKey, p2p_crypto.PubKey, error) {
return p2p_crypto.GenerateKeyPair(p2p_crypto.Ed25519, 0)
}