Skip to content

Commit

Permalink
feat: use computed tags for records (#313)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Tags on a record can now be accessed using the `getTags()` method. Records should be updated with this method and return the properties from the record to include in the tags.

Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Jun 23, 2021
1 parent 6a26337 commit 4e9a48b
Show file tree
Hide file tree
Showing 29 changed files with 417 additions and 208 deletions.
53 changes: 23 additions & 30 deletions src/__tests__/credentials.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('credentials', () => {

testLogger.test('Faber waits for credential proposal from Alice')
let faberCredentialRecord = await waitForCredentialRecord(faberAgent, {
threadId: aliceCredentialRecord.tags.threadId,
threadId: aliceCredentialRecord.threadId,
state: CredentialState.ProposalReceived,
})

Expand All @@ -121,7 +121,7 @@ describe('credentials', () => {

testLogger.test('Alice waits for credential offer from Faber')
aliceCredentialRecord = await waitForCredentialRecord(aliceAgent, {
threadId: faberCredentialRecord.tags.threadId,
threadId: faberCredentialRecord.threadId,
state: CredentialState.OfferReceived,
})

Expand Down Expand Up @@ -153,9 +153,10 @@ describe('credentials', () => {

// below values are not in json object
expect(aliceCredentialRecord.id).not.toBeNull()
expect(aliceCredentialRecord.tags).toEqual({
threadId: faberCredentialRecord.tags.threadId,
connectionId: aliceCredentialRecord.tags.connectionId,
expect(aliceCredentialRecord.getTags()).toEqual({
threadId: faberCredentialRecord.threadId,
connectionId: aliceCredentialRecord.connectionId,
state: aliceCredentialRecord.state,
})
expect(aliceCredentialRecord.type).toBe(CredentialRecord.name)

Expand All @@ -164,7 +165,7 @@ describe('credentials', () => {

testLogger.test('Faber waits for credential request from Alice')
faberCredentialRecord = await waitForCredentialRecord(faberAgent, {
threadId: aliceCredentialRecord.tags.threadId,
threadId: aliceCredentialRecord.threadId,
state: CredentialState.RequestReceived,
})

Expand All @@ -173,7 +174,7 @@ describe('credentials', () => {

testLogger.test('Alice waits for credential from Faber')
aliceCredentialRecord = await waitForCredentialRecord(aliceAgent, {
threadId: faberCredentialRecord.tags.threadId,
threadId: faberCredentialRecord.threadId,
state: CredentialState.CredentialReceived,
})

Expand All @@ -182,18 +183,16 @@ describe('credentials', () => {

testLogger.test('Faber waits for credential ack from Alice')
faberCredentialRecord = await waitForCredentialRecord(faberAgent, {
threadId: faberCredentialRecord.tags.threadId,
threadId: faberCredentialRecord.threadId,
state: CredentialState.Done,
})

expect(aliceCredentialRecord).toMatchObject({
type: CredentialRecord.name,
id: expect.any(String),
createdAt: expect.any(Date),
tags: {
threadId: expect.any(String),
connectionId: expect.any(String),
},
threadId: expect.any(String),
connectionId: expect.any(String),
offerMessage: expect.any(Object),
requestMessage: expect.any(Object),
metadata: {
Expand All @@ -209,10 +208,8 @@ describe('credentials', () => {
type: CredentialRecord.name,
id: expect.any(String),
createdAt: expect.any(Date),
tags: {
threadId: expect.any(String),
connectionId: expect.any(String),
},
threadId: expect.any(String),
connectionId: expect.any(String),
metadata: {
schemaId,
credentialDefinitionId: credDefId,
Expand All @@ -233,7 +230,7 @@ describe('credentials', () => {

testLogger.test('Alice waits for credential offer from Faber')
aliceCredentialRecord = await waitForCredentialRecord(aliceAgent, {
threadId: faberCredentialRecord.tags.threadId,
threadId: faberCredentialRecord.threadId,
state: CredentialState.OfferReceived,
})

Expand Down Expand Up @@ -265,9 +262,10 @@ describe('credentials', () => {

// below values are not in json object
expect(aliceCredentialRecord.id).not.toBeNull()
expect(aliceCredentialRecord.tags).toEqual({
threadId: faberCredentialRecord.tags.threadId,
expect(aliceCredentialRecord.getTags()).toEqual({
threadId: faberCredentialRecord.threadId,
connectionId: aliceConnection.id,
state: aliceCredentialRecord.state,
})
expect(aliceCredentialRecord.type).toBe(CredentialRecord.name)

Expand All @@ -276,7 +274,7 @@ describe('credentials', () => {

testLogger.test('Faber waits for credential request from Alice')
faberCredentialRecord = await waitForCredentialRecord(faberAgent, {
threadId: aliceCredentialRecord.tags.threadId,
threadId: aliceCredentialRecord.threadId,
state: CredentialState.RequestReceived,
})

Expand All @@ -285,7 +283,7 @@ describe('credentials', () => {

testLogger.test('Alice waits for credential from Faber')
aliceCredentialRecord = await waitForCredentialRecord(aliceAgent, {
threadId: faberCredentialRecord.tags.threadId,
threadId: faberCredentialRecord.threadId,
state: CredentialState.CredentialReceived,
})

Expand All @@ -294,36 +292,31 @@ describe('credentials', () => {

testLogger.test('Faber waits for credential ack from Alice')
faberCredentialRecord = await waitForCredentialRecord(faberAgent, {
threadId: faberCredentialRecord.tags.threadId,
threadId: faberCredentialRecord.threadId,
state: CredentialState.Done,
})

expect(aliceCredentialRecord).toMatchObject({
type: CredentialRecord.name,
id: expect.any(String),
createdAt: expect.any(Date),
tags: {
threadId: expect.any(String),
connectionId: expect.any(String),
},
offerMessage: expect.any(Object),
requestMessage: expect.any(Object),
metadata: { requestMetadata: expect.any(Object) },
credentialId: expect.any(String),
state: CredentialState.Done,
threadId: expect.any(String),
})

expect(faberCredentialRecord).toMatchObject({
type: CredentialRecord.name,
id: expect.any(String),
createdAt: expect.any(Date),
tags: {
threadId: expect.any(String),
connectionId: expect.any(String),
},
offerMessage: expect.any(Object),
requestMessage: expect.any(Object),
state: CredentialState.Done,
threadId: expect.any(String),
connectionId: expect.any(String),
})
})
})
26 changes: 12 additions & 14 deletions src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Agent } from '../agent/Agent'
import type { BasicMessage, BasicMessageReceivedEvent } from '../modules/basic-messages'
import type { ConnectionStorageProps } from '../modules/connections'
import type { ConnectionRecordProps } from '../modules/connections'
import type { CredentialRecord, CredentialOfferTemplate, CredentialStateChangedEvent } from '../modules/credentials'
import type { SchemaTemplate, CredentialDefinitionTemplate } from '../modules/ledger'
import type { ProofRecord, ProofState, ProofStateChangedEvent } from '../modules/proofs'
Expand Down Expand Up @@ -66,7 +66,7 @@ export async function waitForProofRecord(
return new Promise((resolve) => {
const listener = (event: ProofStateChangedEvent) => {
const previousStateMatches = previousState === undefined || event.payload.previousState === previousState
const threadIdMatches = threadId === undefined || event.payload.proofRecord.tags.threadId === threadId
const threadIdMatches = threadId === undefined || event.payload.proofRecord.threadId === threadId
const stateMatches = state === undefined || event.payload.proofRecord.state === state

if (previousStateMatches && threadIdMatches && stateMatches) {
Expand Down Expand Up @@ -95,7 +95,7 @@ export async function waitForCredentialRecord(
return new Promise((resolve) => {
const listener = (event: CredentialStateChangedEvent) => {
const previousStateMatches = previousState === undefined || event.payload.previousState === previousState
const threadIdMatches = threadId === undefined || event.payload.credentialRecord.tags.threadId === threadId
const threadIdMatches = threadId === undefined || event.payload.credentialRecord.threadId === threadId
const stateMatches = state === undefined || event.payload.credentialRecord.state === state

if (previousStateMatches && threadIdMatches && stateMatches) {
Expand All @@ -109,16 +109,12 @@ export async function waitForCredentialRecord(
})
}

export async function waitForBasicMessage(
agent: Agent,
{ verkey, content }: { verkey?: string; content?: string }
): Promise<BasicMessage> {
export async function waitForBasicMessage(agent: Agent, { content }: { content?: string }): Promise<BasicMessage> {
return new Promise((resolve) => {
const listener = (event: BasicMessageReceivedEvent) => {
const verkeyMatches = verkey === undefined || event.payload.verkey === verkey
const contentMatches = content === undefined || event.payload.message.content === content

if (verkeyMatches && contentMatches) {
if (contentMatches) {
agent.events.off<BasicMessageReceivedEvent>(BasicMessageEventTypes.BasicMessageReceived, listener)

resolve(event.payload.message)
Expand Down Expand Up @@ -183,6 +179,7 @@ export function getMockConnection({
role = ConnectionRole.Invitee,
id = 'test',
did = 'test-did',
threadId = 'threadId',
verkey = 'key-1',
didDoc = new DidDoc({
id: did,
Expand Down Expand Up @@ -215,10 +212,11 @@ export function getMockConnection({
}),
],
}),
}: Partial<ConnectionStorageProps> = {}) {
}: Partial<ConnectionRecordProps> = {}) {
return new ConnectionRecord({
did,
didDoc,
threadId,
theirDid,
theirDidDoc,
id,
Expand Down Expand Up @@ -284,28 +282,28 @@ export async function issueCredential({
let issuerCredentialRecord = await issuerAgent.credentials.offerCredential(issuerConnectionId, credentialTemplate)

let holderCredentialRecord = await waitForCredentialRecord(holderAgent, {
threadId: issuerCredentialRecord.tags.threadId,
threadId: issuerCredentialRecord.threadId,
state: CredentialState.OfferReceived,
})

holderCredentialRecord = await holderAgent.credentials.acceptOffer(holderCredentialRecord.id)

issuerCredentialRecord = await waitForCredentialRecord(issuerAgent, {
threadId: holderCredentialRecord.tags.threadId,
threadId: holderCredentialRecord.threadId,
state: CredentialState.RequestReceived,
})

issuerCredentialRecord = await issuerAgent.credentials.acceptRequest(issuerCredentialRecord.id)

holderCredentialRecord = await waitForCredentialRecord(holderAgent, {
threadId: issuerCredentialRecord.tags.threadId,
threadId: issuerCredentialRecord.threadId,
state: CredentialState.CredentialReceived,
})

holderCredentialRecord = await holderAgent.credentials.acceptCredential(holderCredentialRecord.id)

issuerCredentialRecord = await waitForCredentialRecord(issuerAgent, {
threadId: issuerCredentialRecord.tags.threadId,
threadId: issuerCredentialRecord.threadId,
state: CredentialState.Done,
})

Expand Down
14 changes: 7 additions & 7 deletions src/__tests__/proofs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('Present Proof', () => {

testLogger.test('Faber waits for presentation proposal from Alice')
let faberProofRecord = await waitForProofRecord(faberAgent, {
threadId: aliceProofRecord.tags.threadId,
threadId: aliceProofRecord.threadId,
state: ProofState.ProposalReceived,
})

Expand All @@ -144,7 +144,7 @@ describe('Present Proof', () => {

testLogger.test('Alice waits for presentation request from Faber')
aliceProofRecord = await waitForProofRecord(aliceAgent, {
threadId: aliceProofRecord.tags.threadId,
threadId: aliceProofRecord.threadId,
state: ProofState.RequestReceived,
})

Expand All @@ -159,7 +159,7 @@ describe('Present Proof', () => {

testLogger.test('Faber waits for presentation from Alice')
faberProofRecord = await waitForProofRecord(faberAgent, {
threadId: aliceProofRecord.tags.threadId,
threadId: aliceProofRecord.threadId,
state: ProofState.PresentationReceived,
})

Expand All @@ -171,7 +171,7 @@ describe('Present Proof', () => {

// Alice waits till it receives presentation ack
aliceProofRecord = await waitForProofRecord(aliceAgent, {
threadId: aliceProofRecord.tags.threadId,
threadId: aliceProofRecord.threadId,
state: ProofState.Done,
})
})
Expand Down Expand Up @@ -211,7 +211,7 @@ describe('Present Proof', () => {

testLogger.test('Alice waits for presentation request from Faber')
let aliceProofRecord = await waitForProofRecord(aliceAgent, {
threadId: faberProofRecord.tags.threadId,
threadId: faberProofRecord.threadId,
state: ProofState.RequestReceived,
})

Expand All @@ -226,7 +226,7 @@ describe('Present Proof', () => {

testLogger.test('Faber waits for presentation from Alice')
faberProofRecord = await waitForProofRecord(faberAgent, {
threadId: aliceProofRecord.tags.threadId,
threadId: aliceProofRecord.threadId,
state: ProofState.PresentationReceived,
})

Expand All @@ -238,7 +238,7 @@ describe('Present Proof', () => {

// Alice waits till it receives presentation ack
aliceProofRecord = await waitForProofRecord(aliceAgent, {
threadId: aliceProofRecord.tags.threadId,
threadId: aliceProofRecord.threadId,
state: ProofState.Done,
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/modules/basic-messages/BasicMessageEvents.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BaseEvent } from '../../agent/Events'
import type { BasicMessage } from './messages'
import type { Verkey } from 'indy-sdk'
import type { BasicMessageRecord } from './repository'

export enum BasicMessageEventTypes {
BasicMessageReceived = 'BasicMessageReceived',
Expand All @@ -10,6 +10,6 @@ export interface BasicMessageReceivedEvent extends BaseEvent {
type: typeof BasicMessageEventTypes.BasicMessageReceived
payload: {
message: BasicMessage
verkey: Verkey
basicMessageRecord: BasicMessageRecord
}
}
4 changes: 4 additions & 0 deletions src/modules/basic-messages/BasicMessageRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum BasicMessageRole {
Sender = 'sender',
Receiver = 'receiver',
}
Loading

0 comments on commit 4e9a48b

Please sign in to comment.