github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/protocol/presentproof/params.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package presentproof 8 9 import ( 10 "encoding/json" 11 12 "github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service" 13 "github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/decorator" 14 ) 15 16 type rawPropose struct { 17 ProposePresentationV2 18 ProposePresentationV3 19 } 20 21 func (r *rawPropose) isV3() bool { 22 if r.ProposePresentationV2.notEmpty() { 23 return false 24 } 25 26 if r.ProposePresentationV3.notEmpty() { 27 return true 28 } 29 30 return false 31 } 32 33 func (p *ProposePresentationV2) notEmpty() bool { 34 return p.ID != "" || p.Type != "" || p.Comment != "" || len(p.Formats) != 0 || len(p.ProposalsAttach) != 0 35 } 36 37 func (p *ProposePresentationV3) notEmpty() bool { 38 return p.ID != "" || p.Type != "" || p.Body.Comment != "" || p.Body.GoalCode != "" || len(p.Attachments) != 0 39 } 40 41 // ProposePresentationParams holds the parameters for proposing a presentation. 42 type ProposePresentationParams struct { 43 // Comment is a field that provides some human readable information about the proposed presentation. 44 // TODO: Should follow DIDComm conventions for l10n. [Issue #1300] 45 Comment string 46 // Formats contains an entry for each proposal~attach array entry, including an optional value of the 47 // attachment @id (if attachments are present) and the verifiable presentation format and version of the attachment. 48 Formats []Format 49 GoalCode string 50 // Attachments is an array of attachments that further define the presentation request being proposed. 51 // This might be used to clarify which formats or format versions are wanted. 52 Attachments []decorator.GenericAttachment 53 } 54 55 // UnmarshalJSON implements json.Unmarshaler. 56 func (p *ProposePresentationParams) UnmarshalJSON(b []byte) error { 57 raw := rawPropose{} 58 59 err := json.Unmarshal(b, &raw) 60 if err != nil { 61 return err 62 } 63 64 if raw.isV3() { 65 p.FromV3(&raw.ProposePresentationV3) 66 } else { 67 p.FromV2(&raw.ProposePresentationV2) 68 } 69 70 return nil 71 } 72 73 // FromDIDCommMsgMap implements service.MsgMapDecoder. 74 func (p *ProposePresentationParams) FromDIDCommMsgMap(msgMap service.DIDCommMsgMap) error { 75 isV2, _ := service.IsDIDCommV2(&msgMap) // nolint:errcheck 76 if isV2 { 77 msgV3 := &ProposePresentationV3{} 78 79 err := msgMap.Decode(msgV3) 80 if err != nil { 81 return err 82 } 83 84 p.FromV3(msgV3) 85 } else { 86 msgV2 := &ProposePresentationV2{} 87 88 err := msgMap.Decode(msgV2) 89 if err != nil { 90 return err 91 } 92 93 p.FromV2(msgV2) 94 } 95 96 return nil 97 } 98 99 // AsV2 translates this presentation proposal into a present-proof 2.0 proposal message. 100 func (p *ProposePresentationParams) AsV2() *ProposePresentationV2 { 101 return &ProposePresentationV2{ 102 Type: ProposePresentationMsgTypeV2, 103 Comment: p.Comment, 104 Formats: p.Formats, 105 ProposalsAttach: decorator.GenericAttachmentsToV1(p.Attachments), 106 } 107 } 108 109 // AsV3 translates this presentation proposal into a present-proof 3.0 proposal message. 110 func (p *ProposePresentationParams) AsV3() *ProposePresentationV3 { 111 return &ProposePresentationV3{ 112 Type: ProposePresentationMsgTypeV3, 113 Body: ProposePresentationV3Body{ 114 GoalCode: p.GoalCode, 115 Comment: p.Comment, 116 }, 117 Attachments: decorator.GenericAttachmentsToV2(p.Attachments), 118 } 119 } 120 121 // FromV2 initializes this presentation proposal from a present-proof 2.0 proposal message. 122 func (p *ProposePresentationParams) FromV2(v2 *ProposePresentationV2) { 123 p.Comment = v2.Comment 124 p.Formats = v2.Formats 125 p.GoalCode = "" 126 p.Attachments = decorator.V1AttachmentsToGeneric(v2.ProposalsAttach) 127 } 128 129 // FromV3 initializes this presentation proposal from a present-proof 3.0 proposal message. 130 func (p *ProposePresentationParams) FromV3(v3 *ProposePresentationV3) { 131 p.Comment = v3.Body.Comment 132 p.Formats = nil 133 p.GoalCode = v3.Body.GoalCode 134 p.Attachments = decorator.V2AttachmentsToGeneric(v3.Attachments) 135 } 136 137 type rawRequest struct { 138 RequestPresentationV2 139 RequestPresentationV3 140 } 141 142 func (r *RequestPresentationV2) notEmpty() bool { 143 return r.ID != "" || 144 r.Type != "" || 145 r.Comment != "" || 146 len(r.Formats) != 0 || 147 len(r.RequestPresentationsAttach) != 0 148 } 149 150 func (r *RequestPresentationV3) notEmpty() bool { 151 return r.ID != "" || 152 r.Type != "" || 153 len(r.Attachments) != 0 || 154 r.Body.GoalCode != "" || 155 r.Body.Comment != "" 156 } 157 158 func (r *rawRequest) isV3() bool { 159 if r.RequestPresentationV2.notEmpty() { 160 return false 161 } 162 163 if r.RequestPresentationV3.notEmpty() { 164 return true 165 } 166 167 return false 168 } 169 170 // RequestPresentationParams holds the parameters for requesting a presentation. 171 type RequestPresentationParams struct { 172 // Comment is a field that provides some human readable information about the proposed presentation. 173 // TODO: Should follow DIDComm conventions for l10n. [Issue #1300] 174 Comment string 175 // WillConfirm is a field that defaults to "false" to indicate that the verifier will or will not 176 // send a post-presentation confirmation ack message. 177 WillConfirm bool 178 // Formats contains an entry for each request_presentations~attach array entry, providing the the value of the 179 // attachment @id and the verifiable presentation request format and version of the attachment. 180 Formats []Format 181 // Attachments is an array of attachments containing the acceptable verifiable presentation requests. 182 Attachments []decorator.GenericAttachment 183 // GoalCode is an optional goal code to indicate the desired use of the requested presentation. 184 GoalCode string 185 } 186 187 // UnmarshalJSON implements json.Unmarshaler. 188 func (p *RequestPresentationParams) UnmarshalJSON(b []byte) error { 189 raw := rawRequest{} 190 191 err := json.Unmarshal(b, &raw) 192 if err != nil { 193 return err 194 } 195 196 if raw.isV3() { 197 p.FromV3(&raw.RequestPresentationV3) 198 } else { 199 p.FromV2(&raw.RequestPresentationV2) 200 } 201 202 return nil 203 } 204 205 // FromDIDCommMsgMap implements service.MsgMapDecoder. 206 func (p *RequestPresentationParams) FromDIDCommMsgMap(msgMap service.DIDCommMsgMap) error { 207 isV2, _ := service.IsDIDCommV2(&msgMap) // nolint:errcheck 208 if isV2 { 209 msgV3 := &RequestPresentationV3{} 210 211 err := msgMap.Decode(msgV3) 212 if err != nil { 213 return err 214 } 215 216 p.FromV3(msgV3) 217 } else { 218 msgV2 := &RequestPresentationV2{} 219 220 err := msgMap.Decode(msgV2) 221 if err != nil { 222 return err 223 } 224 225 p.FromV2(msgV2) 226 } 227 228 return nil 229 } 230 231 // AsV2 translates this presentation request into a present-proof 2.0 request message. 232 func (p *RequestPresentationParams) AsV2() *RequestPresentationV2 { 233 return &RequestPresentationV2{ 234 Type: RequestPresentationMsgTypeV2, 235 Comment: p.Comment, 236 WillConfirm: p.WillConfirm, 237 Formats: p.Formats, 238 RequestPresentationsAttach: decorator.GenericAttachmentsToV1(p.Attachments), 239 } 240 } 241 242 // AsV3 translates this presentation request into a present-proof 3.0 request message. 243 func (p *RequestPresentationParams) AsV3() *RequestPresentationV3 { 244 return &RequestPresentationV3{ 245 Type: RequestPresentationMsgTypeV3, 246 Body: RequestPresentationV3Body{ 247 GoalCode: p.GoalCode, 248 Comment: p.Comment, 249 WillConfirm: p.WillConfirm, 250 }, 251 Attachments: decorator.GenericAttachmentsToV2(p.Attachments), 252 } 253 } 254 255 // FromV2 initializes this presentation request from a present-proof 2.0 request message. 256 func (p *RequestPresentationParams) FromV2(v2 *RequestPresentationV2) { 257 p.Comment = v2.Comment 258 p.WillConfirm = v2.WillConfirm 259 p.Formats = v2.Formats 260 p.Attachments = decorator.V1AttachmentsToGeneric(v2.RequestPresentationsAttach) 261 p.GoalCode = "" 262 } 263 264 // FromV3 initializes this presentation request from a present-proof 3.0 request message. 265 func (p *RequestPresentationParams) FromV3(v3 *RequestPresentationV3) { 266 p.Comment = v3.Body.Comment 267 p.WillConfirm = v3.Body.WillConfirm 268 p.Formats = nil 269 p.Attachments = decorator.V2AttachmentsToGeneric(v3.Attachments) 270 p.GoalCode = v3.Body.GoalCode 271 } 272 273 type rawPresentation struct { 274 PresentationV2 275 PresentationV3 276 } 277 278 func (p *PresentationV2) notEmpty() bool { 279 return p.ID != "" || 280 p.Type != "" || 281 p.Comment != "" || 282 len(p.Formats) != 0 || 283 len(p.PresentationsAttach) != 0 284 } 285 286 func (p *PresentationV3) notEmpty() bool { 287 return p.Type != "" || 288 len(p.Attachments) != 0 || 289 p.Body.GoalCode != "" || 290 p.Body.Comment != "" 291 } 292 293 func (r *rawPresentation) isV3() bool { 294 if r.PresentationV2.notEmpty() { 295 return false 296 } 297 298 if r.PresentationV3.notEmpty() { 299 return true 300 } 301 302 return false 303 } 304 305 // PresentationParams holds the parameters for providing a presentation. 306 type PresentationParams struct { 307 // Comment is a field that provides some human readable information about the provided presentation. 308 // TODO: Should follow DIDComm conventions for l10n. [Issue #1300]. 309 Comment string 310 // Formats contains an entry for each presentations~attach array entry, providing the the value of the attachment 311 // @id and the verifiable presentation format and version of the attachment. 312 Formats []Format 313 // Attachments is an array of attachments containing verifiable presentations. 314 Attachments []decorator.GenericAttachment 315 // GoalCode is an optional goal code to indicate the intended use of the provided presentation(s). 316 GoalCode string 317 } 318 319 // UnmarshalJSON implements json.Unmarshaler. 320 func (p *PresentationParams) UnmarshalJSON(b []byte) error { 321 raw := rawPresentation{} 322 323 err := json.Unmarshal(b, &raw) 324 if err != nil { 325 return err 326 } 327 328 if raw.isV3() { 329 p.FromV3(&raw.PresentationV3) 330 } else { 331 p.FromV2(&raw.PresentationV2) 332 } 333 334 return nil 335 } 336 337 // FromDIDCommMsgMap implements service.MsgMapDecoder. 338 func (p *PresentationParams) FromDIDCommMsgMap(msgMap service.DIDCommMsgMap) error { 339 isV2, _ := service.IsDIDCommV2(&msgMap) // nolint:errcheck 340 if isV2 { 341 msgV3 := &PresentationV3{} 342 343 err := msgMap.Decode(msgV3) 344 if err != nil { 345 return err 346 } 347 348 p.FromV3(msgV3) 349 } else { 350 msgV2 := &PresentationV2{} 351 352 err := msgMap.Decode(msgV2) 353 if err != nil { 354 return err 355 } 356 357 p.FromV2(msgV2) 358 } 359 360 return nil 361 } 362 363 // AsV2 translates this presentation message into a present-proof 2.0 presentation message. 364 func (p *PresentationParams) AsV2() *PresentationV2 { 365 return &PresentationV2{ 366 Type: PresentationMsgTypeV2, 367 Comment: p.Comment, 368 Formats: p.Formats, 369 PresentationsAttach: decorator.GenericAttachmentsToV1(p.Attachments), 370 } 371 } 372 373 // AsV3 translates this presentation message into a present-proof 3.0 presentation message. 374 func (p *PresentationParams) AsV3() *PresentationV3 { 375 return &PresentationV3{ 376 Type: PresentationMsgTypeV3, 377 Body: PresentationV3Body{ 378 GoalCode: p.GoalCode, 379 Comment: p.Comment, 380 }, 381 Attachments: decorator.GenericAttachmentsToV2(p.Attachments), 382 } 383 } 384 385 // FromV2 initializes this presentation message from a present-proof 2.0 presentation message. 386 func (p *PresentationParams) FromV2(v2 *PresentationV2) { 387 p.Comment = v2.Comment 388 p.Formats = v2.Formats 389 p.Attachments = decorator.V1AttachmentsToGeneric(v2.PresentationsAttach) 390 p.GoalCode = "" 391 } 392 393 // FromV3 initializes this presentation message from a present-proof 3.0 presentation message. 394 func (p *PresentationParams) FromV3(v3 *PresentationV3) { 395 p.Comment = v3.Body.Comment 396 p.Formats = nil 397 p.Attachments = decorator.V2AttachmentsToGeneric(v3.Attachments) 398 p.GoalCode = v3.Body.GoalCode 399 }