|
1 | 1 | import { type } from "@js-soft/ts-serval";
|
2 | 2 | import { KeyPairHandle, KeyPairSpec } from "@nmshd/rs-crypto-types";
|
| 3 | +import { CoreBuffer, Encoding } from "src/CoreBuffer"; |
3 | 4 | import { CryptoAsymmetricKeyHandle } from "./CryptoAsymmetricKeyHandle";
|
| 5 | +import { getProviderOrThrow, ProviderIdentifier } from "./CryptoLayerProviders"; |
4 | 6 |
|
5 | 7 | export interface ICryptoPrivateKeyHandle {
|
6 | 8 | keyPairHandle: KeyPairHandle;
|
7 | 9 | spec: KeyPairSpec;
|
| 10 | + toSerializedString(): Promise<string>; |
| 11 | + toPEM(): Promise<string>; |
8 | 12 | }
|
9 | 13 |
|
10 | 14 | export interface ICryptoPrivateKeyHandleStatic {
|
11 | 15 | new (): ICryptoPrivateKeyHandle;
|
12 |
| - fromNativeKey(key: any, spec: KeyPairSpec): Promise<ICryptoPrivateKeyHandle>; |
| 16 | + fromPEM(providerIdent: ProviderIdentifier, pem: string, config: KeyPairSpec): Promise<ICryptoPrivateKeyHandle>; |
| 17 | + fromString( |
| 18 | + providerIdent: ProviderIdentifier, |
| 19 | + value: string, |
| 20 | + config: KeyPairSpec, |
| 21 | + encoding: Encoding |
| 22 | + ): Promise<ICryptoPrivateKeyHandle>; |
| 23 | + // fromNativeKey(providerIdent: ProviderIdentifier, key: any, config: KeyPairSpec): Promise<ICryptoPrivateKeyHandle>; |
13 | 24 | }
|
14 | 25 |
|
15 | 26 | @type("CryptoPrivateKeyHandle")
|
16 |
| -export class CryptoPrivateKeyHandle extends CryptoAsymmetricKeyHandle implements ICryptoPrivateKeyHandle {} |
| 27 | +export class CryptoPrivateKeyHandle extends CryptoAsymmetricKeyHandle implements ICryptoPrivateKeyHandle { |
| 28 | + public async toSerializedString(): Promise<string> { |
| 29 | + const raw = await this.keyPairHandle.extractKey(); |
| 30 | + return CoreBuffer.from(raw).toString(Encoding.Base64_UrlSafe_NoPadding); |
| 31 | + } |
| 32 | + |
| 33 | + public async toPEM(): Promise<string> { |
| 34 | + const raw = await this.keyPairHandle.extractKey(); |
| 35 | + return CoreBuffer.from(raw).toString(Encoding.Pem, "PRIVATE KEY"); |
| 36 | + } |
| 37 | + |
| 38 | + public static async fromString( |
| 39 | + providerIdent: ProviderIdentifier, |
| 40 | + value: string, |
| 41 | + config: KeyPairSpec, |
| 42 | + encoding: Encoding |
| 43 | + ): Promise<CryptoPrivateKeyHandle> { |
| 44 | + const raw = CoreBuffer.fromString(value, encoding).buffer; |
| 45 | + const provider = getProviderOrThrow(providerIdent); |
| 46 | + const keyPairHandle = await provider.importKeyPair(config, new Uint8Array(0), raw); |
| 47 | + return await CryptoPrivateKeyHandle.newFromProviderAndKeyPairHandle(provider, keyPairHandle, { |
| 48 | + keySpec: config |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + public static async fromPEM( |
| 53 | + providerIdent: ProviderIdentifier, |
| 54 | + pem: string, |
| 55 | + config: KeyPairSpec |
| 56 | + ): Promise<CryptoPrivateKeyHandle> { |
| 57 | + return await CryptoPrivateKeyHandle.fromString(providerIdent, pem, config, Encoding.Pem); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +const _testAssign: ICryptoPrivateKeyHandleStatic = CryptoPrivateKeyHandle; |
0 commit comments