LacunaWebPKI: Sign XML Element (NFe - Brazil invoice)
HTML
<script src="https://get.webpkiplugin.com/Scripts/LacunaWebPKI/lacuna-web-pki-2.9.0.js"></script>
<h2>Web PKI - Sign XML Element locally (NFe - Brazil invoice)</h2>
<select id="certificateSelect"></select>
<button id="refreshCertificates" type="button">Refresh Certificates</button><br/><br/>
<button id="signXml" type="button">Sign</button><br/>
<h3>XML to sign</h3>
<textarea id="xmlArea" rows="10" cols="170"></textarea><br />
<h3>Signed XML</h3>
<textarea id="signedXmlArea" rows="5" cols="170"></textarea>
<div id="logPanel"></div>R
JavaScript
// ------------------------------------------------------------------------------------------
// LacunaWebPKI: Sign XML Element locally (NFe - Brazil invoice) Example
var pki = new LacunaWebPKI('AgwAanNmaWRkbGUubmV0AAADAFBybwgAAEAxPPcp3QgAAYfzZkEG61+SKcLfRlncgzS0hT/TtVM40BwPxSGsP3FZHGRPpAjWaAoWEFuFX3ojg7YkDwJ18coqrCPHESHh/Pqvw1Cr2LYnftm4R68IBoxNG3DmE7qSzrgm2V+tP6mMvjBaKefSog7wLVbvOQWMniWvsDEoZgkCqwDujo8ZAvkrz1hsPIEQwsjibupMG/C9rc8OHYYE7QtDpkje/i+YCWqg0W9hh9VmLCtlt1T84SPnWhcgWVzeaYylyb7Clh3k8DWJK5IW/AYcdIdGFp66ztWVFl8PMWMHmIdn2GCQvnn5k1h88HjTi8lxnS275D5ef1czkljk8JXK5Mes0m+mhGU=');
function signXml() {
var request = {
// XML content (can be string or base64 encoded bytes)
content: $('#xmlArea').val(),
// Select the XML elements to sign with namespace and xpath
toSignElementsXPath: './/nf:infNFe',
namespaces: [
{ uri: 'http://www.portalfiscal.inf.br/nfe', prefix: 'nf' }
],
// Output options
output: {
// set to return the signed XML content
mode: pki.outputModes.returnContent
},
// Signature policy (accepts certificates from ICP-Brasil chain only)
policy: pki.xmlPolicies.brazilNFe
};
// Set the user certificate to authenticate the request
request.certificateThumbprint = $('#certificateSelect').val();
if (!request.certificateThumbprint) {
alert('Please select a certificated');
return;
}
log('Signing XML');
pki.signXmlElement(request).success(function (response) {
printResponse(response.signatureInfo);
// with the response.signatureInfo.content value you can upload
// to your server or post with an authenticated request
// authenticated request sample: https://jsfiddle.net/LacunaSoftware/3cv5xjov/
});
}
function printResponse(signatureInfo) {
// get string of Base64 content
var contentText = atob(signatureInfo.content);
...