From 039b6362620d006a55b11f4708b0794c9b118a28 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sun, 26 Sep 2021 14:53:15 +0200 Subject: [PATCH] Added manifest creation, signature creation, pass.strings creation and pass-close methods --- src/PKPass.ts | 69 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) diff --git a/src/PKPass.ts b/src/PKPass.ts index f9619bc..60d2450 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -366,6 +366,53 @@ export default class PKPass extends Bundle { }; } + private [createManifestSymbol]() { + return Object.entries(this[filesSymbol]).reduce( + (acc, [fileName, buffer]) => { + const hashFlow = forge.md.sha1.create(); + + hashFlow.update(buffer.toString("binary")); + + return { + ...acc, + [fileName]: hashFlow.digest().toHex(), + }; + }, + {}, + ); + } + + private [closePassSymbol]() { + const passJson = Buffer.from(JSON.stringify(this[propsSymbol])); + super.addBuffer("pass.json", passJson); + + const localizationEntries = Object.entries(this[localizationSymbol]); + + for ( + let i = localizationEntries.length, + entry: [string, { [key: string]: string }]; + (entry = localizationEntries[--i]); + + ) { + const [lang, translations] = entry; + + super.addBuffer( + `${lang}.lproj/pass.strings`, + createStringFile(translations), + ); + } + + /** + * @TODO pack out fields from FieldsArray + */ + + const manifest = this[createManifestSymbol](); + super.addBuffer("manifest.json", Buffer.from(JSON.stringify(manifest))); + + const signatureBuffer = Signature.create(manifest, this.certificates); + super.addBuffer("signature", signatureBuffer); + } + // ************************* // // *** EXPORTING METHODS *** // // ************************* // @@ -383,9 +430,11 @@ export default class PKPass extends Bundle { * @TODO compile this pass into something usable * @TODO like _patch on old version * @TODO share implementation with getAsStream - * @TODO Build back translations + * @TODO warning if no icon files */ + this[closePassSymbol](); + return super.getAsBuffer(); } @@ -402,9 +451,11 @@ export default class PKPass extends Bundle { * @TODO compile this pass into something usable * @TODO like _patch on old version * @TODO share implementation with getAsBuffer - * @TODO Build back translations + * @TODO warning if no icon files */ + this[closePassSymbol](); + return super.getAsStream(); } @@ -762,3 +813,17 @@ function parseStringsFile(buffer: Buffer) { comments, }; } + +function createStringFile(translations: { [key: string]: string }): Buffer { + const stringContents = []; + + const translationsEntries = Object.entries(translations); + + for (let i = 0; i < translationsEntries.length; i++) { + const [key, value] = translationsEntries[i]; + + stringContents.push(`"${key}" = "${value}";`); + } + + return Buffer.from(stringContents.join("\n")); +}