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
package plc
import (
"crypto/sha256"
"encoding/base32"
"encoding/base64"
"fmt"
"maps"
"strings"
"github.com/bluesky-social/indigo/atproto/atcrypto"
"github.com/bluesky-social/indigo/atproto/atdata"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
)
type GenesisOptions struct {
RotationKeys []string
VerificationMethods map[string]string
AlsoKnownAs []string
Services map[string]Service
Signer atcrypto.PrivateKey
}
func CreateGenesis(opts GenesisOptions) (string, *Operation, error) {
if opts.Signer == nil {
return "", nil, fmt.Errorf("plc: a genesis operation needs a signer")
}
if len(opts.RotationKeys) == 0 {
return "", nil, fmt.Errorf("plc: a genesis operation needs at least one rotation key")
}
op := &Operation{
Type: TypeOperation,
RotationKeys: append([]string{}, opts.RotationKeys...),
VerificationMethods: copyStringMap(opts.VerificationMethods),
AlsoKnownAs: append([]string{}, opts.AlsoKnownAs...),
Services: copyServiceMap(opts.Services),
}
if err := SignOperation(op, opts.Signer); err != nil {
return "", nil, err
}
did, err := OperationDID(op)
if err != nil {
return "", nil, err
}
return did, op, nil
}
func CreateTombstone(prev string, signer atcrypto.PrivateKey) (*Operation, error) {
if signer == nil {
return nil, fmt.Errorf("plc: a tombstone operation needs a signer")
}
p := prev
op := &Operation{Type: TypeTombstone, Prev: &p}
if err := SignOperation(op, signer); err != nil {
return nil, err
}
return op, nil
}
func SignOperation(op *Operation, signer atcrypto.PrivateKey) error {
if op == nil {
return fmt.Errorf("plc: nil operation")
}
op.raw = nil
unsigned, err := atdata.MarshalCBOR(op.object())
if err != nil {
return fmt.Errorf("plc: encode unsigned operation: %w", err)
}
sig, err := signer.HashAndSign(unsigned)
if err != nil {
return fmt.Errorf("plc: sign operation: %w", err)
}
op.Sig = base64.RawURLEncoding.EncodeToString(sig)
return nil
}
func OperationCID(op *Operation) (string, error) {
if op == nil {
return "", fmt.Errorf("plc: nil operation")
}
b, err := atdata.MarshalCBOR(op.SignedObject())
if err != nil {
return "", fmt.Errorf("plc: encode operation: %w", err)
}
sum := sha256.Sum256(b)
mh, err := multihash.Encode(sum[:], multihash.SHA2_256)
if err != nil {
return "", err
}
return cid.NewCidV1(cid.DagCBOR, mh).String(), nil
}
func OperationDID(op *Operation) (string, error) {
if op == nil {
return "", fmt.Errorf("plc: nil operation")
}
b, err := atdata.MarshalCBOR(op.SignedObject())
if err != nil {
return "", fmt.Errorf("plc: encode operation: %w", err)
}
sum := sha256.Sum256(b)
enc := base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(sum[:15])
return "did:plc:" + strings.ToLower(enc), nil
}
func VerifyOperationSignature(op *Operation, rotationKeys []string) (bool, error) {
if op == nil {
return false, fmt.Errorf("plc: nil operation")
}
sig, err := decodeSignature(op.Sig)
if err != nil {
return false, err
}
unsigned, err := atdata.MarshalCBOR(op.UnsignedObject())
if err != nil {
return false, fmt.Errorf("plc: encode unsigned operation: %w", err)
}
var lastErr error
for _, key := range rotationKeys {
pub, err := didKeyPublic(key)
if err != nil {
lastErr = err
continue
}
if err := pub.HashAndVerify(unsigned, sig); err == nil {
return true, nil
} else {
lastErr = err
}
}
if lastErr == nil {
lastErr = fmt.Errorf("plc: no rotation keys")
}
return false, lastErr
}
func didKeyPublic(key string) (atcrypto.PublicKey, error) {
if !strings.HasPrefix(key, "did:key:") {
return nil, fmt.Errorf("plc: rotation key %q is not a did:key", key)
}
return atcrypto.ParsePublicDIDKey(key)
}
func decodeSignature(sig string) ([]byte, error) {
if sig == "" {
return nil, fmt.Errorf("plc: empty signature")
}
b, err := base64.RawURLEncoding.DecodeString(sig)
if err == nil {
return b, nil
}
b, err = base64.URLEncoding.DecodeString(sig)
if err != nil {
return nil, fmt.Errorf("plc: signature is not base64url: %w", err)
}
return b, nil
}
func copyStringMap(in map[string]string) map[string]string {
out := make(map[string]string, len(in))
maps.Copy(out, in)
return out
}
func copyServiceMap(in map[string]Service) map[string]Service {
out := make(map[string]Service, len(in))
maps.Copy(out, in)
return out
}