Skip to content

Commit

Permalink
feat: Refactor inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran committed Jun 29, 2023
1 parent 28a196c commit 623f8ad
Show file tree
Hide file tree
Showing 3 changed files with 256 additions and 137 deletions.
92 changes: 59 additions & 33 deletions src/controllers/issuer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@ export class IssuerController {
const { valid } = validateSpecCompliantPayload(value)
return valid
}).withMessage('Invalid didDocument'),
check('secret.verificationMethod.type')
check('verificationMethodType')
.optional()
.isString()
.isIn([VerificationMethods.Ed255192020, VerificationMethods.Ed255192018, VerificationMethods.JWK])
.withMessage('Invalid verificationMethod'),
check('secret.verificationMethod.id')
.optional()
.isString()
.withMessage('Invalid verificationMethod'),
check('options.methodSpecificIdAlgo').optional().isString().isIn([MethodSpecificIdAlgo.Base58, MethodSpecificIdAlgo.Uuid]).withMessage('Invalid methodSpecificIdAlgo'),
check('options.network').optional().isString().isIn([CheqdNetwork.Mainnet, CheqdNetwork.Testnet]).withMessage('Invalid network'),
.withMessage('Invalid verificationMethod'),
check('methodSpecificIdAlgo').optional().isString().isIn([MethodSpecificIdAlgo.Base58, MethodSpecificIdAlgo.Uuid]).withMessage('Invalid methodSpecificIdAlgo'),
check('network').optional().isString().isIn([CheqdNetwork.Mainnet, CheqdNetwork.Testnet]).withMessage('Invalid network'),
]

public static updateValidator = [
Expand All @@ -42,13 +38,11 @@ export class IssuerController {

public static resourceValidator = [
param('did').exists().isString().contains('did:cheqd').withMessage('Invalid DID'),
check('jobId').custom((value, {req})=>{
if(!value && !(req.body.name && req.body.type && req.body.data)) return false
return true
}).withMessage('name, type and data are required'),
check('name').optional().isString().withMessage('Invalid name'),
check('type').optional().isString().withMessage('Invalid type'),
check('data').optional().isString().withMessage('Invalid data'),
check('name').exists().withMessage('name is required').isString().withMessage('Invalid name'),
check('type').exists().withMessage('type is required').isString().withMessage('Invalid type'),
check('data').exists().withMessage('data is required').isString().withMessage('Invalid data'),
check('encoding').exists().withMessage('encoding is required')
.isString().isIn(['hex', 'base64', 'base64url']).withMessage('Invalid encoding'),
check('alsoKnownAs').optional().isArray().withMessage('Invalid alsoKnownAs'),
check('alsoKnownAs.*.uri').isString().withMessage('Invalid uri'),
check('alsoKnownAs.*.description').isString().withMessage('Invalid description')
Expand Down Expand Up @@ -84,32 +78,39 @@ export class IssuerController {
})
}

const { options, secret } = request.body
const { methodSpecificIdAlgo, network, versionId = v4()} = options
const verificationMethod = secret?.verificationMethod
const { methodSpecificIdAlgo, network, verificationMethodType, assertionMethod=true, serviceEndpoint } = request.body
let didDocument: DIDDocument
let kids: string[] = []
try {
if (options.didDocument) {
didDocument = options.didDocument
} else if (verificationMethod) {
if (request.body.didDocument) {
didDocument = request.body.didDocument
} else if (verificationMethodType) {
const key = await Identity.instance.createKey('Ed25519', response.locals.customerId)
kids.push(key.kid)
didDocument = generateDidDoc({
verificationMethod: verificationMethod.type,
verificationMethodId: verificationMethod.id || 'key-1',
verificationMethod: verificationMethodType || VerificationMethods.Ed255192018,
verificationMethodId: 'key-1',
methodSpecificIdAlgo: (methodSpecificIdAlgo as MethodSpecificIdAlgo) || MethodSpecificIdAlgo.Uuid,
network,
publicKey: key.publicKeyHex
})
didDocument.assertionMethod = didDocument.authentication

if (assertionMethod) {
didDocument.assertionMethod = didDocument.authentication
}

if (serviceEndpoint) {
didDocument.service = [{
id: `${didDocument.id}#service-1`,
type: 'service-1',
serviceEndpoint
}]
}
} else {
return response.status(400).json({
error: 'Provide a DID Document or atleast one verification method'
error: 'Provide a DID Document or the network type to create a DID'
})
}

const did = await Identity.instance.createDid(network, didDocument, response.locals.customerId)
const did = await Identity.instance.createDid(network || didDocument.id.split(':')[2], didDocument, response.locals.customerId)
return response.status(200).json(did)
} catch (error) {
return response.status(500).json({
Expand All @@ -127,8 +128,33 @@ export class IssuerController {
}

try {
const did = await Identity.instance.updateDid(request.body.didDocument, response.locals.customerId)
return response.status(200).json(did)

const { did, service, verificationMethod, authentication } = request.body
let updatedDocument: DIDDocument
if (request.body.didDocument) {
updatedDocument = request.body.didDocument
} else if (did && (service || verificationMethod || authentication)) {
let resolvedDocument: any = await Identity.instance.resolveDid(did)
if(!resolvedDocument?.didDocument || resolvedDocument.didDocumentMetadata.deactivated) {
return response.status(400).send({
error: `${did} is either Deactivated or Not found`
})
} else {
resolvedDocument = resolvedDocument.didDocument
}
if (service) resolvedDocument.service = service
if (verificationMethod) resolvedDocument.verificationMethod = verificationMethod
if (authentication) resolvedDocument.authentication = authentication

updatedDocument = resolvedDocument
} else {
return response.status(400).json({
error: 'Provide a DID Document or atleast one field to update'
})
}

const result = await Identity.instance.updateDid(updatedDocument, response.locals.customerId)
return response.status(200).json(result)
} catch (error) {
return response.status(500).json({
error: `${error}`
Expand Down Expand Up @@ -163,15 +189,15 @@ export class IssuerController {
}

const { did } = request.params
let { data, name, type, alsoKnownAs, version, network } = request.body
let { data, encoding, name, type, alsoKnownAs, version, network } = request.body

let resourcePayload: Partial<MsgCreateResourcePayload> = {}
try {
// check if did is registered on the ledger
let resolvedDocument: any = await Identity.instance.resolveDid(did)
if(!resolvedDocument?.didDocument || resolvedDocument.didDocumentMetadata.deactivated) {
return response.status(400).send({
error: `${did} is a Deactivated DID`
error: `${did} is a either Deactivated or Not found`
})
} else {
resolvedDocument = resolvedDocument.didDocument
Expand All @@ -182,7 +208,7 @@ export class IssuerController {
id: v4(),
name,
resourceType: type,
data: fromString(data, 'base64'),
data: fromString(data, encoding),
version,
alsoKnownAs
}
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/revocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class RevocationController {
]

static queryValidator = [
query('did').isString().withMessage('DID is required')
check('did').isString().withMessage('DID is required')
.contains('did:cheqd:').withMessage('Provide a valid cheqd DID'),
query('statusPurpose').optional().isString().withMessage('statusPurpose should be a string')
.isIn(['suspension', 'revocation']).withMessage('Invalid statuspurpose'),
Expand All @@ -29,11 +29,11 @@ export class RevocationController {
return response.status(400).json({ error: result.array()[0].msg })
}

let { length, encoding } = request.body
let { data, name, statusPurpose, alsoKnownAs, version } = request.body
let { data, name, alsoKnownAs, version, length, encoding } = request.body
const { statusPurpose } = request.query as { statusPurpose: 'revocation' | 'suspension' }

const did = request.query.did as string
data = data ? fromString(data, 'base64') : undefined
data = data ? fromString(data, encoding) : undefined

try {
let result: any
Expand Down
Loading

0 comments on commit 623f8ad

Please sign in to comment.