Skip to content

Commit

Permalink
feat: add issue credential v2 (#745)
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Richardson <mike.richardson@northernblock.io>
  • Loading branch information
NB-MikeRichardson committed May 6, 2022
1 parent 2da845d commit 245223a
Show file tree
Hide file tree
Showing 126 changed files with 10,269 additions and 4,277 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ coverage
logs.txt
logs/
packages/core/src/__tests__/genesis-von.txn
lerna-debug.log
lerna-debug.log
runcredtests.sh
runtests.sh
9 changes: 5 additions & 4 deletions demo/src/Alice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*eslint import/no-cycle: [2, { maxDepth: 1 }]*/
import type { CredentialRecord, ProofRecord } from '@aries-framework/core'
import type { CredentialExchangeRecord, ProofRecord } from '@aries-framework/core'

import { BaseAgent } from './BaseAgent'
import { greenText, Output, redText } from './OutputClass'
Expand Down Expand Up @@ -53,9 +53,10 @@ export class Alice extends BaseAgent {
await this.waitForConnection()
}

public async acceptCredentialOffer(credentialRecord: CredentialRecord) {
await this.agent.credentials.acceptOffer(credentialRecord.id)
console.log(greenText('\nCredential offer accepted!\n'))
public async acceptCredentialOffer(credentialRecord: CredentialExchangeRecord) {
await this.agent.credentials.acceptOffer({
credentialRecordId: credentialRecord.id,
})
}

public async acceptProofRequest(proofRecord: ProofRecord) {
Expand Down
4 changes: 2 additions & 2 deletions demo/src/AliceInquirer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CredentialRecord, ProofRecord } from '@aries-framework/core'
import type { CredentialExchangeRecord, ProofRecord } from '@aries-framework/core'

import { clear } from 'console'
import { textSync } from 'figlet'
Expand Down Expand Up @@ -69,7 +69,7 @@ export class AliceInquirer extends BaseInquirer {
await this.processAnswer()
}

public async acceptCredentialOffer(credentialRecord: CredentialRecord) {
public async acceptCredentialOffer(credentialRecord: CredentialExchangeRecord) {
const confirm = await inquirer.prompt([this.inquireConfirmation(Title.CredentialOfferTitle)])
if (confirm.options === ConfirmOptions.No) {
await this.alice.agent.credentials.declineOffer(credentialRecord.id)
Expand Down
31 changes: 22 additions & 9 deletions demo/src/Faber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import type { ConnectionRecord } from '@aries-framework/core'
import type { CredDef, Schema } from 'indy-sdk'
import type BottomBar from 'inquirer/lib/ui/bottom-bar'

import { AttributeFilter, CredentialPreview, ProofAttributeInfo, utils } from '@aries-framework/core'
import {
CredentialProtocolVersion,
V1CredentialPreview,
AttributeFilter,
ProofAttributeInfo,
utils,
} from '@aries-framework/core'
import { ui } from 'inquirer'

import { BaseAgent } from './BaseAgent'
Expand Down Expand Up @@ -62,23 +68,23 @@ export class Faber extends BaseAgent {
this.printSchema(schemaTemplate.name, schemaTemplate.version, schemaTemplate.attributes)
this.ui.updateBottomBar(greenText('\nRegistering schema...\n', false))
const schema = await this.agent.ledger.registerSchema(schemaTemplate)
this.ui.updateBottomBar('\nSchema registerd!\n')
this.ui.updateBottomBar('\nSchema registered!\n')
return schema
}

private async registerCredentialDefiniton(schema: Schema) {
private async registerCredentialDefinition(schema: Schema) {
this.ui.updateBottomBar('\nRegistering credential definition...\n')
this.credentialDefinition = await this.agent.ledger.registerCredentialDefinition({
schema,
tag: 'latest',
supportRevocation: false,
})
this.ui.updateBottomBar('\nCredential definition registerd!!\n')
this.ui.updateBottomBar('\nCredential definition registered!!\n')
return this.credentialDefinition
}

private getCredentialPreview() {
const credentialPreview = CredentialPreview.fromRecord({
const credentialPreview = V1CredentialPreview.fromRecord({
name: 'Alice Smith',
degree: 'Computer Science',
date: '01/01/2022',
Expand All @@ -88,14 +94,21 @@ export class Faber extends BaseAgent {

public async issueCredential() {
const schema = await this.registerSchema()
const credDef = await this.registerCredentialDefiniton(schema)
const credDef = await this.registerCredentialDefinition(schema)
const credentialPreview = this.getCredentialPreview()
const connectionRecord = await this.getConnectionRecord()

this.ui.updateBottomBar('\nSending credential offer...\n')
await this.agent.credentials.offerCredential(connectionRecord.id, {
credentialDefinitionId: credDef.id,
preview: credentialPreview,

await this.agent.credentials.offerCredential({
connectionId: connectionRecord.id,
protocolVersion: CredentialProtocolVersion.V1,
credentialFormats: {
indy: {
attributes: credentialPreview.attributes,
credentialDefinitionId: credDef.id,
},
},
})
this.ui.updateBottomBar(
`\nCredential offer sent!\n\nGo to the Alice agent to accept the credential offer\n\n${Color.Reset}`
Expand Down
6 changes: 3 additions & 3 deletions demo/src/Listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { FaberInquirer } from './FaberInquirer'
import type {
Agent,
BasicMessageStateChangedEvent,
CredentialRecord,
CredentialExchangeRecord,
CredentialStateChangedEvent,
ProofRecord,
ProofStateChangedEvent,
Expand Down Expand Up @@ -41,7 +41,7 @@ export class Listener {
this.on = false
}

private printCredentialAttributes(credentialRecord: CredentialRecord) {
private printCredentialAttributes(credentialRecord: CredentialExchangeRecord) {
if (credentialRecord.credentialAttributes) {
const attribute = credentialRecord.credentialAttributes
console.log('\n\nCredential preview:')
Expand All @@ -51,7 +51,7 @@ export class Listener {
}
}

private async newCredentialPrompt(credentialRecord: CredentialRecord, aliceInquirer: AliceInquirer) {
private async newCredentialPrompt(credentialRecord: CredentialExchangeRecord, aliceInquirer: AliceInquirer) {
this.printCredentialAttributes(credentialRecord)
this.turnListenerOn()
await aliceInquirer.acceptCredentialOffer(credentialRecord)
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/0-agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const agentConfig: InitConfig = {
isProduction: false,
},
],
logger: new ConsoleLogger(LogLevel.debug),
logger: new ConsoleLogger(LogLevel.info),
}

const agent = new Agent(agentConfig, agentDependencies)
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/7-logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ConsoleLogger, LogLevel } from '@aries-framework/core'

const agentConfig = {
// ... other config properties ...
logger: new ConsoleLogger(LogLevel.debug),
logger: new ConsoleLogger(LogLevel.info),
}
```

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@types/jest": "^26.0.23",
"@types/node": "^15.14.4",
"@types/uuid": "^8.3.1",
"@types/varint": "^6.0.0",
"@types/ws": "^7.4.6",
"@typescript-eslint/eslint-plugin": "^4.26.1",
"@typescript-eslint/parser": "^4.26.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export class Agent {
}

public async receiveMessage(inboundMessage: unknown, session?: TransportSession) {
return await this.messageReceiver.receiveMessage(inboundMessage, session)
await this.messageReceiver.receiveMessage(inboundMessage, session)
}

public get injectionContainer() {
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/agent/__tests__/Agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ConnectionsModule } from '../../modules/connections/ConnectionsModule'
import { ConnectionRepository } from '../../modules/connections/repository/ConnectionRepository'
import { ConnectionService } from '../../modules/connections/services/ConnectionService'
import { TrustPingService } from '../../modules/connections/services/TrustPingService'
import { CredentialRepository, CredentialService } from '../../modules/credentials'
import { CredentialRepository } from '../../modules/credentials'
import { CredentialsModule } from '../../modules/credentials/CredentialsModule'
import { IndyLedgerService } from '../../modules/ledger'
import { LedgerModule } from '../../modules/ledger/LedgerModule'
Expand Down Expand Up @@ -123,7 +123,6 @@ describe('Agent', () => {
expect(container.resolve(ProofRepository)).toBeInstanceOf(ProofRepository)

expect(container.resolve(CredentialsModule)).toBeInstanceOf(CredentialsModule)
expect(container.resolve(CredentialService)).toBeInstanceOf(CredentialService)
expect(container.resolve(CredentialRepository)).toBeInstanceOf(CredentialRepository)

expect(container.resolve(BasicMessagesModule)).toBeInstanceOf(BasicMessagesModule)
Expand Down Expand Up @@ -167,7 +166,6 @@ describe('Agent', () => {
expect(container.resolve(ProofRepository)).toBe(container.resolve(ProofRepository))

expect(container.resolve(CredentialsModule)).toBe(container.resolve(CredentialsModule))
expect(container.resolve(CredentialService)).toBe(container.resolve(CredentialService))
expect(container.resolve(CredentialRepository)).toBe(container.resolve(CredentialRepository))

expect(container.resolve(BasicMessagesModule)).toBe(container.resolve(BasicMessagesModule))
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/decorators/ack/AckDecorator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ describe('Decorators | AckDecoratorExtension', () => {
test('transforms AckDecorator class to JSON', () => {
const message = new TestMessage()
message.setPleaseAck()
expect(message.toJSON()).toEqual({ '~please_ack': {} })
expect(message.toJSON()).toEqual({
'@id': undefined,
'@type': undefined,
'~please_ack': {
on: ['RECEIPT'],
},
})
})

test('transforms Json to AckDecorator class', () => {
Expand Down
19 changes: 18 additions & 1 deletion packages/core/src/decorators/ack/AckDecorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import { IsArray, IsEnum } from 'class-validator'

export enum AckValues {
Receipt = 'RECEIPT',
Outcome = 'OUTCOME',
}

/**
* Represents `~please_ack` decorator
*/
export class AckDecorator {}
export class AckDecorator {
public constructor(options: { on: [AckValues.Receipt] }) {
if (options) {
this.on = options.on
}
}

@IsEnum(AckValues, { each: true })
@IsArray()
public on!: AckValues[]
}
6 changes: 3 additions & 3 deletions packages/core/src/decorators/ack/AckDecoratorExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { BaseMessageConstructor } from '../../agent/BaseMessage'
import { Expose, Type } from 'class-transformer'
import { IsInstance, IsOptional, ValidateNested } from 'class-validator'

import { AckDecorator } from './AckDecorator'
import { AckDecorator, AckValues } from './AckDecorator'

export function AckDecorated<T extends BaseMessageConstructor>(Base: T) {
class AckDecoratorExtension extends Base {
Expand All @@ -14,8 +14,8 @@ export function AckDecorated<T extends BaseMessageConstructor>(Base: T) {
@IsOptional()
public pleaseAck?: AckDecorator

public setPleaseAck() {
this.pleaseAck = new AckDecorator()
public setPleaseAck(on?: [AckValues.Receipt]) {
this.pleaseAck = new AckDecorator({ on: on ?? [AckValues.Receipt] })
}

public getPleaseAck(): AckDecorator | undefined {
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/decorators/attachment/AttachmentExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ export function AttachmentDecorated<T extends BaseMessageConstructor>(Base: T) {
@ValidateNested()
@IsInstance(Attachment, { each: true })
@IsOptional()
public attachments?: Attachment[]
public appendedAttachments?: Attachment[]

public getAttachmentById(id: string): Attachment | undefined {
return this.attachments?.find((attachment) => attachment.id === id)
public getAppendedAttachmentById(id: string): Attachment | undefined {
return this.appendedAttachments?.find((attachment) => attachment.id === id)
}

public addAttachment(attachment: Attachment): void {
if (this.attachments) {
this.attachments?.push(attachment)
public addAppendedAttachment(attachment: Attachment): void {
if (this.appendedAttachments) {
this.appendedAttachments.push(attachment)
} else {
this.attachments = [attachment]
this.appendedAttachments = [attachment]
}
}
}
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/logger/ConsoleLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import { BaseLogger } from './BaseLogger'
import { LogLevel } from './Logger'

/*
* The replacer parameter allows you to specify a function that replaces values with your own. We can use it to control what gets stringified.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ConnectionProblemReportReason } from '.'
import type { ProblemReportErrorOptions } from '../../problem-reports'
import type { ConnectionProblemReportReason } from './ConnectionProblemReportReason'

import { ProblemReportError } from '../../problem-reports'
import { ConnectionProblemReportMessage } from '../messages'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ export class ConnectionService {
}

// Check if the inbound message recipient key is present
// in the recipientKeys of previously sent message ~service decorator
// in the recipientKeys of previously sent message ~service decorator()

if (
!previousSentMessage?.service ||
!previousSentMessage.service.recipientKeys.includes(messageContext.recipientVerkey)
Expand All @@ -522,6 +523,7 @@ export class ConnectionService {

// Check if the inbound message sender key is present
// in the recipientKeys of previously received message ~service decorator

if (
!previousReceivedMessage.service ||
!previousReceivedMessage.service.recipientKeys.includes(messageContext.senderVerkey)
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/modules/credentials/CredentialEvents.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BaseEvent } from '../../agent/Events'
import type { CredentialState } from './CredentialState'
import type { CredentialRecord } from './repository/CredentialRecord'
import type { CredentialExchangeRecord } from './repository/CredentialExchangeRecord'

export enum CredentialEventTypes {
CredentialStateChanged = 'CredentialStateChanged',
Expand All @@ -9,14 +9,14 @@ export enum CredentialEventTypes {
export interface CredentialStateChangedEvent extends BaseEvent {
type: typeof CredentialEventTypes.CredentialStateChanged
payload: {
credentialRecord: CredentialRecord
credentialRecord: CredentialExchangeRecord
previousState: CredentialState | null
}
}

export interface RevocationNotificationReceivedEvent extends BaseEvent {
type: typeof CredentialEventTypes.RevocationNotificationReceived
payload: {
credentialRecord: CredentialRecord
credentialRecord: CredentialExchangeRecord
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum CredentialProtocolVersion {
V1 = 'v1',
V2 = 'v2',
}
Loading

0 comments on commit 245223a

Please sign in to comment.