This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Moved generic utility functions out of didexchange
- DID doc parsing - Destination struct construction Signed-off-by: Filip Burlacu <[email protected]>
- Loading branch information
Filip Burlacu
committed
Dec 19, 2019
1 parent
388646d
commit 7aed2de
Showing
10 changed files
with
464 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.