Skip to content

Commit

Permalink
fix(certificates): 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 5bb78af
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/certificates/generate509/generate509.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { faker } from "@faker-js/faker";

import { create } from "./generate509";

describe("generate509", () => {
it("should generate a CSR", async () => {
const address = `akash1${faker.string.alpha({ length: 38 })}`;
const cert = await create(address);

expect(cert).toMatchObject({
csr: expect.stringMatching(/^-----BEGIN CERTIFICATE-----[\s\S]*-----END CERTIFICATE-----$/),
publicKey: expect.stringMatching(/^-----BEGIN EC PUBLIC KEY-----[\s\S]*-----END EC PUBLIC KEY-----$/),
privateKey: expect.stringMatching(/^-----BEGIN PRIVATE KEY-----[\s\S]*-----END PRIVATE KEY-----$/)
});
});
});
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 @@ -23,7 +23,7 @@ export interface pems {
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 5bb78af

Please sign in to comment.