React Native Requires Chilkat v11.0.0+
React Native
Sign Manifest File to Generate a Passbook .pkpass file
See more Digital Signatures Examples
Demonstrates how to create a Passbook .pkpass archive by creating a signature of a manifest file and then zipping to a .pkpass archive.Note: Chilkat also has the capability to do everything in-memory (no files would be involved). If this is of interest, please send email to support@chilkatsoft.com
Chilkat React Native Downloads
import { Cert, Crypt2, JsonObject, StringBuilder, XmlCertVault, Zip } from '@chilkat/react-native'
async function chilkatExample() {
// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// ---------------------------------------------------------------------------------------------
// Note: Chilkat also has the capability to do everything in-memory (no files would be involved).
// See this example: Sign Manifest File to Generate a Passbook .pkpass in Memory
// ---------------------------------------------------------------------------------------------
// First create the manifest.json
const manifest = new JsonObject();
const crypt = new Crypt2();
const zip = new Zip();
zip.newZip('qa_data/p7s/pass-wallet/example.pkpass');
// Set the AppendFromDir property to prevent that relative paths from being stored in the .pkpass archive.
zip.appendFromDir = 'qa_data/p7s/pass-wallet/';
crypt.hashAlgorithm = 'sha1';
// Return hashes as lowercase hex.
crypt.encodingMode = 'hexlower';
let fileHash = '';
let filePath = '';
filePath = 'qa_data/p7s/pass-wallet/icon.png';
fileHash = await crypt.hashFileENCAsync(filePath);
zip.addFile('icon.png', false);
manifest.updateString('"icon.png"', fileHash);
filePath = 'qa_data/p7s/pass-wallet/icon@2x.png';
fileHash = await crypt.hashFileENCAsync(filePath);
zip.addFile('icon@2x.png', false);
manifest.updateString('"icon@2x.png"', fileHash);
filePath = 'qa_data/p7s/pass-wallet/logo.png';
fileHash = await crypt.hashFileENCAsync(filePath);
zip.addFile('logo.png', false);
manifest.updateString('"logo.png"', fileHash);
filePath = 'qa_data/p7s/pass-wallet/logo@2x.png';
fileHash = await crypt.hashFileENCAsync(filePath);
zip.addFile('logo@2x.png', false);
manifest.updateString('"logo@2x.png"', fileHash);
filePath = 'qa_data/p7s/pass-wallet/pass.json';
fileHash = await crypt.hashFileENCAsync(filePath);
zip.addFile('pass.json', false);
manifest.updateString('"pass.json"', fileHash);
const sbJson = new StringBuilder();
manifest.emitSb(sbJson);
const manifestPath = 'qa_data/p7s/pass-wallet/manifest.json';
sbJson.writeFile(manifestPath, 'utf-8', false);
zip.addFile('manifest.json', false);
// Make sure we have the Apple WWDR intermediate certificate available for
// the cert chain in the signature.
const certVault = new XmlCertVault();
const appleWwdrCert = new Cert();
try {
appleWwdrCert.loadByCommonName('Apple Worldwide Developer Relations Certification Authority');
} catch {
console.log('The Apple WWDR intermediate certificate is not installed.');
console.log('It is available at https://developer.apple.com/certificationauthority/AppleWWDRCA.cer');
console.log('You may alternatively load the .cer like this...');
try {
appleWwdrCert.loadFromFile('qa_data/certs/AppleWWDRCA.cer');
} catch {
console.log(appleWwdrCert.lastErrorText);
return;
}
}
certVault.addCert(appleWwdrCert);
crypt.useCertVault(certVault);
// Use a digital certificate and private key from a PFX file (.pfx or .p12).
const pfxPath = 'qa_data/pfx/cert_test123.pfx';
const pfxPassword = 'test123';
const cert = new Cert();
try {
cert.loadPfxFile(pfxPath, pfxPassword);
} catch {
console.log(cert.lastErrorText);
return;
}
// Provide the signing cert (with associated private key).
try {
crypt.setSigningCert(cert);
} catch {
console.log(crypt.lastErrorText);
return;
}
// Specify the signed attributes to be included.
// (These attributes appear to not be necessary, but we're including
// them just in case they become necessary in the future.)
const jsonSignedAttrs = new JsonObject();
jsonSignedAttrs.updateInt('contentType', 1);
jsonSignedAttrs.updateInt('signingTime', 1);
crypt.signingAttributes = jsonSignedAttrs.emit();
// Sign the manifest JSON file to produce a file named "signature".
const sigPath = 'qa_data/p7s/pass-wallet/signature';
// Create the "signature" file.
try {
await crypt.createP7SAsync(manifestPath, sigPath);
} catch {
console.log(crypt.lastErrorText);
return;
}
zip.addFile('signature', false);
// ---------------------------------------------------------------------------------------------
// Note: Chilkat also has the capability to do everything in-memory (no files would be involved).
// If this is of interest, please send email to support@chilkatsoft.com
// ---------------------------------------------------------------------------------------------
// Create the .pkipass archive (which is a .zip archive containing the required files).
try {
await zip.writeZipAndCloseAsync();
} catch {
console.log(zip.lastErrorText);
return;
}
console.log('Success.');
}