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
/
propose.go
145 lines (115 loc) · 3.34 KB
/
propose.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
141
142
143
144
145
package issuecredential
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/std/decorator"
"github.com/findy-network/findy-common-go/dto"
"github.com/findy-network/findy-wrapper-go/anoncreds"
)
var ProposeCreator = &ProposeFactor{}
type ProposeFactor struct{}
func (f *ProposeFactor) NewMsg(init didcomm.MsgInit) didcomm.MessageHdr {
m := &Propose{
Type: init.Type,
ID: init.AID,
Comment: init.Info,
Thread: decorator.CheckThread(init.Thread, init.AID),
}
return NewPropose(m)
}
func (f *ProposeFactor) NewMessage(data []byte) didcomm.MessageHdr {
return NewProposeMsg(data)
}
func init() {
gob.Register(&ProposeImpl{})
aries.Creator.Add(pltype.IssueCredentialPropose, ProposeCreator)
aries.Creator.Add(pltype.DIDOrgIssueCredentialPropose, ProposeCreator)
}
func NewPropose(r *Propose) *ProposeImpl {
return &ProposeImpl{Propose: r}
}
func NewProposeMsg(data []byte) *ProposeImpl {
var mImpl ProposeImpl
dto.FromJSON(data, &mImpl)
mImpl.checkThread()
return &mImpl
}
// MARK: Helpers
// NewPreviewCredentialRaw creates a new PreviewCredential from JSON array which
// includes anoncreds.CredDefAttrs. NOTE! this is obsolete: for legacy protocol.
func NewPreviewCredentialRaw(values string) PreviewCredential {
type attrT map[string]anoncreds.CredDefAttr
array := make(attrT)
dto.FromJSONStr(values, &array)
var attrs = make([]Attribute, len(array))
i := 0
for key, value := range array {
a := Attribute{Name: key, Value: value.Raw}
attrs[i] = a
i++
}
return PreviewCredential{
Type: pltype.IssueCredentialCredentialPreview,
Attributes: attrs,
}
}
// NewPreviewCredential creates a new PreviewCredential from JSON array which
// includes Attributes as Name Value pairs.
func NewPreviewCredential(values string) PreviewCredential {
var attrs = make([]Attribute, 0, 4)
dto.FromJSONStr(values, &attrs)
return PreviewCredential{
Type: pltype.IssueCredentialCredentialPreview,
Attributes: attrs,
}
}
func PreviewCredentialToValues(credential PreviewCredential) string {
attrs := make([]Attribute, len(credential.Attributes))
for i, str := range credential.Attributes {
// take mime type away
a := Attribute{Name: str.Name, Value: str.Value}
attrs[i] = a
}
return dto.ToJSON(attrs)
}
func PreviewCredentialToCodedValues(credential PreviewCredential) string {
type attrT map[string]anoncreds.CredDefAttr
rMap := make(attrT)
for _, attr := range credential.Attributes {
a := anoncreds.CredDefAttr{}
a.SetRawAries(attr.Value)
rMap[attr.Name] = a
}
return dto.ToJSON(rMap)
}
func (p *ProposeImpl) checkThread() {
p.Propose.Thread = decorator.CheckThread(p.Propose.Thread, p.Propose.ID)
}
// MARK: Struct
type ProposeImpl struct {
*Propose
}
func (p *ProposeImpl) ID() string {
return p.Propose.ID
}
func (p *ProposeImpl) Type() string {
return p.Propose.Type
}
func (p *ProposeImpl) SetID(id string) {
p.Propose.ID = id
}
func (p *ProposeImpl) SetType(t string) {
p.Propose.Type = t
}
func (p *ProposeImpl) JSON() []byte {
return dto.ToJSONBytes(p)
}
func (p *ProposeImpl) Thread() *decorator.Thread {
//if p.Propose.Thread == nil {}
return p.Propose.Thread
}
func (p *ProposeImpl) FieldObj() interface{} {
return p.Propose
}