Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fetch indy schema method #1290

Merged
merged 15 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 @@ -219,11 +219,13 @@ export class IndySdkAnonCredsRegistry implements AnonCredsRegistry {
}
)

const { schema } = await this.fetchIndySchemaWithSeqNo(agentContext, Number(credentialDefinition.schemaId), did)

return {
credentialDefinitionId: credentialDefinition.id,
credentialDefinition: {
issuerId: didFromCredentialDefinitionId(credentialDefinition.id),
schemaId: credentialDefinition.schemaId,
schemaId: schema.schemaId,
tag: credentialDefinition.tag,
type: 'CL',
value: credentialDefinition.value,
Expand Down Expand Up @@ -305,7 +307,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 +511,47 @@ 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
Vickysomtee marked this conversation as resolved.
Show resolved Hide resolved

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
}
}
}
}

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
187 changes: 187 additions & 0 deletions packages/indy-sdk/tests/indy-sdk-anoncreds-registry.e2e.test.ts

Large diffs are not rendered by default.

48 changes: 46 additions & 2 deletions packages/indy-vdr/src/anoncreds/IndyVdrAnonCredsRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
SchemaRequest,
GetCredentialDefinitionRequest,
CredentialDefinitionRequest,
GetTransactionRequest,
} from '@hyperledger/indy-vdr-shared'

import { IndyVdrPoolService } from '../pool'
Expand Down Expand Up @@ -218,12 +219,14 @@ export class IndyVdrAnonCredsRegistry implements AnonCredsRegistry {

const response = await pool.submitReadRequest(request)

if (response.result.data) {
const schema = await this.fetchIndySchemaWithSeqNo(agentContext, response.result.ref, did, 1)
Vickysomtee marked this conversation as resolved.
Show resolved Hide resolved

if (response.result.data && schema) {
return {
credentialDefinitionId: credentialDefinitionId,
credentialDefinition: {
issuerId: didFromCredentialDefinitionId(credentialDefinitionId),
schemaId: response.result.ref.toString(),
schemaId: schema.schema.schemaId,
tag: response.result.tag,
type: 'CL',
value: response.result.data,
Expand Down Expand Up @@ -416,6 +419,47 @@ export class IndyVdrAnonCredsRegistry implements AnonCredsRegistry {
revocationRegistryDefinitionMetadata: {},
}
}

public async fetchIndySchemaWithSeqNo(agentContext: AgentContext, seqNo: number, did: string, type: number) {
Vickysomtee marked this conversation as resolved.
Show resolved Hide resolved
const indyVdrPoolService = agentContext.dependencyManager.resolve(IndyVdrPoolService)

const pool = await indyVdrPoolService.getPoolForDid(agentContext, did)

agentContext.config.logger.debug(`Getting transaction with seqNo '${seqNo}' from ledger '${pool.indyNamespace}'`)
const request = new GetTransactionRequest({ ledgerType: type, seqNo })
Vickysomtee marked this conversation as resolved.
Show resolved Hide resolved

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

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

const schema = response.result.data?.txn.data as SchemaType

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

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

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

export interface IndyVdrRegisterSchemaOptions extends RegisterSchemaOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ describe('IndyVdrAnonCredsRegistry', () => {
credentialDefinitionId: `TL1EaPFCZ8Si5aUrqScBDt:3:CL:${schemaResponse.schemaMetadata.indyLedgerSeqNo}:TAG`,
credentialDefinition: {
issuerId: 'TL1EaPFCZ8Si5aUrqScBDt',
// FIXME: this will change when https://github.com/hyperledger/aries-framework-javascript/issues/1259 is merged
schemaId: `${schemaResponse.schemaMetadata.indyLedgerSeqNo}`,
schemaId: `TL1EaPFCZ8Si5aUrqScBDt:2:test:${dynamicVersion}`,
tag: 'TAG',
type: 'CL',
value: {
Expand Down
6 changes: 3 additions & 3 deletions packages/react-native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"events": "^3.3.0"
},
"devDependencies": {
"@types/indy-sdk-react-native": "npm:@types/[email protected].24",
"@types/indy-sdk-react-native": "npm:@types/[email protected].26",
"@types/react-native": "^0.64.10",
"indy-sdk-react-native": "^0.3.0",
"indy-sdk-react-native": "^0.3.1",
"react": "17.0.1",
"react-native": "0.64.2",
"react-native-fs": "^2.18.0",
Expand All @@ -40,7 +40,7 @@
"typescript": "~4.9.4"
},
"peerDependencies": {
"indy-sdk-react-native": "^0.3.0",
"indy-sdk-react-native": "^0.3.1",
"react-native-fs": "^2.18.0",
"react-native-get-random-values": "^1.7.0"
}
Expand Down