Skip to content

Commit

Permalink
Extract issuer setter/getter to go-schema-processor. Rename user DID …
Browse files Browse the repository at this point in the history
…setter/getter to sender DID.
  • Loading branch information
olomix committed Jan 26, 2024
1 parent c84e85a commit aac3cbd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 37 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/iden3/go-iden3-crypto v0.0.15
github.com/iden3/go-jwz/v2 v2.0.1
github.com/iden3/go-rapidsnark/types v0.0.3
github.com/iden3/go-schema-processor/v2 v2.2.1-0.20240124111525-52e3f8db0f62
github.com/iden3/go-schema-processor/v2 v2.2.1-0.20240126124145-b6f321093157
github.com/jarcoal/httpmock v1.3.1
github.com/lestrrat-go/jwx/v2 v2.0.12
github.com/mr-tron/base58 v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ github.com/iden3/go-rapidsnark/witness/v2 v2.0.0 h1:mkY6VDfwKVJc83QGKmwVXY2LYepi
github.com/iden3/go-rapidsnark/witness/v2 v2.0.0/go.mod h1:3JRjqUfW1hgI9hzLDO0v8z/DUkR0ZUehhYLlnIfRxnA=
github.com/iden3/go-rapidsnark/witness/wazero v0.0.0-20230524142950-0986cf057d4e h1:WeiFCrpj5pLRtSA4Mg03yTrSZhHHqN/k5b6bwxd9/tY=
github.com/iden3/go-rapidsnark/witness/wazero v0.0.0-20230524142950-0986cf057d4e/go.mod h1:UEBifEzw62T6VzIHJeHuUgeLg2U/J9ttf7hOwQEqnYk=
github.com/iden3/go-schema-processor/v2 v2.2.1-0.20240124111525-52e3f8db0f62 h1:FmIHPoJq3MROiqd1rmscO4CdfqGcUgaXujyGsEbtIwA=
github.com/iden3/go-schema-processor/v2 v2.2.1-0.20240124111525-52e3f8db0f62/go.mod h1:BcHVDZyn8q8vUlL+XpOo7hpwXmEjxzO8ao1LkvFsM+k=
github.com/iden3/go-schema-processor/v2 v2.2.1-0.20240126124145-b6f321093157 h1:bvZczBLCisoDGtWF8knmjlLLn7/VWEhfAHV0X6BXAHY=
github.com/iden3/go-schema-processor/v2 v2.2.1-0.20240126124145-b6f321093157/go.mod h1:BcHVDZyn8q8vUlL+XpOo7hpwXmEjxzO8ao1LkvFsM+k=
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
Expand Down
44 changes: 14 additions & 30 deletions resolvers/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,21 @@ import (
"github.com/pkg/errors"
)

type ctxKeyIssuerDID struct{}
type ctxKeyUserDID struct{}
type ctxKeySenderDID struct{}

// WithIssuerDID puts the issuer DID in the context
func WithIssuerDID(ctx context.Context, issuerDID *w3c.DID) context.Context {
return context.WithValue(ctx, ctxKeyIssuerDID{}, issuerDID)
// WithSenderDID puts the user DID in the context
func WithSenderDID(ctx context.Context, userDID *w3c.DID) context.Context {
return context.WithValue(ctx, ctxKeySenderDID{}, userDID)
}

// GetIssuerDID extract the issuer DID from the context.
// Or nil if nothing is found.
func GetIssuerDID(ctx context.Context) *w3c.DID {
return getTpCtx[w3c.DID](ctx, ctxKeyIssuerDID{})
}

// WithUserDID puts the user DID in the context
func WithUserDID(ctx context.Context, userDID *w3c.DID) context.Context {
return context.WithValue(ctx, ctxKeyUserDID{}, userDID)
}

// GetUserDID extract the user DID from the context.
// Or nil if nothing is found.
func GetUserDID(ctx context.Context) *w3c.DID {
return getTpCtx[w3c.DID](ctx, ctxKeyUserDID{})
}

func getTpCtx[T any](ctx context.Context, key any) *T {
v := ctx.Value(key)
// GetSenderDID extract the sender's DID from the context.
// Returns nil if nothing is found.
func GetSenderDID(ctx context.Context) *w3c.DID {
v := ctx.Value(ctxKeySenderDID{})
if v == nil {
return nil
}
return v.(*T)
return v.(*w3c.DID)
}

// AgentResolverConfig options for credential status verification
Expand Down Expand Up @@ -85,18 +69,18 @@ func (r AgentResolver) Resolve(ctx context.Context,
return out, err
}

userDID := GetUserDID(ctx)
if userDID == nil {
return out, errors.New("user DID not found in the context")
senderDID := GetSenderDID(ctx)
if senderDID == nil {
return out, errors.New("sender DID not found in the context")
}
issuerDID := GetUserDID(ctx)
issuerDID := verifiable.GetIssuerDID(ctx)
if issuerDID == nil {
return out, errors.New("issuer DID not found in the context")
}
msg := iden3comm.BasicMessage{
ID: idUUID.String(),
ThreadID: threadUUID.String(),
From: userDID.String(),
From: senderDID.String(),
To: issuerDID.String(),
Type: protocol.RevocationStatusRequestMessageType,
Body: rawBody,
Expand Down
9 changes: 5 additions & 4 deletions resolvers/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestAgentResolver(t *testing.T) {

issuerDID, err := w3c.ParseDID("did:polygonid:polygon:mumbai:2qJp131YoXVu8iLNGfL3TkQAWEr3pqimh2iaPgH3BJ")
require.NoError(t, err)
userDID, err := w3c.ParseDID("did:polygonid:polygon:mumbai:2qFDziX3k3h7To2jDJbQiXFtcozbgSNNvQpb6TgtPE")
senderDID, err := w3c.ParseDID("did:polygonid:polygon:mumbai:2qFDziX3k3h7To2jDJbQiXFtcozbgSNNvQpb6TgtPE")
require.NoError(t, err)

agentConfig := AgentResolverConfig{
Expand All @@ -46,14 +46,15 @@ func TestAgentResolver(t *testing.T) {
httpmock.NewStringResponder(200, `{"body":{"issuer":{"claimsTreeRoot":"d9597e2fef206c9821f2425e513a68c8c793bc93c9216fb883fedaaf72abf51c","revocationTreeRoot":"0000000000000000000000000000000000000000000000000000000000000000","rootOfRoots":"eaa48e4a7d3fe2fabbd939c7df1048c3f647a9a7c9dfadaae836ec78ba673229","state":"96161f3fbbdd68c72bc430dae474e27b157586b33b9fbf4a3f07d75ce275570f"},"mtp":{"existence":false,"siblings":[]}},"from":"did:polygonid:polygon:mumbai:2qJp131YoXVu8iLNGfL3TkQAWEr3pqimh2iaPgH3BJ","id":"9ece0dad-9267-4a52-b611-f0615b0143fb","thid":"8bdc87dc-1755-41d5-b483-26562836068e","to":"did:polygonid:polygon:mumbai:2qFDziX3k3h7To2jDJbQiXFtcozbgSNNvQpb6TgtPE","typ":"application/iden3comm-plain-json","type":"https://iden3-communication.io/revocation/1.0/status"}`))

ctx := context.Background()
ctx = WithIssuerDID(ctx, issuerDID)
ctx = WithUserDID(ctx, userDID)
ctx = verifiable.WithIssuerDID(ctx, issuerDID)
ctx = WithSenderDID(ctx, senderDID)
revocationStatus, err := agentResolver.Resolve(ctx, credStatus)
require.NoError(t, err)

expectedRevocationStatusJSON := `{"issuer":{"state":"96161f3fbbdd68c72bc430dae474e27b157586b33b9fbf4a3f07d75ce275570f","rootOfRoots":"eaa48e4a7d3fe2fabbd939c7df1048c3f647a9a7c9dfadaae836ec78ba673229","claimsTreeRoot":"d9597e2fef206c9821f2425e513a68c8c793bc93c9216fb883fedaaf72abf51c","revocationTreeRoot":"0000000000000000000000000000000000000000000000000000000000000000"},"mtp":{"existence":false,"siblings":[]}}`
var expectedRevocationStatus verifiable.RevocationStatus
_ = json.Unmarshal([]byte(expectedRevocationStatusJSON), &expectedRevocationStatus)
err = json.Unmarshal([]byte(expectedRevocationStatusJSON), &expectedRevocationStatus)
require.NoError(t, err)

assert.Equal(t, revocationStatus, expectedRevocationStatus)

Expand Down

0 comments on commit aac3cbd

Please sign in to comment.