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

refactor: Moved generic utility functions out of DIDExchange #993

Merged
merged 2 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions pkg/didcomm/common/service/destination.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package service

import (
"fmt"

diddoc "github.com/hyperledger/aries-framework-go/pkg/doc/did"
"github.com/hyperledger/aries-framework-go/pkg/framework/aries/api/vdri"
)

// Destination provides the recipientKeys, routingKeys, and serviceEndpoint for an outbound message.
// Can be populated from an Invitation or DIDDoc.
type Destination struct {
RecipientKeys []string
ServiceEndpoint string
RoutingKeys []string
TransportReturnRoute string
}

const (
didCommServiceType = "did-communication"
ed25519KeyType = "Ed25519VerificationKey2018"
)

// GetDestination constructs a Destination struct based on the given DID and parameters
// It resolves the DID using the given VDR, and collects relevant data from the resolved DIDDoc.
func GetDestination(did string, vdr vdri.Registry) (*Destination, error) {
didDoc, err := vdr.Resolve(did)
if err != nil {
return nil, err
}

return CreateDestination(didDoc)
}

// CreateDestination makes a DIDComm Destination object from a DID Doc
func CreateDestination(didDoc *diddoc.Doc) (*Destination, error) {
didCommService, ok := diddoc.LookupService(didDoc, didCommServiceType)
if !ok {
return nil, fmt.Errorf("create destination: missing DID doc service")
}

recipientKeys, ok := diddoc.LookupRecipientKeys(didDoc, didCommServiceType, ed25519KeyType)
if !ok {
return nil, fmt.Errorf("create destination: missing keys")
}

return &Destination{
RecipientKeys: recipientKeys,
ServiceEndpoint: didCommService.ServiceEndpoint,
}, nil
}
143 changes: 143 additions & 0 deletions pkg/didcomm/common/service/destination_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package service

import (
"crypto/ed25519"
"crypto/rand"
"errors"
"fmt"
"testing"
"time"

"github.com/btcsuite/btcutil/base58"
"github.com/stretchr/testify/require"

"github.com/hyperledger/aries-framework-go/pkg/doc/did"
mockdiddoc "github.com/hyperledger/aries-framework-go/pkg/internal/mock/diddoc"
mockvdri "github.com/hyperledger/aries-framework-go/pkg/internal/mock/vdri"
)

func TestGetDestinationFromDID(t *testing.T) {
doc := createDIDDoc()

t.Run("successfully getting destination from public DID", func(t *testing.T) {
vdr := mockvdri.MockVDRIRegistry{ResolveValue: doc}
destination, err := GetDestination(doc.ID, &vdr)
require.NoError(t, err)
require.NotNil(t, destination)
})

t.Run("test public key not found", func(t *testing.T) {
doc.PublicKey = nil
vdr := mockvdri.MockVDRIRegistry{ResolveValue: doc}
destination, err := GetDestination(doc.ID, &vdr)
require.Error(t, err)
require.Contains(t, err.Error(), "missing keys")
require.Nil(t, destination)
})

t.Run("test service not found", func(t *testing.T) {
doc2 := createDIDDoc()
doc2.Service = nil
vdr := mockvdri.MockVDRIRegistry{ResolveValue: doc2}
destination, err := GetDestination(doc2.ID, &vdr)
require.Error(t, err)
require.Contains(t, err.Error(), "missing DID doc service")
require.Nil(t, destination)
})

t.Run("test did document not found", func(t *testing.T) {
vdr := mockvdri.MockVDRIRegistry{ResolveErr: errors.New("resolver error")}
destination, err := GetDestination(doc.ID, &vdr)
require.Error(t, err)
require.Contains(t, err.Error(), "resolver error")
require.Nil(t, destination)
})
}

func TestPrepareDestination(t *testing.T) {
ed25519KeyType := "Ed25519VerificationKey2018"
didCommServiceType := "did-communication"

t.Run("successfully prepared destination", func(t *testing.T) {
dest, err := CreateDestination(mockdiddoc.GetMockDIDDoc())
require.NoError(t, err)
require.NotNil(t, dest)
require.Equal(t, dest.ServiceEndpoint, "https://localhost:8090")
})

t.Run("error while getting service", func(t *testing.T) {
didDoc := mockdiddoc.GetMockDIDDoc()
didDoc.Service = nil

dest, err := CreateDestination(didDoc)
require.Error(t, err)
require.Contains(t, err.Error(), "missing DID doc service")
require.Nil(t, dest)
})

t.Run("error while getting recipient keys from did doc", func(t *testing.T) {
didDoc := mockdiddoc.GetMockDIDDoc()
didDoc.Service[0].RecipientKeys = []string{}

recipientKeys, ok := did.LookupRecipientKeys(didDoc, didCommServiceType, ed25519KeyType)
require.False(t, ok)
require.Nil(t, recipientKeys)
})
}

func createDIDDoc() *did.Doc {
pubKey, _ := generateKeyPair()
return createDIDDocWithKey(pubKey)
}

func generateKeyPair() (string, []byte) {
pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}

return base58.Encode(pubKey[:]), privKey
}

func createDIDDocWithKey(pub string) *did.Doc {
const (
didFormat = "did:%s:%s"
didPKID = "%s#keys-%d"
didServiceID = "%s#endpoint-%d"
method = "test"
)

id := fmt.Sprintf(didFormat, method, pub[:16])
pubKeyID := fmt.Sprintf(didPKID, id, 1)
pubKey := did.PublicKey{
ID: pubKeyID,
Type: "Ed25519VerificationKey2018",
Controller: id,
Value: []byte(pub),
}
services := []did.Service{
{
ID: fmt.Sprintf(didServiceID, id, 1),
Type: "did-communication",
ServiceEndpoint: "http://localhost:58416",
Priority: 0,
RecipientKeys: []string{pubKeyID},
},
}
createdTime := time.Now()
didDoc := &did.Doc{
Context: []string{did.Context},
ID: id,
PublicKey: []did.PublicKey{pubKey},
Service: services,
Created: &createdTime,
Updated: &createdTime,
}

return didDoc
}
119 changes: 0 additions & 119 deletions pkg/didcomm/common/service/mocks/mocks.go

This file was deleted.

8 changes: 0 additions & 8 deletions pkg/didcomm/common/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,3 @@ func (m *DIDCommMsg) ThreadID() (string, error) {

return "", ErrThreadIDNotFound
}

// Destination provides the recipientKeys, routingKeys, and serviceEndpoint populated from Invitation
type Destination struct {
RecipientKeys []string
ServiceEndpoint string
RoutingKeys []string
TransportReturnRoute string
}
7 changes: 6 additions & 1 deletion pkg/didcomm/protocol/didexchange/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,12 @@ func (s *Service) CreateImplicitInvitation(inviterLabel, inviterDID, inviteeLabe
return "", fmt.Errorf("resolve public did[%s]: %w", inviterDID, err)
}

dest, err := prepareDestination(didDoc)
// TODO: hardcoded key type
dest, err := service.CreateDestination(didDoc)
if err != nil {
return "", err
}

thID := generateRandomID()
connRecord := &ConnectionRecord{
ConnectionID: generateRandomID(),
Expand Down
Loading