thunder-bridge 1.5.0 → 2.1.1

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.js CHANGED
@@ -1,3 +1,198 @@
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
+ var INFO = new TextEncoder().encode("thunder-bridge/sealed");
127
+ function refuseAWeakSecret(secret) {
128
+ if (secret.length < MIN_SECRET_CHARS) {
129
+ throw new Error(`the sealing secret needs ${MIN_SECRET_CHARS} characters of randomness`);
130
+ }
131
+ }
132
+
133
+ // src/bank.ts
134
+ var DEFAULT_CURRENCY = "CZK";
135
+ var DEFAULT_LOOK_BACK_SECS = 7 * 24 * 60 * 60;
136
+ function bankAgent(config) {
137
+ refuseAWeakSecret(config.secret);
138
+ return config.gateway.attend({
139
+ onError: config.onError,
140
+ answer: async (paymentHash) => {
141
+ const order = await config.orders(paymentHash);
142
+ if (order === null) {
143
+ return null;
144
+ }
145
+ const currency = order.currency ?? DEFAULT_CURRENCY;
146
+ return await creditedPreimage({
147
+ secret: config.secret,
148
+ asked: {
149
+ iban: accountOf(order.iban),
150
+ reference: order.reference,
151
+ amountMinor: order.amountMinor,
152
+ currency,
153
+ expiresAt: order.expiresAt
154
+ },
155
+ statement: order.statement,
156
+ lookBackSecs: config.lookBackSecs
157
+ });
158
+ }
159
+ });
160
+ }
161
+ async function creditedPreimage(params) {
162
+ const since = unixNow() - (params.lookBackSecs ?? DEFAULT_LOOK_BACK_SECS);
163
+ const landed = (await params.statement(since)).some((credit) => pays(credit, params.asked));
164
+ if (!landed) {
165
+ return null;
166
+ }
167
+ return await hmacHex(params.secret, `preimage|${subjectOf(params.asked)}`);
168
+ }
169
+ function pays(credit, asked) {
170
+ return credit.amountMinor === asked.amountMinor && credit.currency.toUpperCase() === asked.currency.toUpperCase() && namesOnly(credit.reference, asked.reference);
171
+ }
172
+ function namesOnly(written, reference) {
173
+ const wanted = reference.toUpperCase();
174
+ const alike = [...written.toUpperCase().matchAll(wordsShapedLike(wanted))].map(([word]) => word);
175
+ return alike.includes(wanted) && (!/\d/.test(wanted) || alike.every((word) => word === wanted));
176
+ }
177
+ function wordsShapedLike(reference) {
178
+ const shape = [...reference].map((char) => {
179
+ if (/\d/.test(char)) {
180
+ return "\\d";
181
+ }
182
+ return /\p{L}/u.test(char) ? "\\p{L}" : char.replace(/[\^$\\.*+?()[\]{}|/]/g, "\\$&");
183
+ }).join("");
184
+ return new RegExp(`(?<![\\p{L}\\p{N}])${shape}(?![\\p{L}\\p{N}])`, "gu");
185
+ }
186
+ function subjectOf({ iban, amountMinor, currency, expiresAt, reference }) {
187
+ return `${iban}|${amountMinor}|${currency.toUpperCase()}|${expiresAt}|${reference}`;
188
+ }
189
+ function accountOf(iban) {
190
+ return iban.replace(/\s+/g, "").toUpperCase();
191
+ }
192
+ function unixNow() {
193
+ return Math.floor(Date.now() / 1e3);
194
+ }
195
+
1
196
  // src/fio.ts
2
197
  var BASE_URL = "https://fioapi.fio.cz/v1/rest";
3
198
  var MINOR_UNITS = 100;
@@ -110,5 +305,6 @@ function asDate(unix) {
110
305
  return new Date(unix * MILLIS).toISOString().slice(0, DATE_CHARS);
111
306
  }
112
307
  export {
308
+ bankAgent,
113
309
  fioStatement
114
310
  };
@@ -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
@@ -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