-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.go
More file actions
244 lines (192 loc) · 3.34 KB
/
init.go
File metadata and controls
244 lines (192 loc) · 3.34 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/spf13/cobra"
)
var initOperator bool
var initOutput string
var initDomain string
var initServer string
var initCmd = &cobra.Command{
Use: "init",
Short: "Generate identity, keys, and DID document",
Run: func(cmd *cobra.Command, args []string) {
pub, priv, err := GenerateEd25519Keypair()
if err != nil {
fmt.Fprintf(
os.Stderr,
"failed to generate keypair: %v\n",
err,
)
os.Exit(1)
}
publicKeyB64 := EncodePublicKey(pub)
privateKeyB64 := EncodePrivateKey(priv)
if initOperator {
err := initializeOperator(
publicKeyB64,
privateKeyB64,
)
if err != nil {
fmt.Fprintf(
os.Stderr,
"failed to initialize operator: %v\n",
err,
)
os.Exit(1)
}
return
}
err = initializeAgent(
pub,
publicKeyB64,
privateKeyB64,
)
if err != nil {
fmt.Fprintf(
os.Stderr,
"failed to initialize agent: %v\n",
err,
)
os.Exit(1)
}
},
}
func initializeAgent(
publicKey []byte,
publicKeyB64 string,
privateKeyB64 string,
) error {
if initDomain == "" {
return fmt.Errorf(
"--domain is required for agent initialization",
)
}
did := GenerateDID(
initDomain,
publicKey,
)
doc := GenerateDIDDocument(
did,
publicKey,
)
keyPath, err := SavePrivateKeyFile(
"agent.key",
privateKeyB64,
)
if err != nil {
return err
}
cfg := &AgentConfig{
DID: did,
ServerURL: initServer,
PublicKey: publicKeyB64,
KeyConfig: KeyConfig{
Provider: DefaultKeyProvider,
Ref: keyPath,
},
DIDDocument: doc,
}
err = SaveAgentConfig(cfg)
if err != nil {
return err
}
output := map[string]interface{}{
"mode": "agent",
"did": did,
"public_key": publicKeyB64,
"did_document": doc,
"server": initServer,
}
return writeInitOutput(output)
}
func initializeOperator(
publicKeyB64 string,
privateKeyB64 string,
) error {
keyPath, err := SavePrivateKeyFile(
"operator.key",
privateKeyB64,
)
if err != nil {
return err
}
cfg := &OperatorConfig{
Domain: NormalizeDomain(initDomain),
ServerURL: initServer,
RootPublicKey: publicKeyB64,
KeyConfig: KeyConfig{
Provider: DefaultKeyProvider,
Ref: keyPath,
},
}
err = SaveOperatorConfig(cfg)
if err != nil {
return err
}
output := map[string]interface{}{
"mode": "operator",
"domain": cfg.Domain,
"root_public_key": publicKeyB64,
"server": initServer,
}
return writeInitOutput(output)
}
func writeInitOutput(
output interface{},
) error {
data, err := json.MarshalIndent(
output,
"",
" ",
)
if err != nil {
return err
}
if initOutput != "" {
err = os.WriteFile(
initOutput,
data,
0600,
)
if err != nil {
return err
}
fmt.Printf(
"identity written to %s\n",
initOutput,
)
return nil
}
fmt.Println(string(data))
return nil
}
func init() {
initCmd.Flags().BoolVar(
&initOperator,
"operator",
false,
"initialize operator identity",
)
initCmd.Flags().StringVar(
&initDomain,
"domain",
"",
"operator domain",
)
initCmd.Flags().StringVar(
&initServer,
"server",
"http://localhost:8080",
"Auth4Agents server URL",
)
initCmd.Flags().StringVar(
&initOutput,
"output",
"",
"write output to file",
)
rootCmd.AddCommand(initCmd)
}