Skip to content

Commit ac92ca3

Browse files
committed
feat: prototype private key handle
1 parent bab59c5 commit ac92ca3

File tree

1 file changed

+27
-64
lines changed

1 file changed

+27
-64
lines changed

src/CryptoPrivateKeyHandle.ts

Lines changed: 27 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,54 @@
11
import { serialize, type, validate } from "@js-soft/ts-serval";
2-
import { CoreBuffer, Encoding, ICoreBuffer } from "./CoreBuffer";
2+
import { KeyPairHandle, KeyPairSpec, Provider } from "@nmshd/rs-crypto-types";
3+
import { CryptoError } from "./CryptoError";
4+
import { CryptoErrorCode } from "./CryptoErrorCode";
5+
import { getProvider } from "./CryptoLayerProviders";
36
import { CryptoSerializable } from "./CryptoSerializable";
4-
import { CryptoExchangeAlgorithm } from "./exchange/CryptoExchange";
5-
import { CryptoSignatureAlgorithm } from "./signature/CryptoSignatureAlgorithm";
67

78
export interface ICryptoPrivateKeyHandle {
8-
privateKey: ICoreBuffer;
9-
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm;
10-
toString(): string;
11-
toPEM(): string;
9+
keyPairHandle: KeyPairHandle;
10+
spec: KeyPairSpec;
1211
}
1312

1413
export interface ICryptoPrivateKeyHandleStatic {
1514
new (): ICryptoPrivateKeyHandle;
16-
fromPEM(
17-
pem: string,
18-
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm
19-
): Promise<ICryptoPrivateKeyHandle>;
20-
fromString(
21-
value: string,
22-
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm,
23-
encoding: Encoding
24-
): Promise<ICryptoPrivateKeyHandle>;
25-
fromNativeKey(
26-
key: any,
27-
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm
28-
): Promise<ICryptoPrivateKeyHandle>;
15+
fromNativeKey(key: any, spec: KeyPairSpec): Promise<ICryptoPrivateKeyHandle>;
2916
}
3017

3118
@type("CryptoPrivateKeyHandle")
3219
export class CryptoPrivateKeyHandle extends CryptoSerializable implements ICryptoPrivateKeyHandle {
3320
@validate()
3421
@serialize()
35-
public algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm;
22+
public spec: KeyPairSpec;
3623

3724
@validate()
3825
@serialize()
39-
public privateKey: CoreBuffer;
26+
public id: string;
4027

41-
public toPEM(): string {
42-
return this.privateKey.toString(Encoding.Pem, "PRIVATE KEY");
43-
}
44-
45-
public override toString(): string {
46-
return this.privateKey.toString(Encoding.Base64_UrlSafe_NoPadding);
47-
}
48-
49-
protected static stripPEM(pem: string): string {
50-
pem = pem.replace(/-----BEGIN [\w ]* KEY-----/, "");
51-
pem = pem.replace(/-----END [\w ]* KEY-----/, "");
52-
pem = pem.replace(/----- BEGIN [\w ]* KEY -----/, "");
53-
pem = pem.replace(/----- END [\w ]* KEY -----/, "");
54-
pem = pem.replace(/(?:\r\n|\r|\n)/g, "");
55-
return pem;
56-
}
57-
58-
public static fromString(
59-
value: string,
60-
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm,
61-
encoding: Encoding = Encoding.Base64_UrlSafe_NoPadding
62-
): CryptoPrivateKeyHandle {
63-
const buffer: CoreBuffer = CoreBuffer.fromString(value, encoding);
64-
return this.fromAny({ algorithm, privateKey: buffer });
65-
}
66-
67-
public static fromObject(
68-
value: any,
69-
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm
70-
): CryptoPrivateKeyHandle {
71-
const buffer: ICoreBuffer = CoreBuffer.fromObject(value);
28+
@validate()
29+
@serialize()
30+
public providerName: string;
7231

73-
return this.fromAny({ algorithm, privateKey: buffer });
74-
}
32+
public provider: Provider;
7533

76-
public static fromPEM(
77-
pem: string,
78-
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm
79-
): CryptoPrivateKeyHandle {
80-
const value = this.stripPEM(pem);
81-
return this.fromString(value, algorithm, Encoding.Base64);
82-
}
34+
public keyPairHandle: KeyPairHandle;
8335

8436
public static from(value: any): CryptoPrivateKeyHandle {
8537
return this.fromAny(value);
8638
}
8739

88-
public static fromBase64(value: string): CryptoPrivateKeyHandle {
89-
return this.deserialize(CoreBuffer.base64_utf8(value));
40+
public static override async postFrom(value: CryptoPrivateKeyHandle): Promise<CryptoPrivateKeyHandle> {
41+
const provider = getProvider(value.providerName);
42+
if (!provider) {
43+
throw new CryptoError(
44+
CryptoErrorCode.CalFailedLoadingProvider,
45+
`Failed loading provider ${value.providerName}`
46+
);
47+
}
48+
const keyHandle = await provider.loadKeyPair(value.id);
49+
50+
value.keyPairHandle = keyHandle;
51+
value.provider = provider;
52+
return value;
9053
}
9154
}

0 commit comments

Comments
 (0)