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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package plcsubmit
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"maps"
"net/http"
"net/url"
"slices"
"strings"
"time"
"github.com/bluesky-social/indigo/atproto/atcrypto"
"github.com/bluesky-social/indigo/atproto/syntax"
atpds "github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/pds"
"github.com/johnandersen777/socialweb-computer-kcp/internal/atproto/plc"
)
const plcMaxResponseBytes = 1 << 20
var _ atpds.PLCSubmitter = (*HTTPPLCSubmitter)(nil)
type HTTPPLCSubmitter struct {
baseURL string
rotationKey atcrypto.PrivateKey
rotationDID string
client *http.Client
}
func New(baseURL string, rotationKey atcrypto.PrivateKey, client *http.Client) (*HTTPPLCSubmitter, error) {
baseURL = strings.TrimRight(strings.TrimSpace(baseURL), "/")
parsed, err := url.Parse(baseURL)
if err != nil || parsed.Host == "" || (parsed.Scheme != "http" && parsed.Scheme != "https") {
return nil, fmt.Errorf("pds: invalid PLC directory URL %q", baseURL)
}
if parsed.RawQuery != "" || parsed.Fragment != "" {
return nil, fmt.Errorf("pds: the PLC directory URL must not have a query or fragment")
}
if rotationKey == nil {
return nil, errors.New("pds: the PLC submitter needs a rotation key")
}
pub, err := rotationKey.PublicKey()
if err != nil {
return nil, fmt.Errorf("pds: deriving the PLC rotation key: %w", err)
}
if client == nil {
client = &http.Client{Timeout: 15 * time.Second}
}
return &HTTPPLCSubmitter{
baseURL: baseURL,
rotationKey: rotationKey,
rotationDID: pub.DIDKey(),
client: client,
}, nil
}
func (p *HTTPPLCSubmitter) RotationDID() string {
return p.rotationDID
}
func (p *HTTPPLCSubmitter) SubmitGenesis(ctx context.Context, req atpds.PLCGenesisRequest) (atpds.PLCGenesisResult, error) {
if req.SigningKey == "" {
return atpds.PLCGenesisResult{}, errors.New("pds: PLC genesis needs the account signing key")
}
if req.Handle == "" || req.Service == "" {
return atpds.PLCGenesisResult{}, errors.New("pds: PLC genesis needs a handle and a service endpoint")
}
if req.RotationKey != "" && req.RotationKey != p.rotationDID {
return atpds.PLCGenesisResult{}, fmt.Errorf("pds: PLC genesis rotation key does not match the submitter key")
}
verification := req.VerificationMethods
if len(verification) == 0 {
verification = map[string]string{"atproto": req.SigningKey}
}
didRaw, op, err := plc.CreateGenesis(plc.GenesisOptions{
RotationKeys: []string{p.rotationDID},
VerificationMethods: verification,
AlsoKnownAs: append([]string(nil), req.AlsoKnownAs...),
Services: map[string]plc.Service{
"atproto_pds": {Type: "AtprotoPersonalDataServer", Endpoint: req.Service},
},
Signer: p.rotationKey,
})
if err != nil {
return atpds.PLCGenesisResult{}, fmt.Errorf("pds: creating the PLC genesis operation: %w", err)
}
did, err := syntax.ParseDID(didRaw)
if err != nil {
return atpds.PLCGenesisResult{}, fmt.Errorf("pds: the PLC genesis DID is invalid: %w", err)
}
if err := p.submit(ctx, did, op.SignedObject()); err != nil {
return atpds.PLCGenesisResult{}, err
}
raw, err := json.Marshal(op.SignedObject())
if err != nil {
return atpds.PLCGenesisResult{}, fmt.Errorf("pds: encoding the PLC genesis operation: %w", err)
}
return atpds.PLCGenesisResult{DID: did, RotationKey: p.rotationDID, Operation: raw}, nil
}
func (p *HTTPPLCSubmitter) UpdateHandle(ctx context.Context, did syntax.DID, handle string) error {
var ops []plc.Operation
if err := p.getJSON(ctx, p.baseURL+"/"+did.String()+"/log", &ops); err != nil {
return err
}
if len(ops) == 0 {
return fmt.Errorf("pds: the PLC log for %s is empty", did)
}
last := ops[len(ops)-1]
if last.Type != plc.TypeOperation {
return fmt.Errorf("pds: the PLC operation for %s is not updateable", did)
}
prevCID, err := plc.OperationCID(&last)
if err != nil {
return fmt.Errorf("pds: computing the PLC operation cid: %w", err)
}
prev := prevCID
rotationKeys := append([]string(nil), last.RotationKeys...)
verification := make(map[string]string, len(last.VerificationMethods))
maps.Copy(verification, last.VerificationMethods)
services := make(map[string]plc.Service, len(last.Services))
maps.Copy(services, last.Services)
op := &plc.Operation{
Type: plc.TypeOperation,
Prev: &prev,
RotationKeys: rotationKeys,
VerificationMethods: verification,
AlsoKnownAs: []string{"at://" + handle},
Services: services,
}
if err := plc.SignOperation(op, p.rotationKey); err != nil {
return fmt.Errorf("pds: signing the PLC operation: %w", err)
}
return p.submit(ctx, did, op.SignedObject())
}
func (p *HTTPPLCSubmitter) UpdateService(ctx context.Context, did syntax.DID, service string) error {
endpoint, err := plc.NormalizeServiceEndpoint(service)
if err != nil {
return fmt.Errorf("pds: the PLC service endpoint is invalid: %w", err)
}
var doc plc.DIDDocument
if err := p.getJSON(ctx, p.baseURL+"/"+did.String(), &doc); err == nil {
for _, service := range doc.Service {
if service.ID == "#atproto_pds" && service.ServiceEndpoint == endpoint {
return nil
}
}
}
var ops []plc.Operation
if err := p.getJSON(ctx, p.baseURL+"/"+did.String()+"/log", &ops); err != nil {
return err
}
if len(ops) == 0 {
return fmt.Errorf("pds: the PLC log for %s is empty", did)
}
last := ops[len(ops)-1]
if last.Type != plc.TypeOperation {
return fmt.Errorf("pds: the PLC operation for %s is not updateable", did)
}
authorized := slices.Contains(last.RotationKeys, p.rotationDID)
if !authorized {
return fmt.Errorf("pds: the rotation key %s is not in the PLC log for %s", p.rotationDID, did)
}
prevCID, err := plc.OperationCID(&last)
if err != nil {
return fmt.Errorf("pds: computing the PLC operation cid: %w", err)
}
op, err := plc.CreateServiceUpdate(&last, prevCID, endpoint)
if err != nil {
return fmt.Errorf("pds: creating the PLC service update: %w", err)
}
if err := plc.SignOperation(op, p.rotationKey); err != nil {
return fmt.Errorf("pds: signing the PLC operation: %w", err)
}
opCID, err := plc.OperationCID(op)
if err != nil {
return fmt.Errorf("pds: computing the PLC service update cid: %w", err)
}
if err := p.submit(ctx, did, op.SignedObject()); err != nil {
return err
}
return p.verifyServiceUpdate(ctx, did, &last, op, opCID, endpoint)
}
func (p *HTTPPLCSubmitter) verifyServiceUpdate(ctx context.Context, did syntax.DID, prev *plc.Operation, expected *plc.Operation, expectedCID string, endpoint string) error {
var ops []plc.Operation
if err := p.getJSON(ctx, p.baseURL+"/"+did.String()+"/log", &ops); err != nil {
return fmt.Errorf("pds: verifying the PLC service update: %w", err)
}
if len(ops) == 0 {
return fmt.Errorf("pds: verifying the PLC service update: the PLC log for %s is empty", did)
}
last := &ops[len(ops)-1]
lastCID, err := plc.OperationCID(last)
if err != nil {
return fmt.Errorf("pds: verifying the PLC service update: %w", err)
}
if lastCID != expectedCID {
return fmt.Errorf("pds: the PLC service update for %s is not the head operation", did)
}
if err := plc.VerifyServiceOperation(last, prev, endpoint); err != nil {
return fmt.Errorf("pds: verifying the PLC service update: %w", err)
}
if expected.Sig != last.Sig {
return fmt.Errorf("pds: the PLC service update for %s has a different signature", did)
}
var doc plc.DIDDocument
if err := p.getJSON(ctx, p.baseURL+"/"+did.String(), &doc); err != nil {
return fmt.Errorf("pds: verifying the DID document: %w", err)
}
if err := plc.VerifyServiceDocument(&doc, did.String(), endpoint); err != nil {
return fmt.Errorf("pds: verifying the DID document: %w", err)
}
return nil
}
func (p *HTTPPLCSubmitter) submit(ctx context.Context, did syntax.DID, operation map[string]any) error {
body, err := json.Marshal(operation)
if err != nil {
return fmt.Errorf("pds: encoding the PLC operation: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, p.baseURL+"/"+did.String(), bytes.NewReader(body))
if err != nil {
return fmt.Errorf("pds: building the PLC submit request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
resp, err := p.client.Do(req)
if err != nil {
return fmt.Errorf("pds: submitting the PLC operation: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
raw, _ := io.ReadAll(io.LimitReader(resp.Body, plcMaxResponseBytes))
return fmt.Errorf("pds: PLC submit returned HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(raw)))
}
return nil
}
func (p *HTTPPLCSubmitter) getJSON(ctx context.Context, endpoint string, dst any) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return fmt.Errorf("pds: building the PLC request: %w", err)
}
req.Header.Set("Accept", "application/json")
resp, err := p.client.Do(req)
if err != nil {
return fmt.Errorf("pds: reading the PLC log: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
raw, _ := io.ReadAll(io.LimitReader(resp.Body, plcMaxResponseBytes))
return fmt.Errorf("pds: PLC log returned HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(raw)))
}
raw, err := io.ReadAll(io.LimitReader(resp.Body, plcMaxResponseBytes))
if err != nil {
return fmt.Errorf("pds: reading the PLC log: %w", err)
}
if err := json.Unmarshal(raw, dst); err != nil {
return fmt.Errorf("pds: decoding the PLC log: %w", err)
}
return nil
}