github.com/hyperledger/aries-framework-go@v0.3.2/pkg/client/introduce/client.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package introduce 8 9 import ( 10 "errors" 11 "fmt" 12 13 "github.com/hyperledger/aries-framework-go/pkg/client/outofband" 14 "github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service" 15 "github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/decorator" 16 "github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/introduce" 17 outofbandsvc "github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/outofband" 18 ) 19 20 type ( 21 // To introducee descriptor keeps information about the introduction. 22 To introduce.To 23 // PleaseIntroduceTo includes all field from To structure 24 // also it has Discovered the field which should be provided by help-me-discover protocol. 25 PleaseIntroduceTo introduce.PleaseIntroduceTo 26 // Recipient keeps information needed for the service 27 // 'To' field is needed for the proposal message 28 // 'MyDID' and 'TheirDID' fields are needed for sending messages e.g report-problem, proposal, ack etc. 29 Recipient introduce.Recipient 30 // Action contains helpful information about action. 31 Action introduce.Action 32 ) 33 34 // Provider contains dependencies for the introduce protocol and is typically created by using aries.Context(). 35 type Provider interface { 36 Service(id string) (interface{}, error) 37 } 38 39 // ProtocolService defines the introduce service. 40 type ProtocolService interface { 41 service.DIDComm 42 Actions() ([]introduce.Action, error) 43 ActionContinue(piID string, opt introduce.Opt) error 44 ActionStop(piID string, err error) error 45 } 46 47 // Client enable access to introduce API. 48 type Client struct { 49 service.Event 50 service ProtocolService 51 } 52 53 // New return new instance of introduce client. 54 func New(ctx Provider) (*Client, error) { 55 svc, err := ctx.Service(introduce.Introduce) 56 if err != nil { 57 return nil, err 58 } 59 60 introduceSvc, ok := svc.(ProtocolService) 61 if !ok { 62 return nil, errors.New("cast service to Introduce Service failed") 63 } 64 65 return &Client{ 66 Event: introduceSvc, 67 service: introduceSvc, 68 }, nil 69 } 70 71 // SendProposal sends a proposal to the introducees (the client has not published an out-of-band message). 72 func (c *Client) SendProposal(recipient1, recipient2 *Recipient) (string, error) { 73 _recipient1 := introduce.Recipient(*recipient1) 74 _recipient2 := introduce.Recipient(*recipient2) 75 76 proposal1 := introduce.CreateProposal(&_recipient1) 77 proposal2 := introduce.CreateProposal(&_recipient2) 78 79 introduce.WrapWithMetadataPIID(proposal1, proposal2) 80 81 _, err := c.service.HandleOutbound(proposal1, recipient1.MyDID, recipient1.TheirDID) 82 if err != nil { 83 return "", fmt.Errorf("handle outbound: %w", err) 84 } 85 86 return c.service.HandleOutbound(proposal2, recipient2.MyDID, recipient2.TheirDID) 87 } 88 89 // SendProposalWithOOBInvitation sends a proposal to the introducee (the client has published an out-of-band request). 90 func (c *Client) SendProposalWithOOBInvitation(inv *outofband.Invitation, recipient *Recipient) (string, error) { 91 _recipient := introduce.Recipient(*recipient) 92 _req := outofbandsvc.Invitation(*inv) 93 94 proposal := introduce.CreateProposal(&_recipient) 95 introduce.WrapWithMetadataPublicOOBInvitation(proposal, &_req) 96 97 return c.service.HandleOutbound(proposal, recipient.MyDID, recipient.TheirDID) 98 } 99 100 // SendRequest sends a request. 101 // Sending a request means that the introducee is willing to share their own out-of-band message. 102 func (c *Client) SendRequest(to *PleaseIntroduceTo, myDID, theirDID string) (string, error) { 103 _to := introduce.PleaseIntroduceTo(*to) 104 105 return c.service.HandleOutbound(service.NewDIDCommMsgMap(&introduce.Request{ 106 Type: introduce.RequestMsgType, 107 PleaseIntroduceTo: &_to, 108 }), myDID, theirDID) 109 } 110 111 // AcceptProposalWithOOBInvitation is used when introducee wants to provide an out-of-band request. 112 // Introducee can provide this request only after receiving ProposalMsgType. 113 func (c *Client) AcceptProposalWithOOBInvitation(piID string, inv *outofband.Invitation) error { 114 return c.service.ActionContinue(piID, WithOOBInvitation(inv)) 115 } 116 117 // AcceptProposal is used when introducee wants to accept a proposal without providing a OOBRequest. 118 func (c *Client) AcceptProposal(piID string) error { 119 return c.service.ActionContinue(piID, nil) 120 } 121 122 // AcceptRequestWithPublicOOBInvitation is used when introducer wants to provide a published out-of-band request. 123 // Introducer can provide invitation only after receiving RequestMsgType. 124 func (c *Client) AcceptRequestWithPublicOOBInvitation(piID string, inv *outofband.Invitation, to *To) error { 125 return c.service.ActionContinue(piID, WithPublicOOBInvitation(inv, to)) 126 } 127 128 // AcceptRequestWithRecipients is used when the introducer does not have a published out-of-band message on hand 129 // but he is willing to introduce agents to each other. 130 // Introducer can provide recipients only after receiving RequestMsgType. 131 func (c *Client) AcceptRequestWithRecipients(piID string, to *To, recipient *Recipient) error { 132 return c.service.ActionContinue(piID, WithRecipients(to, recipient)) 133 } 134 135 // DeclineProposal is used to reject the proposal. 136 // NOTE: For async usage. 137 func (c *Client) DeclineProposal(piID, reason string) error { 138 return c.service.ActionStop(piID, errors.New(reason)) 139 } 140 141 // DeclineRequest is used to reject the request. 142 // NOTE: For async usage. 143 func (c *Client) DeclineRequest(piID, reason string) error { 144 return c.service.ActionStop(piID, errors.New(reason)) 145 } 146 147 // AcceptProblemReport accepts problem report action. 148 func (c *Client) AcceptProblemReport(piID string) error { 149 return c.service.ActionContinue(piID, nil) 150 } 151 152 // Actions returns unfinished actions for the async usage. 153 func (c *Client) Actions() ([]Action, error) { 154 actions, err := c.service.Actions() 155 if err != nil { 156 return nil, err 157 } 158 159 result := make([]Action, len(actions)) 160 for i, action := range actions { 161 result[i] = Action(action) 162 } 163 164 return result, nil 165 } 166 167 // WithRecipients is used when the introducer does not have a published out-of-band message on hand 168 // but he is willing to introduce agents to each other. 169 // NOTE: Introducer can provide recipients only after receiving RequestMsgType. 170 // USAGE: event.Continue(WithRecipients(to, recipient)). 171 func WithRecipients(to *To, recipient *Recipient) introduce.Opt { 172 _to := introduce.To(*to) 173 _recipient := introduce.Recipient(*recipient) 174 175 return introduce.WithRecipients(&_to, &_recipient) 176 } 177 178 // WithPublicOOBInvitation is used when introducer wants to provide a published out-of-band request. 179 // NOTE: Introducer can provide this request only after receiving RequestMsgType 180 // USAGE: event.Continue(WithPublicOOBInvitation(req, to)). 181 func WithPublicOOBInvitation(req *outofband.Invitation, to *To) introduce.Opt { 182 _to := introduce.To(*to) 183 _req := outofbandsvc.Invitation(*req) 184 185 return introduce.WithPublicOOBInvitation(&_req, &_to) 186 } 187 188 // WithOOBInvitation is used when introducee wants to provide an out-of-band request with an optional 189 // series of attachments. 190 // NOTE: Introducee can provide the request only after receiving ProposalMsgType 191 // USAGE: event.Continue(WithOOBInvitation(inv)). 192 func WithOOBInvitation(req *outofband.Invitation, a ...*decorator.Attachment) introduce.Opt { 193 _req := outofbandsvc.Invitation(*req) 194 195 return introduce.WithOOBInvitation(&_req, a...) 196 }