Skip to content

Commit

Permalink
fix(certificates): adjust cert fields names
Browse files Browse the repository at this point in the history
refs #76
  • Loading branch information
ygrishajev committed May 13, 2024
1 parent 1da68e8 commit 21699d8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/certificates/certificate-manager/CertificateManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ describe("CertificateManager", () => {
now.setMilliseconds(0);
const inOneYear = getTime1yearFrom(now);
const pem = certificateManager.generatePEM(address);
const meta = certificateManager.parsePem(pem.certKey);
const meta = certificateManager.parsePem(pem.cert);

expect(pem).toMatchObject({
certKey: expect.stringMatching(/^-----BEGIN CERTIFICATE-----[\s\S]*-----END CERTIFICATE-----\r\n$/),
cert: expect.stringMatching(/^-----BEGIN CERTIFICATE-----[\s\S]*-----END CERTIFICATE-----\r\n$/),
publicKey: expect.stringMatching(/^-----BEGIN EC PUBLIC KEY-----[\s\S]*-----END EC PUBLIC KEY-----\r\n$/),
privateKey: expect.stringMatching(/^-----BEGIN PRIVATE KEY-----[\s\S]*-----END PRIVATE KEY-----\r\n$/)
});
Expand All @@ -40,10 +40,10 @@ describe("CertificateManager", () => {
validFrom: inOneMonth,
validTo: inTwoMonths
});
const meta = certificateManager.parsePem(pem.certKey);
const meta = certificateManager.parsePem(pem.cert);

expect(pem).toMatchObject({
certKey: expect.stringMatching(/^-----BEGIN CERTIFICATE-----[\s\S]*-----END CERTIFICATE-----\r\n$/),
cert: expect.stringMatching(/^-----BEGIN CERTIFICATE-----[\s\S]*-----END CERTIFICATE-----\r\n$/),
publicKey: expect.stringMatching(/^-----BEGIN EC PUBLIC KEY-----[\s\S]*-----END EC PUBLIC KEY-----\r\n$/),
privateKey: expect.stringMatching(/^-----BEGIN PRIVATE KEY-----[\s\S]*-----END PRIVATE KEY-----\r\n$/)
});
Expand All @@ -56,7 +56,7 @@ describe("CertificateManager", () => {

describe("prototype.parsePem", () => {
it("should extract certificate data", () => {
const cert = certificateManager.parsePem(certificateManager.generatePEM(address).certKey);
const cert = certificateManager.parsePem(certificateManager.generatePEM(address).cert);

expect(cert).toMatchObject({
hSerial: expect.any(String),
Expand Down
6 changes: 3 additions & 3 deletions src/certificates/certificate-manager/CertificateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import rs from "jsrsasign";

export interface CertificatePem {
certKey: string;
cert: string;
publicKey: string;
privateKey: string;
}
Expand Down Expand Up @@ -67,10 +67,10 @@ export class CertificateManager {
cakey: prvKeyObj
});
const publicKey: string = rs.KEYUTIL.getPEM(pubKeyObj, "PKCS8PUB").replaceAll("PUBLIC KEY", "EC PUBLIC KEY");
const certKey: string = cert.getPEM();
const certPEM: string = cert.getPEM();

return {
certKey,
cert: certPEM,
publicKey,
privateKey: rs.KEYUTIL.getPEM(prvKeyObj, "PKCS8PRV")
};
Expand Down
18 changes: 8 additions & 10 deletions src/certificates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ const jrpc = JsonRPC.connect_xhr("https://bridge.testnet.akash.network/akashnetw
export type { pems };

export async function broadcastCertificate(
pem: Pick<CertificatePem, "certKey" | "publicKey">,
pem: Pick<CertificatePem, "cert" | "publicKey">,
owner: string,
client: SigningStargateClient
): Promise<DeliverTxResponse>;
export async function broadcastCertificate(pem: pems, owner: string, client: SigningStargateClient): Promise<DeliverTxResponse>;
export async function broadcastCertificate(
pem: Pick<CertificatePem, "certKey" | "publicKey"> | pems,
pem: Pick<CertificatePem, "cert" | "publicKey"> | pems,
owner: string,
client: SigningStargateClient
): Promise<DeliverTxResponse> {
if ("csr" in pem) {
console.warn("The `csr` field is deprecated. Use `certKey` instead.");
console.warn("The `csr` field is deprecated. Use `cert` instead.");
}
const certKey = "certKey" in pem ? pem.certKey : pem.csr;
const certKey = "cert" in pem ? pem.cert : pem.csr;
const encodedCsr = base64ToUInt(toBase64(certKey));
const encdodedPublicKey = base64ToUInt(toBase64(pem.publicKey));
const message = createStarGateMessage(stargateMessages.MsgCreateCertificate, {
Expand All @@ -46,13 +46,11 @@ export async function createCertificate(bech32Address: string) {
const pem = certificateManager.generatePEM(bech32Address);

return {
...pem,
get csr() {
console.warn("The `csr` field is deprecated. Use `certKey` instead.");
return pem.certKey;
},
certKey: pem.certKey,
publicKey: pem.publicKey,
privateKey: pem.privateKey
console.warn("The `csr` field is deprecated. Use `cert` instead.");
return pem.cert;
}
};
}

Expand Down

0 comments on commit 21699d8

Please sign in to comment.