Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Bhutan-NDI/ngotag-agent-controller …
Browse files Browse the repository at this point in the history
…into pipeline-implementation
  • Loading branch information
Sheetal-ayanworks committed Jun 7, 2024
2 parents fa91043 + 1a15bf9 commit b3a72b5
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 22 deletions.
1 change: 1 addition & 0 deletions samples/cliConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"autoAcceptMediationRequests": false,
"adminPort": 4001,
"tenancy": true,
"schemaFileServerURL": "https://schema.credebl.id/schemas/",
"didRegistryContractAddress": "0x1adeA199dCf07E17232415Cb232442BE52517Add",
"schemaManagerContractAddress": "0x289c7Bd4C7d38cC54bff370d6f9f01b74Df51b11",
"rpcUrl": "https://rpc-amoy.polygon.technology",
Expand Down
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export async function runCliServer() {
webhookUrl: parsed['webhook-url'],
adminPort: parsed['admin-port'],
tenancy: parsed['tenancy'],
schemaFileServerURL: parsed['schemaFileServerURL'],
didRegistryContractAddress: parsed['didRegistryContractAddress'],
schemaManagerContractAddress: parsed['schemaManagerContractAddress'],
rpcUrl: parsed['rpcUrl'],
Expand Down
16 changes: 9 additions & 7 deletions src/cliAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export interface AriesRestConfig {
label: string
walletConfig: WalletConfig
indyLedger: indyLedger[]
adminPort: number
publicDidSeed?: string
endpoints?: string[]
autoAcceptConnections?: boolean
Expand All @@ -93,12 +94,12 @@ export interface AriesRestConfig {
connectionImageUrl?: string
tenancy?: boolean
webhookUrl?: string
adminPort: number
didRegistryContractAddress: string
schemaManagerContractAddress: string
rpcUrl: string
fileServerUrl: string
fileServerToken: string
didRegistryContractAddress?: string
schemaManagerContractAddress?: string
rpcUrl?: string
fileServerUrl?: string
fileServerToken?: string
schemaFileServerURL?: string
}

export async function readRestConfig(path: string) {
Expand Down Expand Up @@ -217,6 +218,7 @@ async function generateSecretKey(length: number = 32): Promise<string> {

export async function runRestAgent(restConfig: AriesRestConfig) {
const {
schemaFileServerURL,
logLevel,
inboundTransports = [],
outboundTransports = [],
Expand Down Expand Up @@ -357,12 +359,12 @@ export async function runRestAgent(restConfig: AriesRestConfig) {
const recordWithToken = genericRecord.find((record) => record?.content?.token !== undefined)
token = recordWithToken?.content.token as string
}

const app = await setupServer(
agent,
{
webhookUrl,
port: adminPort,
schemaFileServerURL,
},
token
)
Expand Down
5 changes: 3 additions & 2 deletions src/controllers/did/DidController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ export class DidController extends Controller {
public async handlePolygon(createDidOptions: DidCreate) {
// need to discuss try catch logic
const { endpoint, network, privatekey } = createDidOptions
if (network !== 'mainnet' && network !== 'testnet') {
const networkName = network?.split(':')[1]
if (networkName !== 'mainnet' && networkName !== 'testnet') {
throw Error('Invalid network type')
}
if (!privatekey || typeof privatekey !== 'string' || !privatekey.trim() || privatekey.length !== 64) {
Expand All @@ -409,7 +410,7 @@ export class DidController extends Controller {
return this.agent.dids.create<PolygonDidCreateOptions>({
method: 'polygon',
options: {
network,
network: networkName,
endpoint,
},
secret: {
Expand Down
31 changes: 24 additions & 7 deletions src/controllers/multi-tenancy/MultiTenancyController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { RestAgentModules, RestMultiTenantAgentModules } from '../../cliAgent'
import type { Version } from '../examples'
import type { RecipientKeyOption } from '../types'
import type { RecipientKeyOption, SchemaMetadata } from '../types'
import type { PolygonDidCreateOptions } from '@ayanworks/credo-polygon-w3c-module/build/dids'
import type {
AcceptProofRequestOptions,
Expand Down Expand Up @@ -49,6 +49,7 @@ import {
} from '@credo-ts/core'
import { QuestionAnswerRole, QuestionAnswerState } from '@credo-ts/question-answer'
import axios from 'axios'
import * as fs from 'fs'

import { CredentialEnum, DidMethod, Network, Role } from '../../enums/enum'
import { BCOVRIN_REGISTER_URL, INDICIO_NYM_URL } from '../../utils/util'
Expand Down Expand Up @@ -992,23 +993,38 @@ export class MultiTenancyController extends Controller {
},
@Path('tenantId') tenantId: string,
@Res() internalServerError: TsoaResponse<500, { message: string }>
): Promise<unknown> {
): Promise<SchemaMetadata> {
try {
let schemaResponse
const { did, schemaName, schema } = createSchemaRequest
if (!did || !schemaName || !schema) {
throw Error('One or more parameters are empty or undefined.')
}

await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => {
//need to add the return type after adding the scham URL
schemaResponse = await tenantAgent.modules.polygon.createSchema({
const schemaResponse = await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => {
return await tenantAgent.modules.polygon.createSchema({
did,
schemaName,
schema,
})
})
return schemaResponse

const configFileData = fs.readFileSync('config.json', 'utf-8')
const config = JSON.parse(configFileData)
if (!config.schemaFileServerURL) {
throw new Error('Please provide valid schema file server URL')
}

if (!schemaResponse?.schemaId) {
throw new Error('Invalid schema response')
}
const schemaPayload: SchemaMetadata = {
schemaUrl: config.schemaFileServerURL + schemaResponse?.schemaId,
did: schemaResponse?.did,
schemaId: schemaResponse?.schemaId,
schemaTxnHash: schemaResponse?.resourceTxnHash,
}

return schemaPayload
} catch (error) {
return internalServerError(500, { message: `something went wrong: ${error}` })
}
Expand Down Expand Up @@ -1415,6 +1431,7 @@ export class MultiTenancyController extends Controller {
}),
outOfBandRecord: outOfBandRecord.toJSON(),
outOfBandRecordId: outOfBandRecord.id,
credentialRequestThId: offerOob.credentialRecord.threadId,
invitationDid: createOfferOptions?.invitationDid ? '' : invitationDid,
}
})
Expand Down
21 changes: 20 additions & 1 deletion src/controllers/polygon/PolygonController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import type { RestAgentModules } from '../../cliAgent'
import type { SchemaMetadata } from '../types'

import { generateSecp256k1KeyPair } from '@ayanworks/credo-polygon-w3c-module'
import { DidOperation } from '@ayanworks/credo-polygon-w3c-module/build/ledger'
import { Agent, CredoError } from '@credo-ts/core'
import * as fs from 'fs'
import { injectable } from 'tsyringe'

import { Route, Tags, Security, Controller, Post, TsoaResponse, Res, Body, Get, Path } from 'tsoa'
Expand Down Expand Up @@ -62,11 +64,28 @@ export class Polygon extends Controller {
})
}

return this.agent.modules.polygon.createSchema({
const schemaResponse = await this.agent.modules.polygon.createSchema({
did,
schemaName,
schema,
})

const schemaServerConfig = fs.readFileSync('config.json', 'utf-8')
const configJson = JSON.parse(schemaServerConfig)
if (!configJson.schemaFileServerURL) {
throw new Error('Please provide valid schema file server URL')
}

if (!schemaResponse?.schemaId) {
throw new Error('Invalid schema response')
}
const schemaPayload: SchemaMetadata = {
schemaUrl: configJson.schemaFileServerURL + schemaResponse?.schemaId,
did: schemaResponse?.did,
schemaId: schemaResponse?.schemaId,
schemaTxnHash: schemaResponse?.resourceTxnHash,
}
return schemaPayload
} catch (error) {
return internalServerError(500, { message: `something went wrong: ${error}` })
}
Expand Down
7 changes: 7 additions & 0 deletions src/controllers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,10 @@ export interface WriteTransaction {
export interface RecipientKeyOption {
recipientKey?: string
}

export interface SchemaMetadata {
did: string
schemaId: string
schemaTxnHash?: string
schemaUrl?: string
}
11 changes: 11 additions & 0 deletions src/routes/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,17 @@ const models: TsoaRoute.Models = {
"type": {"dataType":"string","validators":{}},
},
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
"SchemaMetadata": {
"dataType": "refObject",
"properties": {
"did": {"dataType":"string","required":true},
"schemaId": {"dataType":"string","required":true},
"schemaTxnHash": {"dataType":"string"},
"schemaUrl": {"dataType":"string"},
},
"additionalProperties": false,
},
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
"WriteTransaction": {
"dataType": "refObject",
"properties": {
Expand Down
26 changes: 25 additions & 1 deletion src/routes/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,28 @@
"type": "string",
"example": "1.0.0"
},
"SchemaMetadata": {
"properties": {
"did": {
"type": "string"
},
"schemaId": {
"type": "string"
},
"schemaTxnHash": {
"type": "string"
},
"schemaUrl": {
"type": "string"
}
},
"required": [
"did",
"schemaId"
],
"type": "object",
"additionalProperties": false
},
"WriteTransaction": {
"properties": {
"endorsedTransaction": {
Expand Down Expand Up @@ -4825,7 +4847,9 @@
"description": "Ok",
"content": {
"application/json": {
"schema": {}
"schema": {
"$ref": "#/components/schemas/SchemaMetadata"
}
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import bodyParser from 'body-parser'
import cors from 'cors'
import express from 'express'
import { rateLimit } from 'express-rate-limit'
import * as fs from 'fs'
import { serve, generateHTML } from 'swagger-ui-express'
import { container } from 'tsyringe'

Expand All @@ -24,7 +25,7 @@ import { ValidateError, type Exception } from 'tsoa'

export const setupServer = async (agent: Agent, config: ServerConfig, apiKey?: string) => {
container.registerInstance(Agent, agent)

fs.writeFileSync('config.json', JSON.stringify(config, null, 2))
const app = config.app ?? express()
if (config.cors) app.use(cors())

Expand Down
1 change: 1 addition & 0 deletions src/utils/ServerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface ServerConfig {
webhookUrl?: string
/* Socket server is used for sending events over websocket to clients */
socketServer?: Server
schemaFileServerURL?: string
}
3 changes: 0 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@
version "7.24.5"
resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.24.5.tgz#15ab5b98e101972d171aeef92ac70d8d6718f06a"
integrity sha512-tVQRucExLQ02Boi4vdPp49svNGcfL2GhdTCT9aldhXgCJVAI21EtRfBettiuLUwce/7r6bFdgs6JFkcdTiFttA==
dependencies:
"@ampproject/remapping" "^2.2.0"
"@babel/code-frame" "^7.24.2"
"@babel/generator" "^7.24.5"
Expand Down Expand Up @@ -630,7 +629,6 @@
version "9.4.0"
resolved "https://registry.yarnpkg.com/@digitalcredentials/jsonld-signatures/-/jsonld-signatures-9.4.0.tgz#d5881122c4202449b88a7e2384f8e615ae55582c"
integrity sha512-DnR+HDTm7qpcDd0wcD1w6GdlAwfHjQSgu+ahion8REkCkkMRywF+CLunU7t8AZpFB2Gr/+N8naUtiEBNje1Oew==
dependencies:
"@digitalbazaar/security-context" "^1.0.0"
"@digitalcredentials/jsonld" "^6.0.0"
fast-text-encoding "^1.0.3"
Expand Down Expand Up @@ -4211,7 +4209,6 @@ express@^4.17.1, express@^4.18.1, express@^4.18.3:
version "4.19.2"
resolved "https://registry.yarnpkg.com/express/-/express-4.19.2.tgz#e25437827a3aa7f2a827bc8171bbbb664a356465"
integrity sha512-5T6nhjsT+EOMzuck8JjBHARTHfMht0POzlA60WV2pMD3gyXw2LZnZ+ueGdNxG+0calOJcWKbpFcuzLZ91YWq9Q==
dependencies:
accepts "~1.3.8"
array-flatten "1.1.1"
body-parser "1.20.2"
Expand Down

0 comments on commit b3a72b5

Please sign in to comment.