@tamtamchik/app-store-receipt-parser 1.0.0-pre.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "parser": "@typescript-eslint/parser",
3
+ "extends": "plugin:@typescript-eslint/recommended",
4
+ "parserOptions": {
5
+ "ecmaVersion": 2021,
6
+ "sourceType": "module"
7
+ },
8
+ "rules": {
9
+ "indent": [ "error", 2, { "SwitchCase": 1 } ],
10
+ "semi": "off",
11
+ "quotes": "off",
12
+ "object-curly-spacing": "off",
13
+ "@typescript-eslint/array-type": [ "error", { "default": "array" } ],
14
+ "@typescript-eslint/ban-ts-comment": 0,
15
+ "@typescript-eslint/comma-dangle": [ "error", "always-multiline" ],
16
+ "@typescript-eslint/quotes": [ "error", "single" ],
17
+ "@typescript-eslint/semi": [ "error", "never" ],
18
+ "@typescript-eslint/object-curly-spacing": [ "error", "always" ]
19
+ },
20
+ "env": {
21
+ "node": true
22
+ }
23
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Yuri Tkachenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.js ADDED
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ ReceiptParser: () => ReceiptParser
24
+ });
25
+ module.exports = __toCommonJS(src_exports);
26
+
27
+ // src/ReceiptParser.ts
28
+ var import_asn1js = require("asn1js");
29
+ var ReceiptParser = class _ReceiptParser {
30
+ constructor(receiptString) {
31
+ this.receiptString = receiptString;
32
+ }
33
+ static getTransactionIdsFromBlock(block) {
34
+ const { valueBlock } = block;
35
+ if (!valueBlock || !Array.isArray(valueBlock.value)) {
36
+ return null;
37
+ }
38
+ if (valueBlock.value.length === 3) {
39
+ const result2 = this.extractTransactionIds(valueBlock.value);
40
+ if (result2)
41
+ return result2;
42
+ }
43
+ const result = { transactionIds: [], originalTransactionIds: [] };
44
+ for (const innerBlock of valueBlock.value) {
45
+ const innerIds = this.getTransactionIdsFromBlock(innerBlock);
46
+ if (innerIds) {
47
+ result.transactionIds.push(...innerIds.transactionIds);
48
+ result.originalTransactionIds.push(...innerIds.originalTransactionIds);
49
+ }
50
+ }
51
+ result.transactionIds = [...new Set(result.transactionIds)];
52
+ result.originalTransactionIds = [...new Set(result.originalTransactionIds)];
53
+ return result;
54
+ }
55
+ static getTransactionIdsFromReceiptString(receiptString) {
56
+ const { result } = (0, import_asn1js.fromBER)(Buffer.from(receiptString, "base64"));
57
+ return this.getTransactionIdsFromBlock(result.toJSON());
58
+ }
59
+ /**
60
+ * The transaction ID is encoded as an INTEGER with the value 0x06a7
61
+ */
62
+ static isTransactionIdBlock(block) {
63
+ return block.blockName === "INTEGER" && block.valueBlock.valueHex === "06a7";
64
+ }
65
+ /**
66
+ * The original transaction ID is encoded as an INTEGER with the value 0x06a9
67
+ */
68
+ static isOriginalTransactionIdBlock(block) {
69
+ return block.blockName === "INTEGER" && block.valueBlock.valueHex === "06a9";
70
+ }
71
+ static extractIdFromBlock(block) {
72
+ if (!Array.isArray(block.valueBlock.value)) {
73
+ return null;
74
+ }
75
+ if (block.blockName === "OCTET STRING" && block.valueBlock.value[0].blockName === "UTF8String") {
76
+ return block.valueBlock.value[0].valueBlock.value;
77
+ }
78
+ return null;
79
+ }
80
+ static extractTransactionIds(blocks) {
81
+ const [firstBlock, , lastBlock] = blocks;
82
+ const result = { transactionIds: [], originalTransactionIds: [] };
83
+ if (this.isTransactionIdBlock(firstBlock)) {
84
+ const id = this.extractIdFromBlock(lastBlock);
85
+ if (id)
86
+ result.transactionIds.push(id);
87
+ } else if (this.isOriginalTransactionIdBlock(firstBlock)) {
88
+ const id = this.extractIdFromBlock(lastBlock);
89
+ if (id)
90
+ result.originalTransactionIds.push(id);
91
+ } else {
92
+ return null;
93
+ }
94
+ return result;
95
+ }
96
+ getTransactionIds() {
97
+ return _ReceiptParser.getTransactionIdsFromReceiptString(this.receiptString);
98
+ }
99
+ };
100
+ // Annotate the CommonJS export names for ESM import in node:
101
+ 0 && (module.exports = {
102
+ ReceiptParser
103
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,76 @@
1
+ // src/ReceiptParser.ts
2
+ import { fromBER } from "asn1js";
3
+ var ReceiptParser = class _ReceiptParser {
4
+ constructor(receiptString) {
5
+ this.receiptString = receiptString;
6
+ }
7
+ static getTransactionIdsFromBlock(block) {
8
+ const { valueBlock } = block;
9
+ if (!valueBlock || !Array.isArray(valueBlock.value)) {
10
+ return null;
11
+ }
12
+ if (valueBlock.value.length === 3) {
13
+ const result2 = this.extractTransactionIds(valueBlock.value);
14
+ if (result2)
15
+ return result2;
16
+ }
17
+ const result = { transactionIds: [], originalTransactionIds: [] };
18
+ for (const innerBlock of valueBlock.value) {
19
+ const innerIds = this.getTransactionIdsFromBlock(innerBlock);
20
+ if (innerIds) {
21
+ result.transactionIds.push(...innerIds.transactionIds);
22
+ result.originalTransactionIds.push(...innerIds.originalTransactionIds);
23
+ }
24
+ }
25
+ result.transactionIds = [...new Set(result.transactionIds)];
26
+ result.originalTransactionIds = [...new Set(result.originalTransactionIds)];
27
+ return result;
28
+ }
29
+ static getTransactionIdsFromReceiptString(receiptString) {
30
+ const { result } = fromBER(Buffer.from(receiptString, "base64"));
31
+ return this.getTransactionIdsFromBlock(result.toJSON());
32
+ }
33
+ /**
34
+ * The transaction ID is encoded as an INTEGER with the value 0x06a7
35
+ */
36
+ static isTransactionIdBlock(block) {
37
+ return block.blockName === "INTEGER" && block.valueBlock.valueHex === "06a7";
38
+ }
39
+ /**
40
+ * The original transaction ID is encoded as an INTEGER with the value 0x06a9
41
+ */
42
+ static isOriginalTransactionIdBlock(block) {
43
+ return block.blockName === "INTEGER" && block.valueBlock.valueHex === "06a9";
44
+ }
45
+ static extractIdFromBlock(block) {
46
+ if (!Array.isArray(block.valueBlock.value)) {
47
+ return null;
48
+ }
49
+ if (block.blockName === "OCTET STRING" && block.valueBlock.value[0].blockName === "UTF8String") {
50
+ return block.valueBlock.value[0].valueBlock.value;
51
+ }
52
+ return null;
53
+ }
54
+ static extractTransactionIds(blocks) {
55
+ const [firstBlock, , lastBlock] = blocks;
56
+ const result = { transactionIds: [], originalTransactionIds: [] };
57
+ if (this.isTransactionIdBlock(firstBlock)) {
58
+ const id = this.extractIdFromBlock(lastBlock);
59
+ if (id)
60
+ result.transactionIds.push(id);
61
+ } else if (this.isOriginalTransactionIdBlock(firstBlock)) {
62
+ const id = this.extractIdFromBlock(lastBlock);
63
+ if (id)
64
+ result.originalTransactionIds.push(id);
65
+ } else {
66
+ return null;
67
+ }
68
+ return result;
69
+ }
70
+ getTransactionIds() {
71
+ return _ReceiptParser.getTransactionIdsFromReceiptString(this.receiptString);
72
+ }
73
+ };
74
+ export {
75
+ ReceiptParser
76
+ };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@tamtamchik/app-store-receipt-parser",
3
+ "version": "1.0.0-pre.0",
4
+ "description": "A lightweight TypeScript library for extracting transaction IDs from Apple's ASN.1 encoded receipts.",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./src/index.ts",
8
+ "author": "Yuri Tkachenko <yuri.tam.tkachenko@gmail.com>",
9
+ "license": "MIT",
10
+ "homepage": "https://github.com/tamtamchik/app-store-receipt-parser#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/tamtamchik/app-store-receipt-parser/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+ssh://git@github.com/tamtamchik/app-store-receipt-parser.git"
17
+ },
18
+ "keywords": [
19
+ "apple",
20
+ "receipt",
21
+ "parser",
22
+ "ASN.1",
23
+ "transaction",
24
+ "typescript"
25
+ ],
26
+ "exports": {
27
+ ".": {
28
+ "require": "./dist/index.js",
29
+ "import": "./dist/index.mjs",
30
+ "types": "./src/index.ts"
31
+ }
32
+ },
33
+ "dependencies": {
34
+ "asn1js": "3.0.5"
35
+ },
36
+ "devDependencies": {
37
+ "@types/node": "20.5.1",
38
+ "@typescript-eslint/eslint-plugin": "6.4.0",
39
+ "@typescript-eslint/parser": "6.4.0",
40
+ "eslint": "8.47.0",
41
+ "tsup": "7.2.0",
42
+ "typescript": "5.1.6"
43
+ },
44
+ "scripts": {
45
+ "build": "tsup src/index.ts --format cjs,esm --clean",
46
+ "dev": "npm run build -- --watch src",
47
+ "lint": "eslint src --fix",
48
+ "prepublishOnly": "npm run build"
49
+ }
50
+ }
@@ -0,0 +1,102 @@
1
+ import { fromBER } from 'asn1js'
2
+
3
+ interface Block {
4
+ blockName: string
5
+ valueBlock: {
6
+ valueHex?: string
7
+ value?: Block[] | string
8
+ }
9
+ }
10
+
11
+ export interface TransactionIds {
12
+ transactionIds: string[]
13
+ originalTransactionIds: string[]
14
+ }
15
+
16
+ /**
17
+ * Parses a receipt string and extracts the transaction IDs from it.
18
+ * Warning! This class does not validate the receipt string, only extracts them.
19
+ */
20
+ export class ReceiptParser {
21
+ constructor (private readonly receiptString: string) {}
22
+
23
+ static getTransactionIdsFromBlock (block: Block): TransactionIds | null {
24
+ const { valueBlock } = block
25
+
26
+ if (!valueBlock || !Array.isArray(valueBlock.value)) {
27
+ return null
28
+ }
29
+
30
+ if (valueBlock.value.length === 3) {
31
+ const result = this.extractTransactionIds(valueBlock.value)
32
+ if (result) return result
33
+ }
34
+
35
+ const result: TransactionIds = { transactionIds: [], originalTransactionIds: [] }
36
+
37
+ for (const innerBlock of valueBlock.value) {
38
+ const innerIds = this.getTransactionIdsFromBlock(innerBlock)
39
+ if (innerIds) {
40
+ result.transactionIds.push(...innerIds.transactionIds)
41
+ result.originalTransactionIds.push(...innerIds.originalTransactionIds)
42
+ }
43
+ }
44
+
45
+ // Deduplicate the IDs
46
+ result.transactionIds = [...new Set(result.transactionIds)]
47
+ result.originalTransactionIds = [...new Set(result.originalTransactionIds)]
48
+
49
+ return result
50
+ }
51
+
52
+ static getTransactionIdsFromReceiptString (receiptString: string): TransactionIds | null {
53
+ const { result } = fromBER(Buffer.from(receiptString, 'base64'))
54
+ return this.getTransactionIdsFromBlock(result.toJSON() as Block)
55
+ }
56
+
57
+ /**
58
+ * The transaction ID is encoded as an INTEGER with the value 0x06a7
59
+ */
60
+ private static isTransactionIdBlock (block: Block): boolean {
61
+ return block.blockName === 'INTEGER' && block.valueBlock.valueHex === '06a7'
62
+ }
63
+
64
+ /**
65
+ * The original transaction ID is encoded as an INTEGER with the value 0x06a9
66
+ */
67
+ private static isOriginalTransactionIdBlock (block: Block): boolean {
68
+ return block.blockName === 'INTEGER' && block.valueBlock.valueHex === '06a9'
69
+ }
70
+
71
+ private static extractIdFromBlock (block: Block): string | null {
72
+ if (!Array.isArray(block.valueBlock.value)) {
73
+ return null
74
+ }
75
+ // The transaction ID is encoded as an OCTET STRING containing a UTF8String
76
+ if (block.blockName === 'OCTET STRING' && block.valueBlock.value[0].blockName === 'UTF8String') {
77
+ return block.valueBlock.value[0].valueBlock.value as string
78
+ }
79
+ return null
80
+ }
81
+
82
+ private static extractTransactionIds (blocks: Block[]): TransactionIds | null {
83
+ const [firstBlock, , lastBlock] = blocks
84
+ const result: TransactionIds = { transactionIds: [], originalTransactionIds: [] }
85
+
86
+ if (this.isTransactionIdBlock(firstBlock)) {
87
+ const id = this.extractIdFromBlock(lastBlock)
88
+ if (id) result.transactionIds.push(id)
89
+ } else if (this.isOriginalTransactionIdBlock(firstBlock)) {
90
+ const id = this.extractIdFromBlock(lastBlock)
91
+ if (id) result.originalTransactionIds.push(id)
92
+ } else {
93
+ return null
94
+ }
95
+
96
+ return result
97
+ }
98
+
99
+ getTransactionIds (): TransactionIds | null {
100
+ return ReceiptParser.getTransactionIdsFromReceiptString(this.receiptString)
101
+ }
102
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { ReceiptParser, TransactionIds } from './ReceiptParser'
package/tsconfig.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "esnext",
4
+ "module": "esnext",
5
+ "strict": true,
6
+ "esModuleInterop": true,
7
+ "declaration": true,
8
+ "outDir": "./dist",
9
+ "rootDir": "./src",
10
+ "moduleResolution": "node"
11
+ },
12
+ "include": [
13
+ "src/**/*.ts"
14
+ ],
15
+ "exclude": [
16
+ "node_modules",
17
+ "dist"
18
+ ]
19
+ }