This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
invitation_impl.go
140 lines (115 loc) · 3.61 KB
/
invitation_impl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package v0
import (
"encoding/gob"
"github.com/findy-network/findy-agent/agent/aries"
"github.com/findy-network/findy-agent/agent/didcomm"
"github.com/findy-network/findy-agent/agent/pltype"
"github.com/findy-network/findy-agent/agent/psm"
"github.com/findy-network/findy-agent/agent/service"
"github.com/findy-network/findy-agent/core"
"github.com/findy-network/findy-agent/std/decorator"
"github.com/findy-network/findy-agent/std/didexchange"
"github.com/findy-network/findy-common-go/dto"
"github.com/findy-network/findy-common-go/std/didexchange/invitation"
"github.com/golang/glog"
)
var invitationCreator = &invitationFactor{}
type invitationFactor struct{}
func (f *invitationFactor) NewMsg(init didcomm.MsgInit) didcomm.MessageHdr {
inv, err := invitation.Create(invitation.DIDExchangeVersionV0, invitation.AgentInfo{
InvitationID: init.AID,
InvitationType: init.Type,
})
if err != nil {
glog.Warningf("invitation creation failed %s", err.Error())
return nil
}
return newInvitation(inv)
}
func (f *invitationFactor) NewMessage(data []byte) didcomm.MessageHdr {
return newInvitationMsg(data)
}
func init() {
gob.Register(&invitationImpl{})
aries.Creator.Add(pltype.AriesConnectionInvitation, invitationCreator)
aries.Creator.Add(pltype.DIDOrgAriesConnectionInvitation, invitationCreator)
}
type invitationImpl struct {
didexchange.UnsupportedPwMsgBase
invitation.Invitation
thread *decorator.Thread
}
func newInvitation(inv invitation.Invitation) *invitationImpl {
impl := &invitationImpl{Invitation: inv, thread: &decorator.Thread{}}
impl.checkThread()
return impl
}
func (m *invitationImpl) checkThread() {
m.thread = decorator.CheckThread(m.thread, m.Invitation.ID())
}
func newInvitationMsg(data []byte) *invitationImpl {
dataStr := string(data)
inv, err := invitation.Translate(dataStr)
if err != nil {
glog.Warningf("invitation translation failed %s", dataStr)
return nil
}
impl := newInvitation(inv)
impl.checkThread()
return impl
}
func (m *invitationImpl) Thread() *decorator.Thread {
return m.thread
}
func (m *invitationImpl) ID() string {
return m.Invitation.ID()
}
func (m *invitationImpl) Type() string {
return m.Invitation.Type()
}
func (m *invitationImpl) JSON() []byte {
return dto.ToJSONBytes(m)
}
func (m *invitationImpl) FieldObj() interface{} {
return m.Invitation
}
func (m *invitationImpl) Label() string {
return m.Invitation.Label()
}
func (m *invitationImpl) VerKey() string {
return m.Invitation.Services()[0].RecipientKeysAsB58()[0]
}
func (m *invitationImpl) Endpoint() service.Addr {
return service.Addr{
Endp: m.Invitation.Services()[0].ServiceEndpoint,
Key: m.VerKey(),
}
}
func (m *invitationImpl) RoutingKeys() []string {
return m.Invitation.Services()[0].RoutingKeysAsB58()
}
func (m *invitationImpl) Verify(_ core.DID) error {
return nil
}
func (m *invitationImpl) PayloadToSend(ourLabel string, ourDID core.DID) (didcomm.Payload, psm.SubState, error) {
// build a connection request message to send to another agent
msg := newRequest(&Request{
Label: ourLabel,
Connection: &Connection{
DID: ourDID.Did(),
DIDDoc: ourDID.DOC(),
},
// when out-of-bound and did-exchange protocols are supported we
// should start to save connection_id to Thread.PID
Thread: &decorator.Thread{ID: m.thread.ID},
})
// Create payload to send
return aries.PayloadCreator.NewMsg(m.thread.ID, pltype.AriesConnectionRequest, msg), psm.Sending, nil
}
func (m *invitationImpl) PayloadToWait() (didcomm.Payload, psm.SubState) {
return aries.PayloadCreator.New(
didcomm.PayloadInit{
ID: m.thread.ID,
Type: pltype.AriesConnectionRequest,
}), psm.Waiting
}