github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/messaging/service/basic/basicmsgsvc.go (about)

     1  /*
     2   *
     3   * Copyright SecureKey Technologies Inc. All Rights Reserved.
     4   *
     5   * SPDX-License-Identifier: Apache-2.0
     6   * /
     7   *
     8   */
     9  
    10  // Package basic provide basic message protocol features
    11  //
    12  // Any incoming message of type "https://didcomm.org/basicmessage/1.0/message" can be handled
    13  // by registering `basic.MessageService`.
    14  //
    15  // RFC Reference:
    16  //
    17  // https://github.com/hyperledger/aries-rfcs/tree/master/features/0095-basic-message
    18  //
    19  package basic
    20  
    21  import (
    22  	"fmt"
    23  
    24  	"github.com/hyperledger/aries-framework-go/pkg/common/log"
    25  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
    26  	"github.com/hyperledger/aries-framework-go/pkg/internal/logutil"
    27  )
    28  
    29  const (
    30  	// MessageRequestType is basic message DIDComm message type.
    31  	MessageRequestType = "https://didcomm.org/basicmessage/1.0/message"
    32  
    33  	// error messages.
    34  	errNameAndHandleMandatory = "service name and basic message handle is mandatory"
    35  	errFailedToDecodeMsg      = "unable to decode incoming DID comm message: %w"
    36  
    37  	basicMessage = "basicMessage"
    38  )
    39  
    40  var logger = log.New("aries-framework/basicmsg")
    41  
    42  // MessageHandle is handle function for basic message service which gets called by
    43  // `basic.MessageService` to handle incoming messages.
    44  //
    45  // Args
    46  //
    47  // message : incoming basic message.
    48  // myDID: receiving agent's DID.
    49  // theirDID: sender agent's DID.
    50  //
    51  // Returns
    52  //
    53  // error : handle can return error back to service to notify message dispatcher about failures.
    54  type MessageHandle func(message Message, ctx service.DIDCommContext) error
    55  
    56  // NewMessageService creates basic message service which serves
    57  // incoming basic messages [RFC-0095]
    58  //
    59  //
    60  // Args:
    61  //
    62  // name - is name of this message service (this is mandatory argument).
    63  //
    64  // handle - is handle function to which incoming basic message will be sent(this is mandatory argument).
    65  //
    66  // Returns:
    67  //
    68  // MessageService: basic message service,
    69  //
    70  // error: arg validation errors.
    71  func NewMessageService(name string, handle MessageHandle) (*MessageService, error) {
    72  	if name == "" || handle == nil {
    73  		return nil, fmt.Errorf(errNameAndHandleMandatory)
    74  	}
    75  
    76  	return &MessageService{
    77  		name:   name,
    78  		handle: handle,
    79  	}, nil
    80  }
    81  
    82  // MessageService is message service which transports incoming basic messages to handlers provided.
    83  type MessageService struct {
    84  	name   string
    85  	handle MessageHandle
    86  }
    87  
    88  // Name of basic message service.
    89  func (m *MessageService) Name() string {
    90  	return m.name
    91  }
    92  
    93  // Accept is acceptance criteria for this basic message service.
    94  func (m *MessageService) Accept(msgType string, purpose []string) bool {
    95  	return msgType == MessageRequestType
    96  }
    97  
    98  // HandleInbound for basic message service.
    99  func (m *MessageService) HandleInbound(msg service.DIDCommMsg, ctx service.DIDCommContext) (string, error) {
   100  	basicMsg := Message{}
   101  
   102  	err := msg.Decode(&basicMsg)
   103  	if err != nil {
   104  		return "", fmt.Errorf(errFailedToDecodeMsg, err)
   105  	}
   106  
   107  	logutil.LogDebug(logger, basicMessage, "handleInbound", "received",
   108  		logutil.CreateKeyValueString("msgType", msg.Type()),
   109  		logutil.CreateKeyValueString("msgID", msg.ID()))
   110  
   111  	return "", m.handle(basicMsg, ctx)
   112  }