Skip to content

Commit

Permalink
WithThirdPartyDIDResolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Dec 11, 2024
1 parent 120db93 commit 29a615e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
5 changes: 4 additions & 1 deletion cmd/driver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ func main() {
if err != nil {
log.Fatalf("failed configure PKH resolver %v", err)
}

thirdPartyDidResolvers := services.ThirdPartyDidResolvers{}
thirdPartyDidResolvers["did:pkh"] = pkhResolver
mux := app.Handlers{DidDocumentHandler: &app.DidDocumentHandler{
DidDocumentService: services.NewDidDocumentServices(resolvers, r, revocationResolvers, pkhResolver, services.WithProvers(proverRegistry))},
DidDocumentService: services.NewDidDocumentServices(resolvers, r, revocationResolvers, services.WithProvers(proverRegistry), services.WithThirdPartyDIDResolvers(thirdPartyDidResolvers))},
}

server := http.Server{
Expand Down
35 changes: 20 additions & 15 deletions pkg/services/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/iden3/driver-did-iden3/pkg/document"
"github.com/iden3/driver-did-iden3/pkg/services/ens"
"github.com/iden3/driver-did-iden3/pkg/services/pkh"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-iden3-core/v2/w3c"
"github.com/iden3/go-schema-processor/v2/verifiable"
Expand All @@ -21,12 +20,18 @@ const (
ensResolverKey = "description"
)

type thirdPartyResolver interface {
Resolve(ctx context.Context, did w3c.DID) (*document.DidResolution, error)
}

type ThirdPartyDidResolvers map[string]thirdPartyResolver

type DidDocumentServices struct {
resolvers *ResolverRegistry
ens *ens.Registry
provers *DIDResolutionProverRegistry
revStatusOnChainResolver *resolvers.OnChainResolver
pkhResolver *pkh.Resolver
thirdPartyResolvers ThirdPartyDidResolvers
}

type ResolverOpts struct {
Expand All @@ -43,8 +48,14 @@ func WithProvers(provers *DIDResolutionProverRegistry) DidDocumentOption {
}
}

func NewDidDocumentServices(resolverRegistry *ResolverRegistry, registry *ens.Registry, revStatusOnChainResolver *resolvers.OnChainResolver, pkhResolver *pkh.Resolver, opts ...DidDocumentOption) *DidDocumentServices {
didDocumentService := &DidDocumentServices{resolverRegistry, registry, nil, revStatusOnChainResolver, pkhResolver}
func WithThirdPartyDIDResolvers(thirdPartyResolvers ThirdPartyDidResolvers) DidDocumentOption {
return func(d *DidDocumentServices) {
d.thirdPartyResolvers = thirdPartyResolvers
}
}

func NewDidDocumentServices(resolverRegistry *ResolverRegistry, registry *ens.Registry, revStatusOnChainResolver *resolvers.OnChainResolver, opts ...DidDocumentOption) *DidDocumentServices {
didDocumentService := &DidDocumentServices{resolverRegistry, registry, nil, revStatusOnChainResolver, nil}

for _, opt := range opts {
opt(didDocumentService)
Expand All @@ -64,8 +75,11 @@ func (d *DidDocumentServices) GetDidDocument(ctx context.Context, did string, op
return errResolution, err
}

if isPkhDid(userDID) {
return d.ResolvePkhDid(ctx, *userDID)
didParts := strings.Split(userDID.String(), ":")
didPrefix := fmt.Sprintf("%s:%s", didParts[0], didParts[1])
thirdPartyResolver := d.thirdPartyResolvers[didPrefix]
if thirdPartyResolver != nil {
return thirdPartyResolver.Resolve(ctx, *userDID)
}

userID, err := core.IDFromDID(*userDID)
Expand Down Expand Up @@ -249,11 +263,6 @@ func (d *DidDocumentServices) ResolveENSDomain(ctx context.Context, domain strin
return d.GetDidDocument(ctx, did, nil)
}

// ResolvePkhDid return did document via PKH resolver.
func (d *DidDocumentServices) ResolvePkhDid(ctx context.Context, did w3c.DID) (*document.DidResolution, error) {
return d.pkhResolver.Resolve(ctx, did)
}

func (d *DidDocumentServices) GetGist(ctx context.Context, chain, network string, opts *ResolverOpts) (*verifiable.GistInfo, error) {
if opts == nil {
opts = &ResolverOpts{}
Expand Down Expand Up @@ -320,10 +329,6 @@ func expectedError(err error) (*document.DidResolution, error) {
return nil, err
}

func isPkhDid(did *w3c.DID) bool {
return strings.HasPrefix(did.String(), "did:pkh:")
}

// after discussion we decided not to include state in verification method id,
// so we can have consistent id for verification
// func getRepresentaionID(did string, state IdentityState) string {
Expand Down

0 comments on commit 29a615e

Please sign in to comment.