@qntx/wallet-engine 0.12.0 → 0.14.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.
Files changed (81) hide show
  1. package/README.md +1057 -20
  2. package/dist/apps-fiat24.d.mts +202 -0
  3. package/dist/apps-fiat24.mjs +1378 -0
  4. package/dist/apps-fiat24.mjs.map +1 -0
  5. package/dist/apps-hyperliquid.d.mts +549 -0
  6. package/dist/apps-hyperliquid.mjs +2742 -0
  7. package/dist/apps-hyperliquid.mjs.map +1 -0
  8. package/dist/apps-polymarket.d.mts +729 -0
  9. package/dist/apps-polymarket.mjs +3786 -0
  10. package/dist/apps-polymarket.mjs.map +1 -0
  11. package/dist/apps.d.mts +136 -0
  12. package/dist/apps.mjs +535 -0
  13. package/dist/apps.mjs.map +1 -0
  14. package/dist/connect-BY1C5ZRI.mjs +210 -0
  15. package/dist/connect-BY1C5ZRI.mjs.map +1 -0
  16. package/dist/connect.d.mts +17 -9
  17. package/dist/connect.mjs +3 -185
  18. package/dist/controller-DpNVHVsj.d.mts +2295 -0
  19. package/dist/controller-rMnaxKAH.mjs +46 -0
  20. package/dist/controller-rMnaxKAH.mjs.map +1 -0
  21. package/dist/decimal-COJ4g8qy.mjs +38 -0
  22. package/dist/decimal-COJ4g8qy.mjs.map +1 -0
  23. package/dist/eligibility-BfM9ybYZ.mjs +48 -0
  24. package/dist/eligibility-BfM9ybYZ.mjs.map +1 -0
  25. package/dist/engine-Dic_kkEr.d.mts +1829 -0
  26. package/dist/errors-B8W-hoYW.d.mts +7 -0
  27. package/dist/errors-horUy5k1.mjs +24 -0
  28. package/dist/errors-horUy5k1.mjs.map +1 -0
  29. package/dist/gateway-contract.d.mts +3 -362
  30. package/dist/gateway-contract.mjs +1531 -81
  31. package/dist/gateway-contract.mjs.map +1 -1
  32. package/dist/idb.d.mts +5 -0
  33. package/dist/idb.mjs +122 -46
  34. package/dist/idb.mjs.map +1 -1
  35. package/dist/index-vBgfOFIh.d.mts +2227 -0
  36. package/dist/index.d.mts +58 -515
  37. package/dist/index.mjs +22314 -4901
  38. package/dist/index.mjs.map +1 -1
  39. package/dist/json-BDwV38cj.mjs +15 -0
  40. package/dist/json-BDwV38cj.mjs.map +1 -0
  41. package/dist/link-Ceq8Dggp.mjs +139 -0
  42. package/dist/link-Ceq8Dggp.mjs.map +1 -0
  43. package/dist/primitives-DbSHLZKq.mjs +31 -0
  44. package/dist/primitives-DbSHLZKq.mjs.map +1 -0
  45. package/dist/push-message-AQuzoNFY.d.mts +84 -0
  46. package/dist/push-message-kbnH1A5w.mjs +142 -0
  47. package/dist/push-message-kbnH1A5w.mjs.map +1 -0
  48. package/dist/push.d.mts +13 -0
  49. package/dist/push.mjs +70 -0
  50. package/dist/push.mjs.map +1 -0
  51. package/dist/qr.d.mts +292 -0
  52. package/dist/qr.mjs +1725 -0
  53. package/dist/qr.mjs.map +1 -0
  54. package/dist/registry-CbtwcxkX.mjs +374 -0
  55. package/dist/registry-CbtwcxkX.mjs.map +1 -0
  56. package/dist/risk-DyABgg4W.mjs +757 -0
  57. package/dist/risk-DyABgg4W.mjs.map +1 -0
  58. package/dist/session-CNJFLarP.mjs +18 -0
  59. package/dist/session-CNJFLarP.mjs.map +1 -0
  60. package/dist/socket-ChG9JRT_.mjs +110 -0
  61. package/dist/socket-ChG9JRT_.mjs.map +1 -0
  62. package/dist/spi-BrTw5s98.d.mts +3 -0
  63. package/dist/timer-Cdk5rGM2.mjs +43 -0
  64. package/dist/timer-Cdk5rGM2.mjs.map +1 -0
  65. package/dist/types-BpvioFGo.d.mts +258 -0
  66. package/dist/types-DbftVZ3I.mjs +90 -0
  67. package/dist/types-DbftVZ3I.mjs.map +1 -0
  68. package/dist/walletconnect.d.mts +86 -0
  69. package/dist/walletconnect.mjs +1507 -0
  70. package/dist/walletconnect.mjs.map +1 -0
  71. package/dist/webln-BgrtPXpG.mjs +2593 -0
  72. package/dist/webln-BgrtPXpG.mjs.map +1 -0
  73. package/dist/webln-Bllk_xJ2.d.mts +666 -0
  74. package/package.json +94 -19
  75. package/dist/connect.mjs.map +0 -1
  76. package/dist/errors-DpkJneDe.d.mts +0 -1696
  77. package/dist/registry-Deo5WQe1.d.mts +0 -25
  78. package/dist/registry-wYrcuFNc.mjs +0 -99
  79. package/dist/registry-wYrcuFNc.mjs.map +0 -1
  80. package/dist/webln-DzsI7jVb.mjs +0 -3629
  81. package/dist/webln-DzsI7jVb.mjs.map +0 -1
@@ -0,0 +1,15 @@
1
+ // src/store/json.ts
2
+ /**
3
+ * A stored row's JSON value, or `undefined` when its bytes are not JSON (a torn write, a format
4
+ * another build wrote). Callers treat `undefined` like any other malformed row and skip it.
5
+ */
6
+ const readJson = (bytes) => {
7
+ try {
8
+ return JSON.parse(new TextDecoder().decode(bytes));
9
+ } catch {
10
+ return;
11
+ }
12
+ };
13
+ export { readJson as t };
14
+
15
+ //# sourceMappingURL=json-BDwV38cj.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json-BDwV38cj.mjs","names":[],"sources":["../src/store/json.ts"],"sourcesContent":["/**\n * A stored row's JSON value, or `undefined` when its bytes are not JSON (a torn write, a format\n * another build wrote). Callers treat `undefined` like any other malformed row and skip it.\n */\nexport const readJson = (bytes: Uint8Array): unknown => {\n try {\n return JSON.parse(new TextDecoder().decode(bytes)) as unknown;\n } catch {\n return undefined;\n }\n};\n"],"mappings":";;;;;AAIA,MAAa,YAAY,UAA+B;CACtD,IAAI;EACF,OAAO,KAAK,MAAM,IAAI,YAAY,CAAC,CAAC,OAAO,KAAK,CAAC;CACnD,QAAQ;EACN;CACF;AACF"}
@@ -0,0 +1,139 @@
1
+ import { ChainError, hasHiddenCharacters } from "@qntx/wallet-chains";
2
+ /** The characters contract text may not carry: hidden ones, controls, and invisible marks. */
3
+ const UNSAFE_TEXT = /[\p{Cc}\p{Zl}\p{Zp}]|\u200C|\u200D|\u200E|\u200F|\u061C|\u00AD|[\u{E0000}-\u{E007F}]/u;
4
+ /** The text carries a hidden, control, bidi or zero-width character and must not be shown. */
5
+ const hasUnsafeText = (text) => hasHiddenCharacters(text) || UNSAFE_TEXT.test(text);
6
+ /**
7
+ * Untrusted contract text (a token symbol) as safe-to-show text: trimmed, non-empty, at most `max`
8
+ * code points, with no control, bidi-control or zero-width characters. `undefined` otherwise.
9
+ */
10
+ function safeDisplayText(raw, max) {
11
+ const text = raw.trim();
12
+ if (hasUnsafeText(text)) return;
13
+ let count = 0;
14
+ for (const _char of text) {
15
+ count += 1;
16
+ if (count > max) return;
17
+ }
18
+ return count === 0 ? void 0 : text;
19
+ }
20
+ function isAscii(text) {
21
+ for (const char of text) {
22
+ const code = char.codePointAt(0) ?? 0;
23
+ if (code < 32 || code > 126) return false;
24
+ }
25
+ return true;
26
+ }
27
+ /** A token's `decimals` as an integer within `0..TOKEN_DECIMALS_MAX`, else `undefined`. */
28
+ const safeDecimals = (value) => Number.isInteger(value) && value >= 0 && value <= 36 ? value : void 0;
29
+ // src/transactions/binding.ts
30
+ /**
31
+ * A `solana-sign` lifetime that cannot be proven to belong to the session's cluster. `code` is
32
+ * `invalid-request` like every other binding refusal; `reason` names which check failed.
33
+ */
34
+ var SolanaClusterBindingError = class extends ChainError {
35
+ reason;
36
+ constructor(reason, message) {
37
+ super("invalid-request", message);
38
+ this.name = "SolanaClusterBindingError";
39
+ this.reason = reason;
40
+ }
41
+ };
42
+ /**
43
+ * A `tron-sign` transaction whose reference block cannot be proven to belong to this network —
44
+ * `code` is `invalid-request` like every other binding refusal; `reason` names which check failed.
45
+ */
46
+ var TronBindingError = class extends ChainError {
47
+ reason;
48
+ constructor(reason, message) {
49
+ super("invalid-request", message);
50
+ this.name = "TronBindingError";
51
+ this.reason = reason;
52
+ }
53
+ };
54
+ // src/qr/errors.ts
55
+ /** Every failure the qr layer reports; nothing else may escape a public entry point. */
56
+ var QrError = class extends Error {
57
+ code;
58
+ constructor(code, message) {
59
+ super(message);
60
+ this.name = "QrError";
61
+ this.code = code;
62
+ }
63
+ };
64
+ /** Fold any foreign throw (dcbor, UrError, viem/ox, RangeError) into `code`. */
65
+ const qrGuard = (code, run) => {
66
+ try {
67
+ return run();
68
+ } catch (error) {
69
+ if (error instanceof QrError) throw error;
70
+ throw new QrError(code, error instanceof Error ? error.message : "decode failed");
71
+ }
72
+ };
73
+ /** Throw the `malformed` variant — a payload or record that cannot parse. */
74
+ function malformed(detail) {
75
+ throw new QrError("malformed", detail);
76
+ }
77
+ /** Throw the `unsupported` variant — a well-formed payload outside what the qr layer serves. */
78
+ function unsupported(detail) {
79
+ throw new QrError("unsupported", detail);
80
+ }
81
+ /** Throw the `result-mismatch` variant — a response or export that does not match its request. */
82
+ function resultMismatch(detail) {
83
+ throw new QrError("result-mismatch", detail);
84
+ }
85
+ // src/qr/link.ts
86
+ /** Master fingerprint as a UR carries it: 8 lowercase hex digits. */
87
+ const QR_FINGERPRINT = /^[0-9a-f]{8}$/;
88
+ /** The one signing-key path shape per family a qr-linked account may claim. */
89
+ const QR_LINK_PATHS = {
90
+ evm: /^m\/44'\/60'\/0'\/0\/\d+$/,
91
+ solana: /^m\/44'\/501'\/\d+'\/0'$/,
92
+ tron: /^m\/44'\/195'\/0'\/0\/\d+$/,
93
+ bitcoin: /^m\/(44|49|84|86)'\/(0|1)'\/\d+'$/
94
+ };
95
+ const qrLinkPath = (family, path) => QR_LINK_PATHS[family].test(path);
96
+ /** The Bitcoin mainnet CAIP-2 id — the only network a QR PSBT maps deterministically. */
97
+ const BITCOIN_MAINNET = "bip122:000000000019d6689c085ae165831e93";
98
+ /** The Bitcoin testnet CAIP-2 id — the one testnet genesis an export's coin type can name. */
99
+ const BITCOIN_TESTNET = "bip122:00000000da84f2bafbbc53dee25a72ae";
100
+ /** The `p` of an account path `m/p'/coin'/g'/c/i` gives the template it must be. */
101
+ const PURPOSE_TEMPLATE = {
102
+ 44: "btc.bip44",
103
+ 49: "btc.bip49",
104
+ 84: "btc.bip84",
105
+ 86: "btc.bip86"
106
+ };
107
+ /** A `m/…` path as (index, hardened) steps, or `undefined` when a step is malformed. */
108
+ const pathComponents = (path) => {
109
+ if (!path.startsWith("m/")) return;
110
+ const out = [];
111
+ for (const step of path.slice(2).split("/")) {
112
+ const hardened = step.endsWith("'");
113
+ const digits = hardened ? step.slice(0, -1) : step;
114
+ if (!/^\d+$/.test(digits)) return;
115
+ const index = Number(digits);
116
+ if (!Number.isSafeInteger(index) || index >= 2147483648) return;
117
+ out.push({
118
+ index,
119
+ hardened
120
+ });
121
+ }
122
+ return out;
123
+ };
124
+ /** A `m/…` path and master fingerprint as the UR `Keypath` wire shape. */
125
+ const keypath = (path, sourceFingerprint) => {
126
+ const steps = pathComponents(path) ?? malformed(`bad derivation path ${path}`);
127
+ return {
128
+ components: steps.map((s) => ({
129
+ kind: "index",
130
+ index: s.index,
131
+ hardened: s.hardened
132
+ })),
133
+ sourceFingerprint,
134
+ depth: steps.length
135
+ };
136
+ };
137
+ export { safeDecimals as _, keypath as a, QrError as c, resultMismatch as d, unsupported as f, isAscii as g, hasUnsafeText as h, QR_FINGERPRINT as i, malformed as l, TronBindingError as m, BITCOIN_TESTNET as n, pathComponents as o, SolanaClusterBindingError as p, PURPOSE_TEMPLATE as r, qrLinkPath as s, BITCOIN_MAINNET as t, qrGuard as u, safeDisplayText as v };
138
+
139
+ //# sourceMappingURL=link-Ceq8Dggp.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"link-Ceq8Dggp.mjs","names":[],"sources":["../src/display.ts","../src/transactions/binding.ts","../src/qr/errors.ts","../src/qr/link.ts"],"sourcesContent":["import { hasHiddenCharacters } from \"@qntx/wallet-chains\";\n\nexport const TOKEN_SYMBOL_MAX = 32;\nexport const TOKEN_DECIMALS_MAX = 36;\n\n/** The characters contract text may not carry: hidden ones, controls, and invisible marks. */\nconst UNSAFE_TEXT =\n /[\\p{Cc}\\p{Zl}\\p{Zp}]|\\u200C|\\u200D|\\u200E|\\u200F|\\u061C|\\u00AD|[\\u{E0000}-\\u{E007F}]/u;\n\n/** The text carries a hidden, control, bidi or zero-width character and must not be shown. */\nexport const hasUnsafeText = (text: string): boolean =>\n hasHiddenCharacters(text) || UNSAFE_TEXT.test(text);\n\n/**\n * Untrusted contract text (a token symbol) as safe-to-show text: trimmed, non-empty, at most `max`\n * code points, with no control, bidi-control or zero-width characters. `undefined` otherwise.\n */\nexport function safeDisplayText(raw: string, max: number): string | undefined {\n const text = raw.trim();\n if (hasUnsafeText(text)) {\n return undefined;\n }\n let count = 0;\n for (const _char of text) {\n count += 1;\n if (count > max) {\n return undefined;\n }\n }\n return count === 0 ? undefined : text;\n}\n\nexport function isAscii(text: string): boolean {\n for (const char of text) {\n const code = char.codePointAt(0) ?? 0;\n if (code < 0x20 || code > 0x7e) {\n return false;\n }\n }\n return true;\n}\n\n/** A token's `decimals` as an integer within `0..TOKEN_DECIMALS_MAX`, else `undefined`. */\nexport const safeDecimals = (value: number): number | undefined =>\n Number.isInteger(value) && value >= 0 && value <= TOKEN_DECIMALS_MAX ? value : undefined;\n","import { ChainError } from \"@qntx/wallet-chains\";\n\n/** Why a message's lifetime could not be bound to the configured cluster. */\nexport type SolanaBindingReason =\n | \"genesis-mismatch\"\n | \"blockhash-unknown\"\n | \"nonce-account-missing\"\n | \"nonce-mismatch\"\n | \"nonce-account-in-lookup-table\"\n | \"unconfirmed\"\n | \"testnet-gateway-only\";\n\n/**\n * A `solana-sign` lifetime that cannot be proven to belong to the session's cluster. `code` is\n * `invalid-request` like every other binding refusal; `reason` names which check failed.\n */\nexport class SolanaClusterBindingError extends ChainError {\n readonly reason: SolanaBindingReason;\n\n constructor(reason: SolanaBindingReason, message: string) {\n super(\"invalid-request\", message);\n this.name = \"SolanaClusterBindingError\";\n this.reason = reason;\n }\n}\n\n/** Why a transaction's TaPoS reference could not be bound to this network. */\nexport type TronBindingReason = \"reference-block-unknown\";\n\n/**\n * A `tron-sign` transaction whose reference block cannot be proven to belong to this network —\n * `code` is `invalid-request` like every other binding refusal; `reason` names which check failed.\n */\nexport class TronBindingError extends ChainError {\n readonly reason: TronBindingReason;\n\n constructor(reason: TronBindingReason, message: string) {\n super(\"invalid-request\", message);\n this.name = \"TronBindingError\";\n this.reason = reason;\n }\n}\n","export type QrErrorCode =\n | \"scan-failed\"\n | \"scan-timeout\"\n | \"scan-exhausted\"\n | \"malformed\"\n | \"unsupported\"\n | \"fingerprint-missing\"\n | \"result-mismatch\"\n | \"account-mismatch\"\n | \"network-unknown\"\n | \"chain-mismatch\"\n | \"network-ambiguous\"\n | \"unbound\"\n | \"contract-creation\"\n | \"self-call\"\n | \"multisig-unsupported\"\n | \"nothing-to-sign\"\n | \"sighash-unsupported\"\n | \"wallet-unsupported\";\n\n/** Every failure the qr layer reports; nothing else may escape a public entry point. */\nexport class QrError extends Error {\n readonly code: QrErrorCode;\n\n constructor(code: QrErrorCode, message: string) {\n super(message);\n this.name = \"QrError\";\n this.code = code;\n }\n}\n\n/** Fold any foreign throw (dcbor, UrError, viem/ox, RangeError) into `code`. */\nexport const qrGuard = <T>(code: QrErrorCode, run: () => T): T => {\n try {\n return run();\n } catch (error) {\n if (error instanceof QrError) {\n throw error;\n }\n throw new QrError(code, error instanceof Error ? error.message : \"decode failed\");\n }\n};\n\n/** Throw the `malformed` variant — a payload or record that cannot parse. */\nexport function malformed(detail: string): never {\n throw new QrError(\"malformed\", detail);\n}\n\n/** Throw the `unsupported` variant — a well-formed payload outside what the qr layer serves. */\nexport function unsupported(detail: string): never {\n throw new QrError(\"unsupported\", detail);\n}\n\n/** Throw the `result-mismatch` variant — a response or export that does not match its request. */\nexport function resultMismatch(detail: string): never {\n throw new QrError(\"result-mismatch\", detail);\n}\n","/**\n * The path and fingerprint grammar a phone-linked account declares, and the UR `Keypath` wire shape\n * it encodes to — shared by the export, counterpart and watch-account code. Runtime stays free of\n * chain imports so the watch validation pulls no codecs.\n */\nimport type { Keypath } from \"@qntx/ur/interop\";\nimport type { BitcoinTemplate } from \"@qntx/wallet-chains\";\n\nimport { malformed } from \"./errors.ts\";\n\n/** Master fingerprint as a UR carries it: 8 lowercase hex digits. */\nexport const QR_FINGERPRINT = /^[0-9a-f]{8}$/;\n\n/** The one signing-key path shape per family a qr-linked account may claim. */\nexport const QR_LINK_PATHS: Readonly<Record<\"evm\" | \"solana\" | \"tron\" | \"bitcoin\", RegExp>> = {\n evm: /^m\\/44'\\/60'\\/0'\\/0\\/\\d+$/,\n solana: /^m\\/44'\\/501'\\/\\d+'\\/0'$/,\n tron: /^m\\/44'\\/195'\\/0'\\/0\\/\\d+$/,\n bitcoin: /^m\\/(44|49|84|86)'\\/(0|1)'\\/\\d+'$/,\n};\n\nexport const qrLinkPath = (family: \"evm\" | \"solana\" | \"tron\" | \"bitcoin\", path: string): boolean =>\n QR_LINK_PATHS[family].test(path);\n\n/** The Bitcoin mainnet CAIP-2 id — the only network a QR PSBT maps deterministically. */\nexport const BITCOIN_MAINNET = \"bip122:000000000019d6689c085ae165831e93\";\n\n/** The Bitcoin testnet CAIP-2 id — the one testnet genesis an export's coin type can name. */\nexport const BITCOIN_TESTNET = \"bip122:00000000da84f2bafbbc53dee25a72ae\";\n\n/** The `p` of an account path `m/p'/coin'/g'/c/i` gives the template it must be. */\nexport const PURPOSE_TEMPLATE: Record<number, BitcoinTemplate> = {\n 44: \"btc.bip44\",\n 49: \"btc.bip49\",\n 84: \"btc.bip84\",\n 86: \"btc.bip86\",\n};\n\n/** A `m/…` path as (index, hardened) steps, or `undefined` when a step is malformed. */\nexport const pathComponents = (\n path: string,\n): ReadonlyArray<{ readonly index: number; readonly hardened: boolean }> | undefined => {\n if (!path.startsWith(\"m/\")) {\n return undefined;\n }\n const out: Array<{ index: number; hardened: boolean }> = [];\n for (const step of path.slice(2).split(\"/\")) {\n const hardened = step.endsWith(\"'\");\n const digits = hardened ? step.slice(0, -1) : step;\n if (!/^\\d+$/.test(digits)) {\n return undefined;\n }\n const index = Number(digits);\n if (!Number.isSafeInteger(index) || index >= 0x80000000) {\n return undefined;\n }\n out.push({ index, hardened });\n }\n return out;\n};\n\n/** A `m/…` path and master fingerprint as the UR `Keypath` wire shape. */\nexport const keypath = (path: string, sourceFingerprint: number): Keypath => {\n const steps = pathComponents(path) ?? malformed(`bad derivation path ${path}`);\n return {\n components: steps.map((s) => ({\n kind: \"index\" as const,\n index: s.index,\n hardened: s.hardened,\n })),\n sourceFingerprint,\n depth: steps.length,\n };\n};\n"],"mappings":";;AAMA,MAAM,cACJ;;AAGF,MAAa,iBAAiB,SAC5B,oBAAoB,IAAI,KAAK,YAAY,KAAK,IAAI;;;;;AAMpD,SAAgB,gBAAgB,KAAa,KAAiC;CAC5E,MAAM,OAAO,IAAI,KAAK;CACtB,IAAI,cAAc,IAAI,GACpB;CAEF,IAAI,QAAQ;CACZ,KAAK,MAAM,SAAS,MAAM;EACxB,SAAS;EACT,IAAI,QAAQ,KACV;CAEJ;CACA,OAAO,UAAU,IAAI,KAAA,IAAY;AACnC;AAEA,SAAgB,QAAQ,MAAuB;CAC7C,KAAK,MAAM,QAAQ,MAAM;EACvB,MAAM,OAAO,KAAK,YAAY,CAAC,KAAK;EACpC,IAAI,OAAO,MAAQ,OAAO,KACxB,OAAO;CAEX;CACA,OAAO;AACT;;AAGA,MAAa,gBAAgB,UAC3B,OAAO,UAAU,KAAK,KAAK,SAAS,KAAK,SAAA,KAA8B,QAAQ,KAAA;;;;;;AC5BjF,IAAa,4BAAb,cAA+C,WAAW;CACxD;CAEA,YAAY,QAA6B,SAAiB;EACxD,MAAM,mBAAmB,OAAO;EAChC,KAAK,OAAO;EACZ,KAAK,SAAS;CAChB;AACF;;;;;AASA,IAAa,mBAAb,cAAsC,WAAW;CAC/C;CAEA,YAAY,QAA2B,SAAiB;EACtD,MAAM,mBAAmB,OAAO;EAChC,KAAK,OAAO;EACZ,KAAK,SAAS;CAChB;AACF;;;ACpBA,IAAa,UAAb,cAA6B,MAAM;CACjC;CAEA,YAAY,MAAmB,SAAiB;EAC9C,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;CACd;AACF;;AAG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C7E,OAAO;EACL,YAAY,MAAM,KAAK,OAAO;GAC5B,MAAM;GACN,OAAO,EAAE;GACT,UAAU,EAAE;EACd,EAAE;EACF;EACA,OAAO,MAAM;CACf;AACF"}
@@ -0,0 +1,31 @@
1
+ import * as z$1 from "zod/mini";
2
+ // src/gateway/contract/primitives.ts
3
+ const CLIENT_ID_HEADER = "x-client-id";
4
+ const PROVIDERS_HEADER = "x-gateway-providers";
5
+ /**
6
+ * A request naming one provider id from an earlier `x-gateway-providers` answer is served by that
7
+ * provider only — TRON node paths, so a sequence of reads comes from one upstream.
8
+ */
9
+ const PROVIDER_PIN_HEADER = "x-gateway-provider";
10
+ /**
11
+ * 128 random bits as lowercase hex — drawn per engine session for each gateway scope, never derived
12
+ * from or linked to anything else.
13
+ */
14
+ const clientId = z$1.string().check(z$1.regex(/^[0-9a-f]{32}$/));
15
+ /** CAIP-2 chain id: `namespace:reference`. */
16
+ const caip2 = z$1.string().check(z$1.regex(/^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$/));
17
+ const GATEWAY_ERRORS = [
18
+ "invalid-request",
19
+ "unknown-network",
20
+ "not-found",
21
+ "rate-limited",
22
+ "unavailable",
23
+ "outcome-unknown",
24
+ "internal",
25
+ "too-costly",
26
+ "unsupported"
27
+ ];
28
+ const gatewayError = z$1.object({ error: z$1.enum(GATEWAY_ERRORS) });
29
+ export { caip2 as a, PROVIDER_PIN_HEADER as i, GATEWAY_ERRORS as n, clientId as o, PROVIDERS_HEADER as r, gatewayError as s, CLIENT_ID_HEADER as t };
30
+
31
+ //# sourceMappingURL=primitives-DbSHLZKq.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"primitives-DbSHLZKq.mjs","names":["z"],"sources":["../src/gateway/contract/primitives.ts"],"sourcesContent":["import * as z from \"zod/mini\";\n\nexport const CLIENT_ID_HEADER = \"x-client-id\";\nexport const PROVIDERS_HEADER = \"x-gateway-providers\";\n/**\n * A request naming one provider id from an earlier `x-gateway-providers` answer is served by that\n * provider only — TRON node paths, so a sequence of reads comes from one upstream.\n */\nexport const PROVIDER_PIN_HEADER = \"x-gateway-provider\";\n\n/**\n * 128 random bits as lowercase hex — drawn per engine session for each gateway scope, never derived\n * from or linked to anything else.\n */\nexport const clientId = z.string().check(z.regex(/^[0-9a-f]{32}$/));\n\n/** CAIP-2 chain id: `namespace:reference`. */\nexport const caip2 = z.string().check(z.regex(/^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$/));\n\nexport type Caip2 = z.infer<typeof caip2>;\nexport type ClientId = z.infer<typeof clientId>;\n\nexport const GATEWAY_ERRORS = [\n \"invalid-request\",\n \"unknown-network\",\n \"not-found\",\n \"rate-limited\",\n \"unavailable\",\n \"outcome-unknown\",\n \"internal\",\n \"too-costly\",\n \"unsupported\",\n] as const;\n\nexport const gatewayError = z.object({ error: z.enum(GATEWAY_ERRORS) });\n\nexport type GatewayErrorCode = (typeof GATEWAY_ERRORS)[number];\n"],"mappings":";;AAEA,MAAa,mBAAmB;AAChC,MAAa,mBAAmB;;;;;AAKhC,MAAa,sBAAsB;;;;;AAMnC,MAAa,WAAWA,IAAE,OAAO,CAAC,CAAC,MAAMA,IAAE,MAAM,gBAAgB,CAAC;;AAGlE,MAAa,QAAQA,IAAE,OAAO,CAAC,CAAC,MAAMA,IAAE,MAAM,sCAAsC,CAAC;AAKrF,MAAa,iBAAiB;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;AAEA,MAAa,eAAeA,IAAE,OAAO,EAAE,OAAOA,IAAE,KAAK,cAAc,EAAE,CAAC"}
@@ -0,0 +1,84 @@
1
+ /**
2
+ * The push-notification wire format, frozen by `vectors/push.json`: an APNs/FCM message carries
3
+ * `{v, e, c}` — a 16-byte event id and `c`, the base64url-nopad encoding of `nonce(12) ‖
4
+ * AES-256-GCM ciphertext ‖ tag(16)` under the subscription's payload key with `AAD =
5
+ * ascii(PUSH_AAD_PREFIX + e)`. The plaintext is a small ASCII JSON document in exactly one
6
+ * canonical serialization (`serializePushPlaintext`), so reordered, padded or extended documents
7
+ * are malformed, not merely different.
8
+ *
9
+ * Deliberately zod-free: `@qntx/wallet-engine/push` bundles this module for notification extensions
10
+ * (an iOS NSE, an RN headless task), so it shares the opener's floor — no globals beyond the ES
11
+ * built-ins.
12
+ */
13
+ declare const PUSH_VERSION = 1;
14
+ declare const PUSH_EVENT_KINDS: readonly ["received", "sent", "failed", "reverted"];
15
+ type PushEventKind = (typeof PUSH_EVENT_KINDS)[number];
16
+ /** Subscription-id preimage: `id = hex(SHA-256(utf8(PUSH_ID_DOMAIN) ‖ secret))[0..32]`. */
17
+ declare const PUSH_ID_DOMAIN = "qntx-push-id/1";
18
+ /** Event-id preimage — see {@link pushEventInput}. */
19
+ declare const PUSH_EVENT_DOMAIN = "qntx-push-event/1";
20
+ /** AEAD associated data is `ascii(PUSH_AAD_PREFIX + eventId)`. */
21
+ declare const PUSH_AAD_PREFIX = "qntx-push/1/";
22
+ /** The opener never trial-decrypts with more keys than this. */
23
+ declare const PUSH_KEYS_MAX = 64;
24
+ /** Plaintext cap in bytes — the document is printable ASCII, so characters equal bytes. */
25
+ declare const PUSH_PLAINTEXT_MAX = 1024;
26
+ /** `c` length cap in base64url characters. */
27
+ declare const PUSH_BLOB_MAX = 2048;
28
+ /** What the provider delivers: `v` plus the opaque event id and the sealed blob. */
29
+ type PushMessage = {
30
+ readonly v: "1";
31
+ readonly e: string;
32
+ readonly c: string;
33
+ };
34
+ /** The one transfer involving the address, when there is exactly one — display-only. */
35
+ type PushTransfer = {
36
+ /** Amount, decimal in the asset's smallest unit. */
37
+ readonly q: string;
38
+ /** Decimal places of the asset. */
39
+ readonly d?: number;
40
+ /** Asset symbol. */
41
+ readonly s?: string;
42
+ /** Contract — absent for the native asset. */
43
+ readonly c?: string;
44
+ };
45
+ /** The decrypted event — display-only; activity lists only ever re-read chain data. */
46
+ type PushPlaintext = {
47
+ readonly v: 1;
48
+ readonly k: PushEventKind;
49
+ /** CAIP-2 network id. */
50
+ readonly n: string;
51
+ /** Transaction hash. */
52
+ readonly h: string;
53
+ /** The subscription's address this event touches. */
54
+ readonly a: string;
55
+ /** Observation time, milliseconds since the epoch. */
56
+ readonly t: number;
57
+ readonly x?: PushTransfer;
58
+ };
59
+ /**
60
+ * The event-id preimage: HMAC-SHA256 over this text under the payload key, first 16 bytes hex.
61
+ * `generation` separates a re-included transaction's second "included" from its first.
62
+ */
63
+ declare function pushEventInput(network: string, hash: string, kind: PushEventKind, generation: number): string;
64
+ /** The associated data a message's `e` binds the ciphertext to. */
65
+ declare const pushAad: (eventId: string) => string;
66
+ /**
67
+ * The canonical encoding the gateway encrypts: keys in the order `v,k,n,h,a,t,x` — inside `x`,
68
+ * `q,d,s,c` — with absent optionals omitted. `JSON.stringify` emits keys in insertion order, so
69
+ * building the document fresh is what fixes the order.
70
+ */
71
+ declare function serializePushPlaintext(p: PushPlaintext): string;
72
+ /**
73
+ * The inverse of `serializePushPlaintext` — field types are checked before the canonical
74
+ * re-serialization comparison, which rejects duplicate keys, reordering, whitespace and unknown
75
+ * keys in one step. Returns `undefined` on any violation; never throws.
76
+ */
77
+ declare function parsePushPlaintext(text: string): PushPlaintext | undefined;
78
+ /**
79
+ * The message shape a host pulls out of an APNs `qntx` key or an FCM data map — extra keys are
80
+ * ignored because those maps carry provider SDK entries too. Never throws.
81
+ */
82
+ declare function parsePushMessage(value: unknown): PushMessage | undefined;
83
+ export { serializePushPlaintext as _, PUSH_ID_DOMAIN as a, PUSH_VERSION as c, PushPlaintext as d, PushTransfer as f, pushEventInput as g, pushAad as h, PUSH_EVENT_KINDS as i, PushEventKind as l, parsePushPlaintext as m, PUSH_BLOB_MAX as n, PUSH_KEYS_MAX as o, parsePushMessage as p, PUSH_EVENT_DOMAIN as r, PUSH_PLAINTEXT_MAX as s, PUSH_AAD_PREFIX as t, PushMessage as u };
84
+ //# sourceMappingURL=push-message-AQuzoNFY.d.mts.map
@@ -0,0 +1,142 @@
1
+ // src/gateway/contract/push-message.ts
2
+ /**
3
+ * The push-notification wire format, frozen by `vectors/push.json`: an APNs/FCM message carries
4
+ * `{v, e, c}` — a 16-byte event id and `c`, the base64url-nopad encoding of `nonce(12) ‖
5
+ * AES-256-GCM ciphertext ‖ tag(16)` under the subscription's payload key with `AAD =
6
+ * ascii(PUSH_AAD_PREFIX + e)`. The plaintext is a small ASCII JSON document in exactly one
7
+ * canonical serialization (`serializePushPlaintext`), so reordered, padded or extended documents
8
+ * are malformed, not merely different.
9
+ *
10
+ * Deliberately zod-free: `@qntx/wallet-engine/push` bundles this module for notification extensions
11
+ * (an iOS NSE, an RN headless task), so it shares the opener's floor — no globals beyond the ES
12
+ * built-ins.
13
+ */
14
+ const PUSH_VERSION = 1;
15
+ const PUSH_EVENT_KINDS = [
16
+ "received",
17
+ "sent",
18
+ "failed",
19
+ "reverted"
20
+ ];
21
+ /** Subscription-id preimage: `id = hex(SHA-256(utf8(PUSH_ID_DOMAIN) ‖ secret))[0..32]`. */
22
+ const PUSH_ID_DOMAIN = "qntx-push-id/1";
23
+ /** Event-id preimage — see {@link pushEventInput}. */
24
+ const PUSH_EVENT_DOMAIN = "qntx-push-event/1";
25
+ /** AEAD associated data is `ascii(PUSH_AAD_PREFIX + eventId)`. */
26
+ const PUSH_AAD_PREFIX = "qntx-push/1/";
27
+ /** The opener never trial-decrypts with more keys than this. */
28
+ const PUSH_KEYS_MAX = 64;
29
+ /** Plaintext cap in bytes — the document is printable ASCII, so characters equal bytes. */
30
+ const PUSH_PLAINTEXT_MAX = 1024;
31
+ /** `c` length cap in base64url characters. */
32
+ const PUSH_BLOB_MAX = 2048;
33
+ const CAIP2 = /^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$/;
34
+ const ALNUM = /^[0-9A-Za-z]{1,128}$/;
35
+ const AMOUNT = /^(0|[1-9][0-9]{0,77})$/;
36
+ /** A symbol neither starts nor ends with a space; spaces inside are fine. */
37
+ const SYMBOL = /^[!-~](?:[ -~]{0,14}[!-~])?$/;
38
+ const PRINTABLE = /^[ -~]+$/;
39
+ const EVENT_ID = /^[0-9a-f]{32}$/;
40
+ const BLOB = /^[A-Za-z0-9_-]+$/;
41
+ /** `nonce ‖ tag ‖ one plaintext byte` is 29 bytes, 39 base64url chars unpadded. */
42
+ const BLOB_MIN = Math.ceil(116 / 3);
43
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
44
+ const isKind = (value) => PUSH_EVENT_KINDS.includes(value);
45
+ /**
46
+ * The event-id preimage: HMAC-SHA256 over this text under the payload key, first 16 bytes hex.
47
+ * `generation` separates a re-included transaction's second "included" from its first.
48
+ */
49
+ function pushEventInput(network, hash, kind, generation) {
50
+ return `${PUSH_EVENT_DOMAIN}\n${network}\n${hash}\n${kind}\n${generation}`;
51
+ }
52
+ /** The associated data a message's `e` binds the ciphertext to. */
53
+ const pushAad = (eventId) => PUSH_AAD_PREFIX + eventId;
54
+ /**
55
+ * The canonical encoding the gateway encrypts: keys in the order `v,k,n,h,a,t,x` — inside `x`,
56
+ * `q,d,s,c` — with absent optionals omitted. `JSON.stringify` emits keys in insertion order, so
57
+ * building the document fresh is what fixes the order.
58
+ */
59
+ function serializePushPlaintext(p) {
60
+ const { x } = p;
61
+ const transfer = x === void 0 ? void 0 : {
62
+ q: x.q,
63
+ ...x.d === void 0 ? {} : { d: x.d },
64
+ ...x.s === void 0 ? {} : { s: x.s },
65
+ ...x.c === void 0 ? {} : { c: x.c }
66
+ };
67
+ return JSON.stringify({
68
+ v: p.v,
69
+ k: p.k,
70
+ n: p.n,
71
+ h: p.h,
72
+ a: p.a,
73
+ t: p.t,
74
+ ...transfer === void 0 ? {} : { x: transfer }
75
+ });
76
+ }
77
+ const parseTransfer = (value) => {
78
+ if (!isRecord(value)) return;
79
+ const { q, d, s, c } = value;
80
+ if (typeof q !== "string" || !AMOUNT.test(q)) return;
81
+ if (d !== void 0 && (typeof d !== "number" || !Number.isInteger(d) || d < 0 || d > 255)) return;
82
+ if (s !== void 0 && (typeof s !== "string" || !SYMBOL.test(s))) return;
83
+ if (c !== void 0 && (typeof c !== "string" || !ALNUM.test(c))) return;
84
+ return {
85
+ q,
86
+ ...d === void 0 ? {} : { d },
87
+ ...s === void 0 ? {} : { s },
88
+ ...c === void 0 ? {} : { c }
89
+ };
90
+ };
91
+ /**
92
+ * The inverse of `serializePushPlaintext` — field types are checked before the canonical
93
+ * re-serialization comparison, which rejects duplicate keys, reordering, whitespace and unknown
94
+ * keys in one step. Returns `undefined` on any violation; never throws.
95
+ */
96
+ function parsePushPlaintext(text) {
97
+ if (text.length > 1024 || !PRINTABLE.test(text)) return;
98
+ let value;
99
+ try {
100
+ value = JSON.parse(text);
101
+ } catch {
102
+ return;
103
+ }
104
+ if (!isRecord(value)) return;
105
+ const { v, k, n, h, a, t, x } = value;
106
+ if (v !== 1 || !isKind(k)) return;
107
+ if (typeof n !== "string" || !CAIP2.test(n)) return;
108
+ if (typeof h !== "string" || !ALNUM.test(h)) return;
109
+ if (typeof a !== "string" || !ALNUM.test(a)) return;
110
+ if (typeof t !== "number" || !Number.isSafeInteger(t) || t < 0) return;
111
+ const transfer = x === void 0 ? void 0 : parseTransfer(x);
112
+ if (x !== void 0 && transfer === void 0) return;
113
+ const doc = {
114
+ v: 1,
115
+ k,
116
+ n,
117
+ h,
118
+ a,
119
+ t,
120
+ ...transfer === void 0 ? {} : { x: transfer }
121
+ };
122
+ return serializePushPlaintext(doc) === text ? doc : void 0;
123
+ }
124
+ /**
125
+ * The message shape a host pulls out of an APNs `qntx` key or an FCM data map — extra keys are
126
+ * ignored because those maps carry provider SDK entries too. Never throws.
127
+ */
128
+ function parsePushMessage(value) {
129
+ if (!isRecord(value)) return;
130
+ const { v, e, c } = value;
131
+ if (v !== String(1)) return;
132
+ if (typeof e !== "string" || !EVENT_ID.test(e)) return;
133
+ if (typeof c !== "string" || c.length < BLOB_MIN || c.length > 2048 || !BLOB.test(c)) return;
134
+ return {
135
+ v: "1",
136
+ e,
137
+ c
138
+ };
139
+ }
140
+ export { PUSH_ID_DOMAIN as a, PUSH_VERSION as c, pushAad as d, pushEventInput as f, PUSH_EVENT_KINDS as i, parsePushMessage as l, PUSH_BLOB_MAX as n, PUSH_KEYS_MAX as o, serializePushPlaintext as p, PUSH_EVENT_DOMAIN as r, PUSH_PLAINTEXT_MAX as s, PUSH_AAD_PREFIX as t, parsePushPlaintext as u };
141
+
142
+ //# sourceMappingURL=push-message-kbnH1A5w.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"push-message-kbnH1A5w.mjs","names":[],"sources":["../src/gateway/contract/push-message.ts"],"sourcesContent":["/**\n * The push-notification wire format, frozen by `vectors/push.json`: an APNs/FCM message carries\n * `{v, e, c}` — a 16-byte event id and `c`, the base64url-nopad encoding of `nonce(12) ‖\n * AES-256-GCM ciphertext ‖ tag(16)` under the subscription's payload key with `AAD =\n * ascii(PUSH_AAD_PREFIX + e)`. The plaintext is a small ASCII JSON document in exactly one\n * canonical serialization (`serializePushPlaintext`), so reordered, padded or extended documents\n * are malformed, not merely different.\n *\n * Deliberately zod-free: `@qntx/wallet-engine/push` bundles this module for notification extensions\n * (an iOS NSE, an RN headless task), so it shares the opener's floor — no globals beyond the ES\n * built-ins.\n */\nexport const PUSH_VERSION = 1;\n\nexport const PUSH_EVENT_KINDS = [\"received\", \"sent\", \"failed\", \"reverted\"] as const;\nexport type PushEventKind = (typeof PUSH_EVENT_KINDS)[number];\n\n/** Subscription-id preimage: `id = hex(SHA-256(utf8(PUSH_ID_DOMAIN) ‖ secret))[0..32]`. */\nexport const PUSH_ID_DOMAIN = \"qntx-push-id/1\";\n/** Event-id preimage — see {@link pushEventInput}. */\nexport const PUSH_EVENT_DOMAIN = \"qntx-push-event/1\";\n/** AEAD associated data is `ascii(PUSH_AAD_PREFIX + eventId)`. */\nexport const PUSH_AAD_PREFIX = \"qntx-push/1/\";\n\n/** The opener never trial-decrypts with more keys than this. */\nexport const PUSH_KEYS_MAX = 64;\n/** Plaintext cap in bytes — the document is printable ASCII, so characters equal bytes. */\nexport const PUSH_PLAINTEXT_MAX = 1024;\n/** `c` length cap in base64url characters. */\nexport const PUSH_BLOB_MAX = 2048;\n\n/** What the provider delivers: `v` plus the opaque event id and the sealed blob. */\nexport type PushMessage = {\n readonly v: \"1\";\n readonly e: string;\n readonly c: string;\n};\n\n/** The one transfer involving the address, when there is exactly one — display-only. */\nexport type PushTransfer = {\n /** Amount, decimal in the asset's smallest unit. */\n readonly q: string;\n /** Decimal places of the asset. */\n readonly d?: number;\n /** Asset symbol. */\n readonly s?: string;\n /** Contract — absent for the native asset. */\n readonly c?: string;\n};\n\n/** The decrypted event — display-only; activity lists only ever re-read chain data. */\nexport type PushPlaintext = {\n readonly v: 1;\n readonly k: PushEventKind;\n /** CAIP-2 network id. */\n readonly n: string;\n /** Transaction hash. */\n readonly h: string;\n /** The subscription's address this event touches. */\n readonly a: string;\n /** Observation time, milliseconds since the epoch. */\n readonly t: number;\n readonly x?: PushTransfer;\n};\n\nconst CAIP2 = /^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$/;\nconst ALNUM = /^[0-9A-Za-z]{1,128}$/;\nconst AMOUNT = /^(0|[1-9][0-9]{0,77})$/;\n/** A symbol neither starts nor ends with a space; spaces inside are fine. */\nconst SYMBOL = /^[!-~](?:[ -~]{0,14}[!-~])?$/;\nconst PRINTABLE = /^[ -~]+$/;\nconst EVENT_ID = /^[0-9a-f]{32}$/;\nconst BLOB = /^[A-Za-z0-9_-]+$/;\n/** `nonce ‖ tag ‖ one plaintext byte` is 29 bytes, 39 base64url chars unpadded. */\nconst BLOB_MIN = Math.ceil(((12 + 16 + 1) * 4) / 3);\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === \"object\" && value !== null && !Array.isArray(value);\n\nconst isKind = (value: unknown): value is PushEventKind =>\n (PUSH_EVENT_KINDS as ReadonlyArray<unknown>).includes(value);\n\n/**\n * The event-id preimage: HMAC-SHA256 over this text under the payload key, first 16 bytes hex.\n * `generation` separates a re-included transaction's second \"included\" from its first.\n */\nexport function pushEventInput(\n network: string,\n hash: string,\n kind: PushEventKind,\n generation: number,\n): string {\n return `${PUSH_EVENT_DOMAIN}\\n${network}\\n${hash}\\n${kind}\\n${generation}`;\n}\n\n/** The associated data a message's `e` binds the ciphertext to. */\nexport const pushAad = (eventId: string): string => PUSH_AAD_PREFIX + eventId;\n\n/**\n * The canonical encoding the gateway encrypts: keys in the order `v,k,n,h,a,t,x` — inside `x`,\n * `q,d,s,c` — with absent optionals omitted. `JSON.stringify` emits keys in insertion order, so\n * building the document fresh is what fixes the order.\n */\nexport function serializePushPlaintext(p: PushPlaintext): string {\n const { x } = p;\n const transfer =\n x === undefined\n ? undefined\n : {\n q: x.q,\n ...(x.d === undefined ? {} : { d: x.d }),\n ...(x.s === undefined ? {} : { s: x.s }),\n ...(x.c === undefined ? {} : { c: x.c }),\n };\n return JSON.stringify({\n v: p.v,\n k: p.k,\n n: p.n,\n h: p.h,\n a: p.a,\n t: p.t,\n ...(transfer === undefined ? {} : { x: transfer }),\n });\n}\n\nconst parseTransfer = (value: unknown): PushTransfer | undefined => {\n if (!isRecord(value)) {\n return undefined;\n }\n const { q, d, s, c } = value;\n if (typeof q !== \"string\" || !AMOUNT.test(q)) {\n return undefined;\n }\n if (d !== undefined && (typeof d !== \"number\" || !Number.isInteger(d) || d < 0 || d > 255)) {\n return undefined;\n }\n if (s !== undefined && (typeof s !== \"string\" || !SYMBOL.test(s))) {\n return undefined;\n }\n if (c !== undefined && (typeof c !== \"string\" || !ALNUM.test(c))) {\n return undefined;\n }\n return {\n q,\n ...(d === undefined ? {} : { d }),\n ...(s === undefined ? {} : { s }),\n ...(c === undefined ? {} : { c }),\n };\n};\n\n/**\n * The inverse of `serializePushPlaintext` — field types are checked before the canonical\n * re-serialization comparison, which rejects duplicate keys, reordering, whitespace and unknown\n * keys in one step. Returns `undefined` on any violation; never throws.\n */\nexport function parsePushPlaintext(text: string): PushPlaintext | undefined {\n if (text.length > PUSH_PLAINTEXT_MAX || !PRINTABLE.test(text)) {\n return undefined;\n }\n let value: unknown;\n try {\n value = JSON.parse(text);\n } catch {\n return undefined;\n }\n if (!isRecord(value)) {\n return undefined;\n }\n const { v, k, n, h, a, t, x } = value;\n if (v !== PUSH_VERSION || !isKind(k)) {\n return undefined;\n }\n if (typeof n !== \"string\" || !CAIP2.test(n)) {\n return undefined;\n }\n if (typeof h !== \"string\" || !ALNUM.test(h)) {\n return undefined;\n }\n if (typeof a !== \"string\" || !ALNUM.test(a)) {\n return undefined;\n }\n if (typeof t !== \"number\" || !Number.isSafeInteger(t) || t < 0) {\n return undefined;\n }\n const transfer = x === undefined ? undefined : parseTransfer(x);\n if (x !== undefined && transfer === undefined) {\n return undefined;\n }\n const doc: PushPlaintext = {\n v: PUSH_VERSION,\n k,\n n,\n h,\n a,\n t,\n ...(transfer === undefined ? {} : { x: transfer }),\n };\n return serializePushPlaintext(doc) === text ? doc : undefined;\n}\n\n/**\n * The message shape a host pulls out of an APNs `qntx` key or an FCM data map — extra keys are\n * ignored because those maps carry provider SDK entries too. Never throws.\n */\nexport function parsePushMessage(value: unknown): PushMessage | undefined {\n if (!isRecord(value)) {\n return undefined;\n }\n const { v, e, c } = value;\n if (v !== String(PUSH_VERSION)) {\n return undefined;\n }\n if (typeof e !== \"string\" || !EVENT_ID.test(e)) {\n return undefined;\n }\n if (typeof c !== \"string\" || c.length < BLOB_MIN || c.length > PUSH_BLOB_MAX || !BLOB.test(c)) {\n return undefined;\n }\n return { v: \"1\", e, c };\n}\n"],"mappings":";;;;;;;;;;;;;AAYA,MAAa,eAAe;AAE5B,MAAa,mBAAmB;CAAC;CAAY;CAAQ;CAAU;AAAU;;AAIzE,MAAa,iBAAiB;;AAE9B,MAAa,oBAAoB;;AAEjC,MAAa,kBAAkB;;AAG/B,MAAa,gBAAgB;;AAE7B,MAAa,qBAAqB;;AAElC,MAAa,gBAAgB;AAoC7B,MAAM,QAAQ;AACd,MAAM,QAAQ;AACd,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ACxE,IAAI,CAAC,SAAS,KAAK,GACjB;CAEF,MAAM,EAAE,GAAG,GAAG,MAAM;CACpB,IAAI,MAAM,OAAA,CAAmB,GAC3B;CAEF,IAAI,OAAO,MAAM,YAAY,CAAC,SAAS,KAAK,CAAC,GAC3C;CAEF,IAAI,OAAO,MAAM,YAAY,EAAE,SAAS,YAAY,EAAE,SAAA,QAA0B,CAAC,KAAK,KAAK,CAAC,GAC1F;CAEF,OAAO;EAAE,GAAG;EAAK;EAAG;CAAE;AACxB"}
@@ -0,0 +1,13 @@
1
+ import { d as PushPlaintext, f as PushTransfer, l as PushEventKind, o as PUSH_KEYS_MAX, p as parsePushMessage, u as PushMessage } from "./push-message-AQuzoNFY.mjs";
2
+ /**
3
+ * Opens `message` — whatever the provider delivered — with the device's subscription keys. The
4
+ * first key that authenticates and yields a well-formed event wins; a key that opens the blob but
5
+ * whose plaintext is malformed makes the whole delivery unreadable rather than continuing — another
6
+ * key cannot legitimately open the same blob.
7
+ */
8
+ export declare function openPush(message: unknown, keys: ReadonlyArray<Uint8Array>): {
9
+ readonly keyIndex: number;
10
+ readonly event: PushPlaintext;
11
+ } | undefined;
12
+ export { PUSH_KEYS_MAX, type PushEventKind, type PushMessage, type PushPlaintext, type PushTransfer, parsePushMessage };
13
+ //# sourceMappingURL=push.d.mts.map
package/dist/push.mjs ADDED
@@ -0,0 +1,70 @@
1
+ import { d as pushAad, l as parsePushMessage, o as PUSH_KEYS_MAX, u as parsePushPlaintext } from "./push-message-kbnH1A5w.mjs";
2
+ import { base64urlnopad } from "@scure/base";
3
+ import { gcm } from "@noble/ciphers/aes.js";
4
+ // src/push/index.ts
5
+ /**
6
+ * `@qntx/wallet-engine/push` — opens a delivered push message into its typed event for hosts that
7
+ * run outside the engine (an iOS Notification Service Extension, an Android FCM service, an RN
8
+ * headless task). The dependency floor is `@noble/ciphers` and `@scure/base` over the plain ES
9
+ * built-ins — no timers, no I/O, no Node or Web globals — so a bundled copy runs anywhere.
10
+ *
11
+ * A message opens when one of the device's subscription keys authenticates the AES-256-GCM blob AND
12
+ * the plaintext parses: anything else yields `undefined` and the host falls back to generic copy.
13
+ * `vectors/push.json` freezes the format end to end.
14
+ */
15
+ const NONCE_LENGTH = 12;
16
+ const KEY_LENGTH = 32;
17
+ /** ASCII bytes → string, or `undefined` when a byte falls outside the printable range. */
18
+ const asciiOf = (bytes) => {
19
+ let out = "";
20
+ for (const byte of bytes) {
21
+ if (byte < 32 || byte > 126) return;
22
+ out += String.fromCodePoint(byte);
23
+ }
24
+ return out;
25
+ };
26
+ /** The AAD is already ASCII by construction — this is a byte copy, not a decode. */
27
+ const asciiBytes = (text) => {
28
+ const out = new Uint8Array(text.length);
29
+ for (let i = 0; i < text.length; i++) out[i] = text.codePointAt(i) ?? 0;
30
+ return out;
31
+ };
32
+ /**
33
+ * Opens `message` — whatever the provider delivered — with the device's subscription keys. The
34
+ * first key that authenticates and yields a well-formed event wins; a key that opens the blob but
35
+ * whose plaintext is malformed makes the whole delivery unreadable rather than continuing — another
36
+ * key cannot legitimately open the same blob.
37
+ */
38
+ function openPush(message, keys) {
39
+ const parsed = parsePushMessage(message);
40
+ if (parsed === void 0 || keys.length > 64) return;
41
+ let blob;
42
+ try {
43
+ blob = base64urlnopad.decode(parsed.c);
44
+ } catch {
45
+ return;
46
+ }
47
+ if (blob.length < 29) return;
48
+ const nonce = blob.subarray(0, NONCE_LENGTH);
49
+ const sealed = blob.subarray(NONCE_LENGTH);
50
+ const aad = asciiBytes(pushAad(parsed.e));
51
+ for (const [index, key] of keys.entries()) {
52
+ if (key.length !== KEY_LENGTH) continue;
53
+ let plaintext;
54
+ try {
55
+ plaintext = gcm(key, nonce, aad).decrypt(sealed);
56
+ } catch {
57
+ continue;
58
+ }
59
+ const text = asciiOf(plaintext);
60
+ const event = text === void 0 ? void 0 : parsePushPlaintext(text);
61
+ if (event === void 0) return;
62
+ return {
63
+ keyIndex: index,
64
+ event
65
+ };
66
+ }
67
+ }
68
+ export { PUSH_KEYS_MAX, openPush, parsePushMessage };
69
+
70
+ //# sourceMappingURL=push.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"push.mjs","names":[],"sources":["../src/push/index.ts"],"sourcesContent":["/**\n * `@qntx/wallet-engine/push` — opens a delivered push message into its typed event for hosts that\n * run outside the engine (an iOS Notification Service Extension, an Android FCM service, an RN\n * headless task). The dependency floor is `@noble/ciphers` and `@scure/base` over the plain ES\n * built-ins — no timers, no I/O, no Node or Web globals — so a bundled copy runs anywhere.\n *\n * A message opens when one of the device's subscription keys authenticates the AES-256-GCM blob AND\n * the plaintext parses: anything else yields `undefined` and the host falls back to generic copy.\n * `vectors/push.json` freezes the format end to end.\n */\nimport { gcm } from \"@noble/ciphers/aes.js\";\nimport { base64urlnopad } from \"@scure/base\";\n\nimport {\n PUSH_KEYS_MAX,\n parsePushMessage,\n parsePushPlaintext,\n pushAad,\n} from \"../gateway/contract/push-message.ts\";\nimport type { PushPlaintext } from \"../gateway/contract/push-message.ts\";\n\nexport { PUSH_KEYS_MAX, parsePushMessage } from \"../gateway/contract/push-message.ts\";\nexport type {\n PushEventKind,\n PushMessage,\n PushPlaintext,\n PushTransfer,\n} from \"../gateway/contract/push-message.ts\";\n\nconst NONCE_LENGTH = 12;\nconst TAG_LENGTH = 16;\nconst KEY_LENGTH = 32;\n\n/** ASCII bytes → string, or `undefined` when a byte falls outside the printable range. */\nconst asciiOf = (bytes: Uint8Array): string | undefined => {\n let out = \"\";\n for (const byte of bytes) {\n if (byte < 0x20 || byte > 0x7e) {\n return undefined;\n }\n out += String.fromCodePoint(byte);\n }\n return out;\n};\n\n/** The AAD is already ASCII by construction — this is a byte copy, not a decode. */\nconst asciiBytes = (text: string): Uint8Array => {\n const out = new Uint8Array(text.length);\n for (let i = 0; i < text.length; i++) {\n out[i] = text.codePointAt(i) ?? 0;\n }\n return out;\n};\n\n/**\n * Opens `message` — whatever the provider delivered — with the device's subscription keys. The\n * first key that authenticates and yields a well-formed event wins; a key that opens the blob but\n * whose plaintext is malformed makes the whole delivery unreadable rather than continuing — another\n * key cannot legitimately open the same blob.\n */\nexport function openPush(\n message: unknown,\n keys: ReadonlyArray<Uint8Array>,\n): { readonly keyIndex: number; readonly event: PushPlaintext } | undefined {\n const parsed = parsePushMessage(message);\n if (parsed === undefined || keys.length > PUSH_KEYS_MAX) {\n return undefined;\n }\n let blob: Uint8Array;\n try {\n blob = base64urlnopad.decode(parsed.c);\n } catch {\n return undefined;\n }\n if (blob.length < NONCE_LENGTH + TAG_LENGTH + 1) {\n return undefined;\n }\n const nonce = blob.subarray(0, NONCE_LENGTH);\n const sealed = blob.subarray(NONCE_LENGTH);\n const aad = asciiBytes(pushAad(parsed.e));\n for (const [index, key] of keys.entries()) {\n if (key.length !== KEY_LENGTH) {\n continue;\n }\n let plaintext: Uint8Array;\n try {\n plaintext = gcm(key, nonce, aad).decrypt(sealed);\n } catch {\n continue;\n }\n const text = asciiOf(plaintext);\n const event = text === undefined ? undefined : parsePushPlaintext(text);\n if (event === undefined) {\n return undefined;\n }\n return { keyIndex: index, event };\n }\n return undefined;\n}\n"],"mappings":";;;;;;;;;;;;;;AA6BA,MAAM,eAAe;AAErB,MAAM,aAAa;;AAGnB,MAAM,WAAW,UAA0C;CACzD,IAAI,MAAM;CACV,KAAK,MAAM,QAAQ,OAAO;EACxB,IAAI,OAAO,MAAQ,OAAO,KACxB;EAEF,OAAO,OAAO,cAAc,IAAI;CAClC;CACA,OAAO;AACT;;AAGA,MAAM,cAAc,SAA6B;CAC/C,MAAM,MAAM,IAAI,WAAW,KAAK,MAAM;CACtC,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAC/B,IAAI,KAAK,KAAK,YAAY,CAAC,KAAK;CAElC,OAAO;AACT;;;;;;;AAQA,SAAgB,SACd,SACA,MAC0E;CAC1E,MAAM,SAAS,iBAAiB,OAAO;CACvC,IAAI,WAAW,KAAA,KAAa,KAAK,SAAA,IAC/B;CAEF,IAAI;CACJ,IAAI;EACF,OAAO,eAAe,OAAO,OAAO,CAAC;CACvC,QAAQ;EACN;CACF;CACA,IAAI,KAAK,SAAS,IAChB;CAEF,MAAM,QAAQ,KAAK,SAAS,GAAG,YAAY;CAC3C,MAAM,SAAS,KAAK,SAAS,YAAY;CACzC,MAAM,MAAM,WAAW,QAAQ,OAAO,CAAC,CAAC;CACxC,KAAK,MAAM,CAAC,OAAO,QAAQ,KAAK,QAAQ,GAAG;EACzC,IAAI,IAAI,WAAW,YACjB;EAEF,IAAI;EACJ,IAAI;GACF,YAAY,IAAI,KAAK,OAAO,GAAG,CAAC,CAAC,QAAQ,MAAM;EACjD,QAAQ;GACN;EACF;EACA,MAAM,OAAO,QAAQ,SAAS;EAC9B,MAAM,QAAQ,SAAS,KAAA,IAAY,KAAA,IAAY,mBAAmB,IAAI;EACtE,IAAI,UAAU,KAAA,GACZ;EAEF,OAAO;GAAE,UAAU;GAAO;EAAM;CAClC;AAEF"}