-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba6295b
commit 920ce27
Showing
3 changed files
with
78 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
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') | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters