mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-08-29 04:06:58 +00:00
25 lines
580 B
Go
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)
|
|
}
|