Skip to content

Commit

Permalink
feat: Support Client managed secret mode (#66)
Browse files Browse the repository at this point in the history
* feat: Update payload type

* feat: Support signing response

* build: Fix linting error

* feat: Add node-cache

* feat: Update signature type

* feat: Update resource creation

* feat: Update helper method

* feat: Update swagger

* build: Upgrade cheqd sdk

* fix: rpc urls

* feat: Update error handling

* lint: Fix swagger linting errors
  • Loading branch information
DaevMithran authored Jan 25, 2023
1 parent 09d46a6 commit a8fe2b4
Show file tree
Hide file tree
Showing 15 changed files with 957 additions and 368 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ jobs:
id: set-version
run: |
VERSION=$( git describe --tags ${{ github.sha }})
echo ::set-output name=VERSION::"$VERSION"
echo "VERSION=$VERSION" >> "$GITHUB_OUTPUT"
release-docker:
Expand Down
78 changes: 56 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
"build": "esbuild src/index.ts --platform=node --bundle --minify --outdir=dist --external:swagger-ui-express"
},
"dependencies": {
"@cheqd/sdk": "^1.5.0-develop.1",
"@cheqd/sdk": "^1.5.0-develop.3",
"@cheqd/ts-proto": "^1.0.16-develop.2",
"@cosmjs/proto-signing": "^0.29.2",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-validator": "^6.14.2",
"helmet": "^6.0.0",
"node-cache": "^5.1.2",
"node-fetch": "^3.2.10",
"swagger-ui-express": "^4.5.0"
"swagger-ui-express": "^4.5.0",
"uuid": "^9.0.0"
},
"devDependencies": {
"@semantic-release/changelog": "^6.0.1",
Expand Down
13 changes: 7 additions & 6 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import express from 'express'
import Helmet from 'helmet'
import { DidController } from './controllers/did'
import { CheqdRegistrar } from './service/cheqd'
import * as swagger from 'swagger-ui-express'
import * as swaggerJson from '../swagger.json'

import { DidController } from './controllers/did'
import { CheqdController } from './controllers/cheqd'
import { ResourceController } from './controllers/resource'
import { CheqdRegistrar } from './service/cheqd'

class App {
public express: express.Application
Expand All @@ -31,10 +32,10 @@ class App {
app.get('/', (req, res) => res.redirect('api-docs'))

// did-registrar
app.post(`${URL_PREFIX}/create`, DidController.createValidator, new DidController().create)
app.post(`${URL_PREFIX}/update`, DidController.createValidator, DidController.updateValidator, new DidController().update)
app.post(`${URL_PREFIX}/deactivate`, DidController.createValidator, new DidController().deactivate)
app.post(`${URL_PREFIX}/:did/create-resource`, new ResourceController().create)
app.post(`${URL_PREFIX}/create`, DidController.createValidator, DidController.secretValidator, new DidController().create)
app.post(`${URL_PREFIX}/update`, DidController.updateValidator, DidController.secretValidator, new DidController().update)
app.post(`${URL_PREFIX}/deactivate`, DidController.deactivateValidator, DidController.secretValidator, new DidController().deactivate)
app.post(`${URL_PREFIX}/:did/create-resource`, ResourceController.createValidator, new ResourceController().create)

// cheqd-helpers
app.get(`${URL_PREFIX}/key-pair`, new CheqdController().generateKeys)
Expand Down
40 changes: 28 additions & 12 deletions src/controllers/cheqd.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { createDidPayload, createDidVerificationMethod, createKeyPairBase64, createVerificationKeys } from '@cheqd/sdk'
import { CheqdNetwork, IKeyPair, MethodSpecificIdAlgo, VerificationMethods } from '@cheqd/sdk/build/types'
import { Request, Response } from 'express'
import { validationResult, query } from 'express-validator'

import { createDidPayload, createDidVerificationMethod, createKeyPairHex, createVerificationKeys } from '@cheqd/sdk'
import { CheqdNetwork, MethodSpecificIdAlgo, VerificationMethods } from '@cheqd/sdk/build/types'

import { NetworkType } from '../service/cheqd'

export class CheqdController {

public static didDocValidator = [
query('verificationMethod').isString().isIn([VerificationMethods.Base58, VerificationMethods.JWK]).withMessage('Invalid verificationMethod'),
query('verificationMethod').isString().isIn([VerificationMethods.Ed255192020, VerificationMethods.Ed255192018, VerificationMethods.JWK]).withMessage('Invalid verificationMethod'),
query('methodSpecificIdAlgo').isString().isIn([MethodSpecificIdAlgo.Base58, MethodSpecificIdAlgo.Uuid]).withMessage('Invalid methodSpecificIdAlgo'),
query('methodSpecificIdLength').isNumeric().isIn([16, 32]).withMessage('Invalid methodSpecificIdLength length'),
query('network').optional().isString().isIn([NetworkType.Mainnet, NetworkType.Testnet]).withMessage('Invalid network')
query('network').optional().isString().isIn([NetworkType.Mainnet, NetworkType.Testnet]).withMessage('Invalid network'),
query('publicKeyHex').isString().withMessage('PublicKeyHex is required').isLength({min:64, max:64}).withMessage('PublicKeyHex should be of length 64')
]

public generateKeys(request: Request<{},{},{},{seed?: string}>, response: Response) {
return response.json(createKeyPairBase64(request.query?.seed))
const keyPair = createKeyPairHex(request.query?.seed)

return response.json({
privateKeyHex: keyPair.privateKey,
publicKeyHex: keyPair.publicKey
})
}

public generateDidDoc(request: Request<{},{},{},IDidDocRequest>, response: Response) {
Expand All @@ -26,23 +33,32 @@ export class CheqdController {
})
}

const { verificationMethod, methodSpecificIdAlgo, methodSpecificIdLength, network, keyPair=createKeyPairBase64() } = request.query

const verificationKeys = createVerificationKeys(keyPair, methodSpecificIdAlgo, 'key-1', methodSpecificIdLength, network)
const { verificationMethod, methodSpecificIdAlgo, network, publicKeyHex } = request.query
const verificationKeys = createVerificationKeys(publicKeyHex, methodSpecificIdAlgo, 'key-1', network)
const verificationMethods = createDidVerificationMethod([verificationMethod], [verificationKeys])

return response.json({
didDoc: createDidPayload(verificationMethods, [verificationKeys]),
key: keyPair
key: {
verificationMethodId: (verificationMethods[0]).id,
publicKeyHex
}
})
}

}


export interface IDidDocRequest {
verificationMethod: VerificationMethods
methodSpecificIdAlgo: MethodSpecificIdAlgo
methodSpecificIdLength: number
network: CheqdNetwork
keyPair: IKeyPair
publicKeyHex: string
}

export interface IVerificationMethodRequest {
verificationMethod: VerificationMethods
methodSpecificIdAlgo: MethodSpecificIdAlgo
network: CheqdNetwork
publicKey: string
}
Loading

0 comments on commit a8fe2b4

Please sign in to comment.