Skip to content

Commit

Permalink
Merge pull request #158 from hypersign-protocol/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
varsha766 authored Oct 22, 2024
2 parents 19fc2e1 + 3f996a7 commit 2784828
Show file tree
Hide file tree
Showing 16 changed files with 1,031 additions and 205 deletions.
5 changes: 4 additions & 1 deletion src/credential/dto/register-credential.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { ApiProperty } from '@nestjs/swagger';
import { CredStatus, Namespace } from './create-credential.dto';
import { Type } from 'class-transformer';
import { IsEnum, ValidateNested } from 'class-validator';

export enum SupportedSignatureType {
BJJSignature2021 = 'BJJSignature2021',
Ed25519Signature2020 = 'Ed25519Signature2020',
}
export class RegisterCredentialStatusDto {
@ApiProperty({
name: 'credentialStatus',
Expand Down
135 changes: 107 additions & 28 deletions src/credential/services/credential.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ import { CredentialSSIService } from './credential.ssi.service';
import { HidWalletService } from 'src/hid-wallet/services/hid-wallet.service';
import { CredentialRepository } from '../repository/credential.repository';
import { DidRepository } from 'src/did/repository/did.repository';
import { HypersignDID, HypersignVerifiableCredential } from 'hs-ssi-sdk';
import {
HypersignDID,
HypersignVerifiableCredential,
IKeyType,
} from 'hs-ssi-sdk';
import { VerifyCredentialDto } from '../dto/verify-credential.dto';
import { RegisterCredentialStatusDto } from '../dto/register-credential.dto';
import {
RegisterCredentialStatusDto,
SupportedSignatureType,
} from '../dto/register-credential.dto';
import { getAppVault, getAppMenemonic } from '../../utils/app-vault-service';
import { TxSendModuleService } from 'src/tx-send-module/tx-send-module.service';

Expand Down Expand Up @@ -105,14 +112,43 @@ export class CredentialService {
);
const seed = await this.hidWallet.getSeedFromMnemonic(issuerMnemonic);
const hypersignDid = new HypersignDID();
const { privateKeyMultibase } = await hypersignDid.generateKeys({ seed });

const { didDocument } = await hypersignDid.resolve({ did: issuerDid });
const verificationMethod = didDocument.verificationMethod.find(
(vm) => vm.id === verificationMethodId,
);
// Apps Identity: - used for gas fee
const appMenemonic = await getAppMenemonic(kmsId);
const hypersignVC = await this.credentialSSIService.initateHypersignVC(
appMenemonic,
nameSpace,
);
let privateKeyMultibase;
let hypersignVC;
if (!verificationMethod) {
throw new Error(
`VerificationMethod does not exists for vmId ${verificationMethodId}`,
);
}
if (
verificationMethod &&
verificationMethod.type === IKeyType.Ed25519VerificationKey2020
) {
const key = await hypersignDid.generateKeys({ seed });
privateKeyMultibase = key.privateKeyMultibase;
hypersignVC = await this.credentialSSIService.initateHypersignVC(
appMenemonic,
nameSpace,
);
} else if (
verificationMethod &&
verificationMethod.type === IKeyType.BabyJubJubKey2021
) {
const key = await hypersignDid.bjjDID.generateKeys({
mnemonic: issuerMnemonic,
});
privateKeyMultibase = key.privateKeyMultibase;
hypersignVC = await this.credentialSSIService.initateHypersignBjjVC(
appMenemonic,
nameSpace,
);
}

let credential;

if (schemaId) {
Expand Down Expand Up @@ -160,7 +196,6 @@ export class CredentialService {
privateKeyMultibase,
registerCredential: false,
});

const credStatusTemp = {};
Object.assign(credStatusTemp, credentialStatus);

Expand Down Expand Up @@ -335,20 +370,41 @@ export class CredentialService {
didInfo.kmsId,
);
const seed = await this.hidWallet.getSeedFromMnemonic(issuerMnemonic);
let hypersignVC;
const hypersignDid = new HypersignDID();
const { privateKeyMultibase } = await hypersignDid.generateKeys({ seed });

// Apps Identity: - used for gas fee
const { didDocument } = await hypersignDid.resolve({ did: issuerDid });
const verificationMethod = didDocument.verificationMethod.find(
(vm) => vm.id === verificationMethodId,
);
let privateKeyMultibase;
const appMenemonic = await getAppMenemonic(kmsId);
const nameSpace = namespace
? namespace
: this.config.get('NETWORK')
? this.config.get('NETWORK')
: namespace;
const hypersignVC = await this.credentialSSIService.initateHypersignVC(
appMenemonic,
nameSpace,
);
if (
verificationMethod &&
verificationMethod.type === IKeyType.BabyJubJubKey2021
) {
const key = await hypersignDid.bjjDID.generateKeys({
mnemonic: issuerMnemonic,
});
privateKeyMultibase = key.privateKeyMultibase;
hypersignVC = await this.credentialSSIService.initateHypersignBjjVC(
appMenemonic,
nameSpace,
);
} else {
const key = await hypersignDid.generateKeys({ seed });
privateKeyMultibase = key.privateKeyMultibase;
hypersignVC = await this.credentialSSIService.initateHypersignVC(
appMenemonic,
nameSpace,
);
}
// Apps Identity: - used for gas fee

Logger.log(
'update() method: before calling hypersignVC.resolveCredentialStatus to resolve cred status',
'CredentialService',
Expand Down Expand Up @@ -452,12 +508,25 @@ export class CredentialService {
'verfiyCredential() method: before calling hypersignVC.verify to verify credential',
'CredentialService',
);
verificationResult = await hypersignCredential.verify({
credential: verifyCredentialDto.credentialDocument as any, // will fix it latter
issuerDid: issuer,
verificationMethodId:
verifyCredentialDto.credentialDocument.proof.verificationMethod,
});
if (
verifyCredentialDto.credentialDocument &&
verifyCredentialDto.credentialDocument.proof.type ===
SupportedSignatureType.BJJSignature2021
) {
verificationResult = await hypersignCredential.bjjVC.verify({
credential: verifyCredentialDto.credentialDocument as any, // will fix it latter
issuerDid: issuer,
verificationMethodId:
verifyCredentialDto.credentialDocument.proof.verificationMethod,
});
} else {
verificationResult = await hypersignCredential.verify({
credential: verifyCredentialDto.credentialDocument as any, // will fix it latter
issuerDid: issuer,
verificationMethodId:
verifyCredentialDto.credentialDocument.proof.verificationMethod,
});
}
} catch (e) {
Logger.error(
`verfiyCredential() method: Error:${e.message}`,
Expand Down Expand Up @@ -502,12 +571,22 @@ export class CredentialService {
const { wallet, address } = await this.hidWallet.generateWallet(
appMenemonic,
);

const hypersignVC = await this.credentialSSIService.initateHypersignVC(
appMenemonic,
namespace,
);

let hypersignVC;
if (
proof &&
proof.type &&
proof.type === SupportedSignatureType.BJJSignature2021
) {
hypersignVC = await this.credentialSSIService.initateHypersignBjjVC(
appMenemonic,
namespace,
);
} else {
hypersignVC = await this.credentialSSIService.initateHypersignVC(
appMenemonic,
namespace,
);
}
if (await this.checkAllowence(address)) {
await this.txnService.sendVCTxn(credentialStatus, proof, appMenemonic);
} else {
Expand Down
26 changes: 24 additions & 2 deletions src/credential/services/credential.ssi.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, Scope, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

import { HypersignVerifiableCredential } from 'hs-ssi-sdk';
import { HypersignVerifiableCredential, HypersignSSISdk } from 'hs-ssi-sdk';
import { HidWalletService } from 'src/hid-wallet/services/hid-wallet.service';

@Injectable({ scope: Scope.REQUEST })
Expand All @@ -10,7 +10,10 @@ export class CredentialSSIService {
private readonly config: ConfigService,
private readonly hidWallet: HidWalletService,
) {}
async initateHypersignVC(mnemonic: string,namespace: string): Promise<HypersignVerifiableCredential> {
async initateHypersignVC(
mnemonic: string,
namespace: string,
): Promise<HypersignVerifiableCredential> {
Logger.log('InitateHypersignVC(): starts....', 'CredentialSSIService');
const nodeRpcEndpoint = this.config.get('HID_NETWORK_RPC');
const nodeRestEndpoint = this.config.get('HID_NETWORK_API');
Expand All @@ -30,4 +33,23 @@ export class CredentialSSIService {
await hypersignVC.init();
return hypersignVC;
}
async initateHypersignBjjVC(mnemonic: string, namespace: string) {
Logger.log('InitateHypersignVC(): starts....', 'CredentialSSIService');
const nodeRpcEndpoint = this.config.get('HID_NETWORK_RPC');
const nodeRestEndpoint = this.config.get('HID_NETWORK_API');
Logger.log(
'InitateHypersignVC() method: before getting offlinesigner',
'CredentialSSIService',
);
await this.hidWallet.generateWallet(mnemonic);
const offlineSigner = this.hidWallet.getOfflineSigner();
const hsSSiSdk = new HypersignSSISdk({
offlineSigner,
nodeRpcEndpoint,
nodeRestEndpoint,
namespace: namespace,
});
await hsSSiSdk.init();
return hsSSiSdk.vc.bjjVC;
}
}
61 changes: 54 additions & 7 deletions src/did/controllers/did.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { PaginationDto } from 'src/utils/pagination.dto';
import { Did } from '../schemas/did.schema';
import { DidResponseInterceptor } from '../interceptors/transformResponse.interseptor';
import { GetDidList } from '../dto/fetch-did.dto';
import { RegisterDidDto } from '../dto/register-did.dto';
import { RegisterDidDto, RegisterV2DidDto } from '../dto/register-did.dto';
import { IKeyType } from 'hs-ssi-sdk';
import { AtLeastOneParamPipe } from 'src/utils/Pipes/atleastOneParam.pipe';
import { AddVMResponse, AddVerificationMethodDto } from '../dto/addVm.dto';
Expand Down Expand Up @@ -131,7 +131,7 @@ export class DidController {
}
@UsePipes(
new ValidationPipe({
whitelist: true,
// whitelist: true,
transform: true,
forbidNonWhitelisted: true,
}),
Expand Down Expand Up @@ -169,7 +169,13 @@ export class DidController {
Logger.log('create() method: starts', 'DidController');
const { options } = createDidDto;
const appDetail = req.user;
switch (options?.keyType) {
const keyTypes = Array.isArray(options?.keyType)
? options.keyType
: options?.keyType
? [options.keyType]
: [IKeyType.Ed25519VerificationKey2020];
const keyTypeAtZeroIndex = keyTypes[0];
switch (keyTypeAtZeroIndex) {
case IKeyType.EcdsaSecp256k1RecoveryMethod2020: {
const response = this.didService.createByClientSpec(
createDidDto,
Expand All @@ -186,9 +192,21 @@ export class DidController {

return classToPlain(response, { excludePrefixes: ['transactionHash'] });
}
case IKeyType.BabyJubJubKey2021: {
const response = this.didService.createBjjDid(
createDidDto,
appDetail,
keyTypes,
);
return classToPlain(response, { excludePrefixes: ['transactionHash'] });
}

default:
const response = this.didService.create(createDidDto, appDetail);
const response = this.didService.create(
createDidDto,
appDetail,
keyTypes,
);
return classToPlain(response, { excludePrefixes: ['transactionHash'] });
}
}
Expand Down Expand Up @@ -224,7 +242,7 @@ export class DidController {
return this.didService.addVerificationMethod(addVm);
}

@Post('/sign')
@Post('/auth/sign')
@ApiOkResponse({
description: 'DidDocument is signed successfully',
type: SignedDidDocument,
Expand Down Expand Up @@ -254,7 +272,7 @@ export class DidController {
Logger.log('SignDidDocument() method: starts', 'DidController');
return this.didService.SignDidDocument(signDidDocDto, req.user);
}
@Post('/verify')
@Post('/auth/verify')
@ApiOkResponse({
description: 'DidDocument is verified successfully',
type: VerifyDidDocResponseDto,
Expand All @@ -275,7 +293,6 @@ export class DidController {
required: false,
})
@UsePipes(ValidationPipe)
@UsePipes(new AtLeastOneParamPipe(['did', 'didDocument']))
VerifyDidDocument(
@Headers('Authorization') authorization: string,
@Req() req: any,
Expand Down Expand Up @@ -353,4 +370,34 @@ export class DidController {
const appDetail = req.user;
return this.didService.updateDid(updateDidDto, appDetail);
}
@ApiOkResponse({
description: 'DID Registred',
type: RegisterDidResponse,
})
@ApiBadRequestResponse({
status: 400,
description: 'Error occured at the time of creating did',
type: DidError,
})
@ApiHeader({
name: 'Authorization',
description: 'Bearer <access_token>',
required: false,
})
@ApiHeader({
name: 'Origin',
description: 'Origin as you set in application cors',
required: false,
})
@UsePipes(ValidationPipe)
@Post('register/v2')
registerV2(
@Headers('Authorization') authorization: string,
@Body() registerV2Dto: RegisterV2DidDto,
@Req() req: any,
) {
Logger.log('registerV2() method: starts', 'DidController');
const appDetail = req.user;
return this.didService.registerV2(registerV2Dto, appDetail);
}
}
Loading

0 comments on commit 2784828

Please sign in to comment.