Skip to content

Commit

Permalink
feat: implemented CryptoSignaturePrivateKeyHandle
Browse files Browse the repository at this point in the history
  • Loading branch information
WyvernIXTL committed Feb 19, 2025
1 parent bfce95e commit b2f592c
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/crypto-layer/CryptoAsymmetricKeyHandle.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { SerializableAsync, serialize, type, validate } from "@js-soft/ts-serval";
import { KeyPairHandle, KeyPairSpec, Provider } from "@nmshd/rs-crypto-types";
import { CoreBuffer } from "src/CoreBuffer";
import { CryptoError } from "../CryptoError";
import { CryptoErrorCode } from "../CryptoErrorCode";
import { CryptoSerializableAsync } from "../CryptoSerializable";
import { getProvider } from "./CryptoLayerProviders";

/**
* Loose check if `value` can be initialized as if it was a `CryptoAsymmetricKeyHandle`.
*/
function isCryptoAsymmetricKeyHandle(value: any): value is CryptoAsymmetricKeyHandle {
return typeof value["providerName"] === "string" && typeof value["id"] === "string";
}

@type("CryptoAsymmetricKeyHandle")
export class CryptoAsymmetricKeyHandle extends CryptoSerializableAsync {
@validate()
Expand Down Expand Up @@ -48,8 +56,12 @@ export class CryptoAsymmetricKeyHandle extends CryptoSerializableAsync {
return await this.fromAny(value);
}

public static async fromBase64(value: string): Promise<CryptoAsymmetricKeyHandle> {
return await this.deserialize(CoreBuffer.base64_utf8(value));
}

public static override async postFrom<T extends SerializableAsync>(value: T): Promise<T> {
if (!(value instanceof CryptoAsymmetricKeyHandle)) {
if (!isCryptoAsymmetricKeyHandle(value)) {
throw new CryptoError(CryptoErrorCode.WrongParameters, `Expected 'CryptoAsymmetricKeyHandle'.`);
}
const provider = getProvider({ providerName: value.providerName });
Expand Down
59 changes: 59 additions & 0 deletions src/crypto-layer/signature/CryptoSignaturePrivateKeyHandle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { ISerializable, ISerialized, type } from "@js-soft/ts-serval";
import { KeyPairSpec } from "@nmshd/rs-crypto-types";
import { CoreBuffer } from "src/CoreBuffer";
import { CryptoPrivateKeyHandle } from "../CryptoPrivateKeyHandle";

export interface ICryptoSignaturePrivateKeyHandleSerialized extends ISerialized {
spc: KeyPairSpec; // Specification/Config of key pair stored.
cid: string; // Crypto layer key pair id used for loading key from a provider.
pnm: string; // Provider name
}
export interface ICryptoSignaturePrivateKeyHandle extends ISerializable {
spec: KeyPairSpec;
id: string;
providerName: string;
}

@type("CryptoSignaturePrivateKeyHandle")
export class CryptoSignaturePrivateKeyHandle extends CryptoPrivateKeyHandle {
public override toJSON(verbose = true): ICryptoSignaturePrivateKeyHandleSerialized {
return {
spc: this.spec,
cid: this.id,
pnm: this.providerName,
"@type": verbose ? "CryptoSignaturePrivateKeyHandle" : undefined
};
}

public override toBase64(verbose = true): string {
return CoreBuffer.utf8_base64(this.serialize(verbose));
}

public static override async from(
value: CryptoSignaturePrivateKeyHandle | ICryptoSignaturePrivateKeyHandle
): Promise<CryptoSignaturePrivateKeyHandle> {
return await this.fromAny(value);
}

public static override preFrom(value: any): any {
if (value.cid) {
value = {
spec: value.spc,
id: value.cid,
providerName: value.pnm
};
}

return value;
}

public static async fromJSON(
value: ICryptoSignaturePrivateKeyHandleSerialized
): Promise<CryptoSignaturePrivateKeyHandle> {
return await this.fromAny(value);
}

public static override async fromBase64(value: string): Promise<CryptoSignaturePrivateKeyHandle> {
return await this.deserialize(CoreBuffer.base64_utf8(value));
}
}

0 comments on commit b2f592c

Please sign in to comment.