Skip to content

Commit

Permalink
feat: add fetch indy schema method (#1290)
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Anene <[email protected]>
  • Loading branch information
Vickysomtee authored Feb 17, 2023
1 parent 3e02227 commit 1d782f5
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 28 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@stablelib/ed25519": "^1.0.2",
"@stablelib/random": "^1.0.1",
"@stablelib/sha256": "^1.0.1",
"@types/indy-sdk": "1.16.24",
"@types/indy-sdk": "1.16.26",
"@types/node-fetch": "^2.5.10",
"@types/ws": "^7.4.6",
"abort-controller": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/indy-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"dependencies": {
"@aries-framework/anoncreds": "0.3.3",
"@aries-framework/core": "0.3.3",
"@types/indy-sdk": "1.16.24",
"@types/indy-sdk": "1.16.26",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"rxjs": "^7.2.0",
Expand Down
101 changes: 83 additions & 18 deletions packages/indy-sdk/src/anoncreds/services/IndySdkAnonCredsRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class IndySdkAnonCredsRegistry implements AnonCredsRegistry {
schemaMetadata: {
// NOTE: the seqNo is required by the indy-sdk even though not present in AnonCreds v1.
// For this reason we return it in the metadata.
indyLedgerSeqNo: schema.seqNo,
indyLedgerSeqNo: response.result.txnMetadata.seqNo,
didIndyNamespace: pool.didIndyNamespace,
},
}
Expand Down Expand Up @@ -212,26 +212,43 @@ export class IndySdkAnonCredsRegistry implements AnonCredsRegistry {
)

const [, credentialDefinition] = await indySdk.parseGetCredDefResponse(response)
agentContext.config.logger.debug(
`Got credential definition '${credentialDefinitionId}' from ledger '${pool.didIndyNamespace}'`,
{
credentialDefinition,
const { schema } = await this.fetchIndySchemaWithSeqNo(agentContext, Number(credentialDefinition.schemaId), did)

if (credentialDefinition && schema) {
agentContext.config.logger.debug(
`Got credential definition '${credentialDefinitionId}' from ledger '${pool.didIndyNamespace}'`,
{
credentialDefinition,
}
)

return {
credentialDefinitionId: credentialDefinition.id,
credentialDefinition: {
issuerId: didFromCredentialDefinitionId(credentialDefinition.id),
schemaId: schema.schemaId,
tag: credentialDefinition.tag,
type: 'CL',
value: credentialDefinition.value,
},
credentialDefinitionMetadata: {
didIndyNamespace: pool.didIndyNamespace,
},
resolutionMetadata: {},
}
)
}

agentContext.config.logger.error(`Error retrieving credential definition '${credentialDefinitionId}'`, {
credentialDefinitionId,
})

return {
credentialDefinitionId: credentialDefinition.id,
credentialDefinition: {
issuerId: didFromCredentialDefinitionId(credentialDefinition.id),
schemaId: credentialDefinition.schemaId,
tag: credentialDefinition.tag,
type: 'CL',
value: credentialDefinition.value,
},
credentialDefinitionMetadata: {
didIndyNamespace: pool.didIndyNamespace,
credentialDefinitionId,
credentialDefinitionMetadata: {},
resolutionMetadata: {
error: 'notFound',
message: `unable to resolve credential definition`,
},
resolutionMetadata: {},
}
} catch (error) {
agentContext.config.logger.error(`Error retrieving credential definition '${credentialDefinitionId}'`, {
Expand Down Expand Up @@ -305,7 +322,7 @@ export class IndySdkAnonCredsRegistry implements AnonCredsRegistry {

const request = await indySdk.buildCredDefRequest(options.credentialDefinition.issuerId, {
id: credentialDefinitionId,
schemaId: options.credentialDefinition.schemaId,
schemaId: schemaMetadata.indyLedgerSeqNo.toString(),
tag: options.credentialDefinition.tag,
type: options.credentialDefinition.type,
value: options.credentialDefinition.value,
Expand Down Expand Up @@ -509,6 +526,54 @@ export class IndySdkAnonCredsRegistry implements AnonCredsRegistry {
}
}
}

private async fetchIndySchemaWithSeqNo(agentContext: AgentContext, seqNo: number, did: string) {
const indySdkPoolService = agentContext.dependencyManager.resolve(IndySdkPoolService)
const indySdk = agentContext.dependencyManager.resolve<IndySdk>(IndySdkSymbol)

const { pool } = await indySdkPoolService.getPoolForDid(agentContext, did)
agentContext.config.logger.debug(`Getting transaction with seqNo '${seqNo}' from ledger '${pool.didIndyNamespace}'`)

const request = await indySdk.buildGetTxnRequest(did, 'DOMAIN', seqNo)

agentContext.config.logger.trace(`Submitting get transaction request to ledger '${pool.didIndyNamespace}'`)
const response = await indySdkPoolService.submitReadRequest(pool, request)

const schema = response.result.data as SchemaType

if (schema.txn.type !== '101') {
agentContext.config.logger.error(`Could not get schema from ledger for seq no ${seqNo}'`)
return {}
}

const schemaId = getLegacySchemaId(did, schema.txn.data.data.name, schema.txn.data.data.version)

return {
schema: {
schemaId,
attr_name: schema.txn.data.data.attr_names,
name: schema.txn.data.data.name,
version: schema.txn.data.data.version,
issuerId: did,
seqNo,
},
indyNamespace: pool.didIndyNamespace,
}
}
}

interface SchemaType {
txn: {
data: {
data: {
attr_names: string[]
version: string
name: string
}
}

type: string
}
}

export interface IndySdkRegisterSchemaOptions extends RegisterSchemaOptions {
Expand Down
2 changes: 1 addition & 1 deletion packages/indy-sdk/src/ledger/IndySdkPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class IndySdkPool {
}

public get didIndyNamespace(): string {
return this.didIndyNamespace
return this.config.indyNamespace
}

public get id() {
Expand Down
Loading

0 comments on commit 1d782f5

Please sign in to comment.