React Native Requires Chilkat v11.0.0+
React Native
Sign Manifest File to Generate a Passbook .pkpass in Memory
Demonstrates how to create a Passbook .pkpass archive by creating a signature of a manifest file and then zipping to a .pkpass archive in memoryChilkat React Native Downloads
import { BinData, Cert, Crypt2, JsonObject, 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.
// ---------------------------------------------------------------------------------------------
// This example is the same as Sign Manifest File to Generate a Passbook .pkpass file
// except everything happens in memory (no input files, no output files)
// ---------------------------------------------------------------------------------------------
// First create the manifest.json
const manifest = new JsonObject();
const crypt = new Crypt2();
const zip = new Zip();
zip.newZip('notUsedAndNeverCreated.zip');
crypt.hashAlgorithm = 'sha1';
// Return hashes as lowercase hex.
crypt.encodingMode = 'hexlower';
let digestStr = '';
const pngData = new BinData();
// Assume we load the pngData with bytes for "icon.png" from somewhere, such as a byte array in memory.
zip.addBd('icon.png', pngData);
digestStr = crypt.hashBdENC(pngData);
manifest.updateString('"icon.png"', digestStr);
pngData.clear();
// Assume we load the pngData with bytes for "icon@2x.png" from somewhere...
zip.addBd('icon@2x.png', pngData);
digestStr = crypt.hashBdENC(pngData);
manifest.updateString('"icon@2x.png"', digestStr);
pngData.clear();
// Assume we load the pngData with bytes for "logo.png" from somewhere...
zip.addBd('logo.png', pngData);
digestStr = crypt.hashBdENC(pngData);
manifest.updateString('"logo.png"', digestStr);
pngData.clear();
// Assume we load the pngData with bytes for "logo@2x.png" from somewhere...
zip.addBd('logo@2x.png', pngData);
digestStr = crypt.hashBdENC(pngData);
manifest.updateString('"logo@2x.png"', digestStr);
const passJson = '{ .... }'; // Contains the contents of pass.json
zip.addString('pass.json', passJson, 'utf-8');
digestStr = crypt.hashStringENC(passJson);
manifest.updateString('"pass.json"', digestStr);
zip.addString('manifest.json', manifest.emit(), 'utf-8');
// 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
const bdPfx = new BinData();
// Assume we loaded a PFX into bdPfx....
const pfxPassword = 'test123';
const cert = new Cert();
try {
cert.loadPfxBd(bdPfx, 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 to produce a signature
crypt.encodingMode = 'base64';
const sig = await crypt.signStringENCAsync(manifest.emit());
const bdSig = new BinData();
bdSig.appendEncoded(sig, 'base64');
zip.addBd('signature', bdSig);
// ---------------------------------------------------------------------------------------------
// 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).
// the .zip is written to bdZip
const bdZip = new BinData();
try {
await zip.writeBdAsync(bdZip);
} catch {
console.log(zip.lastErrorText);
return;
}
console.log('Success.');
}