Skip to content

Commit

Permalink
Adding generateSignignKey function
Browse files Browse the repository at this point in the history
  • Loading branch information
mattschoch committed Mar 4, 2024
1 parent ba6295b commit 920ce27
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
38 changes: 38 additions & 0 deletions apps/policy-engine/src/app/core/signing.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { toHex } from '@narval/policy-engine-shared'
import { Alg } from '@narval/signature'
import { Injectable } from '@nestjs/common'
import { secp256k1 } from '@noble/curves/secp256k1'
import { EncryptionService } from '../../encryption/core/encryption.service'

// Optional additional configs, such as for MPC-based DKG.
type KeyGenerationOptions = {
keyId: string
}

type KeyGenerationResponse = {
publicKey: string
encryptedPrivateKey?: string
keyId?: string
}

@Injectable()
export class SigningService {
constructor(private encryptionService: EncryptionService) {}

async generateSigningKey(alg: Alg, options?: KeyGenerationOptions): Promise<KeyGenerationResponse> {

Check failure on line 22 in apps/policy-engine/src/app/core/signing.service.ts

View workflow job for this annotation

GitHub Actions / Build and test

'options' is defined but never used
if (alg === Alg.ES256K) {
const privateKey = toHex(secp256k1.utils.randomPrivateKey())
const publicKey = toHex(secp256k1.getPublicKey(privateKey.slice(2), false))

const encryptedPrivateKey = await this.encryptionService.encrypt(privateKey)
const encryptedPrivateKeyHex = toHex(encryptedPrivateKey)

return {
publicKey,
encryptedPrivateKey: encryptedPrivateKeyHex
}
}

throw new Error('Unsupported algorithm')
}
}
44 changes: 39 additions & 5 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@nestjs/core": "^10.3.0",
"@nestjs/platform-express": "^10.3.3",
"@nestjs/swagger": "^7.2.0",
"@noble/curves": "1.3.0",
"@open-policy-agent/opa-wasm": "^1.8.0",
"@prisma/client": "^5.7.1",
"axios": "^1.6.7",
Expand Down

0 comments on commit 920ce27

Please sign in to comment.