thunder-bridge 1.5.0 → 2.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/dist/bank.cjs CHANGED
@@ -20,10 +20,205 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/entry/bank.ts
21
21
  var bank_exports = {};
22
22
  __export(bank_exports, {
23
+ bankAgent: () => bankAgent,
23
24
  fioStatement: () => fioStatement
24
25
  });
25
26
  module.exports = __toCommonJS(bank_exports);
26
27
 
28
+ // ../core/bytes.ts
29
+ function bytesToHex(bytes) {
30
+ let hex = "";
31
+ for (const byte of bytes) {
32
+ hex += byte.toString(16).padStart(2, "0");
33
+ }
34
+ return hex;
35
+ }
36
+ function hexToBytes(hex) {
37
+ const bytes = new Uint8Array(hex.length >> 1);
38
+ for (let at = 0; at < bytes.length; at++) {
39
+ bytes[at] = parseInt(hex.slice(at * 2, at * 2 + 2), 16);
40
+ }
41
+ return bytes;
42
+ }
43
+
44
+ // ../core/ed25519.ts
45
+ var PKCS8_HEADER = hexToBytes("302e020100300506032b657004220420");
46
+ var SMALL_ORDER = [
47
+ "00".repeat(32),
48
+ `01${"00".repeat(31)}`,
49
+ "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05",
50
+ "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a",
51
+ `ec${"ff".repeat(30)}7f`,
52
+ `ed${"ff".repeat(30)}7f`,
53
+ `ee${"ff".repeat(30)}7f`
54
+ ];
55
+
56
+ // ../core/hmac.ts
57
+ async function hmacHex(secret, payload) {
58
+ const raw = typeof secret === "string" ? new TextEncoder().encode(secret) : secret;
59
+ const key = await crypto.subtle.importKey(
60
+ "raw",
61
+ asBuffer(raw),
62
+ { name: "HMAC", hash: "SHA-256" },
63
+ false,
64
+ ["sign"]
65
+ );
66
+ const message = typeof payload === "string" ? new TextEncoder().encode(payload) : payload;
67
+ return bytesToHex(new Uint8Array(await crypto.subtle.sign("HMAC", key, asBuffer(message))));
68
+ }
69
+ function asBuffer(bytes) {
70
+ return bytes instanceof Uint8Array && bytes.buffer instanceof ArrayBuffer ? bytes : new Uint8Array(bytes);
71
+ }
72
+
73
+ // ../core/sha256.ts
74
+ var ROUND_CONSTANTS = new Uint32Array([
75
+ 1116352408,
76
+ 1899447441,
77
+ 3049323471,
78
+ 3921009573,
79
+ 961987163,
80
+ 1508970993,
81
+ 2453635748,
82
+ 2870763221,
83
+ 3624381080,
84
+ 310598401,
85
+ 607225278,
86
+ 1426881987,
87
+ 1925078388,
88
+ 2162078206,
89
+ 2614888103,
90
+ 3248222580,
91
+ 3835390401,
92
+ 4022224774,
93
+ 264347078,
94
+ 604807628,
95
+ 770255983,
96
+ 1249150122,
97
+ 1555081692,
98
+ 1996064986,
99
+ 2554220882,
100
+ 2821834349,
101
+ 2952996808,
102
+ 3210313671,
103
+ 3336571891,
104
+ 3584528711,
105
+ 113926993,
106
+ 338241895,
107
+ 666307205,
108
+ 773529912,
109
+ 1294757372,
110
+ 1396182291,
111
+ 1695183700,
112
+ 1986661051,
113
+ 2177026350,
114
+ 2456956037,
115
+ 2730485921,
116
+ 2820302411,
117
+ 3259730800,
118
+ 3345764771,
119
+ 3516065817,
120
+ 3600352804,
121
+ 4094571909,
122
+ 275423344,
123
+ 430227734,
124
+ 506948616,
125
+ 659060556,
126
+ 883997877,
127
+ 958139571,
128
+ 1322822218,
129
+ 1537002063,
130
+ 1747873779,
131
+ 1955562222,
132
+ 2024104815,
133
+ 2227730452,
134
+ 2361852424,
135
+ 2428436474,
136
+ 2756734187,
137
+ 3204031479,
138
+ 3329325298
139
+ ]);
140
+ var INITIAL_STATE = new Uint32Array([
141
+ 1779033703,
142
+ 3144134277,
143
+ 1013904242,
144
+ 2773480762,
145
+ 1359893119,
146
+ 2600822924,
147
+ 528734635,
148
+ 1541459225
149
+ ]);
150
+
151
+ // ../core/sealed.ts
152
+ var MIN_SECRET_CHARS = 32;
153
+ function refuseAWeakSecret(secret) {
154
+ if (secret.length < MIN_SECRET_CHARS) {
155
+ throw new Error(`the sealing secret needs ${MIN_SECRET_CHARS} characters of randomness`);
156
+ }
157
+ }
158
+
159
+ // src/bank.ts
160
+ var DEFAULT_CURRENCY = "CZK";
161
+ var DEFAULT_LOOK_BACK_SECS = 7 * 24 * 60 * 60;
162
+ function bankAgent(config) {
163
+ refuseAWeakSecret(config.secret);
164
+ return config.gateway.attend({
165
+ onError: config.onError,
166
+ answer: async (paymentHash) => {
167
+ const order = await config.orders(paymentHash);
168
+ if (order === null) {
169
+ return null;
170
+ }
171
+ const currency = order.currency ?? DEFAULT_CURRENCY;
172
+ return await creditedPreimage({
173
+ secret: config.secret,
174
+ asked: {
175
+ iban: accountOf(order.iban),
176
+ reference: order.reference,
177
+ amountMinor: order.amountMinor,
178
+ currency,
179
+ expiresAt: order.expiresAt
180
+ },
181
+ statement: order.statement,
182
+ lookBackSecs: config.lookBackSecs
183
+ });
184
+ }
185
+ });
186
+ }
187
+ async function creditedPreimage(params) {
188
+ const since = unixNow() - (params.lookBackSecs ?? DEFAULT_LOOK_BACK_SECS);
189
+ const landed = (await params.statement(since)).some((credit) => pays(credit, params.asked));
190
+ if (!landed) {
191
+ return null;
192
+ }
193
+ return await hmacHex(params.secret, `preimage|${subjectOf(params.asked)}`);
194
+ }
195
+ function pays(credit, asked) {
196
+ return credit.amountMinor === asked.amountMinor && credit.currency.toUpperCase() === asked.currency.toUpperCase() && namesOnly(credit.reference, asked.reference);
197
+ }
198
+ function namesOnly(written, reference) {
199
+ const wanted = reference.toUpperCase();
200
+ const alike = [...written.toUpperCase().matchAll(wordsShapedLike(wanted))].map(([word]) => word);
201
+ return alike.includes(wanted) && (!/\d/.test(wanted) || alike.every((word) => word === wanted));
202
+ }
203
+ function wordsShapedLike(reference) {
204
+ const shape = [...reference].map((char) => {
205
+ if (/\d/.test(char)) {
206
+ return "\\d";
207
+ }
208
+ return /\p{L}/u.test(char) ? "\\p{L}" : char.replace(/[\^$\\.*+?()[\]{}|/]/g, "\\$&");
209
+ }).join("");
210
+ return new RegExp(`(?<![\\p{L}\\p{N}])${shape}(?![\\p{L}\\p{N}])`, "gu");
211
+ }
212
+ function subjectOf({ iban, amountMinor, currency, expiresAt, reference }) {
213
+ return `${iban}|${amountMinor}|${currency.toUpperCase()}|${expiresAt}|${reference}`;
214
+ }
215
+ function accountOf(iban) {
216
+ return iban.replace(/\s+/g, "").toUpperCase();
217
+ }
218
+ function unixNow() {
219
+ return Math.floor(Date.now() / 1e3);
220
+ }
221
+
27
222
  // src/fio.ts
28
223
  var BASE_URL = "https://fioapi.fio.cz/v1/rest";
29
224
  var MINOR_UNITS = 100;
@@ -137,5 +332,6 @@ function asDate(unix) {
137
332
  }
138
333
  // Annotate the CommonJS export names for ESM import in node:
139
334
  0 && (module.exports = {
335
+ bankAgent,
140
336
  fioStatement
141
337
  });
package/dist/bank.d.cts CHANGED
@@ -1,5 +1,7 @@
1
- import { S as Statement } from './bank-B3mHISn1.cjs';
2
- export { B as BankTransfer, a as BankTransferParams, b as BankVerifyConfig, C as Credit } from './bank-B3mHISn1.cjs';
1
+ import { S as Statement } from './bank-Bg6RjuO-.cjs';
2
+ export { B as BankAgentConfig, a as BankOrder, b as BankTransfer, c as BankTransferParams, d as BankVerifyConfig, C as Credit, e as bankAgent } from './bank-Bg6RjuO-.cjs';
3
+ import './qr-CF-YeXU1.cjs';
4
+ import './types-DYZ9EkmJ.cjs';
3
5
 
4
6
  /** A Fio account to read credits from, as its own API describes one */
5
7
  interface FioConfig {
package/dist/bank.d.ts CHANGED
@@ -1,5 +1,7 @@
1
- import { S as Statement } from './bank-B3mHISn1.js';
2
- export { B as BankTransfer, a as BankTransferParams, b as BankVerifyConfig, C as Credit } from './bank-B3mHISn1.js';
1
+ import { S as Statement } from './bank-B5MxT_6u.js';
2
+ export { B as BankAgentConfig, a as BankOrder, b as BankTransfer, c as BankTransferParams, d as BankVerifyConfig, C as Credit, e as bankAgent } from './bank-B5MxT_6u.js';
3
+ import './qr-CF-YeXU1.js';
4
+ import './types-DYZ9EkmJ.js';
3
5
 
4
6
  /** A Fio account to read credits from, as its own API describes one */
5
7
  interface FioConfig {
package/dist/bank.js CHANGED
@@ -1,3 +1,197 @@
1
+ // ../core/bytes.ts
2
+ function bytesToHex(bytes) {
3
+ let hex = "";
4
+ for (const byte of bytes) {
5
+ hex += byte.toString(16).padStart(2, "0");
6
+ }
7
+ return hex;
8
+ }
9
+ function hexToBytes(hex) {
10
+ const bytes = new Uint8Array(hex.length >> 1);
11
+ for (let at = 0; at < bytes.length; at++) {
12
+ bytes[at] = parseInt(hex.slice(at * 2, at * 2 + 2), 16);
13
+ }
14
+ return bytes;
15
+ }
16
+
17
+ // ../core/ed25519.ts
18
+ var PKCS8_HEADER = hexToBytes("302e020100300506032b657004220420");
19
+ var SMALL_ORDER = [
20
+ "00".repeat(32),
21
+ `01${"00".repeat(31)}`,
22
+ "26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05",
23
+ "c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a",
24
+ `ec${"ff".repeat(30)}7f`,
25
+ `ed${"ff".repeat(30)}7f`,
26
+ `ee${"ff".repeat(30)}7f`
27
+ ];
28
+
29
+ // ../core/hmac.ts
30
+ async function hmacHex(secret, payload) {
31
+ const raw = typeof secret === "string" ? new TextEncoder().encode(secret) : secret;
32
+ const key = await crypto.subtle.importKey(
33
+ "raw",
34
+ asBuffer(raw),
35
+ { name: "HMAC", hash: "SHA-256" },
36
+ false,
37
+ ["sign"]
38
+ );
39
+ const message = typeof payload === "string" ? new TextEncoder().encode(payload) : payload;
40
+ return bytesToHex(new Uint8Array(await crypto.subtle.sign("HMAC", key, asBuffer(message))));
41
+ }
42
+ function asBuffer(bytes) {
43
+ return bytes instanceof Uint8Array && bytes.buffer instanceof ArrayBuffer ? bytes : new Uint8Array(bytes);
44
+ }
45
+
46
+ // ../core/sha256.ts
47
+ var ROUND_CONSTANTS = new Uint32Array([
48
+ 1116352408,
49
+ 1899447441,
50
+ 3049323471,
51
+ 3921009573,
52
+ 961987163,
53
+ 1508970993,
54
+ 2453635748,
55
+ 2870763221,
56
+ 3624381080,
57
+ 310598401,
58
+ 607225278,
59
+ 1426881987,
60
+ 1925078388,
61
+ 2162078206,
62
+ 2614888103,
63
+ 3248222580,
64
+ 3835390401,
65
+ 4022224774,
66
+ 264347078,
67
+ 604807628,
68
+ 770255983,
69
+ 1249150122,
70
+ 1555081692,
71
+ 1996064986,
72
+ 2554220882,
73
+ 2821834349,
74
+ 2952996808,
75
+ 3210313671,
76
+ 3336571891,
77
+ 3584528711,
78
+ 113926993,
79
+ 338241895,
80
+ 666307205,
81
+ 773529912,
82
+ 1294757372,
83
+ 1396182291,
84
+ 1695183700,
85
+ 1986661051,
86
+ 2177026350,
87
+ 2456956037,
88
+ 2730485921,
89
+ 2820302411,
90
+ 3259730800,
91
+ 3345764771,
92
+ 3516065817,
93
+ 3600352804,
94
+ 4094571909,
95
+ 275423344,
96
+ 430227734,
97
+ 506948616,
98
+ 659060556,
99
+ 883997877,
100
+ 958139571,
101
+ 1322822218,
102
+ 1537002063,
103
+ 1747873779,
104
+ 1955562222,
105
+ 2024104815,
106
+ 2227730452,
107
+ 2361852424,
108
+ 2428436474,
109
+ 2756734187,
110
+ 3204031479,
111
+ 3329325298
112
+ ]);
113
+ var INITIAL_STATE = new Uint32Array([
114
+ 1779033703,
115
+ 3144134277,
116
+ 1013904242,
117
+ 2773480762,
118
+ 1359893119,
119
+ 2600822924,
120
+ 528734635,
121
+ 1541459225
122
+ ]);
123
+
124
+ // ../core/sealed.ts
125
+ var MIN_SECRET_CHARS = 32;
126
+ function refuseAWeakSecret(secret) {
127
+ if (secret.length < MIN_SECRET_CHARS) {
128
+ throw new Error(`the sealing secret needs ${MIN_SECRET_CHARS} characters of randomness`);
129
+ }
130
+ }
131
+
132
+ // src/bank.ts
133
+ var DEFAULT_CURRENCY = "CZK";
134
+ var DEFAULT_LOOK_BACK_SECS = 7 * 24 * 60 * 60;
135
+ function bankAgent(config) {
136
+ refuseAWeakSecret(config.secret);
137
+ return config.gateway.attend({
138
+ onError: config.onError,
139
+ answer: async (paymentHash) => {
140
+ const order = await config.orders(paymentHash);
141
+ if (order === null) {
142
+ return null;
143
+ }
144
+ const currency = order.currency ?? DEFAULT_CURRENCY;
145
+ return await creditedPreimage({
146
+ secret: config.secret,
147
+ asked: {
148
+ iban: accountOf(order.iban),
149
+ reference: order.reference,
150
+ amountMinor: order.amountMinor,
151
+ currency,
152
+ expiresAt: order.expiresAt
153
+ },
154
+ statement: order.statement,
155
+ lookBackSecs: config.lookBackSecs
156
+ });
157
+ }
158
+ });
159
+ }
160
+ async function creditedPreimage(params) {
161
+ const since = unixNow() - (params.lookBackSecs ?? DEFAULT_LOOK_BACK_SECS);
162
+ const landed = (await params.statement(since)).some((credit) => pays(credit, params.asked));
163
+ if (!landed) {
164
+ return null;
165
+ }
166
+ return await hmacHex(params.secret, `preimage|${subjectOf(params.asked)}`);
167
+ }
168
+ function pays(credit, asked) {
169
+ return credit.amountMinor === asked.amountMinor && credit.currency.toUpperCase() === asked.currency.toUpperCase() && namesOnly(credit.reference, asked.reference);
170
+ }
171
+ function namesOnly(written, reference) {
172
+ const wanted = reference.toUpperCase();
173
+ const alike = [...written.toUpperCase().matchAll(wordsShapedLike(wanted))].map(([word]) => word);
174
+ return alike.includes(wanted) && (!/\d/.test(wanted) || alike.every((word) => word === wanted));
175
+ }
176
+ function wordsShapedLike(reference) {
177
+ const shape = [...reference].map((char) => {
178
+ if (/\d/.test(char)) {
179
+ return "\\d";
180
+ }
181
+ return /\p{L}/u.test(char) ? "\\p{L}" : char.replace(/[\^$\\.*+?()[\]{}|/]/g, "\\$&");
182
+ }).join("");
183
+ return new RegExp(`(?<![\\p{L}\\p{N}])${shape}(?![\\p{L}\\p{N}])`, "gu");
184
+ }
185
+ function subjectOf({ iban, amountMinor, currency, expiresAt, reference }) {
186
+ return `${iban}|${amountMinor}|${currency.toUpperCase()}|${expiresAt}|${reference}`;
187
+ }
188
+ function accountOf(iban) {
189
+ return iban.replace(/\s+/g, "").toUpperCase();
190
+ }
191
+ function unixNow() {
192
+ return Math.floor(Date.now() / 1e3);
193
+ }
194
+
1
195
  // src/fio.ts
2
196
  var BASE_URL = "https://fioapi.fio.cz/v1/rest";
3
197
  var MINOR_UNITS = 100;
@@ -110,5 +304,6 @@ function asDate(unix) {
110
304
  return new Date(unix * MILLIS).toISOString().slice(0, DATE_CHARS);
111
305
  }
112
306
  export {
307
+ bankAgent,
113
308
  fioStatement
114
309
  };
@@ -1,4 +1,4 @@
1
- import { W as WalletFailure } from './types-BNPmVnA7.cjs';
1
+ import { W as WalletFailure } from './types-DYZ9EkmJ.cjs';
2
2
 
3
3
  /**
4
4
  * Why an amount was refused. A code rather than a message, because a caller can
@@ -26,7 +26,7 @@ declare class AmountError extends Error {
26
26
  * The way a gateway was caught out, every code is a check that held against the
27
27
  * recipient's own server and failed against what the gateway returned
28
28
  */
29
- type GatewayCheatCode = "address_not_requested" | "hash_mismatch" | "amount_mismatch" | "description_hash_mismatch" | "verify_url_foreign" | "invoice_not_issued" | "preimage_mismatch" | "id_not_mine";
29
+ type GatewayCheatCode = "address_not_requested" | "hash_mismatch" | "amount_mismatch" | "description_hash_mismatch" | "verify_url_foreign" | "invoice_not_issued" | "preimage_mismatch" | "id_not_mine" | "invoice_settled" | "invoice_stale" | "invoice_reused";
30
30
  /**
31
31
  * Thrown when the gateway demonstrably misbehaved, the invoice it returned is
32
32
  * not the one the address you asked for issued, or a settlement it reported
@@ -123,4 +123,4 @@ declare class NoWalletAvailableError extends ProblemError {
123
123
  }, wallets: WalletFailure[]);
124
124
  }
125
125
 
126
- export { AmountError as A, type GatewayCheatCode as G, type IdempotencyConflict as I, NoWalletAvailableError as N, ProblemError as P, UnverifiedRecipientError as U, type WrapRefusalCode as W, type AmountFault as a, GatewayCheatError as b, IdempotencyConflictError as c, WrapRefusedError as d };
126
+ export { AmountError as A, GatewayCheatError as G, type IdempotencyConflict as I, NoWalletAvailableError as N, ProblemError as P, UnverifiedRecipientError as U, type WrapRefusalCode as W, type AmountFault as a, type GatewayCheatCode as b, IdempotencyConflictError as c, WrapRefusedError as d };
@@ -1,4 +1,4 @@
1
- import { W as WalletFailure } from './types-BNPmVnA7.js';
1
+ import { W as WalletFailure } from './types-DYZ9EkmJ.js';
2
2
 
3
3
  /**
4
4
  * Why an amount was refused. A code rather than a message, because a caller can
@@ -26,7 +26,7 @@ declare class AmountError extends Error {
26
26
  * The way a gateway was caught out, every code is a check that held against the
27
27
  * recipient's own server and failed against what the gateway returned
28
28
  */
29
- type GatewayCheatCode = "address_not_requested" | "hash_mismatch" | "amount_mismatch" | "description_hash_mismatch" | "verify_url_foreign" | "invoice_not_issued" | "preimage_mismatch" | "id_not_mine";
29
+ type GatewayCheatCode = "address_not_requested" | "hash_mismatch" | "amount_mismatch" | "description_hash_mismatch" | "verify_url_foreign" | "invoice_not_issued" | "preimage_mismatch" | "id_not_mine" | "invoice_settled" | "invoice_stale" | "invoice_reused";
30
30
  /**
31
31
  * Thrown when the gateway demonstrably misbehaved, the invoice it returned is
32
32
  * not the one the address you asked for issued, or a settlement it reported
@@ -123,4 +123,4 @@ declare class NoWalletAvailableError extends ProblemError {
123
123
  }, wallets: WalletFailure[]);
124
124
  }
125
125
 
126
- export { AmountError as A, type GatewayCheatCode as G, type IdempotencyConflict as I, NoWalletAvailableError as N, ProblemError as P, UnverifiedRecipientError as U, type WrapRefusalCode as W, type AmountFault as a, GatewayCheatError as b, IdempotencyConflictError as c, WrapRefusedError as d };
126
+ export { AmountError as A, GatewayCheatError as G, type IdempotencyConflict as I, NoWalletAvailableError as N, ProblemError as P, UnverifiedRecipientError as U, type WrapRefusalCode as W, type AmountFault as a, type GatewayCheatCode as b, IdempotencyConflictError as c, WrapRefusedError as d };