Skip to content

Commit

Permalink
fix(cert): properly import lib
Browse files Browse the repository at this point in the history
refs #76
  • Loading branch information
ygrishajev committed May 7, 2024
1 parent cfbb507 commit 3e6bc2c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/certificates/generate509/generate509.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { create } from "./generate509";

describe("generate509", () => {
it("should generate a CSR", async () => {
const address = "";
const result = await create(address);
expect(result).toBeDefined();
expect(result.csr).toBeDefined();
expect(result.publicKey).toBeDefined();
expect(result.privateKey).toBeDefined();
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { arrayBufferToString, toBase64 } from "pvutils";

import asn1js from "asn1js";
import * as asn1js from "asn1js";

global.crypto = require("node:crypto");

Expand All @@ -17,13 +17,13 @@ const {
const HASH_ALG = "SHA-256";
const SIGN_ALG = "ECDSA";

export interface pems {
export type pems = {
csr: string;
publicKey: string;
privateKey: string;
}
};

export async function create(address: string) {
export async function create(address: string): Promise<pems> {
// get crypto handler
const crypto = getCrypto();

Expand All @@ -41,13 +41,11 @@ export async function create(address: string) {
const spki = await crypto.exportKey("spki", keyPair.privateKey);
const pkcs8 = await crypto.exportKey("pkcs8", keyPair.privateKey);

const pems = {
return {
csr: `-----BEGIN CERTIFICATE-----\n${formatPEM(toBase64(arrayBufferToString(certBER)))}\n-----END CERTIFICATE-----`,
privateKey: `-----BEGIN PRIVATE KEY-----\n${formatPEM(toBase64(arrayBufferToString(pkcs8)))}\n-----END PRIVATE KEY-----`,
publicKey: `-----BEGIN EC PUBLIC KEY-----\n${formatPEM(toBase64(arrayBufferToString(spki)))}\n-----END EC PUBLIC KEY-----`
};

return pems;
}

async function createCSR(keyPair: { privateKey: string; publicKey: string }, hashAlg: string, { commonName }: { commonName: string }) {
Expand Down
1 change: 1 addition & 0 deletions src/certificates/generate509/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./generate509";

0 comments on commit 3e6bc2c

Please sign in to comment.