-
Notifications
You must be signed in to change notification settings - Fork 2
implement default discovery handler #96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c95741c
implement default discovery handler
ilya-korotya bf3a6d2
move features under interface
ilya-korotya f8ec0c4
add attachments headers
ilya-korotya a6d41d3
fix github copilot comments
ilya-korotya 101afe9
refactor; add Experimental tag
ilya-korotya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package discovery | ||
|
|
||
| import ( | ||
| "context" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/iden3/iden3comm/v2/packers" | ||
| "github.com/iden3/iden3comm/v2/protocol" | ||
| ) | ||
|
|
||
| // Featurer interface for feature handlers | ||
| // # Experimental | ||
| type Featurer interface { | ||
| Handle(ctx context.Context) []protocol.DiscoverFeatureDisclosure | ||
| } | ||
|
|
||
| // Discovery handler | ||
| // # Experimental | ||
| type Discovery struct { | ||
| features map[protocol.DiscoveryProtocolFeatureType]Featurer | ||
| } | ||
|
|
||
| // New creates a new Discovery handler | ||
| // # Experimental | ||
| func New(features map[protocol.DiscoveryProtocolFeatureType]Featurer) *Discovery { | ||
| return &Discovery{ | ||
| features: features, | ||
| } | ||
| } | ||
|
|
||
| // Handle processes a DiscoverFeatureQueriesMessage and returns a DiscoverFeatureDiscloseMessage | ||
| // # Experimental | ||
| func (d *Discovery) Handle(ctx context.Context, | ||
| discoverInputMessage protocol.DiscoverFeatureQueriesMessage) (protocol.DiscoverFeatureDiscloseMessage, error) { | ||
| queries := discoverInputMessage.Body.Queries | ||
|
|
||
| var ( | ||
| disclosures []protocol.DiscoverFeatureDisclosure | ||
| err error | ||
| ) | ||
|
|
||
| for _, query := range queries { | ||
| var disclosuresToAppend []protocol.DiscoverFeatureDisclosure | ||
| if featurer, ok := d.features[query.FeatureType]; ok { | ||
| disclosuresToAppend = featurer.Handle(ctx) | ||
| } | ||
|
|
||
| disclosuresToAppend, err = d.handleMatch(disclosuresToAppend, query.Match) | ||
| if err != nil { | ||
| return protocol.DiscoverFeatureDiscloseMessage{}, err | ||
| } | ||
| disclosures = append(disclosures, disclosuresToAppend...) | ||
| } | ||
|
|
||
| return protocol.DiscoverFeatureDiscloseMessage{ | ||
| ID: uuid.NewString(), | ||
| Typ: packers.MediaTypePlainMessage, | ||
| Type: protocol.DiscoverFeatureDiscloseMessageType, | ||
| ThreadID: discoverInputMessage.ThreadID, | ||
| Body: protocol.DiscoverFeatureDiscloseMessageBody{ | ||
| Disclosures: disclosures, | ||
| }, | ||
| From: discoverInputMessage.To, | ||
| To: discoverInputMessage.From, | ||
| }, nil | ||
| } | ||
|
|
||
| func (d *Discovery) handleMatch(disclosures []protocol.DiscoverFeatureDisclosure, match string) ([]protocol.DiscoverFeatureDisclosure, error) { | ||
| if match == "" || match == "*" { | ||
| return disclosures, nil | ||
| } | ||
|
|
||
| regExp, err := wildcardToRegExp(match) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var filtered []protocol.DiscoverFeatureDisclosure | ||
| for _, disclosure := range disclosures { | ||
| if regExp.MatchString(disclosure.ID) { | ||
| filtered = append(filtered, disclosure) | ||
| } | ||
| } | ||
| return filtered, nil | ||
| } | ||
|
|
||
| func wildcardToRegExp(match string) (*regexp.Regexp, error) { | ||
| // Escape special regex characters and replace `*` with `.*` | ||
| regexPattern := regexp.QuoteMeta(match) | ||
| regexPattern = strings.ReplaceAll(regexPattern, "\\*", ".*") | ||
| return regexp.Compile("^" + regexPattern + "$") | ||
| } | ||
|
ilya-korotya marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.