github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/protocol/didexchange/didex_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 didexchange
    11  
    12  import (
    13  	"bytes"
    14  	"fmt"
    15  	"strings"
    16  
    17  	"github.com/btcsuite/btcutil/base58"
    18  
    19  	"github.com/hyperledger/aries-framework-go/pkg/doc/did"
    20  	"github.com/hyperledger/aries-framework-go/pkg/internal/didcommutil"
    21  )
    22  
    23  const (
    24  	doACAPyInterop = true
    25  )
    26  
    27  // Interop: convert a peer did doc to a "sov-like" did doc, to accommodate current behaviour in aca-py,
    28  //
    29  //	where sovrin dids are used as peer dids.
    30  //
    31  // TODO interop: aca-py issue https://github.com/hyperledger/aries-cloudagent-python/issues/1048
    32  func convertPeerToSov(doc *did.Doc) (*did.Doc, error) {
    33  	if doc == nil {
    34  		return doc, nil
    35  	}
    36  
    37  	didParts := strings.Split(doc.ID, ":")
    38  	if len(didParts) != 3 {
    39  		return nil, fmt.Errorf("peer did not in 3 parts")
    40  	}
    41  
    42  	if didParts[1] != "peer" {
    43  		return doc, nil
    44  	}
    45  
    46  	id := base58.Encode(base58.Decode(didParts[2])[:16])
    47  
    48  	newDID := fmt.Sprintf("did:sov:%s", id)
    49  
    50  	docBytes, err := doc.MarshalJSON()
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	docBytes = bytes.Replace(docBytes, []byte(doc.ID), []byte(newDID), -1)
    56  	err = doc.UnmarshalJSON(docBytes)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	return doc, nil
    62  }
    63  
    64  func interopRecipientKey(doc *did.Doc) (string, error) {
    65  	serviceType := didcommutil.GetServiceType(doc.Service[0].Type)
    66  
    67  	if serviceType == "IndyAgent" {
    68  		return recipientKey(doc)
    69  	}
    70  
    71  	return "", fmt.Errorf("recipientKeyAsDIDKey: invalid DID Doc service type: '%v'", doc.Service[0].Type)
    72  }