···16 return s
17 }
1819- parts := strings.SplitN(s[4:], "-", 2) // Skip "did-" prefix and split on first "-"
20- if len(parts) != 2 {
21- return s
22- }
23-24- return "did:" + parts[0] + ":" + parts[1]
25}
2627// IsFlattenedDid checks if the given string is a flattened DID.
···31 return false
32 }
3334- // Split the string to extract method and identifier
35- parts := strings.SplitN(s[4:], "-", 2) // Skip "did-" prefix and split on first "-"
36- if len(parts) != 2 {
37- return false
38- }
39-40- // Reconstruct as a standard DID format
41 // Example: "did-plc-xyz-abc" becomes "did:plc:xyz-abc"
42- reconstructed := "did:" + parts[0] + ":" + parts[1]
43 re := regexp.MustCompile(`^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$`)
4445 return re.MatchString(reconstructed)
···49// A flattened DID is a DID with the :s swapped to -s to satisfy certain
50// application requirements, such as Go module naming conventions.
51func FlattenDid(s string) string {
52- if !IsFlattenedDid(s) {
53- return s
54- }
55-56- parts := strings.SplitN(s[4:], ":", 2) // Skip "did:" prefix and split on first ":"
57- if len(parts) != 2 {
58- return s
59 }
006061- return "did-" + parts[0] + "-" + parts[1]
00062}
···16 return s
17 }
1819+ return strings.Replace(s, "-", ":", 2)
0000020}
2122// IsFlattenedDid checks if the given string is a flattened DID.
···26 return false
27 }
2829+ // Reconstruct as a standard DID format using Replace
00000030 // Example: "did-plc-xyz-abc" becomes "did:plc:xyz-abc"
31+ reconstructed := strings.Replace(s, "-", ":", 2)
32 re := regexp.MustCompile(`^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$`)
3334 return re.MatchString(reconstructed)
···38// A flattened DID is a DID with the :s swapped to -s to satisfy certain
39// application requirements, such as Go module naming conventions.
40func FlattenDid(s string) string {
41+ if IsDid(s) {
42+ return strings.Replace(s, ":", "-", 2)
0000043 }
44+ return s
45+}
4647+// IsDid checks if the given string is a standard DID.
48+func IsDid(s string) bool {
49+ re := regexp.MustCompile(`^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$`)
50+ return re.MatchString(s)
51}