this repo has no description
1package userutil 2 3import ( 4 "regexp" 5 "strings" 6) 7 8func IsHandleNoAt(s string) bool { 9 // ref: https://atproto.com/specs/handle 10 re := regexp.MustCompile(`^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$`) 11 return re.MatchString(s) 12} 13 14func UnflattenDid(s string) string { 15 if !IsFlattenedDid(s) { 16 return s 17 } 18 19 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} 26 27// IsFlattenedDid checks if the given string is a flattened DID. 28func IsFlattenedDid(s string) bool { 29 // Check if the string starts with "did-" 30 if !strings.HasPrefix(s, "did-") { 31 return false 32 } 33 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 { 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._-]$`) 44 45 return re.MatchString(reconstructed) 46} 47 48// FlattenDid converts a DID to a flattened format. 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 } 60 61 return "did-" + parts[0] + "-" + parts[1] 62}