Skip to content

Commit

Permalink
feat: Remove internal secret mode
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran committed Jan 30, 2023
1 parent 8854932 commit 1978583
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 216 deletions.
6 changes: 3 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class App {
app.get('/', (req, res) => res.redirect('api-docs'))

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

// cheqd-helpers
Expand Down
33 changes: 10 additions & 23 deletions src/controllers/did.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Request, Response } from 'express'
import { validationResult, check } from 'express-validator'

import { ISignInputs, DIDDocument } from '@cheqd/sdk/build/types'
import { DIDDocument } from '@cheqd/sdk/build/types'
import { SignInfo } from '@cheqd/ts-proto/cheqd/did/v2'

import { v4 } from 'uuid'
Expand All @@ -16,19 +16,6 @@ import { DIDModule } from '@cheqd/sdk'

export class DidController {

public static secretValidator = [
check('secret').optional().custom((value)=>{
if (value) {
if(value.signingResponse && value.keys) return false
else if(value.keys) {
return value.keys.every((key: any) => key.privateKeyHex.length == 128
)}
}

return true
}).withMessage(Messages.SecretValidation),
]

public static didDocValidator = [
check('didDocument').custom(async (value, {req})=>{
if((!value || !value.verificationMethod?.length) && !req.body.jobId) return false
Expand Down Expand Up @@ -82,10 +69,10 @@ export class DidController {
versionId = v4()
}

let signInputs: ISignInputs[] | SignInfo[]
let signInputs: SignInfo[]

if (secret.signingResponse || secret.keys) {
signInputs = secret.signingResponse ? convertToSignInfo(secret.signingResponse) : secret.keys!
if (secret.signingResponse) {
signInputs = convertToSignInfo(secret.signingResponse)
} else {
LocalStore.instance.setItem(jobId, {didDocument, state: IState.Action, versionId})
return response.status(200).json(await Responses.GetDIDActionSignatureResponse(jobId, didDocument, versionId))
Expand Down Expand Up @@ -159,9 +146,9 @@ export class DidController {
did = updatedDocument.id!
}

let signInputs: ISignInputs[] | SignInfo[]
if (secret.signingResponse || secret.keys) {
signInputs = secret.signingResponse ? convertToSignInfo(secret.signingResponse) : secret.keys!
let signInputs: SignInfo[]
if (secret.signingResponse) {
signInputs = convertToSignInfo(secret.signingResponse)
} else {
LocalStore.instance.setItem(jobId, {didDocument: updatedDocument, state: IState.Action, versionId})
return response.status(200).json(await Responses.GetDIDActionSignatureResponse(jobId, updatedDocument, versionId))
Expand Down Expand Up @@ -230,9 +217,9 @@ export class DidController {
}
}

let signInputs: ISignInputs[] | SignInfo[]
if (secret.signingResponse || secret.keys) {
signInputs = secret.signingResponse ? convertToSignInfo(secret.signingResponse) : secret.keys!
let signInputs: SignInfo[]
if (secret.signingResponse) {
signInputs = convertToSignInfo(secret.signingResponse)
} else {
LocalStore.instance.setItem(jobId, {didDocument: payload, state: IState.Action, versionId})
return response.status(200).json(Responses.GetDeactivateDidSignatureResponse(jobId, payload, versionId))
Expand Down
7 changes: 3 additions & 4 deletions src/controllers/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Request, Response } from 'express'
import { check, param, validationResult } from 'express-validator'

import { MsgCreateResourcePayload } from '@cheqd/ts-proto/cheqd/resource/v2'
import { ISignInputs } from '@cheqd/sdk/build/types'
import { SignInfo } from '@cheqd/ts-proto/cheqd/did/v2'

import { v4 } from 'uuid'
Expand Down Expand Up @@ -86,10 +85,10 @@ export class ResourceController {
}
}

let signInputs: ISignInputs[] | SignInfo[]
let signInputs: SignInfo[]

if (secret.signingResponse || secret.keys) {
signInputs = secret.signingResponse ? convertToSignInfo(secret.signingResponse) : secret.keys!
if (secret.signingResponse) {
signInputs = convertToSignInfo(secret.signingResponse)
} else {
LocalStore.instance.setResource(jobId, {resource: resourcePayload, state: IState.Action})
return response.status(200).json(Responses.GetResourceActionSignatureResponse(
Expand Down
10 changes: 5 additions & 5 deletions src/service/cheqd.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ICheqdSDKOptions } from '@cheqd/sdk'
import { CheqdSDK, createCheqdSDK, DIDModule, ResourceModule } from '@cheqd/sdk'
import type { AbstractCheqdSDKModule } from '@cheqd/sdk/build/modules/_'
import type { ISignInputs, DIDDocument } from '@cheqd/sdk/build/types'
import type { DIDDocument } from '@cheqd/sdk/build/types'
import { MsgCreateResourcePayload } from '@cheqd/ts-proto/cheqd/resource/v2'
import { SignInfo } from '@cheqd/ts-proto/cheqd/did/v2'
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing'
Expand Down Expand Up @@ -73,7 +73,7 @@ export class CheqdRegistrar {
return this.sdk
}

public async create(signInputs: ISignInputs[] | SignInfo[], didPayload: DIDDocument, versionId: string | undefined) {
public async create(signInputs: SignInfo[], didPayload: DIDDocument, versionId: string | undefined) {
return await this.forceGetSdk()
.createDidTx(
signInputs,
Expand All @@ -86,7 +86,7 @@ export class CheqdRegistrar {
)
}

public async update(signInputs: ISignInputs[] | SignInfo[], didPayload: DIDDocument, versionId: string | undefined) {
public async update(signInputs: SignInfo[], didPayload: DIDDocument, versionId: string | undefined) {
return await this.forceGetSdk()
.updateDidTx(
signInputs,
Expand All @@ -99,7 +99,7 @@ export class CheqdRegistrar {
)
}

public async deactivate(signInputs: ISignInputs[] | SignInfo[], didPayload: DIDDocument, versionId: string | undefined) {
public async deactivate(signInputs: SignInfo[], didPayload: DIDDocument, versionId: string | undefined) {
return await this.forceGetSdk()
.deactivateDidTx(
signInputs,
Expand All @@ -112,7 +112,7 @@ export class CheqdRegistrar {
)
}

public async createResource(signInputs: ISignInputs[] | SignInfo[], resourcePayload: Partial<MsgCreateResourcePayload>) {
public async createResource(signInputs: SignInfo[], resourcePayload: Partial<MsgCreateResourcePayload>) {
return await this.forceGetSdk().createResourceTx(
signInputs,
resourcePayload,
Expand Down
26 changes: 10 additions & 16 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,22 @@ export interface IDIDCreateRequest {
network: NetworkType,
keytype: string
},
secret: {
seed?: string,
keys?: ISignInputs[],
signingResponse?: ISignInfo[]
},
secret: ISecret
didDocument: DIDDocument
}

export interface IDIDUpdateRequest {
jobId: string | null
did: string
options: Record<string, any>,
secret:{
keys?: ISignInputs[],
signingResponse?: ISignInfo[]
},
options: Record<string, any>
secret: ISecret
didDocumentOperation: DidDocumentOperation[]
didDocument: DIDDocument[]
}

export interface IResourceCreateRequest {
jobId: string | null
secret: {
keys?: ISignInputs[],
signingResponse?: ISignInfo[]
}
secret: ISecret
data: any,
name: string,
type: string,
Expand All @@ -53,7 +43,7 @@ export interface IDIDDeactivateRequest {
jobId: string | null
did: string
options: Record<string, any>,
secret: Record<string, any>
secret: ISecret
}

export interface IDidResponse {
Expand All @@ -67,7 +57,7 @@ export interface IDidState {
state: IState
action?: IAction
did: string
secret: Record<string, any>
secret: ISecret
didDocument: DIDDocument
}

Expand All @@ -88,4 +78,8 @@ export enum IAction {
export interface ISignInfo {
verificationMethodId: string,
signature: string
}

export interface ISecret {
signingResponse?: ISignInfo[]
}
Loading

0 comments on commit 1978583

Please sign in to comment.