github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/common/service/destination_interop.go (about) 1 //go:build ACAPyInterop 2 // +build ACAPyInterop 3 4 /* 5 Copyright SecureKey Technologies Inc. All Rights Reserved. 6 7 SPDX-License-Identifier: Apache-2.0 8 */ 9 10 package service 11 12 import ( 13 "fmt" 14 "strings" 15 16 "github.com/btcsuite/btcutil/base58" 17 18 "github.com/hyperledger/aries-framework-go/pkg/common/model" 19 diddoc "github.com/hyperledger/aries-framework-go/pkg/doc/did" 20 "github.com/hyperledger/aries-framework-go/pkg/vdr/fingerprint" 21 ) 22 23 // CreateDestination makes a DIDComm Destination object from a DID Doc as per the DIDComm service conventions: 24 // https://github.com/hyperledger/aries-rfcs/blob/master/features/0067-didcomm-diddoc-conventions/README.md. 25 func CreateDestination(didDoc *diddoc.Doc) (*Destination, error) { 26 didCommService, ok := diddoc.LookupService(didDoc, didCommServiceType) 27 if !ok { 28 // Interop: fallback to using IndyAgent service type 29 didCommService, ok = diddoc.LookupService(didDoc, legacyDIDCommServiceType) 30 if !ok { 31 return nil, fmt.Errorf("create destination: missing DID doc service") 32 } 33 } 34 uri, err := didCommService.ServiceEndpoint.URI() 35 if uri == "" || err != nil { 36 return nil, fmt.Errorf("create destination: no service endpoint on didcomm service block in diddoc: %#v", didDoc) 37 } 38 39 if len(didCommService.RecipientKeys) == 0 { 40 return nil, fmt.Errorf("create destination: no recipient keys on didcomm service block in diddoc: %#v", didDoc) 41 } 42 43 // Interop: service keys that are raw base58 public keys should be converted to did:key format 44 return &Destination{ 45 RecipientKeys: convertAnyB58Keys(didCommService.RecipientKeys), 46 ServiceEndpoint: model.NewDIDCommV1Endpoint(uri), 47 RoutingKeys: convertAnyB58Keys(didCommService.RoutingKeys), 48 MediaTypeProfiles: didCommService.Accept, 49 }, nil 50 } 51 52 func convertAnyB58Keys(keys []string) []string { 53 var didKeys []string 54 55 for _, key := range keys { 56 if key == "" { 57 didKeys = append(didKeys, key) 58 continue 59 } 60 61 // skip if the key is a relative did-url (ie, it starts with ?, /, or #) 62 if strings.Contains("?/#", string(key[0])) { // nolint:gocritic 63 didKeys = append(didKeys, key) 64 continue 65 } 66 67 // skip if the key is already a did 68 if strings.HasPrefix(key, "did:") { 69 didKeys = append(didKeys, key) 70 continue 71 } 72 73 rawKey := base58.Decode(key) 74 if len(rawKey) == 0 { 75 didKeys = append(didKeys, key) 76 continue 77 } 78 79 didKey, _ := fingerprint.CreateDIDKey(rawKey) 80 81 didKeys = append(didKeys, didKey) 82 } 83 84 return didKeys 85 }