Skip to content

Commit

Permalink
fix: Creating VC
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Jul 2, 2020
1 parent d86549c commit df40d7e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
2 changes: 1 addition & 1 deletion __tests__/createVerifiableCredential.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ describe('integration test for creating Verifiable Credentials', () => {
proofFormat: 'jwt',
})

expect(verifiableCredential.proof.jwt).toBeTruthy()
expect(verifiableCredential.proof.jwt).toBeDefined()
})
})
6 changes: 3 additions & 3 deletions __tests__/handleMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import fs from 'fs'

let agent: TAgent<IIdentityManager & IKeyManager & IDataStore & IResolveDid & IW3c>
let dbConnection: Promise<Connection>
const databaseFile = 'database.sqlite'
const databaseFile = 'database3.sqlite'
const JWT =
'eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NkstUiJ9.eyJpYXQiOjE1OTM0NTE3MDAsInR5cGUiOiJzZHIiLCJzdWJqZWN0IjoiZGlkOmV0aHI6cmlua2VieToweDM2MjQ2M2NiZTUyMjhjZTUwMGJlOGUwMzVjZGIyMWI3NzQ1ZjZkYjAiLCJ0YWciOiJzZHItb25lIiwiY2xhaW1zIjpbeyJyZWFzb24iOiJXZSBuZWVkIGl0IiwiY2xhaW1UeXBlIjoibmFtZSIsImVzc2VudGlhbCI6dHJ1ZX1dLCJpc3MiOiJkaWQ6ZXRocjpyaW5rZWJ5OjB4MTM4NGMxZmNlM2Y3MWQ3NjU5NzcwOGY1NGM0ZDEyOGMyNDFkMDBkMiJ9.L-j-gREAuN7DAxDCe1vXJWtMIdmn88HTuTFp2PasTTo_aqvIdGcFtv-rSfvRHkauNq5C3PkXkQWY01VGqpJ-QwE'

Expand Down Expand Up @@ -97,7 +97,7 @@ describe('integration test for handling messages', () => {
proofFormat: 'jwt',
})

expect(verifiableCredential.proof.jwt).toBeTruthy()
expect(verifiableCredential.proof.jwt).toBeDefined()
})

it('should save an SDR message', async () => {
Expand Down Expand Up @@ -136,6 +136,6 @@ describe('integration test for handling messages', () => {
proofFormat: 'jwt',
})

expect(verifiableCredential.proof.jwt).toBeTruthy()
expect(verifiableCredential.proof.jwt).toBeDefined()
})
})
10 changes: 3 additions & 7 deletions packages/daf-w3c/src/__tests__/action-handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const mockDidJwtVc = {
createVerifiableCredentialJwt: jest.fn().mockReturnValue('mockVcJwt'),
createVerifiablePresentationJwt: jest.fn().mockReturnValue('mockVcJwt'),
verifyCredential: jest.fn().mockReturnValue({ payload: {} }),
transformCredentialInput: jest.fn().mockReturnValue('mockTransformedCredential'),
transformPresentationInput: jest.fn().mockReturnValue('mockTransformedPresentation'),
normalizeCredential: jest.fn().mockReturnValue('mockCredential'),
normalizePresentation: jest.fn().mockReturnValue('mockPresentation'),
}
Expand Down Expand Up @@ -69,7 +67,7 @@ const w3c = new W3c()
describe('daf-w3c', () => {
it('handles createVerifiableCredential', async () => {
const credential: W3CCredential = {
'@context': ['https://www.w3.org/2018/credentials/v1323', 'https://www.w3.org/2020/demo/4342323'],
'@context': ['https://www.w3.org/2018/credentials/v1', 'https://www.w3.org/2020/demo/4342323'],
type: ['VerifiableCredential', 'PublicProfile'],
issuer: { id: mockIdentity1.did },
issuanceDate: new Date().toISOString(),
Expand All @@ -94,15 +92,14 @@ describe('daf-w3c', () => {
context,
)
// TODO Update these after refactoring did-jwt-vc
expect(mockDidJwtVc.transformCredentialInput).toBeCalledWith(credential)
expect(context.agent.identityManagerGetIdentity).toBeCalledWith({ did: mockIdentity1.did })
expect(context.agent.dataStoreSaveVerifiableCredential).toBeCalledWith('mockCredential')
expect(vc).toEqual('mockCredential')
})

it('handles createVerifiablePresentation', async () => {
const credential: VerifiableCredential = {
'@context': ['https://www.w3.org/2018/credentials/v1323'],
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential', 'PublicProfile'],
issuer: { id: mockIdentity1.did },
issuanceDate: new Date().toISOString(),
Expand All @@ -122,7 +119,7 @@ describe('daf-w3c', () => {
}

const presentation: W3CPresentation = {
'@context': ['https://www.w3.org/2018/credentials/v1323'],
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiablePresentation'],
holder: mockIdentity1.did,
verifier: [mockIdentity2.did],
Expand All @@ -139,7 +136,6 @@ describe('daf-w3c', () => {
context,
)

expect(mockDidJwtVc.transformPresentationInput).toBeCalledWith(presentation)
expect(context.agent.identityManagerGetIdentity).toBeCalledWith({ did: mockIdentity1.did })
expect(context.agent.dataStoreSaveVerifiablePresentation).toBeCalledWith('mockPresentation')
expect(vp).toEqual('mockPresentation')
Expand Down
10 changes: 4 additions & 6 deletions packages/daf-w3c/src/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ export class W3c implements IAgentPlugin {
context: IContext,
): Promise<VerifiablePresentation> {
try {
const payload = transformPresentationInput(args.presentation)
const identity = await context.agent.identityManagerGetIdentity({ did: args.presentation.holder })
const key = identity.keys.find(k => k.type === 'Secp256k1')
if (!key) throw Error('No signing key')
if (!key) throw Error('No signing key for ' + identity.did)
const signer = (data: string) => context.agent.keyManagerSignJWT({ kid: key.kid, data })
debug('Signing VP with', identity.did)
const jwt = await createVerifiablePresentationJwt(payload, { did: identity.did, signer })
const jwt = await createVerifiablePresentationJwt(args.presentation, { did: identity.did, signer })
debug(jwt)
const presentation = normalizePresentation(jwt)
if (args.save) {
Expand All @@ -95,14 +94,13 @@ export class W3c implements IAgentPlugin {
context: IContext,
): Promise<VerifiableCredential> {
try {
const payload = transformCredentialInput(args.credential)
const identity = await context.agent.identityManagerGetIdentity({ did: args.credential.issuer.id })
const key = identity.keys.find(k => k.type === 'Secp256k1')
if (!key) throw Error('No signing key')
if (!key) throw Error('No signing key for ' + identity.did)
const signer = (data: string) => context.agent.keyManagerSignJWT({ kid: key.kid, data })

debug('Signing VC with', identity.did)
const jwt = await createVerifiableCredentialJwt(payload, { did: identity.did, signer })
const jwt = await createVerifiableCredentialJwt(args.credential, { did: identity.did, signer })
debug(jwt)
const credential = normalizeCredential(jwt)
if (args.save) {
Expand Down

0 comments on commit df40d7e

Please sign in to comment.