···
19
-
parts := strings.SplitN(s[4:], "-", 2) // Skip "did-" prefix and split on first "-"
20
-
if len(parts) != 2 {
24
-
return "did:" + parts[0] + ":" + parts[1]
19
+
return strings.Replace(s, "-", ":", 2)
// IsFlattenedDid checks if the given string is a flattened DID.
···
34
-
// 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 {
40
-
// Reconstruct as a standard DID format
29
+
// Reconstruct as a standard DID format using Replace
// Example: "did-plc-xyz-abc" becomes "did:plc:xyz-abc"
42
-
reconstructed := "did:" + parts[0] + ":" + parts[1]
31
+
reconstructed := strings.Replace(s, "-", ":", 2)
re := regexp.MustCompile(`^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$`)
return re.MatchString(reconstructed)
···
// A flattened DID is a DID with the :s swapped to -s to satisfy certain
// application requirements, such as Go module naming conventions.
func FlattenDid(s string) string {
52
-
if !IsFlattenedDid(s) {
56
-
parts := strings.SplitN(s[4:], ":", 2) // Skip "did:" prefix and split on first ":"
57
-
if len(parts) != 2 {
42
+
return strings.Replace(s, ":", "-", 2)
61
-
return "did-" + parts[0] + "-" + parts[1]
47
+
// 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)