-
Notifications
You must be signed in to change notification settings - Fork 88
/
plc.go
144 lines (114 loc) · 3.04 KB
/
plc.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
package api
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base32"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
did "github.com/whyrusleeping/go-did"
otel "go.opentelemetry.io/otel"
)
type PLCServer struct {
Host string
C *http.Client
}
func (s *PLCServer) GetDocument(ctx context.Context, didstr string) (*did.Document, error) {
ctx, span := otel.Tracer("gosky").Start(ctx, "plsResolveDid")
defer span.End()
if s.C == nil {
s.C = http.DefaultClient
}
req, err := http.NewRequest("GET", s.Host+"/"+didstr, nil)
if err != nil {
return nil, err
}
resp, err := s.C.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("get did request failed (code %d): %s", resp.StatusCode, resp.Status)
}
var doc did.Document
if err := json.NewDecoder(resp.Body).Decode(&doc); err != nil {
return nil, err
}
return &doc, nil
}
func (s *PLCServer) FlushCacheFor(did string) {
return
}
type CreateOp struct {
Type string `json:"type" cborgen:"type"`
SigningKey string `json:"signingKey" cborgen:"signingKey"`
RecoveryKey string `json:"recoveryKey" cborgen:"recoveryKey"`
Handle string `json:"handle" cborgen:"handle"`
Service string `json:"service" cborgen:"service"`
Prev *string `json:"prev" cborgen:"prev"`
Sig string `json:"sig" cborgen:"sig,omitempty"`
}
func (s *PLCServer) CreateDID(ctx context.Context, sigkey *did.PrivKey, recovery string, handle string, service string) (string, error) {
if s.C == nil {
s.C = http.DefaultClient
}
op := CreateOp{
Type: "create",
SigningKey: sigkey.Public().DID(),
RecoveryKey: recovery,
Handle: handle,
Service: service,
}
buf := new(bytes.Buffer)
if err := op.MarshalCBOR(buf); err != nil {
return "", err
}
sig, err := sigkey.Sign(buf.Bytes())
if err != nil {
return "", err
}
op.Sig = base64.RawURLEncoding.EncodeToString(sig)
opdid, err := didForCreateOp(&op)
if err != nil {
return "", err
}
body, err := json.Marshal(op)
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", s.Host+"/"+url.QueryEscape(opdid), bytes.NewReader(body))
if err != nil {
return "", err
}
req.Header.Set("Content-Type", "application/json")
resp, err := s.C.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
b, _ := io.ReadAll(resp.Body)
fmt.Println(string(b))
return "", fmt.Errorf("bad response from create call: %d %s", resp.StatusCode, resp.Status)
}
return opdid, nil
}
func (s *PLCServer) UpdateUserHandle(ctx context.Context, did string, handle string) error {
return fmt.Errorf("handle updates not yet implemented")
}
func didForCreateOp(op *CreateOp) (string, error) {
buf := new(bytes.Buffer)
if err := op.MarshalCBOR(buf); err != nil {
return "", err
}
h := sha256.Sum256(buf.Bytes())
enchash := base32.StdEncoding.EncodeToString(h[:])
enchash = strings.ToLower(enchash)
return "did:plc:" + enchash[:24], nil
}