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

Commit

Permalink
refactor: set DIDcomm V1 VS V2 Endpoint support (#3239)
Browse files Browse the repository at this point in the history
This change sets back the original DIDComm V1's Endpoint logic that was supperceded with the Endpoint struct update for DIDComm V2.
With this change, both ServiceEndpoint structures for V1 and V2 are not supported.

closes #3219

Signed-off-by: Baha Shaaban <baha.shaaban@securekey.com>
  • Loading branch information
baha-ai committed May 26, 2022
1 parent 2d8ae9d commit 18d510d
Show file tree
Hide file tree
Showing 61 changed files with 1,618 additions and 598 deletions.
29 changes: 24 additions & 5 deletions pkg/client/connection/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/google/uuid"

"github.com/hyperledger/aries-framework-go/pkg/common/log"
"github.com/hyperledger/aries-framework-go/pkg/common/model"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/middleware"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/peerdid"
Expand All @@ -22,6 +23,8 @@ import (
"github.com/hyperledger/aries-framework-go/spi/storage"
)

var logger = log.New("aries-framework/pkg/client/connection")

type provider interface {
VDRegistry() vdr.Registry
DIDRotator() *middleware.DIDCommMessageMiddleware
Expand Down Expand Up @@ -80,6 +83,7 @@ func (c *Client) RotateDID(connectionID, signingKID string, opts ...RotateDIDOpt
}

// CreateConnectionV2 creates a DIDComm V2 connection with the given DID.
//nolint:funlen
func (c *Client) CreateConnectionV2(myDID, theirDID string, opts ...CreateConnectionOption) (string, error) {
theirDocRes, err := c.vdr.Resolve(theirDID)
if err != nil {
Expand All @@ -105,16 +109,31 @@ func (c *Client) CreateConnectionV2(myDID, theirDID string, opts ...CreateConnec

connID := uuid.New().String()

uri, err := destination.ServiceEndpoint.URI()
if err != nil {
logger.Debugf("create destination from serviceEndpoint.URI() failed: %w, using value: %s", err, uri)
}

accept, err := destination.ServiceEndpoint.Accept()
if err != nil {
logger.Debugf("create destination from serviceEndpoint.Accept() failed: %w, using value %v", err, accept)
}

routingKeys, err := destination.ServiceEndpoint.RoutingKeys()
if err != nil {
logger.Debugf("create destination from serviceEndpoint.RoutingKeys() failed: %w, using value %v", err, routingKeys)
}

connRec := connection.Record{
ConnectionID: connID,
State: connection.StateNameCompleted,
TheirDID: theirDID,
MyDID: myDID,
ServiceEndPoint: model.Endpoint{
URI: destination.ServiceEndpoint.URI,
Accept: destination.ServiceEndpoint.Accept,
RoutingKeys: destination.ServiceEndpoint.RoutingKeys,
},
ServiceEndPoint: model.NewDIDCommV2Endpoint([]model.DIDCommV2Endpoint{{
URI: uri,
Accept: accept,
RoutingKeys: routingKeys,
}}),
RecipientKeys: destination.RecipientKeys,
Namespace: connection.MyNSPrefix,
DIDCommVersion: service.V2,
Expand Down
2 changes: 2 additions & 0 deletions pkg/client/didexchange/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ func (c *Client) CreateConnection(myDID string, theirDID *did.Doc, options ...Co

conn.ServiceEndPoint = destination.ServiceEndpoint
conn.RecipientKeys = destination.RecipientKeys
conn.RoutingKeys = destination.RoutingKeys
conn.MediaTypeProfiles = destination.MediaTypeProfiles

err = c.didexchangeSvc.CreateConnection(conn.Record, theirDID)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/didexchange/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,13 +773,13 @@ func TestClient_CreateConnection(t *testing.T) {
require.NoError(t, err)

// empty ServiceEndpoint to trigger CreateDestination error
theirDID.Service[0].ServiceEndpoint.URI = ""
theirDID.Service[0].ServiceEndpoint = model.NewDIDCommV1Endpoint("")

_, err = c.CreateConnection(myDID.ID, theirDID,
WithTheirLabel(label), WithThreadID(threadID), WithParentThreadID(parentThreadID),
WithInvitationID(invitationID), WithInvitationDID(invitationDID), WithImplicit(implicit))
require.Contains(t, err.Error(), "createConnection: failed to create destination: "+
"create destination: no service endpoint URI on didcomm service block in diddoc:")
"create destination: service endpoint URI on didcomm v1 service block in diddoc error:")
})
}

Expand Down Expand Up @@ -1516,7 +1516,7 @@ func newPeerDID(t *testing.T) *did.Doc {
d, err := ctx.VDRegistry().Create(
peer.DIDMethod, &did.Doc{Service: []did.Service{{
Type: "did-communication",
ServiceEndpoint: model.Endpoint{URI: "http://agent.example.com/didcomm"},
ServiceEndpoint: model.NewDIDCommV1Endpoint("http://agent.example.com/didcomm"),
}}, VerificationMethod: []did.VerificationMethod{getSigningKey()}})
require.NoError(t, err)

Expand Down
34 changes: 13 additions & 21 deletions pkg/client/messaging/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,9 @@ func TestCommand_Send(t *testing.T) { // nolint: gocognit, gocyclo
{
name: "send message to destination",
option: SendByDestination(&service.Destination{
RecipientKeys: []string{"test"},
ServiceEndpoint: model.Endpoint{
URI: "sdfsdf",
RoutingKeys: []string{"test"},
},
RecipientKeys: []string{"test"},
ServiceEndpoint: model.NewDIDCommV1Endpoint("dfsdf"),
RoutingKeys: []string{"test"},
}),
},
}
Expand Down Expand Up @@ -269,11 +267,9 @@ func TestCommand_Send(t *testing.T) { // nolint: gocognit, gocyclo
name: "send message to destination",
option: []SendMessageOpions{
SendByDestination(&service.Destination{
RecipientKeys: []string{"test"},
ServiceEndpoint: model.Endpoint{
URI: "sdfsdf",
RoutingKeys: []string{"test"},
},
RecipientKeys: []string{"test"},
ServiceEndpoint: model.NewDIDCommV1Endpoint("sdfsdf"),
RoutingKeys: []string{"test"},
}),
WaitForResponse(context.Background(), "sample-response-type"),
},
Expand Down Expand Up @@ -422,11 +418,9 @@ func TestCommand_Send(t *testing.T) { // nolint: gocognit, gocyclo
{
name: "send message to destination - failure 1",
option: SendByDestination(&service.Destination{
RecipientKeys: []string{"test"},
ServiceEndpoint: model.Endpoint{
URI: "sdfsdf",
RoutingKeys: []string{"test"},
},
RecipientKeys: []string{"test"},
ServiceEndpoint: model.NewDIDCommV1Endpoint("sdfsdf"),
RoutingKeys: []string{"test"},
}),
messenger: &mocksvc.MockMessenger{ErrSendToDestination: fmt.Errorf("sample-err-01")},
errorMsg: "sample-err-01",
Expand All @@ -435,11 +429,9 @@ func TestCommand_Send(t *testing.T) { // nolint: gocognit, gocyclo
name: "send message to destination - failure 2",
kms: &mockkms.KeyManager{CrAndExportPubKeyErr: fmt.Errorf("sample-kmserr-01")},
option: SendByDestination(&service.Destination{
RecipientKeys: []string{"test"},
ServiceEndpoint: model.Endpoint{
URI: "sdfsdf",
RoutingKeys: []string{"test"},
},
RecipientKeys: []string{"test"},
ServiceEndpoint: model.NewDIDCommV1Endpoint("sdfsdf"),
RoutingKeys: []string{"test"},
}),
errorMsg: "sample-kmserr-01",
},
Expand All @@ -454,7 +446,7 @@ func TestCommand_Send(t *testing.T) { // nolint: gocognit, gocyclo
option: SendByTheirDID("theirDID-001"),
vdr: &mockvdr.MockVDRegistry{
ResolveFunc: func(didID string, opts ...vdrapi.DIDMethodOption) (doc *did.DocResolution, e error) {
return &did.DocResolution{DIDDocument: mockdiddoc.GetMockDIDDoc(t)}, nil
return &did.DocResolution{DIDDocument: mockdiddoc.GetMockDIDDoc(t, false)}, nil
},
},
errorMsg: "invalid payload data format",
Expand Down
42 changes: 33 additions & 9 deletions pkg/client/outofband/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,13 @@ func validateServices(svcs ...interface{}) error {

// DidDocServiceFunc returns a function that returns a DID doc `service` entry.
// Used when no service entries are specified when creating messages.
//nolint:funlen
//nolint:funlen,gocyclo
func (c *Client) didServiceBlockFunc(p Provider) func(routerConnID string, accept []string) (*did.Service, error) {
return func(routerConnID string, accept []string) (*did.Service, error) {
var (
keyType kms.KeyType
didCommServiceType string
sp model.Endpoint
)

useDIDCommV2 := isDIDCommV2(accept)
Expand All @@ -384,9 +385,14 @@ func (c *Client) didServiceBlockFunc(p Provider) func(routerConnID string, accep
if useDIDCommV2 {
keyType = p.KeyAgreementType()
didCommServiceType = vdr.DIDCommV2ServiceType
sp = model.NewDIDCommV2Endpoint([]model.DIDCommV2Endpoint{{
URI: p.ServiceEndpoint(),
Accept: p.MediaTypeProfiles(),
}})
} else {
keyType = p.KeyType()
didCommServiceType = vdr.DIDCommServiceType
sp = model.NewDIDCommV1Endpoint(p.ServiceEndpoint())
}

if string(keyType) == "" {
Expand Down Expand Up @@ -418,7 +424,7 @@ func (c *Client) didServiceBlockFunc(p Provider) func(routerConnID string, accep
ID: uuid.New().String(),
Type: didCommServiceType,
RecipientKeys: []string{didKey},
ServiceEndpoint: model.Endpoint{URI: p.ServiceEndpoint()},
ServiceEndpoint: sp,
}, nil
}

Expand All @@ -432,15 +438,33 @@ func (c *Client) didServiceBlockFunc(p Provider) func(routerConnID string, accep
return nil, fmt.Errorf("didServiceBlockFunc: create invitation - failed to add key to the router : %w", err)
}

return &did.Service{
ID: uuid.New().String(),
Type: didCommServiceType,
RecipientKeys: []string{didKey},
ServiceEndpoint: model.Endpoint{
var svc *did.Service

if useDIDCommV2 {
sp = model.NewDIDCommV2Endpoint([]model.DIDCommV2Endpoint{{
URI: serviceEndpoint,
Accept: accept,
RoutingKeys: routingKeys,
},
}, nil
}})
svc = &did.Service{
ID: uuid.New().String(),
Type: didCommServiceType,
RecipientKeys: []string{didKey},
ServiceEndpoint: sp,
}
} else {
sp = model.NewDIDCommV1Endpoint(serviceEndpoint)
svc = &did.Service{
ID: uuid.New().String(),
Type: didCommServiceType,
RecipientKeys: []string{didKey},
ServiceEndpoint: sp,
RoutingKeys: routingKeys,
Accept: accept,
}
}

return svc, nil
}
}

Expand Down
75 changes: 37 additions & 38 deletions pkg/client/outofband/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,13 @@ func TestCreateInvitation(t *testing.T) {
})
t.Run("includes the diddoc Service block returned by provider", func(t *testing.T) {
expected := &did.Service{
ID: uuid.New().String(),
Type: uuid.New().String(),
Priority: 0,
RecipientKeys: []string{uuid.New().String()},
ServiceEndpoint: commonmodel.Endpoint{
URI: uuid.New().String(),
RoutingKeys: []string{uuid.New().String()},
},
Properties: nil,
ID: uuid.New().String(),
Type: uuid.New().String(),
Priority: 0,
RecipientKeys: []string{uuid.New().String()},
ServiceEndpoint: commonmodel.NewDIDCommV1Endpoint(uuid.New().String()),
RoutingKeys: []string{uuid.New().String()},
Properties: nil,
}
c, err := New(withTestProvider())
require.NoError(t, err)
Expand Down Expand Up @@ -129,26 +127,31 @@ func TestCreateInvitation(t *testing.T) {
c.didDocSvcFunc = func(conn string, accept []string) (*did.Service, error) {
require.Equal(t, expectedConn, conn)

var serviceType string
var svc *did.Service

if isDIDCommV2(accept) {
serviceType = vdr.DIDCommV2ServiceType
svc = &did.Service{
ServiceEndpoint: commonmodel.NewDIDCommV2Endpoint([]commonmodel.DIDCommV2Endpoint{
{URI: expectedConn, Accept: accept},
}),
Type: vdr.DIDCommV2ServiceType,
}
} else {
serviceType = vdr.DIDCommServiceType
svc = &did.Service{
ServiceEndpoint: commonmodel.NewDIDCommV1Endpoint(expectedConn),
Accept: accept,
Type: vdr.DIDCommServiceType,
}
}

return &did.Service{
ServiceEndpoint: commonmodel.Endpoint{
URI: expectedConn,
Accept: accept,
},
Type: serviceType,
}, nil
return svc, nil
}

inv, err := c.CreateInvitation(nil, WithRouterConnections(expectedConn))
require.NoError(t, err)
require.Equal(t, expectedConn, inv.Services[0].(*did.Service).ServiceEndpoint.URI)
uri, err := inv.Services[0].(*did.Service).ServiceEndpoint.URI()
require.NoError(t, err)
require.Equal(t, expectedConn, uri)
})
t.Run("WithGoal", func(t *testing.T) {
c, err := New(withTestProvider())
Expand All @@ -164,15 +167,13 @@ func TestCreateInvitation(t *testing.T) {
c, err := New(withTestProvider())
require.NoError(t, err)
expected := &did.Service{
ID: uuid.New().String(),
Type: uuid.New().String(),
Priority: 0,
RecipientKeys: []string{uuid.New().String()},
ServiceEndpoint: commonmodel.Endpoint{
URI: uuid.New().String(),
RoutingKeys: []string{uuid.New().String()},
},
Properties: nil,
ID: uuid.New().String(),
Type: uuid.New().String(),
Priority: 0,
RecipientKeys: []string{uuid.New().String()},
ServiceEndpoint: commonmodel.NewDIDCommV1Endpoint(uuid.New().String()),
RoutingKeys: []string{uuid.New().String()},
Properties: nil,
}
inv, err := c.CreateInvitation([]interface{}{expected})
require.NoError(t, err)
Expand All @@ -193,15 +194,13 @@ func TestCreateInvitation(t *testing.T) {
require.NoError(t, err)
didRef := "did:example:234"
svc := &did.Service{
ID: uuid.New().String(),
Type: uuid.New().String(),
Priority: 0,
RecipientKeys: []string{uuid.New().String()},
ServiceEndpoint: commonmodel.Endpoint{
URI: uuid.New().String(),
RoutingKeys: []string{uuid.New().String()},
},
Properties: nil,
ID: uuid.New().String(),
Type: uuid.New().String(),
Priority: 0,
RecipientKeys: []string{uuid.New().String()},
ServiceEndpoint: commonmodel.NewDIDCommV1Endpoint(uuid.New().String()),
RoutingKeys: []string{uuid.New().String()},
Properties: nil,
}
inv, err := c.CreateInvitation([]interface{}{svc, didRef})
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 18d510d

Please sign in to comment.