Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
93 changes: 33 additions & 60 deletions src/controllers/did/DidController.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@

import type { DidResolutionResultProps } from '../types'
import type { PolygonDidCreateOptions } from '@ayanworks/credo-polygon-w3c-module/build/dids'
import type { DidDocument, KeyDidCreateOptions, PeerDidNumAlgo2CreateOptions } from '@credo-ts/core'

import { transformPrivateKeyToPrivateJwk, transformSeedToPrivateJwk } from '@credo-ts/askar'
import {
TypedArrayEncoder,
DidDocumentBuilder,
Expand All @@ -10,20 +12,27 @@ import {
PeerDidNumAlgo,
Kms,
Hasher,
LogLevel,
Agent,
} from '@credo-ts/core'
import { Key, KeyAlgorithm,askar } from '@openwallet-foundation/askar-nodejs'
import axios from 'axios'
import { Request as Req } from 'express'
import { Body, Controller, Example, Get, Path, Post, Route, Tags, Security, Request } from 'tsoa'
import { injectable } from 'tsyringe'

import { DidMethod, Network, Role, SCOPES } from '../../enums'
import { DidMethod, KeyAlgorithmCurve, Network, Role, SCOPES } from '../../enums'
import ErrorHandlingService from '../../errorHandlingService'
import { BadRequestError, InternalServerError } from '../../errors'
import { AgentType } from '../../types'
import { keyAlgorithmToCurve, p521, verkey } from '../../utils/constant'
import { getTypeFromCurve } from '../../utils/helpers'
import { CreateDidResponse, Did, DidRecordExample } from '../examples'
import { DidCreate } from '../types'
import { KeyAlgorithm,askar } from '@openwallet-foundation/askar-nodejs'
import { transformPrivateKeyToPrivateJwk, transformSeedToPrivateJwk } from '@credo-ts/askar'
import { supportedKeyTypesDID } from '../x509/x509.types'
import { container } from 'tsyringe'
import { RestMultiTenantAgentModules } from '../../cliAgent'



@Tags('Dids')
Expand All @@ -36,6 +45,8 @@ export class DidController extends Controller {
* @param did Decentralized Identifier
* @returns DidResolutionResult
*/
private agent = container.resolve(Agent<RestMultiTenantAgentModules>)

@Example<DidResolutionResultProps>(DidRecordExample)
@Get('/:did')
public async getDidRecordByDid(@Request() request: Req, @Path('did') did: Did) {
Expand Down Expand Up @@ -66,7 +77,7 @@ export class DidController extends Controller {
public async writeDid(@Request() request: Req, @Body() createDidOptions: DidCreate) {
let didRes

console.log("dsajsa", askar.version())
this.agent.config.logger.info(`askar version ${askar.version()}`)
try {
if (!createDidOptions.method) {
throw new BadRequestError('Method is required')
Expand Down Expand Up @@ -324,7 +335,6 @@ export class DidController extends Controller {
const buffer = Hasher.hash(verificationKey.publicKey.publicKey, 'sha-256')

const did = TypedArrayEncoder.toBase58(buffer.slice(0, 16))
// const did = `did:indy:${endorserNamespace}:${namespaceIdentifier

let body
if (createDidOptions.network === Network.Indicio_Testnet) {
Expand Down Expand Up @@ -369,13 +379,14 @@ export class DidController extends Controller {
const key = await agent.kms.importKey({ privateJwk })

const publicJwk = Kms.PublicJwk.fromPublicJwk(key.publicJwk)
const completeDid = `${didMethod}:${did}`
await agent.dids.import({
did,
did: completeDid,
keys: [
{
kmsKeyId: key.keyId,
// TODO: Find what to add here
didDocumentRelativeKeyId: ''
didDocumentRelativeKeyId: verkey,

}
]
})
Expand All @@ -395,72 +406,34 @@ export class DidController extends Controller {
if (didOptions.keyType === KeyAlgorithm.Bls12381G2) {
throw new BadRequestError('didOptions.keyType for type "bls12381g2" has been deprecated')
}
if (didOptions.keyType !== KeyAlgorithm.Ed25519) {
throw new BadRequestError('Only ed25519 and bls12381g2 key type supported')
if (didOptions.keyType === p521 as KeyAlgorithm) {
throw new BadRequestError('didOptions.keyType for type p521 is not supported')
}
const normalizedCurve = keyAlgorithmToCurve[didOptions.keyType as KeyAlgorithm]
if (!(normalizedCurve && supportedKeyTypesDID[DidMethod.Key]?.some(kt => kt.crv === normalizedCurve))) {
throw new BadRequestError(`Invalid keyType: ${didOptions.keyType}`)
}


if (!didOptions.did) {
console.log("This is inside if")
// Transform seed to private JWK and
// const { privateJwk } = transformSeedToPrivateJwk({
// type: {
// kty: 'OKP',
// crv: 'Ed25519',
// },
// seed: TypedArrayEncoder.fromString(didOptions.seed),
// })
// console.log("This is privateJwk::::::", JSON.stringify(privateJwk))

// // Import the key using KMS
// const { keyId } = await agent.kms.importKey({ privateJwk })
// console.log("This is keyId", keyId)

// // Create DID using the imported key
// const didResponse = await agent.dids.create<KeyDidCreateOptions>({
// method: 'key',
// options: {
// keyId,
// },
// })
const privateJwk = transformPrivateKeyToPrivateJwk({
privateKey: TypedArrayEncoder.fromString('96213c3d7fc8d4d6754c7a0fd969598e'),
type: {
kty: 'OKP',
crv: 'Ed25519',
},
privateKey: TypedArrayEncoder.fromString(didOptions.seed),
type: getTypeFromCurve(didOptions.keyType ?? KeyAlgorithm.Ed25519),
}).privateJwk

const { keyId } = await agent.kms.importKey({
privateJwk,
})

this.agent.config.logger.info(`This is keyId:::::: ${keyId}`)
const didResponse = await agent.dids.create<KeyDidCreateOptions>({
method: 'key',
method: DidMethod.Key,
options: {
keyId,
},
})
console.log("This is didResponse::::::", JSON.stringify(didResponse))
this.agent.config.logger.info(`This is didResponse:::::: ${JSON.stringify(didResponse)}`)

did = `${didResponse.didState.did}`
didDocument = didResponse.didState.didDocument

// await agent.kms.createKey({
// keyType: didOptions.keyType,
// seed: TypedArrayEncoder.fromString(didOptions.seed),
// })

// didResponse = await agent.dids.create<KeyDidCreateOptions>({
// method: DidMethod.Key,
// options: {
// keyType: KeyAlgorithm.Ed25519,
// },
// secret: {
// privateKey: TypedArrayEncoder.fromString(didOptions.seed),
// },
// })
// did = `${didResponse.didState.did}`
// didDocument = didResponse.didState.didDocument
} else {
did = didOptions.did
const createdDid = await agent.dids.getCreatedDids({
Expand All @@ -470,8 +443,8 @@ export class DidController extends Controller {
didDocument = createdDid[0]?.didDocument
}

console.log('This is did', did)
console.log('This is didDocument', didDocument)
this.agent.config.logger.info(`This is did ${did}` )
this.agent.config.logger.info(`This is didDocument ${didDocument}` )

await agent.dids.import({
did,
Expand Down
144 changes: 72 additions & 72 deletions src/controllers/x509/x509.Controller.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,78 @@
// import { injectable } from '@credo-ts/core'
// import { Request as Req } from 'express'
// import { Body, Controller, Post, Route, Tags, Security, Request, Get } from 'tsoa'
import { injectable } from '@credo-ts/core'
import { Request as Req } from 'express'
import { Body, Controller, Post, Route, Tags, Security, Request, Get } from 'tsoa'

// import { SCOPES } from '../../enums'
// import ErrorHandlingService from '../../errorHandlingService'
// import { X509ImportCertificateOptionsDto } from '../types'
// import { x509ServiceT } from './x509.service'
// import { X509CreateCertificateOptionsDto } from './x509.types'
import { SCOPES } from '../../enums'
import ErrorHandlingService from '../../errorHandlingService'
import { X509ImportCertificateOptionsDto } from '../types'
import { x509ServiceT } from './x509.service'
import { X509CreateCertificateOptionsDto } from './x509.types'

// @Tags('x509')
// @Security('jwt', [SCOPES.TENANT_AGENT, SCOPES.DEDICATED_AGENT])
// @Route('/x509')
// @injectable()
// export class X509Controller extends Controller {
// @Post('/')
// public async createX509Certificate(
// @Request() request: Req,
// @Body() createX509Options: X509CreateCertificateOptionsDto,
// ) {
// try {
// return await x509ServiceT.createCertificate(request, createX509Options)
// } catch (error) {
// throw ErrorHandlingService.handle(error)
// }
// }
@Tags('x509')
@Security('jwt', [SCOPES.TENANT_AGENT, SCOPES.DEDICATED_AGENT])
@Route('/x509')
@injectable()
export class X509Controller extends Controller {
@Post('/')
public async createX509Certificate(
@Request() request: Req,
@Body() createX509Options: X509CreateCertificateOptionsDto,
) {
try {
return await x509ServiceT.createCertificate(request, createX509Options)
} catch (error) {
throw ErrorHandlingService.handle(error)
}
}

// @Post('/import')
// public async ImportX509Certificates(
// @Request() request: Req,
// @Body() importX509Options: X509ImportCertificateOptionsDto,
// ) {
// try {
// return await x509ServiceT.ImportX509Certificates(request, importX509Options)
// } catch (error) {
// throw ErrorHandlingService.handle(error)
// }
// }
@Post('/import')
public async ImportX509Certificates(
@Request() request: Req,
@Body() importX509Options: X509ImportCertificateOptionsDto,
) {
try {
return await x509ServiceT.ImportX509Certificates(request, importX509Options)
} catch (error) {
throw ErrorHandlingService.handle(error)
}
}

// @Post('/trusted')
// public async addTrustedCertificate(
// @Request() request: Req,
// @Body()
// options: {
// certificate: string
// },
// ) {
// try {
// return await x509ServiceT.addTrustedCertificate(request, options)
// } catch (error) {
// throw ErrorHandlingService.handle(error)
// }
// }
@Post('/trusted')
public async addTrustedCertificate(
@Request() request: Req,
@Body()
options: {
certificate: string
},
) {
try {
return await x509ServiceT.addTrustedCertificate(request, options)
} catch (error) {
throw ErrorHandlingService.handle(error)
}
}

// @Get('/trusted')
// public async getTrustedCertificates(@Request() request: Req) {
// try {
// return await x509ServiceT.getTrustedCertificates(request)
// } catch (error) {
// throw ErrorHandlingService.handle(error)
// }
// }
@Get('/trusted')
public async getTrustedCertificates(@Request() request: Req) {
try {
return await x509ServiceT.getTrustedCertificates(request)
} catch (error) {
throw ErrorHandlingService.handle(error)
}
}

// @Post('/decode')
// public async decodeCertificate(
// @Request() request: Req,
// @Body()
// options: {
// certificate: string
// },
// ) {
// try {
// return await x509ServiceT.decodeCertificate(request, options)
// } catch (error) {
// throw ErrorHandlingService.handle(error)
// }
// }
// }
@Post('/decode')
public async decodeCertificate(
@Request() request: Req,
@Body()
options: {
certificate: string
},
) {
try {
return await x509ServiceT.decodeCertificate(request, options)
} catch (error) {
throw ErrorHandlingService.handle(error)
}
}
}
Loading