@coderoftime/fints 0.0.0-stage → 0.2.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/CHANGELOG.md +152 -0
- package/LICENSE +28 -0
- package/README.md +246 -2
- package/dist/client.d.ts +48 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +199 -0
- package/dist/client.js.map +1 -0
- package/dist/core/account.d.ts +168 -0
- package/dist/core/account.d.ts.map +1 -0
- package/dist/core/account.js +245 -0
- package/dist/core/account.js.map +1 -0
- package/dist/core/amount.d.ts +63 -0
- package/dist/core/amount.d.ts.map +1 -0
- package/dist/core/amount.js +192 -0
- package/dist/core/amount.js.map +1 -0
- package/dist/core/balance.d.ts +55 -0
- package/dist/core/balance.d.ts.map +1 -0
- package/dist/core/balance.js +54 -0
- package/dist/core/balance.js.map +1 -0
- package/dist/core/bank-info.d.ts +49 -0
- package/dist/core/bank-info.d.ts.map +1 -0
- package/dist/core/bank-info.js +52 -0
- package/dist/core/bank-info.js.map +1 -0
- package/dist/core/client.d.ts +161 -0
- package/dist/core/client.d.ts.map +1 -0
- package/dist/core/client.js +2 -0
- package/dist/core/client.js.map +1 -0
- package/dist/core/config.d.ts +80 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +153 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/errors.d.ts +153 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +247 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/feedback.d.ts +72 -0
- package/dist/core/feedback.d.ts.map +1 -0
- package/dist/core/feedback.js +127 -0
- package/dist/core/feedback.js.map +1 -0
- package/dist/core/transaction.d.ts +103 -0
- package/dist/core/transaction.d.ts.map +1 -0
- package/dist/core/transaction.js +96 -0
- package/dist/core/transaction.js.map +1 -0
- package/dist/core/transport.d.ts +64 -0
- package/dist/core/transport.d.ts.map +1 -0
- package/dist/core/transport.js +149 -0
- package/dist/core/transport.js.map +1 -0
- package/dist/formats/camt.d.ts +32 -0
- package/dist/formats/camt.d.ts.map +1 -0
- package/dist/formats/camt.js +242 -0
- package/dist/formats/camt.js.map +1 -0
- package/dist/formats/mt940.d.ts +54 -0
- package/dist/formats/mt940.d.ts.map +1 -0
- package/dist/formats/mt940.js +376 -0
- package/dist/formats/mt940.js.map +1 -0
- package/dist/formats/readers.d.ts +15 -0
- package/dist/formats/readers.d.ts.map +1 -0
- package/dist/formats/readers.js +42 -0
- package/dist/formats/readers.js.map +1 -0
- package/dist/formats/record-budget.d.ts +7 -0
- package/dist/formats/record-budget.d.ts.map +1 -0
- package/dist/formats/record-budget.js +17 -0
- package/dist/formats/record-budget.js.map +1 -0
- package/dist/formats/sepa-purpose.d.ts +28 -0
- package/dist/formats/sepa-purpose.d.ts.map +1 -0
- package/dist/formats/sepa-purpose.js +89 -0
- package/dist/formats/sepa-purpose.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/session/app-approval.d.ts +66 -0
- package/dist/session/app-approval.d.ts.map +1 -0
- package/dist/session/app-approval.js +226 -0
- package/dist/session/app-approval.js.map +1 -0
- package/dist/session/dialog-runner.d.ts +31 -0
- package/dist/session/dialog-runner.d.ts.map +1 -0
- package/dist/session/dialog-runner.js +111 -0
- package/dist/session/dialog-runner.js.map +1 -0
- package/dist/session/login-guard.d.ts +78 -0
- package/dist/session/login-guard.d.ts.map +1 -0
- package/dist/session/login-guard.js +181 -0
- package/dist/session/login-guard.js.map +1 -0
- package/dist/session/sepa-accounts.d.ts +40 -0
- package/dist/session/sepa-accounts.d.ts.map +1 -0
- package/dist/session/sepa-accounts.js +112 -0
- package/dist/session/sepa-accounts.js.map +1 -0
- package/dist/session/session-setup.d.ts +80 -0
- package/dist/session/session-setup.d.ts.map +1 -0
- package/dist/session/session-setup.js +182 -0
- package/dist/session/session-setup.js.map +1 -0
- package/dist/session/session-state.d.ts +50 -0
- package/dist/session/session-state.d.ts.map +1 -0
- package/dist/session/session-state.js +182 -0
- package/dist/session/session-state.js.map +1 -0
- package/dist/session/signed-dialog.d.ts +77 -0
- package/dist/session/signed-dialog.d.ts.map +1 -0
- package/dist/session/signed-dialog.js +170 -0
- package/dist/session/signed-dialog.js.map +1 -0
- package/dist/session/tan-selection.d.ts +96 -0
- package/dist/session/tan-selection.d.ts.map +1 -0
- package/dist/session/tan-selection.js +189 -0
- package/dist/session/tan-selection.js.map +1 -0
- package/dist/session/user-prompts.d.ts +65 -0
- package/dist/session/user-prompts.d.ts.map +1 -0
- package/dist/session/user-prompts.js +184 -0
- package/dist/session/user-prompts.js.map +1 -0
- package/dist/tan/challenge.d.ts +135 -0
- package/dist/tan/challenge.d.ts.map +1 -0
- package/dist/tan/challenge.js +184 -0
- package/dist/tan/challenge.js.map +1 -0
- package/dist/tan/flicker.d.ts +42 -0
- package/dist/tan/flicker.d.ts.map +1 -0
- package/dist/tan/flicker.js +296 -0
- package/dist/tan/flicker.js.map +1 -0
- package/dist/tan/tan-image.d.ts +22 -0
- package/dist/tan/tan-image.d.ts.map +1 -0
- package/dist/tan/tan-image.js +43 -0
- package/dist/tan/tan-image.js.map +1 -0
- package/dist/tan/tan-models.d.ts +149 -0
- package/dist/tan/tan-models.d.ts.map +1 -0
- package/dist/tan/tan-models.js +153 -0
- package/dist/tan/tan-models.js.map +1 -0
- package/dist/util/async.d.ts +22 -0
- package/dist/util/async.d.ts.map +1 -0
- package/dist/util/async.js +45 -0
- package/dist/util/async.js.map +1 -0
- package/dist/util/bytes.d.ts +22 -0
- package/dist/util/bytes.d.ts.map +1 -0
- package/dist/util/bytes.js +128 -0
- package/dist/util/bytes.js.map +1 -0
- package/dist/util/call-context.d.ts +12 -0
- package/dist/util/call-context.d.ts.map +1 -0
- package/dist/util/call-context.js +30 -0
- package/dist/util/call-context.js.map +1 -0
- package/dist/util/dates.d.ts +55 -0
- package/dist/util/dates.d.ts.map +1 -0
- package/dist/util/dates.js +189 -0
- package/dist/util/dates.js.map +1 -0
- package/dist/util/errors.d.ts +17 -0
- package/dist/util/errors.d.ts.map +1 -0
- package/dist/util/errors.js +24 -0
- package/dist/util/errors.js.map +1 -0
- package/dist/util/immutable-map.d.ts +16 -0
- package/dist/util/immutable-map.d.ts.map +1 -0
- package/dist/util/immutable-map.js +38 -0
- package/dist/util/immutable-map.js.map +1 -0
- package/dist/util/json.d.ts +17 -0
- package/dist/util/json.d.ts.map +1 -0
- package/dist/util/json.js +62 -0
- package/dist/util/json.js.map +1 -0
- package/dist/util/text.d.ts +37 -0
- package/dist/util/text.d.ts.map +1 -0
- package/dist/util/text.js +107 -0
- package/dist/util/text.js.map +1 -0
- package/dist/util/xml.d.ts +36 -0
- package/dist/util/xml.d.ts.map +1 -0
- package/dist/util/xml.js +194 -0
- package/dist/util/xml.js.map +1 -0
- package/dist/v3/account-extension.d.ts +25 -0
- package/dist/v3/account-extension.d.ts.map +1 -0
- package/dist/v3/account-extension.js +173 -0
- package/dist/v3/account-extension.js.map +1 -0
- package/dist/v3/bank-connection.d.ts +34 -0
- package/dist/v3/bank-connection.d.ts.map +1 -0
- package/dist/v3/bank-connection.js +58 -0
- package/dist/v3/bank-connection.js.map +1 -0
- package/dist/v3/dialog.d.ts +112 -0
- package/dist/v3/dialog.d.ts.map +1 -0
- package/dist/v3/dialog.js +241 -0
- package/dist/v3/dialog.js.map +1 -0
- package/dist/v3/message.d.ts +85 -0
- package/dist/v3/message.d.ts.map +1 -0
- package/dist/v3/message.js +303 -0
- package/dist/v3/message.js.map +1 -0
- package/dist/v3/parameter-store.d.ts +75 -0
- package/dist/v3/parameter-store.d.ts.map +1 -0
- package/dist/v3/parameter-store.js +143 -0
- package/dist/v3/parameter-store.js.map +1 -0
- package/dist/v3/parameters.d.ts +105 -0
- package/dist/v3/parameters.d.ts.map +1 -0
- package/dist/v3/parameters.js +450 -0
- package/dist/v3/parameters.js.map +1 -0
- package/dist/v3/protocol.d.ts +61 -0
- package/dist/v3/protocol.d.ts.map +1 -0
- package/dist/v3/protocol.js +113 -0
- package/dist/v3/protocol.js.map +1 -0
- package/dist/v3/queries.d.ts +51 -0
- package/dist/v3/queries.d.ts.map +1 -0
- package/dist/v3/queries.js +120 -0
- package/dist/v3/queries.js.map +1 -0
- package/dist/v3/segments.d.ts +106 -0
- package/dist/v3/segments.d.ts.map +1 -0
- package/dist/v3/segments.js +325 -0
- package/dist/v3/segments.js.map +1 -0
- package/dist/v3/syntax.d.ts +127 -0
- package/dist/v3/syntax.d.ts.map +1 -0
- package/dist/v3/syntax.js +367 -0
- package/dist/v3/syntax.js.map +1 -0
- package/package.json +67 -4
- package/src/client.ts +252 -0
- package/src/core/account.ts +323 -0
- package/src/core/amount.ts +211 -0
- package/src/core/balance.ts +85 -0
- package/src/core/bank-info.ts +66 -0
- package/src/core/client.ts +186 -0
- package/src/core/config.ts +182 -0
- package/src/core/errors.ts +277 -0
- package/src/core/feedback.ts +140 -0
- package/src/core/transaction.ts +161 -0
- package/src/core/transport.ts +189 -0
- package/src/formats/camt.ts +266 -0
- package/src/formats/mt940.ts +419 -0
- package/src/formats/readers.ts +49 -0
- package/src/formats/record-budget.ts +18 -0
- package/src/formats/sepa-purpose.ts +94 -0
- package/src/index.ts +70 -0
- package/src/session/app-approval.ts +278 -0
- package/src/session/dialog-runner.ts +122 -0
- package/src/session/login-guard.ts +221 -0
- package/src/session/sepa-accounts.ts +115 -0
- package/src/session/session-setup.ts +184 -0
- package/src/session/session-state.ts +204 -0
- package/src/session/signed-dialog.ts +192 -0
- package/src/session/tan-selection.ts +203 -0
- package/src/session/user-prompts.ts +193 -0
- package/src/tan/challenge.ts +261 -0
- package/src/tan/flicker.ts +308 -0
- package/src/tan/tan-image.ts +38 -0
- package/src/tan/tan-models.ts +252 -0
- package/src/util/async.ts +48 -0
- package/src/util/bytes.ts +129 -0
- package/src/util/call-context.ts +41 -0
- package/src/util/dates.ts +210 -0
- package/src/util/errors.ts +27 -0
- package/src/util/immutable-map.ts +47 -0
- package/src/util/json.ts +67 -0
- package/src/util/text.ts +112 -0
- package/src/util/xml.ts +183 -0
- package/src/v3/account-extension.ts +185 -0
- package/src/v3/bank-connection.ts +67 -0
- package/src/v3/dialog.ts +283 -0
- package/src/v3/message.ts +373 -0
- package/src/v3/parameter-store.ts +160 -0
- package/src/v3/parameters.ts +524 -0
- package/src/v3/protocol.ts +148 -0
- package/src/v3/queries.ts +190 -0
- package/src/v3/segments.ts +374 -0
- package/src/v3/syntax.ts +369 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { latin1Decode } from '../util/bytes.js';
|
|
2
|
+
import { copyDate } from '../util/dates.js';
|
|
3
|
+
import { characterReference } from '../util/text.js';
|
|
4
|
+
import { challengeInstruction, FlickerCode, hasEmbeddedFlickerCode } from './flicker.js';
|
|
5
|
+
import { TanImage } from './tan-image.js';
|
|
6
|
+
import type { TanMedium, TanMethod } from './tan-models.js';
|
|
7
|
+
|
|
8
|
+
/** Challenge text banks send when there is nothing to show. */
|
|
9
|
+
const noChallenge = 'nochallenge';
|
|
10
|
+
|
|
11
|
+
/** `message`, or `undefined` where it is empty or the filler banks send when there is nothing to show. */
|
|
12
|
+
export function challengeText(message: string | undefined): string | undefined {
|
|
13
|
+
return message === undefined || message === '' || message === noChallenge ? undefined : message;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const lineBreak = /<br\b[^<>]*>/gi;
|
|
17
|
+
const blockTag = /<\/?(?:p|li)\b[^<>]*>/gi;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The formatting tags a structured challenge may carry besides line breaks, paragraphs and list items (PIN/TAN, Data
|
|
21
|
+
* Dictionary "Challenge"). Angle brackets around anything else, such as `<50 EUR>`, are part of the text the user
|
|
22
|
+
* approves, so they stay. Excluding `<` and `>` from the tag keeps the search linear.
|
|
23
|
+
*/
|
|
24
|
+
const formattingTag = /<\/?(?:b|i|u|ul|ol)\b[^<>]*>/gi;
|
|
25
|
+
const entityReference = /&(#x?[0-9a-fA-F]+|[a-zA-Z]+);/g;
|
|
26
|
+
const blankLines = /\n{3,}/g;
|
|
27
|
+
|
|
28
|
+
const namedEntities: Record<string, string> = {
|
|
29
|
+
amp: '&',
|
|
30
|
+
lt: '<',
|
|
31
|
+
gt: '>',
|
|
32
|
+
quot: '"',
|
|
33
|
+
apos: "'",
|
|
34
|
+
nbsp: ' ',
|
|
35
|
+
auml: 'ä',
|
|
36
|
+
ouml: 'ö',
|
|
37
|
+
uuml: 'ü',
|
|
38
|
+
Auml: 'Ä',
|
|
39
|
+
Ouml: 'Ö',
|
|
40
|
+
Uuml: 'Ü',
|
|
41
|
+
szlig: 'ß',
|
|
42
|
+
euro: '€',
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** The character `name` refers to; `undefined` for an unknown name and for a number that is not a character. */
|
|
46
|
+
function decodeEntity(name: string): string | undefined {
|
|
47
|
+
return characterReference(name) ?? (Object.hasOwn(namedEntities, name) ? namedEntities[name] : undefined);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** What `TanChallenge` takes. */
|
|
51
|
+
export interface TanChallengeInit {
|
|
52
|
+
method: TanMethod;
|
|
53
|
+
reference: string;
|
|
54
|
+
message?: string | undefined;
|
|
55
|
+
hhdUc?: Uint8Array | undefined;
|
|
56
|
+
flickerCode?: FlickerCode | undefined;
|
|
57
|
+
image?: TanImage | undefined;
|
|
58
|
+
isDecoupled?: boolean;
|
|
59
|
+
validUntil?: Date | undefined;
|
|
60
|
+
tanMediumName?: string | undefined;
|
|
61
|
+
attempt?: number;
|
|
62
|
+
resolved?: Promise<void> | undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A request from the bank to authorize an action with a TAN or an app approval. */
|
|
66
|
+
export class TanChallenge {
|
|
67
|
+
/** The TAN method the challenge is for. */
|
|
68
|
+
readonly method: TanMethod;
|
|
69
|
+
|
|
70
|
+
/** Bank reference of the pending action, one character per byte the bank sent (ISO 8859-1). */
|
|
71
|
+
readonly reference: string;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Challenge text as the bank sent it, including the formatting tags of a structured challenge
|
|
75
|
+
* (`TanMethod.structuredChallenge`); `plainMessage` is the text to show. Show either as text only, never as HTML
|
|
76
|
+
* (`innerHTML`, `v-html`): it is the bank's text, not markup to trust.
|
|
77
|
+
*/
|
|
78
|
+
readonly message: string | undefined;
|
|
79
|
+
|
|
80
|
+
/** Raw challenge data for TAN generators (HHD_UC). */
|
|
81
|
+
readonly hhdUc: Uint8Array | undefined;
|
|
82
|
+
|
|
83
|
+
/** chipTAN optical flicker code, if the challenge contains one. */
|
|
84
|
+
readonly flickerCode: FlickerCode | undefined;
|
|
85
|
+
|
|
86
|
+
/** photoTAN / QR-TAN image, if the challenge contains one. */
|
|
87
|
+
readonly image: TanImage | undefined;
|
|
88
|
+
|
|
89
|
+
/** Approval happens in a separate app; see `TanCallback` for what to answer. */
|
|
90
|
+
readonly isDecoupled: boolean;
|
|
91
|
+
|
|
92
|
+
/** The exact moment until which the bank accepts the TAN or the approval, where it states date and time of day. */
|
|
93
|
+
readonly validUntil: Date | undefined;
|
|
94
|
+
|
|
95
|
+
/** Name of the TAN medium the bank sent the challenge to. */
|
|
96
|
+
readonly tanMediumName: string | undefined;
|
|
97
|
+
|
|
98
|
+
/** Counts repeated prompts for the same decoupled approval. */
|
|
99
|
+
readonly attempt: number;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Settles when the client needs no more answer to this challenge: a later prompt for the approval replaces it, or the
|
|
103
|
+
* bank confirmed the approval, or the approval failed, timed out or was cancelled. Set where the client asks the bank
|
|
104
|
+
* by itself while the callback shows the challenge (`TanCallback`), `undefined` otherwise. It never rejects.
|
|
105
|
+
*/
|
|
106
|
+
readonly resolved: Promise<void> | undefined;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A challenge as the bank sent it, with a copy of `validUntil`, which no other challenge shares. Use
|
|
110
|
+
* `TanChallenge.create` to have a flicker code or image detected in the data.
|
|
111
|
+
*/
|
|
112
|
+
constructor(init: TanChallengeInit) {
|
|
113
|
+
this.method = init.method;
|
|
114
|
+
this.reference = init.reference;
|
|
115
|
+
this.message = init.message;
|
|
116
|
+
this.hhdUc = init.hhdUc;
|
|
117
|
+
this.flickerCode = init.flickerCode;
|
|
118
|
+
this.image = init.image;
|
|
119
|
+
this.isDecoupled = init.isDecoupled ?? false;
|
|
120
|
+
this.validUntil = copyDate(init.validUntil);
|
|
121
|
+
this.tanMediumName = init.tanMediumName;
|
|
122
|
+
this.attempt = init.attempt ?? 1;
|
|
123
|
+
this.resolved = init.resolved;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Builds a challenge and detects a photoTAN image or chipTAN flicker code in `hhdUc` or `message`. */
|
|
127
|
+
static create(init: Omit<TanChallengeInit, 'flickerCode' | 'image' | 'resolved'>): TanChallenge {
|
|
128
|
+
let image: TanImage | undefined;
|
|
129
|
+
let flicker: FlickerCode | undefined;
|
|
130
|
+
const data = init.hhdUc !== undefined && init.hhdUc.length > 0 ? init.hhdUc : undefined;
|
|
131
|
+
if (data !== undefined) {
|
|
132
|
+
image = TanImage.tryParse(data);
|
|
133
|
+
if (image === undefined) flicker = FlickerCode.tryParse(latin1Decode(data));
|
|
134
|
+
} else if (init.message !== undefined && hasEmbeddedFlickerCode(init.message)) {
|
|
135
|
+
flicker = FlickerCode.tryParse(init.message);
|
|
136
|
+
}
|
|
137
|
+
return new TanChallenge({
|
|
138
|
+
...init,
|
|
139
|
+
message: challengeText(init.message),
|
|
140
|
+
hhdUc: data,
|
|
141
|
+
flickerCode: flicker,
|
|
142
|
+
image,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Whether the user can report the approval in the banking app; `false` where only the bank's confirmation completes
|
|
148
|
+
* it, which `TanCallback` tells how to handle.
|
|
149
|
+
*/
|
|
150
|
+
get manualConfirmation(): boolean {
|
|
151
|
+
return !this.isDecoupled || this.method.decoupledManualConfirmation;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* `message` as the user reads it.
|
|
156
|
+
*
|
|
157
|
+
* A method with structured challenges (`TanMethod.structuredChallenge`) may use the formatting tags the specification
|
|
158
|
+
* defines. Line breaks, paragraphs and list items become new lines. Bold, italics, underlining and list tags are
|
|
159
|
+
* removed, and the entity references of such markup are resolved. Any other challenge stays as the bank sent it,
|
|
160
|
+
* which is how the specification wants it shown.
|
|
161
|
+
*
|
|
162
|
+
* Show it as text, such as with `textContent`, or `{{ }}` in Vue, and `white-space: pre-line` for its line breaks;
|
|
163
|
+
* never as HTML (`innerHTML`, `v-html`). It keeps the tags the specification does not define, and `<` becomes
|
|
164
|
+
* `<`, so that as HTML the bank's text could run scripts in the app.
|
|
165
|
+
*/
|
|
166
|
+
get plainMessage(): string | undefined {
|
|
167
|
+
const text = this.message;
|
|
168
|
+
if (text === undefined) return undefined;
|
|
169
|
+
const instruction = challengeInstruction(text);
|
|
170
|
+
if (!this.method.structuredChallenge) return instruction;
|
|
171
|
+
return instruction
|
|
172
|
+
.replace(lineBreak, '\n')
|
|
173
|
+
.replace(blockTag, '\n')
|
|
174
|
+
.replace(formattingTag, '')
|
|
175
|
+
.replace(entityReference, (match, name: string) => decodeEntity(name) ?? match)
|
|
176
|
+
.replace(blankLines, '\n\n')
|
|
177
|
+
.trim();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Copies of a challenge for the prompts of one approval. Not methods of `TanChallenge`, so that they are not part of its
|
|
182
|
+
// API; they stand beside the class, so that a field added to it is added here as well.
|
|
183
|
+
|
|
184
|
+
/** `challenge` again, counted as the next prompt for the same approval, with the bank's new instructions `message`. */
|
|
185
|
+
export function nextAttempt(challenge: TanChallenge, message?: string): TanChallenge {
|
|
186
|
+
return copyChallenge(challenge, { attempt: challenge.attempt + 1, message: message ?? challenge.message });
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** `challenge` with `TanChallenge.resolved` set to `resolved`. */
|
|
190
|
+
export function withResolved(challenge: TanChallenge, resolved: Promise<void>): TanChallenge {
|
|
191
|
+
return copyChallenge(challenge, { resolved });
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function copyChallenge(challenge: TanChallenge, changes: Partial<TanChallengeInit>): TanChallenge {
|
|
195
|
+
return new TanChallenge({
|
|
196
|
+
method: challenge.method,
|
|
197
|
+
reference: challenge.reference,
|
|
198
|
+
message: challenge.message,
|
|
199
|
+
hhdUc: challenge.hhdUc,
|
|
200
|
+
flickerCode: challenge.flickerCode,
|
|
201
|
+
image: challenge.image,
|
|
202
|
+
isDecoupled: challenge.isDecoupled,
|
|
203
|
+
validUntil: challenge.validUntil,
|
|
204
|
+
tanMediumName: challenge.tanMediumName,
|
|
205
|
+
attempt: challenge.attempt,
|
|
206
|
+
resolved: challenge.resolved,
|
|
207
|
+
...changes,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** The user's answer to a `TanChallenge`: `TanAnswer.tan(…)`, `TanAnswer.approved` or `TanAnswer.cancelled`. */
|
|
212
|
+
export type TanAnswer =
|
|
213
|
+
/**
|
|
214
|
+
* The TAN the user entered; an empty one cancels a TAN challenge. Blanks around it do not count, and digits of
|
|
215
|
+
* another width, such as full-width ones, count as the digits they are (Unicode NFKC).
|
|
216
|
+
*/
|
|
217
|
+
| { readonly kind: 'tan'; readonly tan: string }
|
|
218
|
+
/** The user reports the approval in the banking app (`TanChallenge.isDecoupled`); the client then asks the bank. */
|
|
219
|
+
| { readonly kind: 'approved' }
|
|
220
|
+
/** The user cancels: the client ends the bank dialog, and the call fails with `FinTsTanCancelledError`. */
|
|
221
|
+
| { readonly kind: 'cancelled' };
|
|
222
|
+
|
|
223
|
+
const approvedAnswer: TanAnswer = Object.freeze({ kind: 'approved' });
|
|
224
|
+
const cancelledAnswer: TanAnswer = Object.freeze({ kind: 'cancelled' });
|
|
225
|
+
|
|
226
|
+
/** The answers a `TanCallback` returns. */
|
|
227
|
+
export const TanAnswer = Object.freeze({
|
|
228
|
+
/** The TAN the user entered; an empty one cancels a TAN challenge (`TanAnswer`). */
|
|
229
|
+
tan: (tan: string): TanAnswer => Object.freeze({ kind: 'tan', tan }),
|
|
230
|
+
/** The user reports the approval in the banking app (`TanChallenge.isDecoupled`); the client then asks the bank. */
|
|
231
|
+
approved: approvedAnswer,
|
|
232
|
+
/** The user cancels: the client ends the bank dialog, and the call fails with `FinTsTanCancelledError`. */
|
|
233
|
+
cancelled: cancelledAnswer,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Shows the `TanChallenge` to the user and returns the `TanAnswer`: `TanAnswer.tan(…)` for a TAN, `TanAnswer.approved`
|
|
238
|
+
* once the user reports an approval in the banking app (`TanChallenge.isDecoupled`), `TanAnswer.cancelled` to cancel
|
|
239
|
+
* either.
|
|
240
|
+
*
|
|
241
|
+
* The call fails with a `TypeError`, without reaching the bank, for anything but a `TanAnswer`, for `approved` to a TAN
|
|
242
|
+
* challenge, for a TAN to an app approval the user can report, and for a TAN that the method rules out: longer than
|
|
243
|
+
* `TanMethod.maxTanLength`, with anything but the digits 0 to 9 where `TanMethod.numericTan`, or that FinTS 3.0 cannot
|
|
244
|
+
* send (ISO 8859-1 only). Where the user cannot report the approval (`TanChallenge.manualConfirmation` is `false`),
|
|
245
|
+
* offer no confirm button: any `TanAnswer` but `cancelled` is ignored.
|
|
246
|
+
*
|
|
247
|
+
* If the bank has not confirmed a reported approval yet, the callback is called again with the next
|
|
248
|
+
* `TanChallenge.attempt` and the bank's latest instructions. Where the client polls the bank by itself, it sets
|
|
249
|
+
* `TanChallenge.resolved`: close the prompt when it settles, the callback's result is no longer waited for then. New
|
|
250
|
+
* instructions of the bank then replace the open prompt with the next attempt, which the callback gets after the
|
|
251
|
+
* handlers of the earlier prompt's `resolved` have run.
|
|
252
|
+
*/
|
|
253
|
+
export type TanCallback = (challenge: TanChallenge) => TanAnswer | Promise<TanAnswer>;
|
|
254
|
+
|
|
255
|
+
/** Picks one of several allowed TAN methods; the first is used when this returns `undefined`. */
|
|
256
|
+
export type TanMethodSelector = (
|
|
257
|
+
methods: readonly TanMethod[],
|
|
258
|
+
) => TanMethod | undefined | Promise<TanMethod | undefined>;
|
|
259
|
+
|
|
260
|
+
/** Picks one of several active TAN media; the first is used when this returns `undefined`. */
|
|
261
|
+
export type TanMediumSelector = (media: readonly TanMedium[]) => TanMedium | undefined | Promise<TanMedium | undefined>;
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { FormatError } from '../util/errors.js';
|
|
2
|
+
import { withoutWhitespace } from '../util/text.js';
|
|
3
|
+
|
|
4
|
+
/** Version of the chipTAN HHD specification a flicker code follows: 1.3, or 1.4, which adds control bytes. */
|
|
5
|
+
export type HhdVersion = 'hhd13' | 'hhd14';
|
|
6
|
+
|
|
7
|
+
// A combined challenge embeds the flicker code in the challenge text:
|
|
8
|
+
// `CHLGUC` + four-digit length + code + `CHLGTEXT` + four digits + instruction text.
|
|
9
|
+
const codeMarker = 'CHLGUC';
|
|
10
|
+
const textMarker = 'CHLGTEXT';
|
|
11
|
+
const markerLengthDigits = 4;
|
|
12
|
+
|
|
13
|
+
/** Whether `message` is a combined challenge with an embedded flicker code. */
|
|
14
|
+
export function hasEmbeddedFlickerCode(message: string): boolean {
|
|
15
|
+
return message.includes(codeMarker);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The instruction text of a combined challenge; `message` itself if it is none. */
|
|
19
|
+
export function challengeInstruction(message: string): string {
|
|
20
|
+
const start = message.indexOf(textMarker);
|
|
21
|
+
if (start < 0) return message;
|
|
22
|
+
const textStart = start + textMarker.length + markerLengthDigits;
|
|
23
|
+
return textStart < message.length ? message.substring(textStart) : '';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Digit counts of the challenge length (LC) and of the data element lengths (LDE) to try when parsing.
|
|
28
|
+
*
|
|
29
|
+
* HHD 1.4 states the LC with three digits, HHD 1.3 with two. Some banks state the LDE of HHD 1.4 with three digits;
|
|
30
|
+
* a code that both LDE widths read is taken with two, as the specification has it.
|
|
31
|
+
*/
|
|
32
|
+
const layouts = [
|
|
33
|
+
{ lcDigits: 3, lengthDigits: 2 },
|
|
34
|
+
{ lcDigits: 3, lengthDigits: 3 },
|
|
35
|
+
{ lcDigits: 2, lengthDigits: 2 },
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
/** The most bytes a length field states: HHD 1.3 flags ASCII data in bit 4 of it, HHD 1.4 in bit 6. */
|
|
39
|
+
const maxElementLength: Readonly<Record<HhdVersion, number>> = { hhd13: 0x0f, hhd14: 0x3f };
|
|
40
|
+
|
|
41
|
+
/** The most bytes of a rendered code, which its one-byte length (LC) states. */
|
|
42
|
+
const maxCodeLength = 0xff;
|
|
43
|
+
|
|
44
|
+
/** The characters a TAN generator shows; one byte each. */
|
|
45
|
+
const printableAscii = /^[\x20-\x7E]*$/;
|
|
46
|
+
|
|
47
|
+
function hex(value: number, width: number): string {
|
|
48
|
+
return value.toString(16).toUpperCase().padStart(width, '0');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A chipTAN optical challenge (flicker code) in HHD 1.3 or 1.4 format. */
|
|
52
|
+
export class FlickerCode {
|
|
53
|
+
private constructor(
|
|
54
|
+
/** The HHD version the code follows, told by the control bytes of its start code. */
|
|
55
|
+
readonly version: HhdVersion,
|
|
56
|
+
/** Length of the challenge (LC) as stated at the start of the code. */
|
|
57
|
+
readonly lc: number,
|
|
58
|
+
private readonly startCode: StartCode,
|
|
59
|
+
private readonly dataElements: readonly DataElement[],
|
|
60
|
+
) {}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Parses the challenge data sent by the bank; throws `FormatError` if it is not a flicker code a TAN generator can
|
|
64
|
+
* take: one with characters after the length it states, an empty data element, data other than printable ASCII, or
|
|
65
|
+
* more data than its length fields can state, such as more than 15 bytes in an element in HHD 1.3 or 255 bytes in all.
|
|
66
|
+
*/
|
|
67
|
+
static parse(code: string): FlickerCode {
|
|
68
|
+
const cleaned = extractCode(code);
|
|
69
|
+
for (const layout of layouts) {
|
|
70
|
+
try {
|
|
71
|
+
return FlickerCode.parseLayout(new Reader(cleaned), layout.lcDigits, layout.lengthDigits);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
// Not this layout; try the next one.
|
|
74
|
+
if (!(error instanceof FormatError)) throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
throw new FormatError('Invalid flicker code', code);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Parses `code`, or returns `undefined` if it is not a flicker code. */
|
|
81
|
+
static tryParse(code: string): FlickerCode | undefined {
|
|
82
|
+
try {
|
|
83
|
+
return FlickerCode.parse(code);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (error instanceof FormatError) return undefined;
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private static parseLayout(code: Reader, lcDigits: number, lengthDigits: number): FlickerCode {
|
|
91
|
+
const lc = code.decimal(lcDigits);
|
|
92
|
+
const challenge = new Reader(code.take(lc));
|
|
93
|
+
// The start code has a hex length field whose top bit announces control bytes (HHD 1.4); the top bit of each
|
|
94
|
+
// control byte announces another one.
|
|
95
|
+
const startLength = challenge.hex(2);
|
|
96
|
+
const controlBytes: number[] = [];
|
|
97
|
+
if ((startLength & 0x80) !== 0) {
|
|
98
|
+
do {
|
|
99
|
+
controlBytes.push(challenge.hex(2));
|
|
100
|
+
} while ((controlBytes.at(-1)! & 0x80) !== 0 && controlBytes.length < 10);
|
|
101
|
+
}
|
|
102
|
+
const startCode = new StartCode(challenge.take(startLength & 0x3f), controlBytes);
|
|
103
|
+
const dataElements: DataElement[] = [];
|
|
104
|
+
for (let i = 0; i < 3; i++) {
|
|
105
|
+
dataElements.push(
|
|
106
|
+
new DataElement(challenge.isAtEnd ? undefined : challenge.take(challenge.dataLength(lengthDigits))),
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
// Only a layout whose fields fill exactly the LC characters, which end the code, can be the right one.
|
|
110
|
+
if (!challenge.isAtEnd || !code.isAtEnd) throw new FormatError('Flicker code fields do not match its length');
|
|
111
|
+
const flicker = new FlickerCode(controlBytes.length === 0 ? 'hhd13' : 'hhd14', lc, startCode, dataElements);
|
|
112
|
+
if (!flicker.fitsTanGenerator) throw new FormatError('Flicker code data the TAN generator cannot take');
|
|
113
|
+
return flicker;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Whether the code renders as the bank sent it: its data printable ASCII, every length within its field. */
|
|
117
|
+
private get fitsTanGenerator(): boolean {
|
|
118
|
+
const elements = [this.startCode, ...this.dataElements];
|
|
119
|
+
return elements.every((element) => element.fits(this.version)) && this.payload().length <= 2 * maxCodeLength;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The data of the start code, which opens the challenge. */
|
|
123
|
+
get startCodeData(): string {
|
|
124
|
+
return this.startCode.data ?? '';
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The first data element of the challenge; `undefined` if the code leaves it out. */
|
|
128
|
+
get dataElement1(): string | undefined {
|
|
129
|
+
return this.dataElements[0]?.data;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The second data element of the challenge; `undefined` if the code leaves it out. */
|
|
133
|
+
get dataElement2(): string | undefined {
|
|
134
|
+
return this.dataElements[1]?.data;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The third data element of the challenge; `undefined` if the code leaves it out. */
|
|
138
|
+
get dataElement3(): string | undefined {
|
|
139
|
+
return this.dataElements[2]?.data;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The hex string transmitted to the TAN generator, including checksums. */
|
|
143
|
+
render(): string {
|
|
144
|
+
const payload = this.payload();
|
|
145
|
+
return `${payload}${this.luhnChecksum()}${xorChecksum(payload)}`;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private payload(): string {
|
|
149
|
+
let s = this.startCode.renderLength(this.version);
|
|
150
|
+
for (const c of this.startCode.controlBytes) s += hex(c, 2);
|
|
151
|
+
s += this.startCode.renderData();
|
|
152
|
+
for (const element of this.dataElements) {
|
|
153
|
+
s += element.renderLength(this.version);
|
|
154
|
+
s += element.renderData();
|
|
155
|
+
}
|
|
156
|
+
return `${hex(Math.floor((s.length + 2) / 2), 2)}${s}`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private luhnChecksum(): string {
|
|
160
|
+
let s = '';
|
|
161
|
+
for (const c of this.startCode.controlBytes) s += hex(c, 2);
|
|
162
|
+
s += this.startCode.renderData();
|
|
163
|
+
for (const element of this.dataElements) if (element.data !== undefined) s += element.renderData();
|
|
164
|
+
let sum = 0;
|
|
165
|
+
for (let i = 0; i + 1 < s.length; i += 2) {
|
|
166
|
+
sum += parseInt(s[i]!, 16) + digitSum(2 * parseInt(s[i + 1]!, 16));
|
|
167
|
+
}
|
|
168
|
+
const mod = sum % 10;
|
|
169
|
+
return mod === 0 ? '0' : hex(10 - mod, 1);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Frames to animate: each is `[clock, bit0, bit1, bit2, bit3]` for the five bars. */
|
|
173
|
+
frames(): boolean[][] {
|
|
174
|
+
const code = `0FFF${this.render()}`;
|
|
175
|
+
const result: boolean[][] = [];
|
|
176
|
+
for (let i = 0; i + 1 < code.length; i += 2) {
|
|
177
|
+
for (const nibble of [code[i + 1]!, code[i]!]) {
|
|
178
|
+
const v = parseInt(nibble, 16);
|
|
179
|
+
const bits = [(v & 1) !== 0, (v & 2) !== 0, (v & 4) !== 0, (v & 8) !== 0];
|
|
180
|
+
result.push([true, ...bits]);
|
|
181
|
+
result.push([false, ...bits]);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return result;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The code of a combined challenge, or `code` itself without whitespace. */
|
|
189
|
+
function extractCode(code: string): string {
|
|
190
|
+
const compact = withoutWhitespace(code);
|
|
191
|
+
const start = compact.indexOf(codeMarker);
|
|
192
|
+
const codeStart = start + codeMarker.length + markerLengthDigits;
|
|
193
|
+
const end = compact.indexOf(textMarker);
|
|
194
|
+
return start >= 0 && end >= codeStart ? compact.substring(codeStart, end) : compact;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function xorChecksum(payload: string): string {
|
|
198
|
+
let x = 0;
|
|
199
|
+
for (const c of payload) x ^= parseInt(c, 16);
|
|
200
|
+
return hex(x, 1);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function digitSum(n: number): number {
|
|
204
|
+
let sum = 0;
|
|
205
|
+
while (n !== 0) {
|
|
206
|
+
sum += n % 10;
|
|
207
|
+
n = Math.floor(n / 10);
|
|
208
|
+
}
|
|
209
|
+
return sum;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** Reads the fields of a flicker code from left to right; throws `FormatError` when a field is missing or malformed. */
|
|
213
|
+
class Reader {
|
|
214
|
+
private position = 0;
|
|
215
|
+
|
|
216
|
+
constructor(private readonly code: string) {}
|
|
217
|
+
|
|
218
|
+
get remaining(): number {
|
|
219
|
+
return this.code.length - this.position;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
get isAtEnd(): boolean {
|
|
223
|
+
return this.remaining === 0;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
take(length: number): string {
|
|
227
|
+
if (length > this.remaining) throw new FormatError('Flicker code too short');
|
|
228
|
+
const field = this.code.substring(this.position, this.position + length);
|
|
229
|
+
this.position += length;
|
|
230
|
+
return field;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
decimal(digits: number): number {
|
|
234
|
+
return parseInt(Reader.match(/^[0-9]+$/, this.take(digits)), 10);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
hex(digits: number): number {
|
|
238
|
+
return parseInt(Reader.match(/^[0-9A-Fa-f]+$/, this.take(digits)), 16);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* A data element length field (LDE) of `digits` decimal digits: 1 to 63 characters. The challenge leaves out an
|
|
243
|
+
* element without data, and sets none of the flags in bits 6 and 7, which only the rendered code carries.
|
|
244
|
+
*/
|
|
245
|
+
dataLength(digits: number): number {
|
|
246
|
+
const length = this.decimal(digits);
|
|
247
|
+
if (length === 0 || length > 0x3f) throw new FormatError('Invalid data element length');
|
|
248
|
+
return length;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private static match(pattern: RegExp, field: string): string {
|
|
252
|
+
if (!pattern.test(field)) throw new FormatError('Invalid length field', field);
|
|
253
|
+
return field;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* A data element of the flicker code: a length field (LDE) followed by the data; `undefined` data if the code leaves
|
|
259
|
+
* the element out.
|
|
260
|
+
*/
|
|
261
|
+
class DataElement {
|
|
262
|
+
constructor(readonly data: string | undefined) {}
|
|
263
|
+
|
|
264
|
+
get isBcd(): boolean {
|
|
265
|
+
return this.data === undefined || /^[0-9]+$/.test(this.data);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/** Whether the data are printable ASCII, and no more bytes than the length field of `version` can state. */
|
|
269
|
+
fits(version: HhdVersion): boolean {
|
|
270
|
+
const d = this.data;
|
|
271
|
+
return d === undefined || (printableAscii.test(d) && this.renderData().length / 2 <= maxElementLength[version]);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
renderData(): string {
|
|
275
|
+
const d = this.data;
|
|
276
|
+
if (d === undefined) return '';
|
|
277
|
+
if (!this.isBcd) {
|
|
278
|
+
let ascii = '';
|
|
279
|
+
for (let i = 0; i < d.length; i++) ascii += hex(d.charCodeAt(i), 2);
|
|
280
|
+
return ascii;
|
|
281
|
+
}
|
|
282
|
+
return d.length % 2 === 1 ? `${d}F` : d;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
renderLength(version: HhdVersion): string {
|
|
286
|
+
if (this.data === undefined) return '';
|
|
287
|
+
const length = Math.floor(this.renderData().length / 2);
|
|
288
|
+
if (this.isBcd) return hex(length, 2);
|
|
289
|
+
if (version === 'hhd14') return hex(length + 0x40, 2);
|
|
290
|
+
return `1${hex(length, 1)}`;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** The start code: a data element whose length field may announce control bytes (HHD 1.4). */
|
|
295
|
+
class StartCode extends DataElement {
|
|
296
|
+
constructor(
|
|
297
|
+
data: string | undefined,
|
|
298
|
+
readonly controlBytes: readonly number[],
|
|
299
|
+
) {
|
|
300
|
+
super(data);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
override renderLength(version: HhdVersion): string {
|
|
304
|
+
const s = super.renderLength(version);
|
|
305
|
+
if (version === 'hhd13' || this.controlBytes.length === 0) return s;
|
|
306
|
+
return hex(parseInt(s, 16) + 0x80, 2);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { base64Encode, latin1Decode } from '../util/bytes.js';
|
|
2
|
+
|
|
3
|
+
/** The formats of photoTAN and QR-TAN images: raster images, which unlike SVG cannot carry scripts. */
|
|
4
|
+
const imageMimeTypes = new Set(['image/png', 'image/jpeg', 'image/gif', 'image/webp', 'image/bmp']);
|
|
5
|
+
|
|
6
|
+
/** An image challenge (photoTAN, QR-TAN) with its MIME type. */
|
|
7
|
+
export class TanImage {
|
|
8
|
+
constructor(
|
|
9
|
+
/** The media type of `bytes`, for example `image/png`. */
|
|
10
|
+
readonly mimeType: string,
|
|
11
|
+
/** The encoded image. */
|
|
12
|
+
readonly bytes: Uint8Array,
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Reads the bank's image container: a two-byte length and the MIME type, then a two-byte length and the image.
|
|
17
|
+
*
|
|
18
|
+
* `undefined` unless the MIME type, in any case, is that of a PNG, JPEG, GIF, WebP or BMP image, and the container
|
|
19
|
+
* holds the whole image.
|
|
20
|
+
*/
|
|
21
|
+
static tryParse(data: Uint8Array): TanImage | undefined {
|
|
22
|
+
if (data.length < 4) return undefined;
|
|
23
|
+
const mimeLength = (data[0]! << 8) | data[1]!;
|
|
24
|
+
if (mimeLength === 0 || data.length < 2 + mimeLength + 2) return undefined;
|
|
25
|
+
const mime = latin1Decode(data.subarray(2, 2 + mimeLength)).toLowerCase();
|
|
26
|
+
if (!imageMimeTypes.has(mime)) return undefined;
|
|
27
|
+
const offset = 2 + mimeLength;
|
|
28
|
+
const imageLength = (data[offset]! << 8) | data[offset + 1]!;
|
|
29
|
+
const start = offset + 2;
|
|
30
|
+
if (start + imageLength > data.length) return undefined;
|
|
31
|
+
return new TanImage(mime, data.subarray(start, start + imageLength));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The image as a `data:` URL, for the `src` of an `<img>` only: the bytes are the bank's, whatever their type says. */
|
|
35
|
+
toDataUrl(): string {
|
|
36
|
+
return `data:${this.mimeType};base64,${base64Encode(this.bytes)}`;
|
|
37
|
+
}
|
|
38
|
+
}
|