diff --git a/test/crypto-layer/CryptoSignaturePrivateKeyHandle.test.ts b/test/crypto-layer/CryptoSignaturePrivateKeyHandle.test.ts index aed6e0b..2fffe74 100644 --- a/test/crypto-layer/CryptoSignaturePrivateKeyHandle.test.ts +++ b/test/crypto-layer/CryptoSignaturePrivateKeyHandle.test.ts @@ -3,26 +3,40 @@ import { CryptoHashAlgorithm, CryptoSignature } from "@nmshd/crypto"; import { KeyPairSpec } from "@nmshd/rs-crypto-types"; import { expect } from "chai"; import { CoreBuffer } from "src/CoreBuffer"; +import { CryptoSignaturePrivateKeyHandle } from "src/crypto-layer/signature/CryptoSignaturePrivateKeyHandle"; import { CryptoSignatures } from "src/signature/CryptoSignatures"; -import { expectCryptoSignatureKeypairHandle } from "./CryptoSignatureKeypairHandle.test"; + +export async function expectCryptoSignaturePrivateKeyHandle( + value: CryptoSignaturePrivateKeyHandle, + id: string, + spec: KeyPairSpec, + providerName: string +): Promise { + expect(value).to.be.instanceOf(CryptoSignaturePrivateKeyHandle); + expect(value.id).to.equal(id); + expect(value.providerName).to.equal(providerName); + expect(value.spec).to.deep.equal(spec); + expect(value.keyPairHandle).to.be.ok; + expect(value.provider).to.be.ok; + expect(await value.keyPairHandle.id()).to.equal(id); + expect(await value.provider.providerName()).to.equal(providerName); +} export class CryptoSignaturePrivateKeyHandleTest { public static run(): void { describe("CryptoSignaturePrivateKeyHandle", function () { - describe("sign() with SoftwareProvider", function () { - it("sign() and verify() with P256", async function () { - const spec: KeyPairSpec = { - asym_spec: "P256", - cipher: null, - signing_hash: "Sha2_512", - ephemeral: false, - non_exportable: false - }; - const cryptoKeyPairHandle = await CryptoSignatures.generateKeypairHandle( - { providerName: "SoftwareProvider" }, - spec - ); - await expectCryptoSignatureKeypairHandle(cryptoKeyPairHandle, "SoftwareProvider", spec); + describe("CryptoSignaturePrivateKeyHandle SoftwareProvider P256 Sha2_512", function () { + const spec: KeyPairSpec = { + asym_spec: "P256", + cipher: null, + signing_hash: "Sha2_512", + ephemeral: false, + non_exportable: false + }; + const providerIdent = { providerName: "SoftwareProvider" }; + + it("sign and verify", async function () { + const cryptoKeyPairHandle = await CryptoSignatures.generateKeypairHandle(providerIdent, spec); const data = new CoreBuffer("0123456789ABCDEF"); const signature = await CryptoSignatures.sign( @@ -41,6 +55,51 @@ export class CryptoSignaturePrivateKeyHandleTest { true ); }); + + it("toJSON and fromJSON", async function () { + const cryptoKeyPairHandle = await CryptoSignatures.generateKeypairHandle(providerIdent, spec); + const privateKeyHandle = cryptoKeyPairHandle.privateKey; + const id = privateKeyHandle.id; + const providerName = privateKeyHandle.providerName; + + const serializedPrivateKeyHandle = privateKeyHandle.toJSON(); + expect(serializedPrivateKeyHandle).to.be.instanceOf(Object); + expect(serializedPrivateKeyHandle.cid).to.equal(id); + expect(serializedPrivateKeyHandle.pnm).to.equal(providerName); + expect(serializedPrivateKeyHandle.spc).to.deep.equal(spec); + expect(serializedPrivateKeyHandle["@type"]).to.equal("CryptoSignaturePrivateKeyHandle"); + + const loadedPrivateKeyHandle = + await CryptoSignaturePrivateKeyHandle.fromJSON(serializedPrivateKeyHandle); + await expectCryptoSignaturePrivateKeyHandle(loadedPrivateKeyHandle, id, spec, providerName); + }); + + it("toBase64 and fromBase64", async function () { + const cryptoKeyPairHandle = await CryptoSignatures.generateKeypairHandle(providerIdent, spec); + const privateKeyHandle = cryptoKeyPairHandle.privateKey; + const id = privateKeyHandle.id; + const providerName = privateKeyHandle.providerName; + + const serializedPrivateKey = privateKeyHandle.toBase64(); + expect(serializedPrivateKey).to.be.ok; + const deserializedPrivateKey = CryptoSignaturePrivateKeyHandle.fromBase64(serializedPrivateKey); + await expectCryptoSignaturePrivateKeyHandle(await deserializedPrivateKey, id, spec, providerName); + }); + + // eslint-disable-next-line jest/expect-expect + it("from", async function () { + const cryptoKeyPairHandle = await CryptoSignatures.generateKeypairHandle(providerIdent, spec); + const privateKeyHandle = cryptoKeyPairHandle.privateKey; + const id = privateKeyHandle.id; + const providerName = privateKeyHandle.providerName; + + const loadedPrivateKeyHandle = await CryptoSignaturePrivateKeyHandle.from({ + spec: spec, + id: id, + providerName: providerName + }); + await expectCryptoSignaturePrivateKeyHandle(loadedPrivateKeyHandle, id, spec, providerName); + }); }); }); } diff --git a/test/index.ts b/test/index.ts index d1d8ed0..0f875b8 100644 --- a/test/index.ts +++ b/test/index.ts @@ -5,6 +5,7 @@ import { getAllProviders, getProviderCapabilities } from "@nmshd/rs-crypto-node"; +import chai from "chai"; import { BufferTest } from "./BufferTest.test"; import { CryptoLayerProviderTest } from "./crypto-layer/CryptoLayerProviderTest.test"; import { CryptoSignatureKeypairHandleTest } from "./crypto-layer/CryptoSignatureKeypairHandle.test"; @@ -24,6 +25,8 @@ import { CryptoSignatureTest } from "./crypto/CryptoSignature.test"; import { CryptoStateTest } from "./crypto/CryptoStateTest.test"; import { SodiumWrapperTest } from "./crypto/SodiumWrapperTest.test"; +chai.config.truncateThreshold = 0; + // This is valid: https://mochajs.org/#delayed-root-suite // eslint-disable-next-line @typescript-eslint/no-floating-promises (async function () {