go scratch code for atproto
1package didplc
2
3import (
4 "encoding/json"
5 "io"
6 "os"
7 "testing"
8
9 "github.com/bluesky-social/indigo/atproto/crypto"
10 "github.com/bluesky-social/indigo/atproto/syntax"
11
12 "github.com/stretchr/testify/assert"
13)
14
15func loadTestLogEntries(t *testing.T, p string) []LogEntry {
16 f, err := os.Open(p)
17 if err != nil {
18 t.Fatal(err)
19 }
20 defer func() { _ = f.Close() }()
21
22 fileBytes, err := io.ReadAll(f)
23 if err != nil {
24 t.Fatal(err)
25 }
26
27 var entries []LogEntry
28 if err := json.Unmarshal(fileBytes, &entries); err != nil {
29 t.Fatal(err)
30 }
31
32 return entries
33}
34
35func TestLogEntryValidate(t *testing.T) {
36 assert := assert.New(t)
37
38 list := []string{
39 "testdata/log_bskyapp.json",
40 "testdata/log_legacy_dholms.json",
41 "testdata/log_bnewbold_robocracy.json",
42 }
43 for _, p := range list {
44 entries := loadTestLogEntries(t, p)
45 for _, le := range entries {
46 assert.NoError(le.Validate())
47 }
48 }
49}
50
51func TestCreatePLC(t *testing.T) {
52 assert := assert.New(t)
53
54 priv, err := crypto.GeneratePrivateKeyP256()
55 if err != nil {
56 t.Fatal(err)
57 }
58 pub, err := priv.PublicKey()
59 if err != nil {
60 t.Fatal(err)
61 }
62 pubDIDKey := pub.DIDKey()
63 handleURI := "at://handle.example.com"
64 endpoint := "https://pds.example.com"
65 op := RegularOp{
66 Type: "plc_operation",
67 RotationKeys: []string{pubDIDKey},
68 VerificationMethods: map[string]string{
69 "atproto": pubDIDKey,
70 },
71 AlsoKnownAs: []string{handleURI},
72 Services: map[string]OpService{
73 "atproto_pds": OpService{
74 Type: "AtprotoPersonalDataServer",
75 Endpoint: endpoint,
76 },
77 },
78 Prev: nil,
79 Sig: nil,
80 }
81 assert.NoError(op.Sign(priv))
82 assert.NoError(op.VerifySignature(pub))
83 did, err := op.DID()
84 if err != nil {
85 t.Fatal(err)
86 }
87 _, err = syntax.ParseDID(did)
88 assert.NoError(err)
89
90 le := LogEntry{
91 DID: did,
92 Operation: OpEnum{Regular: &op},
93 CID: op.CID().String(),
94 Nullified: false,
95 CreatedAt: syntax.DatetimeNow().String(),
96 }
97 assert.NoError(le.Validate())
98
99 _, err = op.Doc(did)
100 assert.NoError(err)
101}