Skip to content

Commit 5ff1648

Browse files
committed
added logger
1 parent da42f7b commit 5ff1648

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/logger/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
const LOGGING = process.env.LOGGING == "1";
3+
4+
export const log = (type: string, source: string, message: string) => {
5+
if (!LOGGING) return;
6+
console.log(`\x1b[33m${new Date().toLocaleString()}\x1b[0m: [\x1b[36m${source} ${type}\x1b[0m] ${message}`);
7+
}

src/parser/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { XMLBuilder, XmlBuilderOptions, XMLParser } from "fast-xml-parser";
22
import _ from "lodash";
3+
import { log } from "../logger";
34

45
export interface XMLObject {[tag: string]: any};
56
export type XMLQueryResult = XMLObject[] | undefined;
@@ -140,7 +141,7 @@ export class XMLDocument {
140141

141142
return true;
142143
} catch(error: any) {
143-
console.log(error.message);
144+
log("Info", "Parser", error.message);
144145
}
145146

146147
return false;

src/zatca/signing/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { XMLDocument } from "../../parser";
99
import { generateQR } from "../qr";
1010
import defaultUBLExtensions from "../templates/ubl_sign_extension_template";
1111
import defaultUBLExtensionsSignedProperties, {defaultUBLExtensionsSignedPropertiesForSigning} from "../templates/ubl_extension_signed_properties_template";
12+
import { log } from "../../logger";
1213

1314
/**
1415
* Removes (UBLExtensions (Signing), Signature Envelope, and QR data) Elements. Then canonicalizes the XML to c14n.
@@ -149,15 +150,15 @@ export const generateSignedXMLString = ({invoice_xml, certificate_string, privat
149150

150151
// 1: Invoice Hash
151152
const invoice_hash = getInvoiceHash(invoice_xml);
152-
console.log("Invoice hash: ", invoice_hash);
153+
log("Info", "Signer", `Invoice hash: ${invoice_hash}`);
153154

154155
// 2: Certificate hash and certificate info
155156
const cert_info = getCertificateInfo(certificate_string);
156-
console.log("Certificate info: ", cert_info);
157+
log("Info", "Signer", `Certificate info: ${JSON.stringify(cert_info)}`);
157158

158159
// 3: Digital Certificate
159160
const digital_signature = createInvoiceDigitalSignature(invoice_hash, private_key_string);
160-
console.log("Digital signature: ", digital_signature);
161+
log("Info", "Signer", `Digital signature: ${digital_signature}`);
161162

162163
// 4: QR
163164
const qr = generateQR({
@@ -166,7 +167,7 @@ export const generateSignedXMLString = ({invoice_xml, certificate_string, privat
166167
public_key: cert_info.public_key,
167168
certificate_signature: cert_info.signature
168169
});
169-
console.log("QR: ", qr);
170+
log("Info", "Signer", `QR: ${qr}`);
170171

171172

172173
// Set Signed properties
@@ -183,7 +184,7 @@ export const generateSignedXMLString = ({invoice_xml, certificate_string, privat
183184
const signed_properties_bytes = Buffer.from(ubl_signature_signed_properties_xml_string_for_signing);
184185
let signed_properties_hash = createHash("sha256").update(signed_properties_bytes).digest('hex');
185186
signed_properties_hash = Buffer.from(signed_properties_hash).toString("base64");
186-
console.log("Signed properites hash: ", signed_properties_hash);
187+
log("Info", "Signer", `Signed properites hash: ${signed_properties_hash}`);
187188

188189
// UBL Extensions
189190
let ubl_signature_xml_string = defaultUBLExtensions(

0 commit comments

Comments
 (0)