@hsuite/smart-engines-cli 1.8.0 → 2.0.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/README.md +1 -0
- package/dist/__tests__/helpers/xrpl-signing.d.ts +26 -0
- package/dist/__tests__/helpers/xrpl-signing.d.ts.map +1 -0
- package/dist/__tests__/helpers/xrpl-signing.js +39 -0
- package/dist/__tests__/helpers/xrpl-signing.js.map +1 -0
- package/dist/_lib/cluster-provisioning-report.d.ts +29 -0
- package/dist/_lib/cluster-provisioning-report.d.ts.map +1 -0
- package/dist/_lib/cluster-provisioning-report.js +35 -0
- package/dist/_lib/cluster-provisioning-report.js.map +1 -0
- package/dist/_lib/create-outcome.d.ts +43 -0
- package/dist/_lib/create-outcome.d.ts.map +1 -0
- package/dist/_lib/create-outcome.js +121 -0
- package/dist/_lib/create-outcome.js.map +1 -0
- package/dist/_lib/decimal-compare.d.ts +23 -0
- package/dist/_lib/decimal-compare.d.ts.map +1 -0
- package/dist/_lib/decimal-compare.js +69 -0
- package/dist/_lib/decimal-compare.js.map +1 -0
- package/dist/_lib/env.d.ts +6 -0
- package/dist/_lib/env.d.ts.map +1 -1
- package/dist/_lib/env.js +77 -8
- package/dist/_lib/env.js.map +1 -1
- package/dist/_lib/error-message.d.ts +7 -0
- package/dist/_lib/error-message.d.ts.map +1 -0
- package/dist/_lib/error-message.js +21 -0
- package/dist/_lib/error-message.js.map +1 -0
- package/dist/_lib/order-deadline.d.ts +58 -0
- package/dist/_lib/order-deadline.d.ts.map +1 -0
- package/dist/_lib/order-deadline.js +112 -0
- package/dist/_lib/order-deadline.js.map +1 -0
- package/dist/_lib/payment-guard.d.ts +59 -0
- package/dist/_lib/payment-guard.d.ts.map +1 -0
- package/dist/_lib/payment-guard.js +188 -0
- package/dist/_lib/payment-guard.js.map +1 -0
- package/dist/_lib/payment-lock.d.ts +27 -0
- package/dist/_lib/payment-lock.d.ts.map +1 -0
- package/dist/_lib/payment-lock.js +199 -0
- package/dist/_lib/payment-lock.js.map +1 -0
- package/dist/_lib/payment-record.d.ts +70 -0
- package/dist/_lib/payment-record.d.ts.map +1 -0
- package/dist/_lib/payment-record.js +78 -0
- package/dist/_lib/payment-record.js.map +1 -0
- package/dist/_lib/runtime-env.d.ts.map +1 -1
- package/dist/_lib/runtime-env.js +8 -0
- package/dist/_lib/runtime-env.js.map +1 -1
- package/dist/_lib/subscription-client.d.ts +136 -5
- package/dist/_lib/subscription-client.d.ts.map +1 -1
- package/dist/_lib/subscription-client.js +38 -41
- package/dist/_lib/subscription-client.js.map +1 -1
- package/dist/_lib/xrp-payment.d.ts +33 -0
- package/dist/_lib/xrp-payment.d.ts.map +1 -0
- package/dist/_lib/xrp-payment.js +99 -0
- package/dist/_lib/xrp-payment.js.map +1 -0
- package/dist/_lib/xrpl-login-signer.d.ts +54 -0
- package/dist/_lib/xrpl-login-signer.d.ts.map +1 -0
- package/dist/_lib/xrpl-login-signer.js +102 -0
- package/dist/_lib/xrpl-login-signer.js.map +1 -0
- package/dist/commands/app-lifecycle.d.ts.map +1 -1
- package/dist/commands/app-lifecycle.js +8 -2
- package/dist/commands/app-lifecycle.js.map +1 -1
- package/dist/commands/deploy.d.ts.map +1 -1
- package/dist/commands/deploy.js +8 -0
- package/dist/commands/deploy.js.map +1 -1
- package/dist/commands/faucet.d.ts +1 -1
- package/dist/commands/faucet.d.ts.map +1 -1
- package/dist/commands/faucet.js +2 -7
- package/dist/commands/faucet.js.map +1 -1
- package/dist/commands/personhood.d.ts +2 -2
- package/dist/commands/personhood.d.ts.map +1 -1
- package/dist/commands/personhood.js +11 -14
- package/dist/commands/personhood.js.map +1 -1
- package/dist/commands/redeploy.d.ts.map +1 -1
- package/dist/commands/redeploy.js +6 -0
- package/dist/commands/redeploy.js.map +1 -1
- package/dist/commands/subscribe.d.ts +68 -7
- package/dist/commands/subscribe.d.ts.map +1 -1
- package/dist/commands/subscribe.js +793 -91
- package/dist/commands/subscribe.js.map +1 -1
- package/dist/commands/topup.d.ts +19 -7
- package/dist/commands/topup.d.ts.map +1 -1
- package/dist/commands/topup.js +142 -15
- package/dist/commands/topup.js.map +1 -1
- package/dist/commands/transactions.d.ts +28 -31
- package/dist/commands/transactions.d.ts.map +1 -1
- package/dist/commands/transactions.js +36 -41
- package/dist/commands/transactions.js.map +1 -1
- package/dist/commands/upgrade.d.ts.map +1 -1
- package/dist/commands/upgrade.js +22 -3
- package/dist/commands/upgrade.js.map +1 -1
- package/dist/commands/verify.d.ts +19 -2
- package/dist/commands/verify.d.ts.map +1 -1
- package/dist/commands/verify.js +158 -23
- package/dist/commands/verify.js.map +1 -1
- package/dist/index.js +0 -4
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/dist/commands/governance.d.ts +0 -42
- package/dist/commands/governance.d.ts.map +0 -1
- package/dist/commands/governance.js +0 -155
- package/dist/commands/governance.js.map +0 -1
- package/dist/commands/hist-balance.d.ts +0 -52
- package/dist/commands/hist-balance.d.ts.map +0 -1
- package/dist/commands/hist-balance.js +0 -134
- package/dist/commands/hist-balance.js.map +0 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.errorMessage = errorMessage;
|
|
4
|
+
/**
|
|
5
|
+
* A printable reason for whatever was thrown. An `Error` gives its message (its
|
|
6
|
+
* name when the message is empty); a thrown string is itself; anything else is
|
|
7
|
+
* JSON — never "undefined" and never "[object Object]".
|
|
8
|
+
*/
|
|
9
|
+
function errorMessage(err) {
|
|
10
|
+
if (err instanceof Error)
|
|
11
|
+
return err.message || err.name;
|
|
12
|
+
if (typeof err === 'string')
|
|
13
|
+
return err;
|
|
14
|
+
try {
|
|
15
|
+
return JSON.stringify(err) ?? String(err);
|
|
16
|
+
}
|
|
17
|
+
catch {
|
|
18
|
+
return String(err);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=error-message.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-message.js","sourceRoot":"","sources":["../../src/_lib/error-message.ts"],"names":[],"mappings":";;AAKA,oCAQC;AAbD;;;;GAIG;AACH,SAAgB,YAAY,CAAC,GAAY;IACvC,IAAI,GAAG,YAAY,KAAK;QAAE,OAAO,GAAG,CAAC,OAAO,IAAI,GAAG,CAAC,IAAI,CAAC;IACzD,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,GAAG,CAAC;IACxC,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC;IAC5C,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;IACrB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { SubscriptionClient } from './subscription-client';
|
|
2
|
+
/** The subset of a quote response {@link renderOrderDeadline} /
|
|
3
|
+
* {@link ensureUnexpiredQuote} actually need — satisfied by both
|
|
4
|
+
* `DepositInstructions` and `SubscriptionClient.topUp`'s response. */
|
|
5
|
+
export type OrderQuoteFields = {
|
|
6
|
+
orderId?: string;
|
|
7
|
+
expiresAt?: string;
|
|
8
|
+
amount?: string;
|
|
9
|
+
destinationTag?: number;
|
|
10
|
+
rate?: {
|
|
11
|
+
hsuiteUsd: string;
|
|
12
|
+
source: string;
|
|
13
|
+
};
|
|
14
|
+
priceUsd?: number;
|
|
15
|
+
hsuiteDue?: string;
|
|
16
|
+
/** #3699 PR2 — an order priced in XRP says what to send here (XRP drops); `amount` stays the HST it ends up as. */
|
|
17
|
+
pay?: {
|
|
18
|
+
asset: string;
|
|
19
|
+
amount: string;
|
|
20
|
+
address: string;
|
|
21
|
+
destinationTag: number;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Print the quote's deadline (local time + minutes remaining) and the
|
|
26
|
+
* locked rate source alongside the amount. No-op when the server didn't
|
|
27
|
+
* wire a price-locked order (`SubscriptionOrderService` unbound, or an
|
|
28
|
+
* older server) — `expiresAt` is absent in that case, same as today.
|
|
29
|
+
*/
|
|
30
|
+
export declare function renderOrderDeadline(quote: Pick<OrderQuoteFields, 'expiresAt' | 'rate'>): void;
|
|
31
|
+
/**
|
|
32
|
+
* The Destination Tag line of a quote's payment instructions. The order is matched by the tag, so a
|
|
33
|
+
* payment that omits it (typed by hand, from a wallet whose "destination tag" field was left empty)
|
|
34
|
+
* is not attributed to the order. `!== undefined`, not truthiness: `0` is a valid tag (#3765, #3917).
|
|
35
|
+
*/
|
|
36
|
+
export declare function renderDestinationTag(quote: Pick<OrderQuoteFields, 'destinationTag'>): void;
|
|
37
|
+
/**
|
|
38
|
+
* The XUMM deeplink of an XRPL payment instruction. Carries `dt=` whenever the quote has a
|
|
39
|
+
* destination tag (`0` included): a deeplink without it pays an untagged payment, which the
|
|
40
|
+
* platform attributes only to the sole OPEN order on the address (#3917 item 2).
|
|
41
|
+
*/
|
|
42
|
+
export declare function xummSendDeeplink(d: Pick<OrderQuoteFields, 'destinationTag'> & {
|
|
43
|
+
walletAddress: string;
|
|
44
|
+
amount: string;
|
|
45
|
+
tokenId: string;
|
|
46
|
+
}): string;
|
|
47
|
+
/**
|
|
48
|
+
* Before showing or paying a quote: if its order has already expired, requote
|
|
49
|
+
* it and return a quote for the NEW amount/tag instead — never put an expired
|
|
50
|
+
* order's stale amount in front of a payer (#3699 D8, #3765). A no-op (returns `quote` unchanged) when
|
|
51
|
+
* there is no `orderId`/`expiresAt` to check (older server / order creation
|
|
52
|
+
* unavailable) or the quote is still within its deadline. Preserves every
|
|
53
|
+
* OTHER field on `quote` (e.g. `walletAddress`/`depositAddress`,
|
|
54
|
+
* `success`) — a requote never moves the deposit account, only the
|
|
55
|
+
* economic terms this function overrides.
|
|
56
|
+
*/
|
|
57
|
+
export declare function ensureUnexpiredQuote<T extends OrderQuoteFields>(client: Pick<SubscriptionClient, 'requoteOrder'>, quote: T): Promise<T>;
|
|
58
|
+
//# sourceMappingURL=order-deadline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"order-deadline.d.ts","sourceRoot":"","sources":["../../src/_lib/order-deadline.ts"],"names":[],"mappings":"AAoBA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAGhE;;sEAEsE;AACtE,MAAM,MAAM,gBAAgB,GAAG;IAC7B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,IAAI,CAAC,EAAE;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC;IAC7C,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,mHAAmH;IACnH,GAAG,CAAC,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,cAAc,EAAE,MAAM,CAAA;KAAE,CAAC;CAClF,CAAC;AAEF;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,IAAI,CAAC,gBAAgB,EAAE,WAAW,GAAG,MAAM,CAAC,GAAG,IAAI,CAY7F;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,IAAI,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,GAAG,IAAI,CAG1F;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAC9B,CAAC,EAAE,IAAI,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,GAAG;IAAE,aAAa,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GACvG,MAAM,CAGR;AAED;;;;;;;;;GASG;AACH,wBAAsB,oBAAoB,CAAC,CAAC,SAAS,gBAAgB,EACnE,MAAM,EAAE,IAAI,CAAC,kBAAkB,EAAE,cAAc,CAAC,EAChD,KAAK,EAAE,CAAC,GACP,OAAO,CAAC,CAAC,CAAC,CA2CZ"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.renderOrderDeadline = renderOrderDeadline;
|
|
7
|
+
exports.renderDestinationTag = renderDestinationTag;
|
|
8
|
+
exports.xummSendDeeplink = xummSendDeeplink;
|
|
9
|
+
exports.ensureUnexpiredQuote = ensureUnexpiredQuote;
|
|
10
|
+
/**
|
|
11
|
+
* #3699 PR1b-ii, D8 — deadline display + requote-before-pay guard shared by
|
|
12
|
+
* `hsuite subscribe` / `hsuite topup` (both auto-pay a price-locked-order
|
|
13
|
+
* quote carrying `orderId`/`expiresAt`/`rate`/`destinationTag` — PR1a wired
|
|
14
|
+
* the fields, PR1b-i wired the server-side expiry/requote enforcement; THIS
|
|
15
|
+
* closes the CLI's own half: show the deadline, and never submit a payment
|
|
16
|
+
* against an order the server has already — or is about to have — marked
|
|
17
|
+
* expired).
|
|
18
|
+
*
|
|
19
|
+
* Generic over the two distinct response shapes that carry these fields
|
|
20
|
+
* (`DepositInstructions` for subscribe/upgrade, `SubscriptionClient.topUp`'s
|
|
21
|
+
* inline response for topup) rather than forcing either one into the
|
|
22
|
+
* other's shape.
|
|
23
|
+
*
|
|
24
|
+
* `hsuite upgrade` does not auto-pay today (manual XUMM flow only), so there is
|
|
25
|
+
* no auto-pay call site to guard — but a manual payer is shown the quote, so it
|
|
26
|
+
* gets the same {@link ensureUnexpiredQuote}, {@link renderDestinationTag} and
|
|
27
|
+
* {@link xummSendDeeplink} (#3917 item 2).
|
|
28
|
+
*/
|
|
29
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
30
|
+
const xrp_payment_1 = require("./xrp-payment");
|
|
31
|
+
/**
|
|
32
|
+
* Print the quote's deadline (local time + minutes remaining) and the
|
|
33
|
+
* locked rate source alongside the amount. No-op when the server didn't
|
|
34
|
+
* wire a price-locked order (`SubscriptionOrderService` unbound, or an
|
|
35
|
+
* older server) — `expiresAt` is absent in that case, same as today.
|
|
36
|
+
*/
|
|
37
|
+
function renderOrderDeadline(quote) {
|
|
38
|
+
if (!quote.expiresAt)
|
|
39
|
+
return;
|
|
40
|
+
const expiresAtMs = new Date(quote.expiresAt).getTime();
|
|
41
|
+
const remainingMin = Math.max(0, Math.round((expiresAtMs - Date.now()) / 60_000));
|
|
42
|
+
console.log(chalk_1.default.gray(` deadline: ${new Date(quote.expiresAt).toLocaleString()} (${remainingMin} min remaining)`));
|
|
43
|
+
if (quote.rate) {
|
|
44
|
+
console.log(chalk_1.default.gray(` rate: ${quote.rate.hsuiteUsd} USD/HST (source: ${quote.rate.source})`));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The Destination Tag line of a quote's payment instructions. The order is matched by the tag, so a
|
|
49
|
+
* payment that omits it (typed by hand, from a wallet whose "destination tag" field was left empty)
|
|
50
|
+
* is not attributed to the order. `!== undefined`, not truthiness: `0` is a valid tag (#3765, #3917).
|
|
51
|
+
*/
|
|
52
|
+
function renderDestinationTag(quote) {
|
|
53
|
+
if (quote.destinationTag === undefined)
|
|
54
|
+
return;
|
|
55
|
+
console.log(chalk_1.default.gray(` tag: ${quote.destinationTag} (Destination Tag — REQUIRED on the payment)`));
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The XUMM deeplink of an XRPL payment instruction. Carries `dt=` whenever the quote has a
|
|
59
|
+
* destination tag (`0` included): a deeplink without it pays an untagged payment, which the
|
|
60
|
+
* platform attributes only to the sole OPEN order on the address (#3917 item 2).
|
|
61
|
+
*/
|
|
62
|
+
function xummSendDeeplink(d) {
|
|
63
|
+
const tag = d.destinationTag !== undefined ? `&dt=${encodeURIComponent(String(d.destinationTag))}` : '';
|
|
64
|
+
return `xumm://xrpl/send?to=${encodeURIComponent(d.walletAddress)}&amount=${encodeURIComponent(d.amount)}&token=${encodeURIComponent(d.tokenId)}${tag}`;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Before showing or paying a quote: if its order has already expired, requote
|
|
68
|
+
* it and return a quote for the NEW amount/tag instead — never put an expired
|
|
69
|
+
* order's stale amount in front of a payer (#3699 D8, #3765). A no-op (returns `quote` unchanged) when
|
|
70
|
+
* there is no `orderId`/`expiresAt` to check (older server / order creation
|
|
71
|
+
* unavailable) or the quote is still within its deadline. Preserves every
|
|
72
|
+
* OTHER field on `quote` (e.g. `walletAddress`/`depositAddress`,
|
|
73
|
+
* `success`) — a requote never moves the deposit account, only the
|
|
74
|
+
* economic terms this function overrides.
|
|
75
|
+
*/
|
|
76
|
+
async function ensureUnexpiredQuote(client, quote) {
|
|
77
|
+
if (!quote.orderId || !quote.expiresAt)
|
|
78
|
+
return quote;
|
|
79
|
+
if (new Date(quote.expiresAt).getTime() > Date.now())
|
|
80
|
+
return quote;
|
|
81
|
+
console.log(chalk_1.default.yellow(`\n quote expired (orderId=${quote.orderId}) — requoting at the current rate...`));
|
|
82
|
+
const requoted = await client.requoteOrder(quote.orderId);
|
|
83
|
+
// A requote asks for the REMAINDER and never carries a reserve surcharge (the one-time charge
|
|
84
|
+
// for the deposit account was already counted in the first quote), so the first deposit's
|
|
85
|
+
// `baseAmount` / `reserveSurcharge` line items must not survive next to the new amount.
|
|
86
|
+
const { baseAmount: _base, reserveSurcharge: _surcharge, ...carried } = quote;
|
|
87
|
+
// #3699 PR2 — an XRP order's requote asks the remainder in XRP DROPS (`pay`); `amount` stays the HST the
|
|
88
|
+
// deposit account ends up holding (the requote's re-priced HST target, when it names one).
|
|
89
|
+
const xrp = requoted.pay.asset === 'XRP';
|
|
90
|
+
const next = {
|
|
91
|
+
...carried,
|
|
92
|
+
...(xrp
|
|
93
|
+
? {
|
|
94
|
+
pay: requoted.pay,
|
|
95
|
+
// Read structurally: this CLI may be built against an SDK that predates `SubscriptionOrder.xrp`.
|
|
96
|
+
amount: requoted.xrp?.hstTarget ?? carried.amount,
|
|
97
|
+
}
|
|
98
|
+
: { amount: requoted.pay.amount }),
|
|
99
|
+
destinationTag: requoted.pay.destinationTag,
|
|
100
|
+
orderId: requoted.orderId,
|
|
101
|
+
expiresAt: requoted.expiresAt,
|
|
102
|
+
rate: requoted.rate,
|
|
103
|
+
priceUsd: requoted.priceUsd,
|
|
104
|
+
hsuiteDue: requoted.hsuiteDue,
|
|
105
|
+
};
|
|
106
|
+
const deadline = next.expiresAt ? new Date(next.expiresAt).toLocaleString() : 'n/a';
|
|
107
|
+
console.log(chalk_1.default.yellow(xrp
|
|
108
|
+
? ` new amount: ${(0, xrp_payment_1.dropsToXrp)(requoted.pay.amount)} XRP (${requoted.pay.amount} drops, tag ${next.destinationTag}), new deadline: ${deadline}\n`
|
|
109
|
+
: ` new amount: ${next.amount} HST (tag ${next.destinationTag}), new deadline: ${deadline}\n`));
|
|
110
|
+
return next;
|
|
111
|
+
}
|
|
112
|
+
//# sourceMappingURL=order-deadline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"order-deadline.js","sourceRoot":"","sources":["../../src/_lib/order-deadline.ts"],"names":[],"mappings":";;;;;AA4CA,kDAYC;AAOD,oDAGC;AAOD,4CAKC;AAYD,oDA8CC;AAxID;;;;;;;;;;;;;;;;;;GAkBG;AACH,kDAA0B;AAE1B,+CAA2C;AAiB3C;;;;;GAKG;AACH,SAAgB,mBAAmB,CAAC,KAAmD;IACrF,IAAI,CAAC,KAAK,CAAC,SAAS;QAAE,OAAO;IAC7B,MAAM,WAAW,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,SAAS,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AS;KACzB,CAAC;IACP,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,cAAc,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC;IACpF,OAAO,CAAC,GAAG,CACT,eAAK,CAAC,MAAM,CACV,GAAG;QACD,CAAC,CAAC,iBAAiB,IAAA,wBAAU,EAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,SAAS,QAAQ,CAAC,GAAG,CAAC,MAAM,eAAe,IAAI,CAAC,cAAc,oBAAoB,QAAQ,IAAI;QAChJ,CAAC,CAAC,iBAAiB,IAAI,CAAC,MAAM,aAAa,IAAI,CAAC,cAAc,oBAAoB,QAAQ,IAAI,CACjG,CACF,CAAC;IACF,OAAO,IAAI,CAAC;AACd,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { PENDING_PAYMENT_KEY, PENDING_TOPUP_PAYMENT_KEY, type PendingPayment } from './payment-record';
|
|
2
|
+
/**
|
|
3
|
+
* The env key a flow keeps its record under. One slot per flow: a `topup` must not read a deposit
|
|
4
|
+
* payment `subscribe` could not clear (and the other way round), or an old, credited payment would
|
|
5
|
+
* refuse a new one.
|
|
6
|
+
*/
|
|
7
|
+
export type PaymentRecordKey = typeof PENDING_PAYMENT_KEY | typeof PENDING_TOPUP_PAYMENT_KEY;
|
|
8
|
+
/** The one ledger call the lookup needs; the `xrpl` client and every test fake satisfy it. */
|
|
9
|
+
export type LedgerQueryClient = {
|
|
10
|
+
request(req: Record<string, unknown>): Promise<{
|
|
11
|
+
result: any;
|
|
12
|
+
}>;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* The payment left this process but no verdict came back (a timeout, a dropped websocket …), or an
|
|
16
|
+
* earlier run's payment may still validate: it may land. A plain "fix the cause and re-run" would
|
|
17
|
+
* invite a second payment, so it gets its own message at each command.
|
|
18
|
+
*/
|
|
19
|
+
export declare class PaymentOutcomeUnknownError extends Error {
|
|
20
|
+
constructor(cause: unknown);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The payment an earlier run sent is on the ledger (validated `tesSUCCESS`), but the platform has
|
|
24
|
+
* not credited it. Thrown INSTEAD of paying — whatever the new quote asks.
|
|
25
|
+
*/
|
|
26
|
+
export declare class RecordedPaymentLandedError extends Error {
|
|
27
|
+
readonly txHash: string;
|
|
28
|
+
constructor(txHash: string);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The payment record in the env file, whoever's it is, or `undefined` when there is none. An
|
|
32
|
+
* unreadable value is an ERROR, not "none": it could be hiding a payment that landed.
|
|
33
|
+
*/
|
|
34
|
+
export declare function readRecordedPayment(envPath: string, key: PaymentRecordKey): PendingPayment | undefined;
|
|
35
|
+
/** The payment an earlier run recorded for `appId`: none when there is no record, or it is another entity's. */
|
|
36
|
+
export declare function readPendingPayment(envPath: string, appId: string, key: PaymentRecordKey): PendingPayment | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Settle the payment an earlier run recorded BEFORE paying again: ask the ledger what became of THAT
|
|
39
|
+
* transaction. `delivered` refuses ({@link RecordedPaymentLandedError}); `pending` refuses as unknown
|
|
40
|
+
* ({@link PaymentOutcomeUnknownError}); `failed` and `never-landed` mean it delivered nothing and can
|
|
41
|
+
* never deliver, so the run goes on (and its own record replaces this one). FAILS CLOSED: a lookup
|
|
42
|
+
* that errors is an error, never "carry on and pay".
|
|
43
|
+
*/
|
|
44
|
+
export declare function refuseIfRecordedPaymentMayLand(client: LedgerQueryClient, envPath: string, appId: string, key: PaymentRecordKey): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Record the payment about to be submitted. Fails loud — with NOTHING submitted — when the file
|
|
47
|
+
* cannot be written (as the pending create is recorded, #3766): a payment this CLI cannot keep track
|
|
48
|
+
* of is exactly the one a requote or a resumed run would make it pay a second time. The record is
|
|
49
|
+
* single-slot per flow, so writing this one forgets another entity's unfinished record; that is said,
|
|
50
|
+
* not silent.
|
|
51
|
+
*/
|
|
52
|
+
export declare function recordPendingPayment(envPath: string, payment: PendingPayment, key: PaymentRecordKey): void;
|
|
53
|
+
/**
|
|
54
|
+
* Forget `appId`'s recorded payment once the platform has CREDITED it. Only that entity's: another
|
|
55
|
+
* entity's record is its own guard. Best effort: a record that survives only makes a later run ask
|
|
56
|
+
* the ledger about a payment that is credited anyway.
|
|
57
|
+
*/
|
|
58
|
+
export declare function clearPendingPayment(envPath: string, appId: string, key: PaymentRecordKey): void;
|
|
59
|
+
//# sourceMappingURL=payment-guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payment-guard.d.ts","sourceRoot":"","sources":["../../src/_lib/payment-guard.ts"],"names":[],"mappings":"AAaA,OAAO,EAEL,mBAAmB,EACnB,yBAAyB,EAIzB,KAAK,cAAc,EAEpB,MAAM,kBAAkB,CAAC;AAE1B;;;;GAIG;AACH,MAAM,MAAM,gBAAgB,GAAG,OAAO,mBAAmB,GAAG,OAAO,yBAAyB,CAAC;AAE7F,8FAA8F;AAC9F,MAAM,MAAM,iBAAiB,GAAG;IAC9B,OAAO,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC;QAAE,MAAM,EAAE,GAAG,CAAA;KAAE,CAAC,CAAC;CACjE,CAAC;AAEF;;;;GAIG;AACH,qBAAa,0BAA2B,SAAQ,KAAK;gBACvC,KAAK,EAAE,OAAO;CAI3B;AAED;;;GAGG;AACH,qBAAa,0BAA2B,SAAQ,KAAK;IACvC,QAAQ,CAAC,MAAM,EAAE,MAAM;gBAAd,MAAM,EAAE,MAAM;CAIpC;AAkDD;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,gBAAgB,GAAG,cAAc,GAAG,SAAS,CAatG;AAED,gHAAgH;AAChH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,gBAAgB,GACpB,cAAc,GAAG,SAAS,CAG5B;AAED;;;;;;GAMG;AACH,wBAAsB,8BAA8B,CAClD,MAAM,EAAE,iBAAiB,EACzB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,gBAAgB,GACpB,OAAO,CAAC,IAAI,CAAC,CAsBf;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,EAAE,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAoB1G;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,gBAAgB,GAAG,IAAI,CAM/F"}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.RecordedPaymentLandedError = exports.PaymentOutcomeUnknownError = void 0;
|
|
7
|
+
exports.readRecordedPayment = readRecordedPayment;
|
|
8
|
+
exports.readPendingPayment = readPendingPayment;
|
|
9
|
+
exports.refuseIfRecordedPaymentMayLand = refuseIfRecordedPaymentMayLand;
|
|
10
|
+
exports.recordPendingPayment = recordPendingPayment;
|
|
11
|
+
exports.clearPendingPayment = clearPendingPayment;
|
|
12
|
+
/**
|
|
13
|
+
* The I/O half of the recorded-payment guard (#3917 items 3 and 5): the env file that remembers
|
|
14
|
+
* the payment a run SENT, and the ledger lookup that says what became of it. The format and the
|
|
15
|
+
* verdicts are `payment-record.ts`; this module is shared by every command that pays from the
|
|
16
|
+
* developer wallet (`subscribe`, `topup`) so that a resumed run never pays twice.
|
|
17
|
+
*
|
|
18
|
+
* The record is written BEFORE the signed blob leaves the process and kept until the platform has
|
|
19
|
+
* credited the payment. On the next run the ledger is asked about THAT transaction — never about the
|
|
20
|
+
* deposit account's balance, which cannot say whose HST it holds.
|
|
21
|
+
*/
|
|
22
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
23
|
+
const env_1 = require("./env");
|
|
24
|
+
const error_message_1 = require("./error-message");
|
|
25
|
+
const payment_record_1 = require("./payment-record");
|
|
26
|
+
/**
|
|
27
|
+
* The payment left this process but no verdict came back (a timeout, a dropped websocket …), or an
|
|
28
|
+
* earlier run's payment may still validate: it may land. A plain "fix the cause and re-run" would
|
|
29
|
+
* invite a second payment, so it gets its own message at each command.
|
|
30
|
+
*/
|
|
31
|
+
class PaymentOutcomeUnknownError extends Error {
|
|
32
|
+
constructor(cause) {
|
|
33
|
+
super((0, error_message_1.errorMessage)(cause), { cause });
|
|
34
|
+
this.name = 'PaymentOutcomeUnknownError';
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.PaymentOutcomeUnknownError = PaymentOutcomeUnknownError;
|
|
38
|
+
/**
|
|
39
|
+
* The payment an earlier run sent is on the ledger (validated `tesSUCCESS`), but the platform has
|
|
40
|
+
* not credited it. Thrown INSTEAD of paying — whatever the new quote asks.
|
|
41
|
+
*/
|
|
42
|
+
class RecordedPaymentLandedError extends Error {
|
|
43
|
+
txHash;
|
|
44
|
+
constructor(txHash) {
|
|
45
|
+
super(`payment ${txHash}, sent by an earlier run, is on the ledger`);
|
|
46
|
+
this.txHash = txHash;
|
|
47
|
+
this.name = 'RecordedPaymentLandedError';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.RecordedPaymentLandedError = RecordedPaymentLandedError;
|
|
51
|
+
/**
|
|
52
|
+
* The `txnNotFound` answer of a `tx` lookup, in either shape xrpl.js raises it, with the server's
|
|
53
|
+
* `searched_all` (true only when it held every ledger in the range asked for). `undefined` for
|
|
54
|
+
* any other failure.
|
|
55
|
+
*/
|
|
56
|
+
function txnNotFoundOf(err) {
|
|
57
|
+
const data = err?.data;
|
|
58
|
+
if (data?.error === 'txnNotFound')
|
|
59
|
+
return { searchedAll: data.searched_all === true };
|
|
60
|
+
if (/txnNotFound/.test(err instanceof Error ? err.message : ''))
|
|
61
|
+
return { searchedAll: false };
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Ask the ledger for a transaction by hash, over `[LastLedgerSequence - 256, LastLedgerSequence]`
|
|
66
|
+
* when the record carries one (the only range the payment can be in). `found: false` is its
|
|
67
|
+
* `txnNotFound`; any other failure throws.
|
|
68
|
+
*/
|
|
69
|
+
async function lookupTx(client, payment) {
|
|
70
|
+
const lls = payment.lastLedgerSequence;
|
|
71
|
+
let res;
|
|
72
|
+
try {
|
|
73
|
+
res = await client.request({
|
|
74
|
+
command: 'tx',
|
|
75
|
+
transaction: payment.txHash,
|
|
76
|
+
...(lls !== undefined ? { min_ledger: Math.max(1, lls - payment_record_1.LLS_HISTORY_WINDOW), max_ledger: lls } : {}),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
const notFound = txnNotFoundOf(err);
|
|
81
|
+
if (notFound)
|
|
82
|
+
return { found: false, ...notFound };
|
|
83
|
+
throw err;
|
|
84
|
+
}
|
|
85
|
+
const result = res.result ?? {};
|
|
86
|
+
if (result.error === 'txnNotFound')
|
|
87
|
+
return { found: false, searchedAll: result.searched_all === true };
|
|
88
|
+
if (result.error !== undefined)
|
|
89
|
+
throw new Error(String(result.error));
|
|
90
|
+
return {
|
|
91
|
+
found: true,
|
|
92
|
+
validated: result.validated === true,
|
|
93
|
+
...(typeof result.meta?.TransactionResult === 'string' ? { result: result.meta.TransactionResult } : {}),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
/** The validated ledger's index, or `undefined` when the ledger does not give a usable one. */
|
|
97
|
+
async function validatedLedgerIndex(client) {
|
|
98
|
+
const res = await client.request({ command: 'ledger', ledger_index: 'validated' });
|
|
99
|
+
const index = Number(res.result?.ledger_index ?? res.result?.ledger?.ledger_index);
|
|
100
|
+
return Number.isFinite(index) ? index : undefined;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The payment record in the env file, whoever's it is, or `undefined` when there is none. An
|
|
104
|
+
* unreadable value is an ERROR, not "none": it could be hiding a payment that landed.
|
|
105
|
+
*/
|
|
106
|
+
function readRecordedPayment(envPath, key) {
|
|
107
|
+
const raw = (0, env_1.readEnv)(envPath).get(key);
|
|
108
|
+
if (raw === undefined || raw === '')
|
|
109
|
+
return undefined;
|
|
110
|
+
const parsed = (0, payment_record_1.parsePendingPayment)(raw);
|
|
111
|
+
if (!parsed) {
|
|
112
|
+
throw new Error(`${key} in ${envPath} is not a payment record this CLI wrote (${JSON.stringify(raw)}) — ` +
|
|
113
|
+
'refusing to pay: it could be hiding a deposit payment that already landed. Check whether ' +
|
|
114
|
+
'the developer wallet already sent HST to the deposit account; if it did not, delete that ' +
|
|
115
|
+
`${key} line from the file and re-run.`);
|
|
116
|
+
}
|
|
117
|
+
return parsed;
|
|
118
|
+
}
|
|
119
|
+
/** The payment an earlier run recorded for `appId`: none when there is no record, or it is another entity's. */
|
|
120
|
+
function readPendingPayment(envPath, appId, key) {
|
|
121
|
+
const recorded = readRecordedPayment(envPath, key);
|
|
122
|
+
return recorded?.appId === appId ? recorded : undefined;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Settle the payment an earlier run recorded BEFORE paying again: ask the ledger what became of THAT
|
|
126
|
+
* transaction. `delivered` refuses ({@link RecordedPaymentLandedError}); `pending` refuses as unknown
|
|
127
|
+
* ({@link PaymentOutcomeUnknownError}); `failed` and `never-landed` mean it delivered nothing and can
|
|
128
|
+
* never deliver, so the run goes on (and its own record replaces this one). FAILS CLOSED: a lookup
|
|
129
|
+
* that errors is an error, never "carry on and pay".
|
|
130
|
+
*/
|
|
131
|
+
async function refuseIfRecordedPaymentMayLand(client, envPath, appId, key) {
|
|
132
|
+
const recorded = readPendingPayment(envPath, appId, key);
|
|
133
|
+
if (!recorded)
|
|
134
|
+
return;
|
|
135
|
+
let verdict;
|
|
136
|
+
try {
|
|
137
|
+
// The validated ledger FIRST: a payment absent from a complete search whose LastLedgerSequence
|
|
138
|
+
// is behind an index read BEFORE the search cannot have validated in between.
|
|
139
|
+
const validated = await validatedLedgerIndex(client);
|
|
140
|
+
verdict = (0, payment_record_1.classifyRecordedPayment)(await lookupTx(client, recorded), recorded, validated);
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
throw new Error(`could not look up payment ${recorded.txHash}, which an earlier run recorded (${(0, error_message_1.errorMessage)(err)}) — ` +
|
|
144
|
+
'refusing to pay without knowing it did not land', { cause: err });
|
|
145
|
+
}
|
|
146
|
+
if (verdict === 'delivered')
|
|
147
|
+
throw new RecordedPaymentLandedError(recorded.txHash);
|
|
148
|
+
if (verdict === 'pending') {
|
|
149
|
+
throw new PaymentOutcomeUnknownError(new Error(`payment ${recorded.txHash}, sent by an earlier run, has not settled on the ledger yet`));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Record the payment about to be submitted. Fails loud — with NOTHING submitted — when the file
|
|
154
|
+
* cannot be written (as the pending create is recorded, #3766): a payment this CLI cannot keep track
|
|
155
|
+
* of is exactly the one a requote or a resumed run would make it pay a second time. The record is
|
|
156
|
+
* single-slot per flow, so writing this one forgets another entity's unfinished record; that is said,
|
|
157
|
+
* not silent.
|
|
158
|
+
*/
|
|
159
|
+
function recordPendingPayment(envPath, payment, key) {
|
|
160
|
+
try {
|
|
161
|
+
const existing = readRecordedPayment(envPath, key);
|
|
162
|
+
if (existing && existing.appId !== payment.appId) {
|
|
163
|
+
console.log(chalk_1.default.yellow(` warning: replacing the unfinished payment record of smart-app entity ${existing.appId} ` +
|
|
164
|
+
`(tx ${existing.txHash}) — resuming that entity can no longer ask the ledger about it.`));
|
|
165
|
+
}
|
|
166
|
+
(0, env_1.upsertEnv)(envPath, { [key]: (0, payment_record_1.formatPendingPayment)(payment) });
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
throw new Error(`cannot record deposit payment ${payment.txHash} in ${envPath} (${(0, error_message_1.errorMessage)(err)}) — refusing ` +
|
|
170
|
+
'to pay: a payment this CLI cannot keep track of could be paid again after a requote or by a resumed run. ' +
|
|
171
|
+
'Nothing was submitted and nothing has been paid.', { cause: err });
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Forget `appId`'s recorded payment once the platform has CREDITED it. Only that entity's: another
|
|
176
|
+
* entity's record is its own guard. Best effort: a record that survives only makes a later run ask
|
|
177
|
+
* the ledger about a payment that is credited anyway.
|
|
178
|
+
*/
|
|
179
|
+
function clearPendingPayment(envPath, appId, key) {
|
|
180
|
+
try {
|
|
181
|
+
if (readPendingPayment(envPath, appId, key))
|
|
182
|
+
(0, env_1.removeEnvKeys)(envPath, [key]);
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
// see above
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
//# sourceMappingURL=payment-guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payment-guard.js","sourceRoot":"","sources":["../../src/_lib/payment-guard.ts"],"names":[],"mappings":";;;;;;AA+GA,kDAaC;AAGD,gDAOC;AASD,wEA2BC;AASD,oDAoBC;AAOD,kDAMC;AApND;;;;;;;;;GASG;AACH,kDAA0B;AAC1B,+BAA0D;AAC1D,mDAA+C;AAC/C,qDAS0B;AAc1B;;;;GAIG;AACH,MAAa,0BAA2B,SAAQ,KAAK;IACnD,YAAY,KAAc;QACxB,KAAK,CAAC,IAAA,4BAAY,EAAC,KAAK,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,4BAA4B,CAAC;IAC3C,CAAC;CACF;AALD,gEAKC;AAED;;;GAGG;AACH,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AAI,CAAC;QACH,IAAI,kBAAkB,CAAC,OAAO,EAAE,KAAK,EAAE,GAAG,CAAC;YAAE,IAAA,mBAAa,EAAC,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAC7E,CAAC;IAAC,MAAM,CAAC;QACP,YAAY;IACd,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** The lock file is the env file's path plus this. */
|
|
2
|
+
export declare const PAYMENT_LOCK_SUFFIX = ".payment-lock";
|
|
3
|
+
/** Who holds a lock: what the lock file says. */
|
|
4
|
+
export type PaymentLockOwner = {
|
|
5
|
+
pid: number;
|
|
6
|
+
host: string;
|
|
7
|
+
/** The command that took it (`hsuite topup`). */
|
|
8
|
+
command: string;
|
|
9
|
+
/** ISO time it was taken. */
|
|
10
|
+
since: string;
|
|
11
|
+
/** Random per acquisition: lets a holder tell its own lock from one that replaced it. */
|
|
12
|
+
token: string;
|
|
13
|
+
};
|
|
14
|
+
/** Another run holds the payment lock for this env file (or the lock file cannot be read). */
|
|
15
|
+
export declare class PaymentLockHeldError extends Error {
|
|
16
|
+
readonly lockPath: string;
|
|
17
|
+
readonly owner: PaymentLockOwner | undefined;
|
|
18
|
+
constructor(lockPath: string, owner: PaymentLockOwner | undefined, message: string);
|
|
19
|
+
}
|
|
20
|
+
/** The lock path for an env file. Resolved through symlinks, so every spelling of one file shares one lock. */
|
|
21
|
+
export declare function paymentLockPath(envPath: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Run `fn` holding the payment lock of `envPath`. A second caller on the same env file is refused
|
|
24
|
+
* with {@link PaymentLockHeldError} while `fn` runs — before it reads, asks or pays anything.
|
|
25
|
+
*/
|
|
26
|
+
export declare function withPaymentLock<T>(envPath: string, command: string, fn: () => Promise<T>): Promise<T>;
|
|
27
|
+
//# sourceMappingURL=payment-lock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"payment-lock.d.ts","sourceRoot":"","sources":["../../src/_lib/payment-lock.ts"],"names":[],"mappings":"AAiCA,sDAAsD;AACtD,eAAO,MAAM,mBAAmB,kBAAkB,CAAC;AAEnD,iDAAiD;AACjD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,iDAAiD;IACjD,OAAO,EAAE,MAAM,CAAC;IAChB,6BAA6B;IAC7B,KAAK,EAAE,MAAM,CAAC;IACd,yFAAyF;IACzF,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,8FAA8F;AAC9F,qBAAa,oBAAqB,SAAQ,KAAK;IAE3C,QAAQ,CAAC,QAAQ,EAAE,MAAM;IACzB,QAAQ,CAAC,KAAK,EAAE,gBAAgB,GAAG,SAAS;gBADnC,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,gBAAgB,GAAG,SAAS,EAC5C,OAAO,EAAE,MAAM;CAKlB;AAED,+GAA+G;AAC/G,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAEvD;AAkID;;;GAGG;AACH,wBAAsB,eAAe,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAiB3G"}
|