Skip to content

Commit

Permalink
feat: add from record method to cred preview (#428)
Browse files Browse the repository at this point in the history
* feat: add from record method to cred preview

Signed-off-by: Timo Glastra <timo@animo.id>
  • Loading branch information
TimoGlastra authored Aug 20, 2021
1 parent 87bc589 commit 895f7d0
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ConnectionService } from '../../connections/services/ConnectionService'
import type { StoreCredentialOptions } from '../../indy/services/IndyHolderService'
import type { CredentialStateChangedEvent } from '../CredentialEvents'
import type { CredentialPreviewAttribute } from '../messages'
import type { CredentialRecordMetadata, CustomCredentialTags } from '../repository/CredentialRecord'
import type { CredentialOfferTemplate } from '../services'

Expand All @@ -21,7 +22,6 @@ import { CredentialUtils } from '../CredentialUtils'
import {
CredentialAckMessage,
CredentialPreview,
CredentialPreviewAttribute,
INDY_CREDENTIAL_ATTACHMENT_ID,
INDY_CREDENTIAL_OFFER_ATTACHMENT_ID,
INDY_CREDENTIAL_REQUEST_ATTACHMENT_ID,
Expand Down Expand Up @@ -52,19 +52,9 @@ const connection = getMockConnection({
state: ConnectionState.Complete,
})

const credentialPreview = new CredentialPreview({
attributes: [
new CredentialPreviewAttribute({
name: 'name',
mimeType: 'text/plain',
value: 'John',
}),
new CredentialPreviewAttribute({
name: 'age',
mimeType: 'text/plain',
value: '99',
}),
],
const credentialPreview = CredentialPreview.fromRecord({
name: 'John',
age: '99',
})

const offerAttachment = new Attachment({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,30 @@ export class CredentialPreview {
public toJSON(): Record<string, unknown> {
return JsonTransformer.toJSON(this)
}

/**
* Create a credential preview from a record with name and value entries.
*
* @example
* const preview = CredentialPreview.fromRecord({
* name: "Bob",
* age: "20"
* })
*/
public static fromRecord(record: Record<string, string>) {
const attributes = Object.entries(record).map(
([name, value]) =>
new CredentialPreviewAttribute({
name,
mimeType: 'text/plain',
value,
})
)

return new CredentialPreview({
attributes,
})
}
}

interface CredentialPreviewAttributeOptions {
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/modules/ledger/LedgerModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,19 @@ export class LedgerModule {
return this.ledgerService.getSchema(id)
}

public async registerCredentialDefinition(credentialDefinitionTemplate: CredentialDefinitionTemplate) {
public async registerCredentialDefinition(
credentialDefinitionTemplate: Omit<CredentialDefinitionTemplate, 'signatureType'>
) {
const did = this.wallet.publicDid?.did

if (!did) {
throw new AriesFrameworkError('Agent has no public DID.')
}

return this.ledgerService.registerCredentialDefinition(did, credentialDefinitionTemplate)
return this.ledgerService.registerCredentialDefinition(did, {
...credentialDefinitionTemplate,
signatureType: 'CL',
})
}

public async getCredentialDefinition(id: string) {
Expand Down
5 changes: 3 additions & 2 deletions packages/core/tests/connectionless-credentials.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { SubjectInboundTransporter } from '../../../tests/transport/SubjectInbou
import { SubjectOutboundTransporter } from '../../../tests/transport/SubjectOutboundTransport'
import { Agent } from '../src/agent/Agent'
import {
CredentialPreview,
AutoAcceptCredential,
CredentialEventTypes,
CredentialRecord,
CredentialState,
} from '../src/modules/credentials'

import { getBaseConfig, previewFromAttributes, prepareForIssuance, waitForCredentialRecordSubject } from './helpers'
import { getBaseConfig, prepareForIssuance, waitForCredentialRecordSubject } from './helpers'
import testLogger from './logger'

const faberConfig = getBaseConfig('Faber connection-less Credentials', {
Expand All @@ -24,7 +25,7 @@ const aliceConfig = getBaseConfig('Alice connection-less Credentials', {
endpoint: 'rxjs:alice',
})

const credentialPreview = previewFromAttributes({
const credentialPreview = CredentialPreview.fromRecord({
name: 'John',
age: '99',
})
Expand Down
8 changes: 4 additions & 4 deletions packages/core/tests/credentials-auto-accept.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type { Agent } from '../src/agent/Agent'
import type { ConnectionRecord } from '../src/modules/connections'

import { AutoAcceptCredential, CredentialRecord, CredentialState } from '../src/modules/credentials'
import { AutoAcceptCredential, CredentialPreview, CredentialRecord, CredentialState } from '../src/modules/credentials'
import { JsonTransformer } from '../src/utils/JsonTransformer'
import { sleep } from '../src/utils/sleep'

import { previewFromAttributes, setupCredentialTests, waitForCredentialRecord } from './helpers'
import { setupCredentialTests, waitForCredentialRecord } from './helpers'
import testLogger from './logger'

const credentialPreview = previewFromAttributes({
const credentialPreview = CredentialPreview.fromRecord({
name: 'John',
age: '99',
})

const newCredentialPreview = previewFromAttributes({
const newCredentialPreview = CredentialPreview.fromRecord({
name: 'John',
age: '99',
lastname: 'Appleseed',
Expand Down
23 changes: 4 additions & 19 deletions packages/core/tests/credentials.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,16 @@ import type { Agent } from '../src/agent/Agent'
import type { ConnectionRecord } from '../src/modules/connections'

import { Attachment, AttachmentData } from '../src/decorators/attachment/Attachment'
import {
CredentialPreview,
CredentialPreviewAttribute,
CredentialRecord,
CredentialState,
} from '../src/modules/credentials'
import { CredentialPreview, CredentialRecord, CredentialState } from '../src/modules/credentials'
import { JsonTransformer } from '../src/utils/JsonTransformer'
import { LinkedAttachment } from '../src/utils/LinkedAttachment'

import { setupCredentialTests, waitForCredentialRecord } from './helpers'
import testLogger from './logger'

const credentialPreview = new CredentialPreview({
attributes: [
new CredentialPreviewAttribute({
name: 'name',
mimeType: 'text/plain',
value: 'John',
}),
new CredentialPreviewAttribute({
name: 'age',
mimeType: 'text/plain',
value: '99',
}),
],
const credentialPreview = CredentialPreview.fromRecord({
name: 'John',
age: '99',
})

describe('credentials', () => {
Expand Down
15 changes: 1 addition & 14 deletions packages/core/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {
ConnectionState,
CredentialEventTypes,
CredentialPreview,
CredentialPreviewAttribute,
CredentialState,
DidCommService,
DidDoc,
Expand Down Expand Up @@ -284,18 +283,6 @@ export async function registerDefinition(
return credentialDefinition
}

export function previewFromAttributes(attributes: Record<string, string>): CredentialPreview {
return new CredentialPreview({
attributes: Object.entries(attributes).map(
([name, value]) =>
new CredentialPreviewAttribute({
name,
value,
})
),
})
}

export async function prepareForIssuance(agent: Agent, attributes: string[]) {
const publicDid = agent.publicDid?.did

Expand Down Expand Up @@ -558,7 +545,7 @@ export async function setupCredentialTests(
}

export async function setupProofsTest(faberName: string, aliceName: string, autoAcceptProofs?: AutoAcceptProof) {
const credentialPreview = previewFromAttributes({
const credentialPreview = CredentialPreview.fromRecord({
name: 'John',
age: '99',
})
Expand Down
11 changes: 3 additions & 8 deletions tests/e2e-test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import type { Agent } from '@aries-framework/core'

import {
issueCredential,
makeConnection,
prepareForIssuance,
presentProof,
previewFromAttributes,
} from '../packages/core/tests/helpers'
import { issueCredential, makeConnection, prepareForIssuance, presentProof } from '../packages/core/tests/helpers'

import {
CredentialPreview,
AttributeFilter,
CredentialState,
MediationState,
Expand Down Expand Up @@ -53,7 +48,7 @@ export async function e2eTest({
issuerConnectionId: senderRecipientConnection.id,
credentialTemplate: {
credentialDefinitionId: definition.id,
preview: previewFromAttributes({
preview: CredentialPreview.fromRecord({
name: 'John',
age: '25',
// year month day
Expand Down

0 comments on commit 895f7d0

Please sign in to comment.