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
package market
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"regexp"
"strings"
"time"
"github.com/bluesky-social/indigo/atproto/syntax"
)
const ContractAssociationLifetime = 24 * time.Hour
const (
OnNetworkKindIroh = "iroh"
OnNetworkKindHTTPS = "https"
OnNetworkKindWebRTC = "webrtc"
OnNetworkKindSandbox = "sandbox"
GuestPublicKeyKindAgeX25519 = "age-x25519"
GuestSignalKindOffer = "offer"
GuestSignalKindAnswer = "answer"
GuestSubscribeEventsErrFutureCursor = "FutureCursor"
GuestSecretsTag = "secrets"
MaxGuestSecretsCiphertext = 65536
GuestSecretsViaGetSecrets = "getSecrets"
GuestSecretsViaSealed = "sealed"
)
func OnNetworkKind(r ComputeEventsVMOnNetwork) string {
switch {
case r.Kind != "":
return r.Kind
case r.Tag != "":
return OnNetworkKindHTTPS
default:
return OnNetworkKindIroh
}
}
func IsGuestEventNSID(nsid string) bool {
return strings.HasPrefix(nsid, GuestEventNSIDPrefix) && len(nsid) > len(GuestEventNSIDPrefix)
}
func IsRequesterGuestEventNSID(nsid string) bool {
switch nsid {
case ComputeEventsGuestPublicKeyNSID, ComputeEventsGuestSignalWebRTCNSID, ComputeEventsGuestSecretsNSID,
ComputeEventsGuestAgentStartNSID, ComputeEventsGuestSecretsUpdatedNSID, ComputeEventsGuestR2StartNSID,
ComputeEventsGuestR2ReleaseNSID:
return true
}
return false
}
func IsGuestOriginatedEventNSID(nsid string) bool {
switch nsid {
case ComputeEventsGuestPublicKeyNSID, ComputeEventsGuestSignalWebRTCNSID, ComputeEventsGuestSecretsAppliedNSID,
ComputeEventsGuestSecretsRequestedNSID, ComputeEventsGuestRequesterDIDNSID, ComputeEventsGuestR2ReleasedNSID:
return true
}
return false
}
func ValidGuestRequesterDID(did, authority string) error {
parsed, err := syntax.ParseDID(strings.TrimSpace(did))
switch {
case err != nil || parsed.String() != did:
return fmt.Errorf("market: the guest requester DID %q is not a DID", did)
case parsed.Method() != "plc":
return fmt.Errorf("market: the guest requester DID %s is not a did:plc; a guest-reported did:web would make the authority fetch a host the guest names", did)
case did == authority:
return fmt.Errorf("market: the guest requester DID %s is the authority's own DID", did)
}
return nil
}
func ContractScopedRKey(service, did, acceptURI string) string {
sum := sha256.Sum256([]byte(did))
return service + "-" + hex.EncodeToString(sum[:])[:32] + "-" + acceptURI[strings.LastIndex(acceptURI, "/")+1:]
}
var guestRequesterNameRE = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$`)
func ValidGuestRequesterName(name string) error {
if !guestRequesterNameRE.MatchString(name) {
return fmt.Errorf("market: the guest requester name %q is not a DNS label", name)
}
return nil
}