···1616 return s
1717 }
18181919- parts := strings.SplitN(s[4:], "-", 2) // Skip "did-" prefix and split on first "-"
2020- if len(parts) != 2 {
2121- return s
2222- }
2323-2424- return "did:" + parts[0] + ":" + parts[1]
1919+ return strings.Replace(s, "-", ":", 2)
2520}
26212722// IsFlattenedDid checks if the given string is a flattened DID.
···3126 return false
3227 }
33283434- // Split the string to extract method and identifier
3535- parts := strings.SplitN(s[4:], "-", 2) // Skip "did-" prefix and split on first "-"
3636- if len(parts) != 2 {
3737- return false
3838- }
3939-4040- // Reconstruct as a standard DID format
2929+ // Reconstruct as a standard DID format using Replace
4130 // Example: "did-plc-xyz-abc" becomes "did:plc:xyz-abc"
4242- reconstructed := "did:" + parts[0] + ":" + parts[1]
3131+ reconstructed := strings.Replace(s, "-", ":", 2)
4332 re := regexp.MustCompile(`^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$`)
44334534 return re.MatchString(reconstructed)
···4938// A flattened DID is a DID with the :s swapped to -s to satisfy certain
5039// application requirements, such as Go module naming conventions.
5140func FlattenDid(s string) string {
5252- if !IsFlattenedDid(s) {
5353- return s
5454- }
5555-5656- parts := strings.SplitN(s[4:], ":", 2) // Skip "did:" prefix and split on first ":"
5757- if len(parts) != 2 {
5858- return s
4141+ if IsDid(s) {
4242+ return strings.Replace(s, ":", "-", 2)
5943 }
4444+ return s
4545+}
60466161- return "did-" + parts[0] + "-" + parts[1]
4747+// IsDid checks if the given string is a standard DID.
4848+func IsDid(s string) bool {
4949+ re := regexp.MustCompile(`^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$`)
5050+ return re.MatchString(s)
6251}