package deed import ( "encoding/hex" "fmt" "time" "git.smesh.lol/orly/pkg/nostr/encoders/event" "git.smesh.lol/orly/pkg/nostr/encoders/ints" ) // Succession label namespace and value. An owner key rotation is a NIP-32 label // event signed by the outgoing key. A bare key change is not a rotation. const ( // SuccessionNamespace is the NIP-32 L namespace. SuccessionNamespace = "musiquay.succession" // SuccessionLabel is the NIP-32 l value. SuccessionLabel = "succession" // LabelKind is the NIP-32 label event kind. LabelKind = 1985 ) // Succession is a parsed key-rotation delegation. type Succession struct { Event *event.E // Outgoing is the key that signed the delegation. Outgoing []byte // Incoming is the key named by the p tag. Incoming []byte // ScopeCoordinate is the optional a-tag coordinate; empty means the // delegation covers every asset owned by the outgoing key. ScopeCoordinate string // ScopeAsset is the optional asset tag; empty means unscoped. ScopeAsset string // Prev is an optional previous delegation id. Prev string // Expires is the optional unix expiry; zero means no expiry. Expires int64 } // ParseSuccession structurally validates a succession delegation. func ParseSuccession(ev *event.E) (*Succession, error) { if ev == nil { return nil, fmt.Errorf("deed: nil succession delegation") } if ev.Kind != LabelKind { return nil, fmt.Errorf("deed: kind %d is not a NIP-32 label", ev.Kind) } if valid, err := ev.Verify(); err != nil { return nil, fmt.Errorf("deed: verify succession: %w", err) } else if !valid { return nil, fmt.Errorf("deed: succession signature invalid") } if !hasLabel(ev, SuccessionNamespace, SuccessionLabel) { return nil, fmt.Errorf("deed: event is not a %s label", SuccessionNamespace) } p := ev.Tags.GetFirst([]byte("p")) if p == nil { return nil, fmt.Errorf("deed: succession delegation has no incoming p tag") } incoming, err := hex.DecodeString(string(p.ValueHex())) if err != nil || len(incoming) != 32 { return nil, fmt.Errorf("deed: succession delegation has an invalid incoming key") } s := &Succession{ Event: ev, Outgoing: append([]byte(nil), ev.Pubkey...), Incoming: incoming, ScopeCoordinate: tagValue(ev, TagCoordinate), ScopeAsset: tagValue(ev, TagAsset), Prev: tagValue(ev, "e"), } if s.ScopeAsset != "" && !IsAssetID(s.ScopeAsset) { return nil, fmt.Errorf("deed: succession delegation has an invalid asset scope") } if raw := tagValue(ev, "expiration"); raw != "" { n := ints.New(0) rem, err := n.Unmarshal([]byte(raw)) if err != nil || len(rem) > 0 { return nil, fmt.Errorf("deed: succession delegation has an invalid expiration") } s.Expires = n.Int64() } if s.Expires != 0 && s.Expires <= time.Now().Unix() { return nil, fmt.Errorf("deed: succession delegation expired") } if equalBytes(s.Outgoing, s.Incoming) { return nil, fmt.Errorf("deed: succession delegation names the same key") } return s, nil } // hasLabel reports whether a NIP-32 event carries the namespace and value. func hasLabel(ev *event.E, namespace, value string) bool { okNamespace := false for _, t := range ev.Tags.GetAll([]byte("L")) { if string(t.Value()) == namespace { okNamespace = true break } } if !okNamespace { return false } for _, t := range ev.Tags.GetAll([]byte("l")) { if string(t.Value()) != value { continue } // The third element is the namespace, when present. if len(t.T) >= 3 && string(t.T[2]) != namespace { continue } return true } return false } // ResolveSuccessor follows a succession chain from original to target. // // The assetID, when non-empty, filters out delegations scoped to another asset. // An unscoped delegation applies to every asset. Expired delegations are // ignored. The chain must be continuous: each hop's Outgoing must be the key // reached by the previous hop. func ResolveSuccessor(original, target []byte, assetID string, delegations []*event.E) (bool, error) { if equalBytes(original, target) { return true, nil } parsed := make([]*Succession, 0, len(delegations)) for _, ev := range delegations { s, err := ParseSuccession(ev) if err != nil { continue } if s.ScopeAsset != "" && assetID != "" && s.ScopeAsset != assetID { continue } parsed = append(parsed, s) } // Breadth-first over the delegation graph, with a visited set so a cycle // cannot loop. visited := map[string]bool{string(original): true} frontier := [][]byte{original} for len(frontier) > 0 { next := make([][]byte, 0, len(parsed)) for _, cur := range frontier { for _, s := range parsed { if !equalBytes(s.Outgoing, cur) { continue } if equalBytes(s.Incoming, target) { return true, nil } if visited[string(s.Incoming)] { continue } visited[string(s.Incoming)] = true next = append(next, s.Incoming) } } frontier = next } return false, fmt.Errorf("deed: no valid succession path from %x to %x", original, target) }