Sample code for 30+ languages & platforms
React Native

Italian FatturaPA (e-Invoice) Signed XML (CADES-BES P7M) using USB SmartCard Reader

See more CAdES Examples

Demonstrates Italian e-Invoice (FatturaPA) signing by using a private key stored on a USB smartcard reader.

Chilkat React Native Downloads

React Native
import { Cert, Crypt2, JsonObject } from '@chilkat/react-native'

async function chilkatExample() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  const crypt = new Crypt2();
  crypt.verboseLogging = true;

  const cert = new Cert();
  // Use your smart card user PIN for signing.
  cert.smartCardPin = '0000';

  try {
    cert.loadFromSmartcard('');
  } catch {
    console.log(cert.lastErrorText);
    return;
  }

  try {
    crypt.setSigningCert(cert);
  } catch {
    console.log(crypt.lastErrorText);
    return;
  }

  // The CadesEnabled property applies to all methods that create PKCS7 signatures. 
  // To create a CAdES-BES signature, set this property equal to true.
  crypt.cadesEnabled = true;

  crypt.hashAlgorithm = 'sha256';

  const signedAttrs = new JsonObject();
  signedAttrs.updateInt('contentType', 1);
  signedAttrs.updateInt('signingTime', 1);
  signedAttrs.updateInt('messageDigest', 1);
  signedAttrs.updateInt('signingCertificateV2', 1);
  crypt.signingAttributes = signedAttrs.emit();

  // Load XML such as the following:
  //  <p:FatturaElettronica xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2" versione="FPR12">
  //     <FatturaElettronicaHeader>
  //        <DatiTrasmissione>
  // 	...
  //        </DatiTrasmissione>
  //        <CedentePrestatore>
  // 	...
  //        </CedentePrestatore>
  //        <CessionarioCommittente>
  // 	...
  //        </CessionarioCommittente>
  //     </FatturaElettronicaHeader>
  //     <FatturaElettronicaBody>
  //        <DatiGenerali>
  //           <DatiGeneraliDocumento>
  // 		...
  //           </DatiGeneraliDocumento>
  //        </DatiGenerali>
  //        <DatiBeniServizi>
  // 	...
  //        </DatiBeniServizi>
  //     </FatturaElettronicaBody>
  //  </p:FatturaElettronica>

  const inputXmlPath = 'c:/someDir/e-Invoice.xml';
  const outputP7mPath = 'c:/someDir/signed.p7m';

  // Create the CAdES-BES attached signature, which contains the original data.
  try {
    await crypt.createP7MAsync(inputXmlPath, outputP7mPath);
  } catch {
    console.log(crypt.lastErrorText);
    return;
  }

  console.log('Success.');
}