Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bjj/temp #157

Merged
merged 6 commits into from
Oct 22, 2024
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
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 { 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 @@
);
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 @@
privateKeyMultibase,
registerCredential: false,
});

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

Expand Down Expand Up @@ -335,20 +370,41 @@
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 All @@ -361,7 +417,7 @@
'CredentialService',
);

const { wallet, address } = await this.hidWallet.generateWallet(

Check warning on line 420 in src/credential/services/credential.service.ts

View workflow job for this annotation

GitHub Actions / build

'wallet' is assigned a value but never used
appMenemonic,
);
let updatedCredResult;
Expand Down Expand Up @@ -452,12 +508,25 @@
'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 All @@ -481,7 +550,7 @@
);

const { credentialStatus, namespace } = registerCredentialDto;
const credentialId = credentialStatus.id;

Check warning on line 553 in src/credential/services/credential.service.ts

View workflow job for this annotation

GitHub Actions / build

'credentialId' is assigned a value but never used
const { kmsId } = appDetail;
Logger.log(
'registerCredentialStatus() method: initialising edv service',
Expand All @@ -499,15 +568,25 @@

delete credentialStatus['proof'];

const { wallet, address } = await this.hidWallet.generateWallet(

Check warning on line 571 in src/credential/services/credential.service.ts

View workflow job for this annotation

GitHub Actions / build

'wallet' is assigned a value but never used
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
21 changes: 20 additions & 1 deletion 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 Down Expand Up @@ -33,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;
}
}
56 changes: 52 additions & 4 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 @@ -352,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);
}
}
35 changes: 18 additions & 17 deletions src/did/dto/create-did.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ApiHideProperty, ApiProperty } from '@nestjs/swagger';
import { Exclude, Type } from 'class-transformer';
import {
IsArray,

Check warning on line 4 in src/did/dto/create-did.dto.ts

View workflow job for this annotation

GitHub Actions / build

'IsArray' is defined but never used
IsBoolean,
IsEnum,
IsObject,
Expand All @@ -16,6 +16,7 @@
import { IsDid } from 'src/utils/customDecorator/did.decorator';
import { ValidatePublicKeyMultibase } from 'src/utils/customDecorator/pubKeyMultibase.decorator';
import { IVerificationRelationships, IKeyType } from 'hs-ssi-sdk';
import { IsKeyTypeArrayOrSingle } from 'src/utils/customDecorator/keyType.decorator';

export enum Namespace {
testnet = 'testnet',
Expand All @@ -24,14 +25,14 @@
export class Options {
@ApiProperty({
description:
'Verification Method Keytype Ed25519VerificationKey2020 or EcdsaSecp256k1RecoveryMethod2020',
'Verification Method Keytype Ed25519VerificationKey2020 or EcdsaSecp256k1RecoveryMethod2020 or BabyJubJubKey2021',
example: 'keyType:EcdsaSecp256k1RecoveryMethod2020',
name: 'keyType',
required: false,
})
@ValidateIf((o) => o.keyType !== undefined)
@IsEnum(IKeyType)
keyType: IKeyType;
@IsKeyTypeArrayOrSingle()
keyType: IKeyType | IKeyType[];

@ApiProperty({
name: 'chainId',
Expand Down Expand Up @@ -67,18 +68,18 @@
@IsBoolean()
register?: boolean = false; // keeping it for time being will remove it later

@ApiProperty({
description:
'verificationRelationships defines verification methods to be used for which purposes',
example: 'authentication/ assertionMethod',
name: 'verificationRelationships',
required: false,
isArray: true,
})
@IsOptional()
@IsArray()
@IsEnum(IVerificationRelationships, { each: true })
verificationRelationships?: IVerificationRelationships[];
// @ApiProperty({
// description:
// 'verificationRelationships defines verification methods to be used for which purposes',
// example: 'authentication/ assertionMethod',
// name: 'verificationRelationships',
// required: false,
// isArray: true,
// })
// @IsOptional()
// @IsArray()
// @IsEnum(IVerificationRelationships, { each: true })
// verificationRelationships?: IVerificationRelationships[];

@ApiProperty({
name: 'name',
Expand Down Expand Up @@ -115,11 +116,11 @@
description: ' keyType used for verification',
required: false,
example: {
keyType: 'Ed25519VerificationKey2020',
keyType: ['Ed25519VerificationKey2020'],
chainId: '0x1',
publicKey: 'z76tzt4XCb6FNqC3CPZvsxRfEDX5HHQc2VPux4DeZYndW',
walletAddress: '0x01978e553Df0C54A63e2E063DFFe71c688d91C76',
verificationRelationships: ['assertionMethod', 'authentication'],
// verificationRelationships: ['assertionMethod', 'authentication'],
},
})
@IsOptional()
Expand Down
Loading
Loading