Skip to content
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
20 changes: 18 additions & 2 deletions modules/key-card/src/drawKeycard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import { jsPDF } from 'jspdf';
import type { jsPDF } from 'jspdf';
import * as QRCode from 'qrcode';
import { IDrawKeyCard } from './types';
import { splitKeys } from './utils';
type jsPDFModule = typeof import('jspdf');

async function loadJSPDF(): Promise<jsPDFModule> {
let jsPDF: jsPDFModule;

if (typeof window !== 'undefined' && typeof window.document !== 'undefined') {
// We are in the browser
jsPDF = await import('jspdf');
} else {
// We are in Node.js
jsPDF = require('jspdf');
}
return jsPDF;
}

enum KeyCurveName {
ed25519 = 'EDDSA',
Expand Down Expand Up @@ -97,12 +111,14 @@ export async function drawKeycard({
walletLabel,
curve,
}: IDrawKeyCard): Promise<jsPDF> {
const jsPDFModule = await loadJSPDF();

// document details
const width = 8.5 * 72;
let y = 0;

// Create the PDF instance
const doc = new jsPDF('portrait', 'pt', 'letter'); // jshint ignore:line
const doc = new jsPDFModule.jsPDF('portrait', 'pt', 'letter'); // jshint ignore:line
doc.setFont('helvetica');

// PDF Header Area - includes the logo and company name
Expand Down
11 changes: 4 additions & 7 deletions modules/key-card/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import jsPDF from 'jspdf';

import { generateQrData } from './generateQrData';
import { generateFaq } from './faq';
import { drawKeycard } from './drawKeycard';
Expand All @@ -13,17 +11,16 @@ export * from './utils';
export * from './types';

export async function generateKeycard(params: GenerateKeycardParams): Promise<void> {
let keycard: jsPDF;
if ('coin' in params) {
const questions = generateFaq(params.coin.fullName);
const qrData = generateQrData(params);
keycard = await drawKeycard({ ...params, questions, qrData });
const keycard = await drawKeycard({ ...params, questions, qrData });
keycard.save(`BitGo Keycard for ${params.walletLabel}.pdf`);
} else if ('curve' in params) {
const data = generateParamsForKeyCreation(params);
keycard = await drawKeycard(data);
const keycard = await drawKeycard(data);
keycard.save(`BitGo Keycard for ${params.walletLabel}.pdf`);
} else {
throw new Error('Either curve or coin must be provided');
}
// Save the PDF on the user's browser
keycard.save(`BitGo Keycard for ${params.walletLabel}.pdf`);
}