Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cert): properly import lib #87

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
Loading