Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Accept goal and goalCode to create connection invitation #785

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import java.util.UUID
* @param updatedAt
* @param thid
* @param label
* @param goalCode
* @param goal
* @param role
* @param protocolState
* @param invitation
Expand All @@ -31,6 +33,8 @@ case class ConnectionRecord(
updatedAt: Option[Instant],
thid: String,
label: Option[String],
goalCode: Option[String],
goal: Option[String],
role: Role,
protocolState: ProtocolState,
invitation: Invitation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ trait ConnectionService {

def createConnectionInvitation(
label: Option[String],
goalCode: Option[String],
goal: Option[String],
pairwiseDID: DidId
): ZIO[WalletAccessContext, ConnectionServiceError, ConnectionRecord]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,21 @@ private class ConnectionServiceImpl(

override def createConnectionInvitation(
label: Option[String],
goalCode: Option[String],
goal: Option[String],
pairwiseDID: DidId
): ZIO[WalletAccessContext, ConnectionServiceError, ConnectionRecord] =
for {
invitation <- ZIO.succeed(ConnectionInvitation.makeConnectionInvitation(pairwiseDID))
invitation <- ZIO.succeed(ConnectionInvitation.makeConnectionInvitation(pairwiseDID, goalCode, goal))
record <- ZIO.succeed(
ConnectionRecord(
id = UUID.fromString(invitation.id),
createdAt = Instant.now,
updatedAt = None,
thid = invitation.id,
label = label,
goalCode = goalCode,
goal = goal,
role = ConnectionRecord.Role.Inviter,
protocolState = ConnectionRecord.ProtocolState.InvitationGenerated,
invitation = invitation,
Expand Down Expand Up @@ -129,6 +133,8 @@ private class ConnectionServiceImpl(
// TODO: According to the standard, we should rather use 'pthid' and not 'thid'
thid = invitation.id,
label = None,
goalCode = invitation.body.goal_code,
goal = invitation.body.goal,
role = ConnectionRecord.Role.Invitee,
protocolState = ConnectionRecord.ProtocolState.InvitationReceived,
invitation = invitation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ class ConnectionServiceNotifier(

override def createConnectionInvitation(
label: Option[String],
goalCode: Option[String],
goal: Option[String],
pairwiseDID: DidId
): ZIO[WalletAccessContext, ConnectionServiceError, ConnectionRecord] =
notifyOnSuccess(svc.createConnectionInvitation(label, pairwiseDID))
notifyOnSuccess(svc.createConnectionInvitation(label, goalCode, goal, pairwiseDID))

override def receiveConnectionInvitation(
invitation: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import java.util.UUID

object MockConnectionService extends Mock[ConnectionService] {

object CreateConnectionInvitation extends Effect[(Option[String], DidId), ConnectionServiceError, ConnectionRecord]
object CreateConnectionInvitation
extends Effect[(Option[String], Option[String], Option[String], DidId), ConnectionServiceError, ConnectionRecord]
object ReceiveConnectionInvitation extends Effect[String, ConnectionServiceError, ConnectionRecord]
object AcceptConnectionInvitation extends Effect[(UUID, DidId), ConnectionServiceError, ConnectionRecord]
object MarkConnectionRequestSent extends Effect[UUID, ConnectionServiceError, ConnectionRecord]
Expand All @@ -29,9 +30,11 @@ object MockConnectionService extends Mock[ConnectionService] {
} yield new ConnectionService {
override def createConnectionInvitation(
label: Option[String],
goalCode: Option[String],
goal: Option[String],
pairwiseDID: DidId
): IO[ConnectionServiceError, ConnectionRecord] =
proxy(CreateConnectionInvitation, label, pairwiseDID)
proxy(CreateConnectionInvitation, label, goalCode, goal, pairwiseDID)

override def receiveConnectionInvitation(invitation: String): IO[ConnectionServiceError, ConnectionRecord] =
proxy(ReceiveConnectionInvitation, invitation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ object ConnectionRepositorySpecSuite {
None,
UUID.randomUUID().toString,
None,
None,
None,
ConnectionRecord.Role.Inviter,
ConnectionRecord.ProtocolState.InvitationGenerated,
Invitation(
id = UUID.randomUUID().toString,
from = DidId("did:prism:aaa"),
body = Invitation
.Body(goal_code = "io.atalaprism.connect", goal = "Establish a trust connection between two peers", Nil)
.Body(
goal_code = Some("io.atalaprism.connect"),
goal = Some("Establish a trust connection between two peers"),
Nil
)
),
None,
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ object ConnectionServiceImplSpec extends ZIOSpecDefault {
for {
svc <- ZIO.service[ConnectionService]
did = DidId("did:peer:INVITER")
record <- svc.createConnectionInvitation(Some("Test connection invitation"), did)
record <- svc.createConnectionInvitation(
Some("Test connection invitation"),
Some("Test goal code"),
Some("Test goal"),
did
)
} yield {
assertTrue(record.label.contains("Test connection invitation")) &&
assertTrue(record.protocolState == ProtocolState.InvitationGenerated) &&
Expand All @@ -37,7 +42,8 @@ object ConnectionServiceImplSpec extends ZIOSpecDefault {
assertTrue(record.updatedAt.isEmpty) &&
assertTrue(record.invitation.from == did) &&
assertTrue(record.invitation.attachments.isEmpty) &&
assertTrue(record.invitation.body.goal_code == "io.atalaprism.connect") &&
assertTrue(record.invitation.body.goal_code == Some("Test goal code")) &&
assertTrue(record.invitation.body.goal == Some("Test goal")) &&
assertTrue(record.invitation.body.accept.isEmpty)
}
}, {
Expand All @@ -46,6 +52,8 @@ object ConnectionServiceImplSpec extends ZIOSpecDefault {
svc <- ZIO.service[ConnectionService]
createdRecord <- svc.createConnectionInvitation(
Some("Test connection invitation"),
Some("Test goal code"),
Some("Test goal"),
DidId("did:peer:INVITER")
)
foundRecord <- svc.getConnectionRecord(createdRecord.id)
Expand All @@ -61,10 +69,14 @@ object ConnectionServiceImplSpec extends ZIOSpecDefault {
svc <- ZIO.service[ConnectionService]
createdRecord1 <- svc.createConnectionInvitation(
Some("Test connection invitation #1"),
Some("Test goal code"),
Some("Test goal"),
DidId("did:peer:INVITER")
)
createdRecord2 <- svc.createConnectionInvitation(
Some("Test connection invitation #2"),
Some("Test goal code"),
Some("Test goal"),
DidId("did:peer:INVITER")
)
records <- svc.getConnectionRecords()
Expand All @@ -81,10 +93,14 @@ object ConnectionServiceImplSpec extends ZIOSpecDefault {
inviteeSvc <- ZIO.service[ConnectionService].provideLayer(connectionServiceLayer)
inviterRecord <- inviterSvc.createConnectionInvitation(
Some("Inviter"),
Some("Test goal code"),
Some("Test goal"),
DidId("did:peer:INVITER")
)
inviteeRecord <- inviteeSvc.createConnectionInvitation(
Some("Invitee"),
Some("Test goal code"),
Some("Test goal"),
DidId("did:peer:INVITEE")
)
allInviterRecords <- inviterSvc.getConnectionRecords()
Expand All @@ -103,6 +119,8 @@ object ConnectionServiceImplSpec extends ZIOSpecDefault {
inviteeSvc <- ZIO.service[ConnectionService].provideLayer(connectionServiceLayer)
inviterRecord <- inviterSvc.createConnectionInvitation(
Some("Test connection invitation"),
Some("Test goal code"),
Some("Test goal"),
DidId("did:peer:INVITER")
)
inviteeRecord <- inviteeSvc.receiveConnectionInvitation(inviterRecord.invitation.toBase64)
Expand Down Expand Up @@ -136,6 +154,8 @@ object ConnectionServiceImplSpec extends ZIOSpecDefault {
inviteeSvc <- ZIO.service[ConnectionService].provideLayer(connectionServiceLayer)
inviterRecord <- inviterSvc.createConnectionInvitation(
Some("Test connection invitation"),
Some("Test goal code"),
Some("Test goal"),
DidId("did:peer:INVITER")
)
inviteeRecord <- inviteeSvc.receiveConnectionInvitation(inviterRecord.invitation.toBase64)
Expand All @@ -159,6 +179,8 @@ object ConnectionServiceImplSpec extends ZIOSpecDefault {
inviteeSvc <- ZIO.service[ConnectionService].provideLayer(connectionServiceLayer)
inviterRecord <- inviterSvc.createConnectionInvitation(
Some("Test connection invitation"),
Some("Test goal code"),
Some("Test goal"),
DidId("did:peer:INVITER")
)
inviteeRecord <- inviteeSvc.receiveConnectionInvitation(inviterRecord.invitation.toBase64)
Expand Down Expand Up @@ -186,6 +208,8 @@ object ConnectionServiceImplSpec extends ZIOSpecDefault {
inviteeSvc <- ZIO.service[ConnectionService].provideLayer(connectionServiceLayer)
inviterRecord <- inviterSvc.createConnectionInvitation(
Some("Test connection invitation"),
Some("Test goal code"),
Some("Test goal"),
DidId("did:peer:INVITER")
)
inviteeRecord <- inviteeSvc.receiveConnectionInvitation(inviterRecord.invitation.toBase64)
Expand Down Expand Up @@ -213,6 +237,8 @@ object ConnectionServiceImplSpec extends ZIOSpecDefault {
inviteeSvc <- ZIO.service[ConnectionService].provideLayer(connectionServiceLayer)
inviterRecord <- inviterSvc.createConnectionInvitation(
Some("Test connection invitation"),
Some("Test goal code"),
Some("Test goal"),
DidId("did:peer:INVITER")
)
inviteeRecord <- inviteeSvc.receiveConnectionInvitation(inviterRecord.invitation.toBase64)
Expand Down Expand Up @@ -246,6 +272,8 @@ object ConnectionServiceImplSpec extends ZIOSpecDefault {
inviteeSvc <- ZIO.service[ConnectionService].provideLayer(connectionServiceLayer)
inviterRecord <- inviterSvc.createConnectionInvitation(
Some("Test connection invitation"),
Some("Test goal code"),
Some("Test goal"),
DidId("did:peer:INVITER")
)
inviteeRecord <- inviteeSvc.receiveConnectionInvitation(inviterRecord.invitation.toBase64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ object ConnectionServiceNotifierSpec extends ZIOSpecDefault {
None,
UUID.randomUUID().toString,
None,
None,
None,
ConnectionRecord.Role.Inviter,
ProtocolState.InvitationGenerated,
Invitation(from = DidId("did:peer:INVITER"), Invitation.Body("", "", Nil)),
Invitation(from = DidId("did:peer:INVITER"), Invitation.Body(None, None, Nil)),
None,
None,
5,
Expand Down Expand Up @@ -71,7 +73,12 @@ object ConnectionServiceNotifierSpec extends ZIOSpecDefault {
cs <- ZIO.service[ConnectionService]
ens <- ZIO.service[EventNotificationService]
did = DidId("did:peer:INVITER")
connectionRecord <- cs.createConnectionInvitation(Some("test"), did)
connectionRecord <- cs.createConnectionInvitation(
Some("test"),
Some("test-goal-code"),
Some("test-goal"),
did
)
_ <- cs.receiveConnectionRequest(
ConnectionRequest(
from = DidId("did:peer:INVITER"),
Expand Down Expand Up @@ -106,6 +113,8 @@ object ConnectionServiceNotifierSpec extends ZIOSpecDefault {
inviterDID = DidId("did:peer:INVITER")
inviterRecord <- inviterSvc.createConnectionInvitation(
Some("Test connection invitation"),
Some("Test goal code"),
Some("Test goal"),
inviterDID
)
inviteeSvc <- ZIO.service[ConnectionService]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

-- Introduce a goal and goal code for connection
ALTER TABLE public.connection_records
ADD COLUMN "goal_code" TEXT,
ADD COLUMN "goal" TEXT;
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ class JdbcConnectionRepository(xa: Transactor[ContextAwareTask], xb: Transactor[
| meta_retries,
| meta_next_retry,
| meta_last_failure,
| wallet_id
| wallet_id,
| goal_code,
| goal
| ) values (
| ${record.id},
| ${record.createdAt},
Expand All @@ -72,7 +74,9 @@ class JdbcConnectionRepository(xa: Transactor[ContextAwareTask], xb: Transactor[
| ${record.metaRetries},
| ${record.metaNextRetry},
| ${record.metaLastFailure},
| current_setting('app.current_wallet_id')::UUID
| current_setting('app.current_wallet_id')::UUID,
| ${record.goalCode},
| ${record.goal}
| )
""".stripMargin.update

Expand All @@ -92,6 +96,8 @@ class JdbcConnectionRepository(xa: Transactor[ContextAwareTask], xb: Transactor[
| updated_at,
| thid,
| label,
| goal_code,
| goal,
| role,
| protocol_state,
| invitation,
Expand Down Expand Up @@ -148,6 +154,8 @@ class JdbcConnectionRepository(xa: Transactor[ContextAwareTask], xb: Transactor[
| updated_at,
| thid,
| label,
| goal_code,
| goal,
| role,
| protocol_state,
| invitation,
Expand Down Expand Up @@ -175,6 +183,8 @@ class JdbcConnectionRepository(xa: Transactor[ContextAwareTask], xb: Transactor[
| updated_at,
| thid,
| label,
| goal_code,
| goal,
| role,
| protocol_state,
| invitation,
Expand Down Expand Up @@ -212,6 +222,8 @@ class JdbcConnectionRepository(xa: Transactor[ContextAwareTask], xb: Transactor[
| updated_at,
| thid,
| label,
| goal_code,
| goal,
| role,
| protocol_state,
| invitation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ object InvitationPrograms {
"https://didcomm.org/out-of-band/2.0/invitation",
getNewMsgId,
merdiator.id,
Body("request-mediate", "RequestMediate", Seq("didcomm/v2", "didcomm/aip2;env=rfc587"))
Body(Some("request-mediate"), Some("RequestMediate"), Seq("didcomm/v2", "didcomm/aip2;env=rfc587"))
)
_ <- ZIO.log(s"createInvitationV2 from '${merdiator.id}'")
result = invitation.asJson.deepDropNullValues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ object ConnectionInvitation {

/** Make a invitation to establish a trust connection between two peers */
def makeConnectionInvitation(from: DidId): Invitation = {
makeConnectionInvitation(
from = from,
goalCode = Some("io.atalaprism.connect"),
goal = Some(s"Establish a trust connection between two peers using the protocol '${ConnectionRequest.`type`}'")
)
}

def makeConnectionInvitation(from: DidId, goalCode: Option[String], goal: Option[String]): Invitation = {
Invitation(
from = from,
body = Invitation.Body(
goal_code = "io.atalaprism.connect",
goal = s"Establish a trust connection between two peers using the protocol '${ConnectionRequest.`type`}'",
goal_code = goalCode,
goal = goal,
Nil
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ object ConnectionRequest {
ConnectionRequest(
`type` = ConnectionRequest.`type`,
body = Body(
goal_code = Some(invitation.body.goal_code),
goal = Some(invitation.body.goal),
goal_code = invitation.body.goal_code,
goal = invitation.body.goal,
accept = Some(invitation.body.accept)
),
thid = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ final case class Invitation(
object Invitation {

final case class Body(
goal_code: String,
goal: String, // TODO goal can be optional
goal_code: Option[String],
goal: Option[String],
accept: Seq[String]
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class OutOfBandSpec extends FunSuite {
DidId(
"did:peer:2.Ez6LSmLmWmTvwjgLSuUaEQHdHSFWPwyibgzomWjFmnC6FhLnU.Vz6MktNgLh4N1u9KNhDiqe8KZ8bsLzLcqsifoNiUtBoSs9jxf.SeyJpZCI6Im5ldy1pZCIsInQiOiJkbSIsInMiOiJodHRwOi8vMTI3LjAuMC4xOjgwMDAiLCJhIjpbImRpZGNvbW0vdjIiXX0"
),
Body("request-mediate", "RequestMediate", Seq("didcomm/v2", "didcomm/aip2;env=rfc587")),
Body(Some("request-mediate"), Some("RequestMediate"), Seq("didcomm/v2", "didcomm/aip2;env=rfc587")),
)

assertEquals(ret, Right(expected))
Expand Down
Loading
Loading