Files

25 lines
580 B
Go

package did
import (
"fmt"
"net/url"
)
// GenerateDIDFromURL computes a did:web identifier from a public URL.
// Per the did:web spec, ports are percent-encoded (`:` → `%3A`).
func GenerateDIDFromURL(publicURL string) string {
u, err := url.Parse(publicURL)
if err != nil {
return fmt.Sprintf("did:web:%s", publicURL)
}
hostname := u.Hostname()
if hostname == "" {
hostname = "localhost"
}
port := u.Port()
if port != "" && port != "80" && port != "443" {
return fmt.Sprintf("did:web:%s%%3A%s", hostname, port)
}
return fmt.Sprintf("did:web:%s", hostname)
}