-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRequestPresentation.kt
229 lines (213 loc) · 8.86 KB
/
RequestPresentation.kt
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package io.iohk.atala.prism.walletsdk.prismagent.protocols.proofOfPresentation
import io.iohk.atala.prism.apollo.uuid.UUID
import io.iohk.atala.prism.walletsdk.domain.models.AttachmentDescriptor
import io.iohk.atala.prism.walletsdk.domain.models.DID
import io.iohk.atala.prism.walletsdk.domain.models.Message
import io.iohk.atala.prism.walletsdk.prismagent.GOAL_CODE
import io.iohk.atala.prism.walletsdk.prismagent.PROOF_TYPES
import io.iohk.atala.prism.walletsdk.prismagent.PrismAgentError
import io.iohk.atala.prism.walletsdk.prismagent.WILL_CONFIRM
import io.iohk.atala.prism.walletsdk.prismagent.protocols.ProtocolType
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
@Serializable
/**
* The `RequestPresentation` class represents a request for presentation of credentials or proofs in a DIDComm protocol.
*
* @property id The unique identifier of the request.
* @property body The content of the request.
* @property attachments The array of attachment descriptors associated with the request.
* @property thid The thread ID of the request message. Default value is `null`.
* @property from The DID of the sender of the request.
* @property to The DID of the recipient of the request.
*/
data class RequestPresentation(
val id: String = UUID.randomUUID4().toString(),
val body: Body,
val attachments: Array<AttachmentDescriptor>,
val thid: String? = null,
val from: DID,
val to: DID
) {
val type = ProtocolType.DidcommRequestPresentation.value
/**
* Creates a new [Message] object based on the current state of the [RequestPresentation] instance.
* The [Message] object contains information about the sender, recipient, message body, and other metadata.
* This method is typically used to convert a [RequestPresentation] instance into a [Message] object for communication purposes.
*
* @return The newly created [Message] object.
*/
fun makeMessage(): Message {
return Message(
id = this.id,
piuri = this.type,
from = this.from,
to = this.to,
body = Json.encodeToString(this.body),
attachments = this.attachments,
thid = this.thid
)
}
/**
* Checks if this [RequestPresentation] object is equal to the specified [other] object.
*
* Two [RequestPresentation] objects are considered equal if they meet the following conditions:
* - The two objects have the same class type.
* - The id, body, attachments, thid, from, to, and type properties of the two objects are also equal.
*
* @param other The object to compare with this [RequestPresentation] object.
* @return true if the specified [other] object is equal to this [RequestPresentation] object, false otherwise.
*/
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
other as RequestPresentation
if (id != other.id) return false
if (body != other.body) return false
if (!attachments.contentEquals(other.attachments)) return false
if (thid != other.thid) return false
if (from != other.from) return false
if (to != other.to) return false
return type == other.type
}
/**
* Calculates the hash code for the [RequestPresentation] object.
*
* The hash code is calculated based on the following properties of the [RequestPresentation] object:
* - [id]
* - [body]
* - [attachments]
* - [thid]
* - [from]
* - [to]
* - [type]
*
* @return The hash code value for the [RequestPresentation] object.
*/
override fun hashCode(): Int {
var result = id.hashCode()
result = 31 * result + body.hashCode()
result = 31 * result + attachments.contentHashCode()
result = 31 * result + (thid?.hashCode() ?: 0)
result = 31 * result + from.hashCode()
result = 31 * result + to.hashCode()
result = 31 * result + type.hashCode()
return result
}
companion object {
/**
* Converts a given [Message] object to a [RequestPresentation] object.
*
* @param fromMessage The [Message] object to convert.
* @return The converted [RequestPresentation] object.
* @throws PrismAgentError.InvalidMessageType if the [Message] object does not represent the correct protocol
* or if it is missing the "from" and "to" fields.
*/
@JvmStatic
@Throws(PrismAgentError.InvalidMessageType::class)
fun fromMessage(fromMessage: Message): RequestPresentation {
if (fromMessage.piuri == ProtocolType.DidcommRequestPresentation.value &&
fromMessage.from != null &&
fromMessage.to != null
) {
return RequestPresentation(
id = fromMessage.id,
body = Json.decodeFromString(fromMessage.body),
attachments = fromMessage.attachments,
thid = fromMessage.thid,
from = fromMessage.from,
to = fromMessage.to
)
} else {
throw PrismAgentError.InvalidMessageType(
type = fromMessage.piuri,
shouldBe = ProtocolType.DidcommRequestPresentation.value
)
}
}
/**
* Creates a [RequestPresentation] object based on the given [msg].
*
* @param msg The [Message] object representing a proposal.
* @return The newly created [RequestPresentation] object.
* @throws PrismAgentError.InvalidMessageType if the message type is invalid.
*/
@JvmStatic
@Throws(PrismAgentError.InvalidMessageType::class)
fun makeRequestFromProposal(msg: Message): RequestPresentation {
val request = ProposePresentation(msg)
return RequestPresentation(
body = Body(
goalCode = request.body.goalCode,
comment = request.body.comment,
willConfirm = false,
proofTypes = request.body.proofTypes
),
attachments = request.attachments,
thid = msg.id,
from = request.to,
to = request.from
)
}
}
/**
* Represents a class that encapsulates the body of a message.
*
* @property goalCode The goal code associated with the body.
* @property comment The comment associated with the body.
* @property willConfirm A boolean indicating whether confirmation is required.
* @property proofTypes An array of proof types.
*/
@Serializable
data class Body @JvmOverloads constructor(
@SerialName(GOAL_CODE)
val goalCode: String? = null,
val comment: String? = null,
@SerialName(WILL_CONFIRM)
val willConfirm: Boolean? = false,
@SerialName(PROOF_TYPES)
val proofTypes: Array<ProofTypes>
) {
/**
* Checks if this [Body] object is equal to the specified [other] object.
*
* Two [Body] objects are considered equal if they meet the following conditions:
* - The two objects have the same class type.
* - The goalCode, comment, willConfirm, and proofTypes properties of the two objects are also equal.
*
* @param other The object to compare with this [Body] object.
* @return true if the specified [other] object is equal to this [Body] object, false otherwise.
*/
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
other as Body
if (goalCode != other.goalCode) return false
if (comment != other.comment) return false
if (willConfirm != other.willConfirm) return false
if (!proofTypes.contentEquals(other.proofTypes)) return false
return true
}
/**
* Calculates the hash code for the current object.
*
* The hash code is calculated based on the values of the following properties:
* - goalCode
* - comment
* - willConfirm
* - proofTypes
*
* @return The hash code value for the current object.
*/
override fun hashCode(): Int {
var result = goalCode?.hashCode() ?: 0
result = 31 * result + (comment?.hashCode() ?: 0)
result = 31 * result + (willConfirm?.hashCode() ?: 0)
result = 31 * result + proofTypes.contentHashCode()
return result
}
}
}