@@ -9,6 +9,7 @@ import { XMLDocument } from "../../parser";
99import { generateQR } from "../qr" ;
1010import defaultUBLExtensions from "../templates/ubl_sign_extension_template" ;
1111import 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.
@@ -137,32 +138,27 @@ interface generateSignatureXMLParams {
137138}
138139/**
139140 * Main signing function.
140- * Signs the invoice according to the following steps:
141- * - Get the invoice hash
142- * - Invoice must be cleaned up first before hashing and indentations, trailing lines, spaces must match.
143- * - Get the certificate hash
144- * -
145141 * @param invoice_xml XMLDocument of invoice to be signed.
146142 * @param certificate_string String signed EC certificate.
147143 * @param private_key_string String ec-secp256k1 private key;
148- * @returns
144+ * @returns signed_invoice_string: string, invoice_hash: string, qr: string
149145 */
150146export const generateSignedXMLString = ( { invoice_xml, certificate_string, private_key_string} : generateSignatureXMLParams ) :
151- { signed_invoice_string : string , invoice_hash : string } => {
147+ { signed_invoice_string : string , invoice_hash : string , qr : string } => {
152148
153149 const invoice_copy : XMLDocument = new XMLDocument ( invoice_xml . toString ( { no_header : false } ) ) ;
154150
155151 // 1: Invoice Hash
156152 const invoice_hash = getInvoiceHash ( invoice_xml ) ;
157- console . log ( "Invoice hash: " , invoice_hash ) ;
153+ log ( "Info" , "Signer" , ` Invoice hash: ${ invoice_hash } ` ) ;
158154
159155 // 2: Certificate hash and certificate info
160156 const cert_info = getCertificateInfo ( certificate_string ) ;
161- console . log ( "Certificate info: " , cert_info ) ;
157+ log ( "Info" , "Signer" , ` Certificate info: ${ JSON . stringify ( cert_info ) } ` ) ;
162158
163159 // 3: Digital Certificate
164160 const digital_signature = createInvoiceDigitalSignature ( invoice_hash , private_key_string ) ;
165- console . log ( "Digital signature: " , digital_signature ) ;
161+ log ( "Info" , "Signer" , ` Digital signature: ${ digital_signature } ` ) ;
166162
167163 // 4: QR
168164 const qr = generateQR ( {
@@ -171,7 +167,7 @@ export const generateSignedXMLString = ({invoice_xml, certificate_string, privat
171167 public_key : cert_info . public_key ,
172168 certificate_signature : cert_info . signature
173169 } ) ;
174- console . log ( "QR: " , qr ) ;
170+ log ( "Info" , "Signer" , `QR: ${ qr } ` ) ;
175171
176172
177173 // Set Signed properties
@@ -188,7 +184,7 @@ export const generateSignedXMLString = ({invoice_xml, certificate_string, privat
188184 const signed_properties_bytes = Buffer . from ( ubl_signature_signed_properties_xml_string_for_signing ) ;
189185 let signed_properties_hash = createHash ( "sha256" ) . update ( signed_properties_bytes ) . digest ( 'hex' ) ;
190186 signed_properties_hash = Buffer . from ( signed_properties_hash ) . toString ( "base64" ) ;
191- console . log ( "Signed properites hash: " , signed_properties_hash ) ;
187+ log ( "Info" , "Signer" , ` Signed properites hash: ${ signed_properties_hash } ` ) ;
192188
193189 // UBL Extensions
194190 let ubl_signature_xml_string = defaultUBLExtensions (
@@ -208,7 +204,7 @@ export const generateSignedXMLString = ({invoice_xml, certificate_string, privat
208204 let signed_invoice_string : string = signed_invoice . toString ( { no_header : false } ) ;
209205 signed_invoice_string = signedPropertiesIndentationFix ( signed_invoice_string ) ;
210206
211- return { signed_invoice_string : signed_invoice_string , invoice_hash : invoice_hash } ;
207+ return { signed_invoice_string : signed_invoice_string , invoice_hash : invoice_hash , qr } ;
212208}
213209
214210
0 commit comments