Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
feat: mediator supports didcomm v1/v2 per-connection (#3303)
Browse files Browse the repository at this point in the history
Signed-off-by: Filip Burlacu <filip.burlacu@securekey.com>
  • Loading branch information
Moopli committed Jul 26, 2022
1 parent 78317f6 commit c28f931
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 19 deletions.
1 change: 1 addition & 0 deletions pkg/didcomm/protocol/mediator/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type Request struct {
Type string `json:"@type,omitempty"`
ID string `json:"@id,omitempty"`
DIDCommV2 bool `json:"didcomm_v2,omitempty"`
decorator.Timing `json:"~timing,omitempty"`
}

Expand Down
48 changes: 38 additions & 10 deletions pkg/didcomm/protocol/mediator/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,22 +388,24 @@ func (s *Service) handleInboundRequest(c *callback) error {
return fmt.Errorf("handleInboundRequest: route request message unmarshal : %w", err)
}

err = validateRequestVersion(s.mediaTypeProfiles, request.DIDCommV2)
if err != nil {
return err
}

grant, err := outboundGrant(
c.msg.ID(),
c.options,
s.endpoint,
func() (string, error) {
for _, mtp := range s.mediaTypeProfiles {
switch mtp {
case transport.MediaTypeDIDCommV2Profile, transport.MediaTypeAIP2RFC0587Profile:
_, pubKeyBytes, e := s.kms.CreateAndExportPubKeyBytes(s.keyAgreementType)
if e != nil {
return "", fmt.Errorf("outboundGrant from handleInboundRequest: kms failed to create "+
"and export %v key: %w", s.keyAgreementType, e)
}

return kmsdidkey.BuildDIDKeyByKeyType(pubKeyBytes, s.keyAgreementType)
if request.DIDCommV2 {
_, pubKeyBytes, e := s.kms.CreateAndExportPubKeyBytes(s.keyAgreementType)
if e != nil {
return "", fmt.Errorf("outboundGrant from handleInboundRequest: kms failed to create "+
"and export %v key: %w", s.keyAgreementType, e)
}

return kmsdidkey.BuildDIDKeyByKeyType(pubKeyBytes, s.keyAgreementType)
}

_, pubKeyBytes, er := s.kms.CreateAndExportPubKeyBytes(kms.ED25519Type)
Expand All @@ -424,6 +426,28 @@ func (s *Service) handleInboundRequest(c *callback) error {
return s.outbound.SendToDID(service.NewDIDCommMsgMap(grant), c.myDID, c.theirDID)
}

func validateRequestVersion(mtps []string, requestedV2 bool) error {
if requestedV2 {
for _, mtp := range mtps {
if transport.IsDIDCommV2(mtp) {
return nil
}
}

return fmt.Errorf("client requested didcomm v2 mediation from mediator " +
"that does not support didcomm v2")
}

for _, mtp := range mtps {
if !transport.IsDIDCommV2(mtp) {
return nil
}
}

return fmt.Errorf("client requested didcomm v1 mediation from mediator " +
"that does not support didcomm v1")
}

func outboundGrant(
msgID string, opts *Options,
defaultEndpoint string, defaultKey func() (string, error)) (*Grant, error) {
Expand Down Expand Up @@ -596,6 +620,10 @@ func (s *Service) doRegistration(record *connection.Record, req *Request, timeou
// demonstrates? additionally `ExpiresTime` would need to be migrated to int64
req.ExpiresTime = time.Now().UTC().Add(timeout)

if record.DIDCommVersion == service.V2 {
req.DIDCommV2 = true
}

// send message to the router
if err = s.outbound.SendToDID(service.NewDIDCommMsgMap(req), record.MyDID, record.TheirDID); err != nil {
return fmt.Errorf("send route request: %w", err)
Expand Down
4 changes: 4 additions & 0 deletions pkg/didcomm/protocol/mediator/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/model"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/messagepickup"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/transport"
"github.com/hyperledger/aries-framework-go/pkg/doc/did"
vdrapi "github.com/hyperledger/aries-framework-go/pkg/framework/aries/api/vdr"
mockdispatcher "github.com/hyperledger/aries-framework-go/pkg/mock/didcomm/dispatcher"
Expand Down Expand Up @@ -312,6 +313,7 @@ func TestServiceRequestMsg(t *testing.T) {
return nil
},
},
MediaTypeProfilesValue: []string{transport.MediaTypeAIP2RFC0019Profile},
})
require.NoError(t, err)

Expand Down Expand Up @@ -402,6 +404,7 @@ func TestEvents(t *testing.T) {
return nil
},
},
MediaTypeProfilesValue: []string{transport.MediaTypeAIP2RFC0019Profile},
})
require.NoError(t, err)

Expand Down Expand Up @@ -512,6 +515,7 @@ func TestEvents(t *testing.T) {
return nil
},
},
MediaTypeProfilesValue: []string{transport.MediaTypeAIP2RFC0019Profile},
})
require.NoError(t, err)

Expand Down
17 changes: 17 additions & 0 deletions pkg/didcomm/transport/media_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,20 @@ func MediaTypeProfiles() []string {
MediaTypeProfileDIDCommAIP1,
}
}

// IsDIDCommV2 returns true iff mtp is one of:
// MediaTypeV2EncryptedEnvelope, MediaTypeV2EncryptedEnvelopeV1PlaintextPayload, MediaTypeAIP2RFC0587Profile,
// MediaTypeDIDCommV2Profile, or MediaTypeV2PlaintextPayload.
func IsDIDCommV2(mtp string) bool {
v2MTPs := map[string]struct{}{
MediaTypeV2EncryptedEnvelope: {},
MediaTypeV2EncryptedEnvelopeV1PlaintextPayload: {},
MediaTypeAIP2RFC0587Profile: {},
MediaTypeDIDCommV2Profile: {},
MediaTypeV2PlaintextPayload: {},
}

_, ok := v2MTPs[mtp]

return ok
}
1 change: 0 additions & 1 deletion scripts/check_go_integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ go test -count=1 -v -cover . -p 1 -timeout=30m -race -run didcomm_remote_crypto,
go test -count=1 -v -cover . -p 1 -timeout=45m -race -run outofband
DEFAULT_KEY_TYPE="ecdsap256ieee1363" DEFAULT_KEY_AGREEMENT_TYPE="p256kw" go test -count=1 -v -cover . -p 1 -timeout=10m -race -run didcommv2
CARL_MEDIA_TYPE_PROFILES="didcomm/aip1" CARL_KEYAGREEMENT_TYPE="X25519ECDHKW" DAVE_MEDIA_TYPE_PROFILES="didcomm/aip2;env=rfc19" DAVE_KEYAGREEMENT_TYPE="X25519ECDHKW" go test -count=1 -v -cover . -p 1 -timeout=20m -race -run aries_router_controller
CARL_MEDIA_TYPE_PROFILES="didcomm/aip2;env=rfc587" CARL_KEYAGREEMENT_TYPE="X25519ECDHKW" DAVE_MEDIA_TYPE_PROFILES="didcomm/v2" DAVE_KEYAGREEMENT_TYPE="X25519ECDHKW" go test -count=1 -v -cover . -p 1 -timeout=20m -race -run aries_router_controller

cd $PWD

Expand Down
4 changes: 2 additions & 2 deletions test/bdd/features/aries_didcommv2_mediator_e2e_sdk.feature
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ Feature: DIDComm v2 Transport between two Agents through DIDComm v2 Routers [SDK
| keyType | keyAgreementType | mediaTypeProfile |
| "ED25519" | "X25519ECDHKW" | "didcomm/aip1" |
| "ED25519" | "X25519ECDHKW" | "didcomm/aip2;env=rfc19" |
| "ED25519" | "X25519ECDHKW" | "didcomm/aip2;env=rfc587" |
| "ED25519" | "NISTP384ECDHKW" | "didcomm/v2" |
# | "ED25519" | "X25519ECDHKW" | "didcomm/aip2;env=rfc587" |
# | "ED25519" | "NISTP384ECDHKW" | "didcomm/v2" |

@aries_didcommv2_router_sdk_nistpkw_key_agreement
Scenario Outline: DID Exchange between two Edge Agents(without Inbound, DIDComm v2 is one way only) through Routers, without router didexchange
Expand Down
12 changes: 6 additions & 6 deletions test/bdd/features/aries_mediator_e2e_sdk.feature
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ Feature: DIDComm Transport between two Agents through DIDComm Routers [SDK]
| keyType | keyAgreementType | mediaTypeProfile |
| "ED25519" | "X25519ECDHKW" | "didcomm/aip1" |
| "ED25519" | "X25519ECDHKW" | "didcomm/aip2;env=rfc19" |
| "ED25519" | "X25519ECDHKW" | "didcomm/aip2;env=rfc587" |
| "ED25519" | "NISTP384ECDHKW" | "didcomm/v2" |
# | "ED25519" | "X25519ECDHKW" | "didcomm/aip2;env=rfc587" |
# | "ED25519" | "NISTP384ECDHKW" | "didcomm/v2" |

Scenario Outline: Decentralized Identifier(DID) Exchange between two Edge Agents through Routers
# DID Exchange between Alice and her Router
Expand Down Expand Up @@ -142,8 +142,8 @@ Feature: DIDComm Transport between two Agents through DIDComm Routers [SDK]
| keyType | keyAgreementType | mediaTypeProfile |
| "ED25519" | "X25519ECDHKW" | "didcomm/aip1" |
| "ED25519" | "X25519ECDHKW" | "didcomm/aip2;env=rfc19" |
| "ED25519" | "X25519ECDHKW" | "didcomm/aip2;env=rfc587" |
| "ED25519" | "NISTP384ECDHKW" | "didcomm/v2" |
# | "ED25519" | "X25519ECDHKW" | "didcomm/aip2;env=rfc587" |
# | "ED25519" | "NISTP384ECDHKW" | "didcomm/v2" |

# https://wiki.hyperledger.org/display/ARIES/DIDComm+MediatorRouter
Scenario Outline: Decentralized Identifier(DID) Exchange between two Edge Agents(without Inbound) through Routers(HTTP/WS)
Expand Down Expand Up @@ -257,5 +257,5 @@ Feature: DIDComm Transport between two Agents through DIDComm Routers [SDK]
| keyType | keyAgreementType | mediaTypeProfile |
| "ED25519" | "X25519ECDHKW" | "didcomm/aip1" |
| "ED25519" | "X25519ECDHKW" | "didcomm/aip2;env=rfc19" |
| "ED25519" | "X25519ECDHKW" | "didcomm/aip2;env=rfc587" |
| "ED25519" | "NISTP384ECDHKW" | "didcomm/v2" |
# | "ED25519" | "X25519ECDHKW" | "didcomm/aip2;env=rfc587" |
# | "ED25519" | "NISTP384ECDHKW" | "didcomm/v2" |

0 comments on commit c28f931

Please sign in to comment.