Skip to content

Commit

Permalink
format data
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra committed Mar 30, 2024
1 parent bd70502 commit 1630420
Show file tree
Hide file tree
Showing 10 changed files with 1,504 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
DidCommCredentialsCreateOfferResponse,
DidCommCredentialExchangeRecord,
DidCommCredentialsGetFormatDataResponse,
} from './CredentialsControllerTypes'

import { CredentialState, RecordNotFoundError, CredentialRole } from '@credo-ts/core'
Expand All @@ -11,7 +12,11 @@ import { RequestWithAgent } from '../../../authentication'
import { apiErrorResponse } from '../../../utils/response'
import { RecordId, ThreadId } from '../../types'

import { credentialExchangeRecordExample, didCommCredentialsCreateOfferResponse } from './CredentialsControllerExamples'
import {
credentialExchangeRecordExample,
didCommCredentialsCreateOfferResponse,
didCommCredentialsGetFormatDataExample,
} from './CredentialsControllerExamples'
import {
AcceptCredentialRequestOptions,
OfferCredentialOptions,
Expand Down Expand Up @@ -77,6 +82,28 @@ export class CredentialsController extends Controller {
}
}

/**
* Retrieve the format data associated with a credential exchange
*/
@Get('/:credentialExchangeId/format-data')
@Example<DidCommCredentialsGetFormatDataResponse>(didCommCredentialsGetFormatDataExample)
public async getFormatDateForCredentialExchange(
@Request() request: RequestWithAgent,
@Path('credentialExchangeId') credentialExchangeId: RecordId,
): Promise<DidCommCredentialsGetFormatDataResponse> {
try {
const formatData = await request.user.agent.credentials.getFormatData(credentialExchangeId)
return formatData
} catch (error) {
if (error instanceof RecordNotFoundError) {
this.setStatus(404)
return apiErrorResponse(`credential exchange with id "${credentialExchangeId}" not found.`)

Check warning on line 100 in packages/rest/src/controllers/didcomm/credentials/CredentialsController.ts

View check run for this annotation

Codecov / codecov/patch

packages/rest/src/controllers/didcomm/credentials/CredentialsController.ts#L99-L100

Added lines #L99 - L100 were not covered by tests
}

return apiErrorResponse(error)

Check warning on line 103 in packages/rest/src/controllers/didcomm/credentials/CredentialsController.ts

View check run for this annotation

Codecov / codecov/patch

packages/rest/src/controllers/didcomm/credentials/CredentialsController.ts#L103

Added line #L103 was not covered by tests
}
}

/**
* Deletes a credential exchange record in the credential repository.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
DidCommCredentialsCreateOfferResponse,
DidCommCredentialExchangeRecord,
DidCommCredentialsGetFormatDataResponse,
} from './CredentialsControllerTypes'

import { CredentialExchangeRecord, CredentialRole, CredentialState } from '@credo-ts/core'
Expand All @@ -25,3 +26,97 @@ export const didCommCredentialsCreateOfferResponse: DidCommCredentialsCreateOffe
'@type': 'https://didcomm.org/issue-credential/1.0/offer-credential',
},
}

export const didCommCredentialsGetFormatDataExample: DidCommCredentialsGetFormatDataResponse = {
offerAttributes: [
{
'mime-type': 'text/plain',
name: 'prop1',
value: 'Alice',
},
{
'mime-type': 'text/plain',
name: 'prop2',
value: 'Bob',
},
],
offer: {
anoncreds: {
schema_id: 'schema:gSl0JkGIcmRif593Q6XYGsJndHGOzm1jWRFa-Lwrz9o',
cred_def_id: 'credential-definition:_p5hLM-uQa1zWnn3tBlSZjLHN3_jrHOq48HZg9x0WNU',
key_correctness_proof: {
c: '99492103123537767435780273140747875256790346132054146213963860002839562036500',
xz_cap:
'580439401883808314813871954452291264672809884147416670644868202601349938534100012582121009892201828623456062583253712122390217453057344836461080181231409709931412795314081026936945930855825367373487243407763891173044774127684739520982825154953842442273813308991169090527022468436144816388847602118294979269313055482643679093387752228933342461551523525381348315077261080667315083376665151670823724165412812789868725296452469727913099297098501657154305553628601670242915979173502981519452691532364945800381784094698957899207092852251635741642974685343076909543754528139524390330974942129175514270934196165227837447039632098505018004458669992226178741765564079908532417644335223001107373553800636',
xr_cap: [
[
'prop2',
'995399175086393290156643492153533410460088296042299023071321712767423823125266832866393832677059827513393172141469698615988398481324616362988306901757713912635730675164176333343113543425145312139791479962174299119310631546985779208764729297940146997294517195382469523212546412350946365060108495261812541753519099438498374096964273878810634998095141423219246735648908552325440689746900928839923227716273385790878307688202350090032242160533886272557839856485554366696826060620961486642370355665610549033927229155523847761590023494696062955267842162962594983582064910274022357686360792641497125074245699228183099824304332072233714267713611371961270313585807822270849608871260298873632530881181868',
],
[
'prop1',
'1332724481340046129877312153176690921485462825381114334570976976911683740241735795674573565883450256485008453985793480391949571168596993894012183159420293482819645749037942541031221713594705004813170460963384907003707588246318592228064786935434434436493334335846541085008786621396358737393954898248156584956789603719162774096077144488775239706705659235938222621701852501004590177199881330248514156406621561858785958857377365259412279939255354717554715259409741463035067370342340375245558605525782229283165954317144433290958178245662795970871238028493809988026433044315609370187889271966194713168137139044017058936597616750741033931501545012220127203710939560438531093698046054460328575918119603',
],
[
'master_secret',
'555027667428826242052970636449944760049716355932181005994059994704109863707489157610003651613465020982803541959788424930381416736220275982020574537155052939117498439344162350344720165428803548470057405536121354129959126518248455145191069141936666484179865018445996249913288495893190150238690838395991946573833346853053011240922319143499605693196163287075552588023831726135219961691799755462617042764987540423466441420500952919498995496310058387923814988140775894040589217067406516222451334977386486085574931826246195042640621402064770528877288327158750876201208151309067281791780644173220251685733148275860559683101771901936515172131760435059336917112277256735606589048477347147443664213752574',
],
],
},
nonce: '692456490106322710069916',
},
},
request: {
anoncreds: {
entropy: '283559102751594275830315',
cred_def_id: 'credential-definition:_p5hLM-uQa1zWnn3tBlSZjLHN3_jrHOq48HZg9x0WNU',
blinded_ms: {
u: '58231223348966103131811162273993912136371812752782223808009746737780125568145868523057859605972791511440980139494915406073803687889151020231709198213752951834097534156853321419009300133041701988088059045538840265627332554454194121024426435019582352173715814900227749253559086664858523212471480396321468257159168804913614969187532210509785588812584841739263332234841113948381329216546405289807839367671066394371976377285491314443468087042916655417085254764424047492227119854758243993637314070997551720795324547265481480108567082864623565773819911308131308207210554182277214230022063294515760720713009700825428955532510',
ur: null,
hidden_attributes: ['master_secret'],
committed_attributes: {},
},
blinded_ms_correctness_proof: {
c: '14842005166951621146114950228033228166587445948917220775188417180669817458071',
v_dash_cap:
'525886886600441444565563165744451574453581209783186527553278087235218067249143670252864427868001371270984533684519358843687605475581166282935142905703276466255860485038094631924069785904354326434488518339194078002719755861388964711768508348545543255979806148850295089036263281958934849704765108579167429147052286486914403929145073420962893811107068894159729549508196847548809587052009560288805123173353269086571490649648022184768357155256457950107309654761616752977676620176099125951088465313509009701650195907112862265146926434945766292668030673219665354811192934496571751927032511309995906445544597811901863475749538496709548405261395672321054035313087600543076125543620519913607917930629021139372239900295387614530',
m_caps: {
master_secret:
'26403860841992559132025470865700367282768662423342071327016182400364926167433347005018624226591156120350434943733972616556764215219552899959288997912500104383298723878965850738210',
},
r_caps: {},
},
nonce: '539202924201893565376900',
},
},
credential: {
anoncreds: {
schema_id: 'schema:gSl0JkGIcmRif593Q6XYGsJndHGOzm1jWRFa-Lwrz9o',
cred_def_id: 'credential-definition:_p5hLM-uQa1zWnn3tBlSZjLHN3_jrHOq48HZg9x0WNU',
rev_reg_id: undefined,
values: {
prop2: {
raw: 'Bob',
encoded: '93006290325627508022776103386395994712401809437930957652111221015872244345185',
},
prop1: {
raw: 'Alice',
encoded: '27034640024117331033063128044004318218486816931520886405535659934417438781507',
},
},
signature: {
p_credential: {
m_2: '2911715207125951422480156302046291340123870729973317007174252437265217048587',
a: '11395635159250760503996843059477894862759042446759456478905609052258966666054297486499529657198356040094543149614125789362925680451864913383442428405747925006507714967572810462914901849679778018440516366649116468683963048348928719272415537003338969559562235424937950447758592924825449445925605210390900510823781990795216551732658029427081784108220873232530576542198769893886026016329571813137791144743390726969182823512920008254774588891824872946440672297966557771368796896826221080416623922702670373253252601697257131639339789773605316559189611430460668913621031924922363795873865565810614212399140440158978630736492',
e: '259344723055062059907025491480697571938277889515152306249728583105665800713306759149981690559193987143012367913206299323899696942213235956742930090344543936461575196618473935962381',
v: '8001814536142162607770363548208093772576030885155777393828891920810388269643639795739496611196183494954511428986568490597372615758581717334821354274023290392255038227577524325982981782454790052567586095566736421356705577874795304121932343609694749077725813635912948953884849597222055594720249473114949732531603732043206297827614841392072041631247914713339543298071003341199125553155575573253915872830335388346577731204615565550928758657918974242548635243688870260125103464458103572944515798752393429291619708048708181234777305612276265184799282497088401357918659516303709468557162638343709893397545583781671672656111029322715215691886249339655634731004758885743276863891497637192845057331073900220047078918373696507486114747681960422431667787893756346748792880297182146033915387360507174461962945213023817226802833132149',
},
r_credential: null,
},
signature_correctness_proof: {
se: '18071888966807930445969341878476115756105579849496822693675536304985965652940046036953323270875830549601629354090648450651925366597175546789259250284156036563261793522690459846803775756759065096398577328540423797501842695683583228588830053641258351279667129839173861484988969984108220499135535938554712154909596949678407619115337420038902354926030012219508589609561675458487780368202144228827896948708718677481449347040231630306565656593268126349021640222232240758703745752333545546720963799056251898068823571706878796745922121208045372883817285034174020297113979889276046972260648750830806544618009796203666715262922',
c: '32233748970469592854527577130712976905395946986728509546042467955793436240507',
},
},
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
CredentialRole,
CredentialState,
CredentialExchangeRecord as CredoCredentialExchangeRecord,
GetCredentialFormatDataReturn,
} from '@credo-ts/core'
import type { PlaintextMessage } from '@credo-ts/core/build/types'

Expand All @@ -31,6 +32,11 @@ export interface DidCommCredentialExchangeRecord extends CredoBaseRecord {
credentialAttributes?: CredentialPreviewAttributeOptions[]
}

export interface DidCommCredentialExchangeWithFormatData {
credentialExchange: DidCommCredentialExchangeRecord
formatData?: GetCredentialFormatDataReturn<CredentialFormats>
}

export function credentialExchangeRecordToApiModel(
record: CredoCredentialExchangeRecord,
): DidCommCredentialExchangeRecord {
Expand Down Expand Up @@ -113,3 +119,12 @@ export interface AcceptCredentialRequestOptions {
autoAcceptCredential?: AutoAcceptCredential
comment?: string
}

export interface DidCommCredentialsGetFormatDataResponse
extends Omit<GetCredentialFormatDataReturn<CredentialFormats>, 'offerAttributes'> {
offerAttributes?: Array<{
'mime-type'?: string
name: string
value: string
}>
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,85 @@ describe('BasicMessagesController', () => {
expect(acceptResponse.statusCode).toBe(200)

await credentialIssued

const formatData = await request(app).get(`/didcomm/credentials/${receiverExchangeResponse.body[0].id}/format-data`)
expect(formatData.body).toEqual({
credential: {
anoncreds: {
cred_def_id: 'credential-definition:_p5hLM-uQa1zWnn3tBlSZjLHN3_jrHOq48HZg9x0WNU',
rev_reg: null,
rev_reg_id: null,
schema_id: 'schema:gSl0JkGIcmRif593Q6XYGsJndHGOzm1jWRFa-Lwrz9o',
signature: {
p_credential: expect.any(Object),
r_credential: null,
},
signature_correctness_proof: {
c: expect.any(String),
se: expect.any(String),
},
values: {
prop1: {
encoded: '27034640024117331033063128044004318218486816931520886405535659934417438781507',
raw: 'Alice',
},
prop2: {
encoded: '93006290325627508022776103386395994712401809437930957652111221015872244345185',
raw: 'Bob',
},
},
witness: null,
},
},
offer: {
anoncreds: {
cred_def_id: 'credential-definition:_p5hLM-uQa1zWnn3tBlSZjLHN3_jrHOq48HZg9x0WNU',
key_correctness_proof: {
c: expect.any(String),
xr_cap: expect.arrayContaining([
['master_secret', expect.any(String)],
['prop1', expect.any(String)],
['prop2', expect.any(String)],
]),
xz_cap: expect.any(String),
},
nonce: expect.any(String),
schema_id: 'schema:gSl0JkGIcmRif593Q6XYGsJndHGOzm1jWRFa-Lwrz9o',
},
},
offerAttributes: [
{
'mime-type': 'text/plain',
name: 'prop1',
value: 'Alice',
},
{
'mime-type': 'text/plain',
name: 'prop2',
value: 'Bob',
},
],
request: {
anoncreds: {
blinded_ms: {
committed_attributes: {},
hidden_attributes: ['master_secret'],
u: expect.any(String),
ur: null,
},
blinded_ms_correctness_proof: {
c: expect.any(String),
m_caps: {
master_secret: expect.any(String),
},
r_caps: {},
v_dash_cap: expect.any(String),
},
cred_def_id: 'credential-definition:_p5hLM-uQa1zWnn3tBlSZjLHN3_jrHOq48HZg9x0WNU',
entropy: expect.any(String),
nonce: expect.any(String),
},
},
})
})
})
35 changes: 33 additions & 2 deletions packages/rest/src/controllers/didcomm/proofs/ProofsController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { DidCommProofsCreateRequestResponse, DidCommProofExchangeRecord } from './ProofsControllerTypes'
import type {
DidCommProofsCreateRequestResponse,
DidCommProofExchangeRecord,
DidCommProofsGetFormatDataResponse,
} from './ProofsControllerTypes'

import { ProofRole, ProofState, RecordNotFoundError } from '@credo-ts/core'
import { Body, Controller, Delete, Example, Get, Path, Post, Query, Request, Route, Security, Tags } from 'tsoa'
Expand All @@ -8,7 +12,11 @@ import { RequestWithAgent } from '../../../authentication'
import { apiErrorResponse } from '../../../utils/response'
import { RecordId, ThreadId } from '../../types'

import { didCommProofsCreateRequestResponse, proofExchangeRecordExample } from './ProofsControllerExamples'
import {
didCommProofsCreateRequestResponse,
didCommProofsGetFormatDataExample,
proofExchangeRecordExample,
} from './ProofsControllerExamples'
import {
DidCommProofsCreateRequestOptions,
DidCommProofsAcceptRequestOptions,
Expand Down Expand Up @@ -48,6 +56,29 @@ export class ProofsController extends Controller {
return proofs.map(proofExchangeRecordToApiModel)
}

/**
* Retrieve the format data associated with a proof exchange
*/
@Get('/:proofExchangeId/format-data')
@Example<DidCommProofsGetFormatDataResponse>(didCommProofsGetFormatDataExample)
public async getFormatDateForProofExchange(
@Request() request: RequestWithAgent,
@Path('proofExchangeId') proofExchangeId: RecordId,
): Promise<DidCommProofsGetFormatDataResponse> {
try {
const formatData = await request.user.agent.proofs.getFormatData(proofExchangeId)
return formatData
} catch (error) {
if (error instanceof RecordNotFoundError) {
this.setStatus(404)
return apiErrorResponse(`proof exchange with id "${proofExchangeId}" not found.`)

Check warning on line 74 in packages/rest/src/controllers/didcomm/proofs/ProofsController.ts

View check run for this annotation

Codecov / codecov/patch

packages/rest/src/controllers/didcomm/proofs/ProofsController.ts#L73-L74

Added lines #L73 - L74 were not covered by tests
}

this.setStatus(500)
return apiErrorResponse(error)

Check warning on line 78 in packages/rest/src/controllers/didcomm/proofs/ProofsController.ts

View check run for this annotation

Codecov / codecov/patch

packages/rest/src/controllers/didcomm/proofs/ProofsController.ts#L77-L78

Added lines #L77 - L78 were not covered by tests
}
}

/**
* Retrieve proof exchange by proof exchange id
*/
Expand Down
Loading

0 comments on commit 1630420

Please sign in to comment.