@armory-sh/base 0.2.27-alpha.23.81 → 0.2.28-alpha.23.83
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/client-hooks-runtime.d.ts +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +22 -5
- package/dist/payment-requirements.d.ts +7 -0
- package/package.json +1 -1
- package/src/client-hooks-runtime.ts +20 -0
- package/src/index.ts +1 -0
- package/src/payment-requirements.ts +44 -0
- package/src/validation.ts +3 -4
|
@@ -2,6 +2,7 @@ import type { ClientHook, PaymentPayloadContext, PaymentRequiredContext } from "
|
|
|
2
2
|
import type { PaymentRequirementsV2 } from "./types/v2";
|
|
3
3
|
export declare const runOnPaymentRequiredHooks: <TWallet>(hooks: ClientHook<TWallet>[] | undefined, context: PaymentRequiredContext) => Promise<void>;
|
|
4
4
|
export declare const selectRequirementWithHooks: <TWallet>(hooks: ClientHook<TWallet>[] | undefined, context: PaymentRequiredContext) => Promise<PaymentRequirementsV2>;
|
|
5
|
+
export declare const getRequirementAttemptOrderWithHooks: <TWallet>(hooks: ClientHook<TWallet>[] | undefined, context: PaymentRequiredContext) => Promise<PaymentRequirementsV2[]>;
|
|
5
6
|
export declare const runBeforeSignPaymentHooks: <TWallet>(hooks: ClientHook<TWallet>[] | undefined, context: PaymentPayloadContext<TWallet>) => Promise<void>;
|
|
6
7
|
export declare const runAfterPaymentResponseHooks: <TWallet>(hooks: ClientHook<TWallet>[] | undefined, context: PaymentPayloadContext<TWallet> & {
|
|
7
8
|
response: Response;
|
package/dist/index.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export { PaymentException, SigningError, X402ClientError, } from "./errors";
|
|
|
11
11
|
export type { FacilitatorClientConfig, SupportedKind, SupportedResponse, } from "./payment-client";
|
|
12
12
|
export { decodePayloadHeader, extractPayerAddress, getSupported, settlePayment, verifyPayment, } from "./payment-client";
|
|
13
13
|
export type { PaymentConfig, ResolvedRequirementsConfig, } from "./payment-requirements";
|
|
14
|
-
export { createPaymentRequirements, findRequirementByNetwork, } from "./payment-requirements";
|
|
14
|
+
export { createPaymentRequirements, findRequirementByAccepted, findRequirementByNetwork, } from "./payment-requirements";
|
|
15
15
|
export { calculateValidBefore, detectX402Version, generateNonce, getPaymentHeaderName, parseJsonOrBase64, } from "./protocol";
|
|
16
16
|
export type { AcceptPaymentOptions, ArmoryPaymentResult, FacilitatorConfig, FacilitatorSettleResult, FacilitatorVerifyResult, NetworkId, PaymentError, PaymentErrorCode, PaymentResult, PayToAddress, PricingConfig, ResolvedFacilitator, ResolvedNetwork, ResolvedPaymentConfig, ResolvedToken, SettlementMode, TokenId, ValidationError, } from "./types/api";
|
|
17
17
|
export type { BeforePaymentHook, ClientHook, ClientHookErrorContext, ExtensionHook, HookConfig, HookRegistry, HookResult, OnPaymentRequiredHook, PaymentPayloadContext, PaymentRequiredContext, } from "./types/hooks";
|
package/dist/index.js
CHANGED
|
@@ -1311,17 +1311,16 @@ var validateAcceptConfig = (options, payTo, amount) => {
|
|
|
1311
1311
|
}
|
|
1312
1312
|
const tokens = [];
|
|
1313
1313
|
for (const tokenId of tokenIds) {
|
|
1314
|
-
let
|
|
1314
|
+
let matches = 0;
|
|
1315
1315
|
for (const network of networks) {
|
|
1316
1316
|
const resolved = resolveToken(tokenId, network);
|
|
1317
1317
|
if ("code" in resolved) {
|
|
1318
1318
|
continue;
|
|
1319
1319
|
}
|
|
1320
1320
|
tokens.push(resolved);
|
|
1321
|
-
|
|
1322
|
-
break;
|
|
1321
|
+
matches += 1;
|
|
1323
1322
|
}
|
|
1324
|
-
if (
|
|
1323
|
+
if (matches === 0) {
|
|
1325
1324
|
return {
|
|
1326
1325
|
success: false,
|
|
1327
1326
|
error: createError(
|
|
@@ -1522,6 +1521,24 @@ function findRequirementByNetwork(requirements, network) {
|
|
|
1522
1521
|
if (!netConfig) return void 0;
|
|
1523
1522
|
return requirements.find((r) => r.network === netConfig.caip2Id);
|
|
1524
1523
|
}
|
|
1524
|
+
function findRequirementByAccepted(requirements, accepted) {
|
|
1525
|
+
const acceptedPayTo = typeof accepted.payTo === "string" ? accepted.payTo.toLowerCase() : "";
|
|
1526
|
+
const exactMatch = requirements.find(
|
|
1527
|
+
(requirement) => requirement.scheme === accepted.scheme && requirement.network === accepted.network && requirement.amount === accepted.amount && requirement.asset.toLowerCase() === accepted.asset.toLowerCase() && typeof requirement.payTo === "string" && requirement.payTo.toLowerCase() === acceptedPayTo
|
|
1528
|
+
);
|
|
1529
|
+
if (exactMatch) {
|
|
1530
|
+
return exactMatch;
|
|
1531
|
+
}
|
|
1532
|
+
const payToAwareMatch = requirements.find(
|
|
1533
|
+
(requirement) => requirement.scheme === accepted.scheme && requirement.network === accepted.network && acceptedPayTo.length > 0 && typeof requirement.payTo === "string" && requirement.payTo.toLowerCase() === acceptedPayTo
|
|
1534
|
+
);
|
|
1535
|
+
if (payToAwareMatch) {
|
|
1536
|
+
return payToAwareMatch;
|
|
1537
|
+
}
|
|
1538
|
+
return requirements.find(
|
|
1539
|
+
(requirement) => requirement.scheme === accepted.scheme && requirement.network === accepted.network
|
|
1540
|
+
);
|
|
1541
|
+
}
|
|
1525
1542
|
|
|
1526
1543
|
// src/protocol.ts
|
|
1527
1544
|
function parseJsonOrBase64(value) {
|
|
@@ -1736,4 +1753,4 @@ function validateRouteConfig(config) {
|
|
|
1736
1753
|
return null;
|
|
1737
1754
|
}
|
|
1738
1755
|
|
|
1739
|
-
export { EIP712_TYPES, ERC20_ABI, EURC_BASE, NETWORKS, PAYMENT_REQUIRED_HEADER, PAYMENT_RESPONSE_HEADER, PAYMENT_SIGNATURE_HEADER, PaymentException, SCHEMES, SKL_SKALE_BASE, SKL_SKALE_BASE_SEPOLIA, SigningError, TOKENS, USDC_BASE, USDC_BASE_SEPOLIA, USDC_DOMAIN, USDC_SKALE_BASE, USDC_SKALE_BASE_SEPOLIA, USDT_SKALE_BASE, USDT_SKALE_BASE_SEPOLIA, V2_HEADERS, WBTC_SKALE_BASE, WBTC_SKALE_BASE_SEPOLIA, WETH_SKALE_BASE, WETH_SKALE_BASE_SEPOLIA, X402ClientError, X402_VERSION, addressToAssetId, assetIdToAddress, caip2ToNetwork, calculateValidBefore, checkFacilitatorSupport, combineSignature as combineSignatureV2, createEIP712Domain, createError, createNonce, createPaymentRequiredHeaders, createPaymentRequirements, createSettlementHeaders, createTransferWithAuthorization, decodeBase64ToUtf8, decodePayloadHeader, decodePayment, decodePaymentV2, decodeSettlementResponse, decodeSettlementV2, decodeX402Response, detectPaymentVersion, detectX402Version, encodePayment, encodePaymentV2, encodeSettlementResponse, encodeSettlementV2, encodeUtf8ToBase64, encodeX402Response, extractPayerAddress, extractPaymentFromHeaders, findMatchingRoute, findRequirementByNetwork, fromAtomicUnits, generateNonce, getAllCustomTokens, getAllTokens, getAvailableNetworks, getAvailableTokens, getCurrentTimestamp, getCustomToken, getEURCTokens, getMainnets, getNetworkByChainId, getNetworkConfig, getPaymentHeaderName, getSKLTokens, getSupported, getTestnets, getToken, getTokensByChain, getTokensBySymbol, getTxHash, getUSDCTokens, getUSDTTokens, getWBTCTokens, getWETHTokens, isAddress2 as isAddress, isCAIP2ChainId, isCAIPAssetId, isCustomToken, isExactEvmPayload, isPaymentPayload, isPaymentPayloadV2, isPaymentRequiredV2, isPaymentV2, isResolvedNetwork, isResolvedToken, isSettlementSuccessful, isSettlementV2, isValidAddress, isValidationError, isX402V2Payload, isX402V2PaymentRequired, isX402V2Requirements, isX402V2Settlement, matchRoute, networkToCaip2, normalizeAddress, normalizeBase64Url, normalizeNetworkName, parseJsonOrBase64, parseRoutePattern, parseSignature as parseSignatureV2, registerToken, resolveFacilitator, resolveNetwork, resolveToken, runAfterPaymentResponseHooks, runBeforeSignPaymentHooks, runOnPaymentRequiredHooks, safeBase64Decode2 as safeBase64Decode, safeBase64Encode2 as safeBase64Encode, selectRequirementWithHooks, settlePayment, toAtomicUnits, toBase64Url, unregisterToken, validateAcceptConfig, validatePaymentConfig, validateRouteConfig, validateTransferWithAuthorization, verifyPayment };
|
|
1756
|
+
export { EIP712_TYPES, ERC20_ABI, EURC_BASE, NETWORKS, PAYMENT_REQUIRED_HEADER, PAYMENT_RESPONSE_HEADER, PAYMENT_SIGNATURE_HEADER, PaymentException, SCHEMES, SKL_SKALE_BASE, SKL_SKALE_BASE_SEPOLIA, SigningError, TOKENS, USDC_BASE, USDC_BASE_SEPOLIA, USDC_DOMAIN, USDC_SKALE_BASE, USDC_SKALE_BASE_SEPOLIA, USDT_SKALE_BASE, USDT_SKALE_BASE_SEPOLIA, V2_HEADERS, WBTC_SKALE_BASE, WBTC_SKALE_BASE_SEPOLIA, WETH_SKALE_BASE, WETH_SKALE_BASE_SEPOLIA, X402ClientError, X402_VERSION, addressToAssetId, assetIdToAddress, caip2ToNetwork, calculateValidBefore, checkFacilitatorSupport, combineSignature as combineSignatureV2, createEIP712Domain, createError, createNonce, createPaymentRequiredHeaders, createPaymentRequirements, createSettlementHeaders, createTransferWithAuthorization, decodeBase64ToUtf8, decodePayloadHeader, decodePayment, decodePaymentV2, decodeSettlementResponse, decodeSettlementV2, decodeX402Response, detectPaymentVersion, detectX402Version, encodePayment, encodePaymentV2, encodeSettlementResponse, encodeSettlementV2, encodeUtf8ToBase64, encodeX402Response, extractPayerAddress, extractPaymentFromHeaders, findMatchingRoute, findRequirementByAccepted, findRequirementByNetwork, fromAtomicUnits, generateNonce, getAllCustomTokens, getAllTokens, getAvailableNetworks, getAvailableTokens, getCurrentTimestamp, getCustomToken, getEURCTokens, getMainnets, getNetworkByChainId, getNetworkConfig, getPaymentHeaderName, getSKLTokens, getSupported, getTestnets, getToken, getTokensByChain, getTokensBySymbol, getTxHash, getUSDCTokens, getUSDTTokens, getWBTCTokens, getWETHTokens, isAddress2 as isAddress, isCAIP2ChainId, isCAIPAssetId, isCustomToken, isExactEvmPayload, isPaymentPayload, isPaymentPayloadV2, isPaymentRequiredV2, isPaymentV2, isResolvedNetwork, isResolvedToken, isSettlementSuccessful, isSettlementV2, isValidAddress, isValidationError, isX402V2Payload, isX402V2PaymentRequired, isX402V2Requirements, isX402V2Settlement, matchRoute, networkToCaip2, normalizeAddress, normalizeBase64Url, normalizeNetworkName, parseJsonOrBase64, parseRoutePattern, parseSignature as parseSignatureV2, registerToken, resolveFacilitator, resolveNetwork, resolveToken, runAfterPaymentResponseHooks, runBeforeSignPaymentHooks, runOnPaymentRequiredHooks, safeBase64Decode2 as safeBase64Decode, safeBase64Encode2 as safeBase64Encode, selectRequirementWithHooks, settlePayment, toAtomicUnits, toBase64Url, unregisterToken, validateAcceptConfig, validatePaymentConfig, validateRouteConfig, validateTransferWithAuthorization, verifyPayment };
|
|
@@ -20,3 +20,10 @@ export interface ResolvedRequirementsConfig {
|
|
|
20
20
|
}
|
|
21
21
|
export declare function createPaymentRequirements(config: PaymentConfig): ResolvedRequirementsConfig;
|
|
22
22
|
export declare function findRequirementByNetwork(requirements: PaymentRequirementsV2[], network: string): PaymentRequirementsV2 | undefined;
|
|
23
|
+
export declare function findRequirementByAccepted(requirements: PaymentRequirementsV2[], accepted: {
|
|
24
|
+
scheme: string;
|
|
25
|
+
network: string;
|
|
26
|
+
amount: string;
|
|
27
|
+
asset: string;
|
|
28
|
+
payTo: string;
|
|
29
|
+
}): PaymentRequirementsV2 | undefined;
|
package/package.json
CHANGED
|
@@ -90,6 +90,26 @@ export const selectRequirementWithHooks = async <TWallet>(
|
|
|
90
90
|
return selected;
|
|
91
91
|
};
|
|
92
92
|
|
|
93
|
+
export const getRequirementAttemptOrderWithHooks = async <TWallet>(
|
|
94
|
+
hooks: ClientHook<TWallet>[] | undefined,
|
|
95
|
+
context: PaymentRequiredContext,
|
|
96
|
+
): Promise<PaymentRequirementsV2[]> => {
|
|
97
|
+
if (!context.accepts.length) {
|
|
98
|
+
throw new Error("No payment requirements found in accepts array");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const hasSelectorHook = Boolean(hooks?.some((hook) => hook.selectRequirement));
|
|
102
|
+
if (!hasSelectorHook) {
|
|
103
|
+
const first = context.accepts[0];
|
|
104
|
+
context.selectedRequirement = first;
|
|
105
|
+
context.requirements = first;
|
|
106
|
+
return context.accepts;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const selected = await selectRequirementWithHooks(hooks, context);
|
|
110
|
+
return [selected];
|
|
111
|
+
};
|
|
112
|
+
|
|
93
113
|
export const runBeforeSignPaymentHooks = async <TWallet>(
|
|
94
114
|
hooks: ClientHook<TWallet>[] | undefined,
|
|
95
115
|
context: PaymentPayloadContext<TWallet>,
|
package/src/index.ts
CHANGED
|
@@ -254,3 +254,47 @@ export function findRequirementByNetwork(
|
|
|
254
254
|
|
|
255
255
|
return requirements.find((r) => r.network === netConfig.caip2Id);
|
|
256
256
|
}
|
|
257
|
+
|
|
258
|
+
export function findRequirementByAccepted(
|
|
259
|
+
requirements: PaymentRequirementsV2[],
|
|
260
|
+
accepted: {
|
|
261
|
+
scheme: string;
|
|
262
|
+
network: string;
|
|
263
|
+
amount: string;
|
|
264
|
+
asset: string;
|
|
265
|
+
payTo: string;
|
|
266
|
+
},
|
|
267
|
+
): PaymentRequirementsV2 | undefined {
|
|
268
|
+
const acceptedPayTo =
|
|
269
|
+
typeof accepted.payTo === "string" ? accepted.payTo.toLowerCase() : "";
|
|
270
|
+
const exactMatch = requirements.find(
|
|
271
|
+
(requirement) =>
|
|
272
|
+
requirement.scheme === accepted.scheme &&
|
|
273
|
+
requirement.network === accepted.network &&
|
|
274
|
+
requirement.amount === accepted.amount &&
|
|
275
|
+
requirement.asset.toLowerCase() === accepted.asset.toLowerCase() &&
|
|
276
|
+
typeof requirement.payTo === "string" &&
|
|
277
|
+
requirement.payTo.toLowerCase() === acceptedPayTo,
|
|
278
|
+
);
|
|
279
|
+
if (exactMatch) {
|
|
280
|
+
return exactMatch;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const payToAwareMatch = requirements.find(
|
|
284
|
+
(requirement) =>
|
|
285
|
+
requirement.scheme === accepted.scheme &&
|
|
286
|
+
requirement.network === accepted.network &&
|
|
287
|
+
acceptedPayTo.length > 0 &&
|
|
288
|
+
typeof requirement.payTo === "string" &&
|
|
289
|
+
requirement.payTo.toLowerCase() === acceptedPayTo,
|
|
290
|
+
);
|
|
291
|
+
if (payToAwareMatch) {
|
|
292
|
+
return payToAwareMatch;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return requirements.find(
|
|
296
|
+
(requirement) =>
|
|
297
|
+
requirement.scheme === accepted.scheme &&
|
|
298
|
+
requirement.network === accepted.network,
|
|
299
|
+
);
|
|
300
|
+
}
|
package/src/validation.ts
CHANGED
|
@@ -573,17 +573,16 @@ export const validateAcceptConfig = (
|
|
|
573
573
|
|
|
574
574
|
const tokens: ResolvedToken[] = [];
|
|
575
575
|
for (const tokenId of tokenIds) {
|
|
576
|
-
let
|
|
576
|
+
let matches = 0;
|
|
577
577
|
for (const network of networks) {
|
|
578
578
|
const resolved = resolveToken(tokenId, network);
|
|
579
579
|
if ("code" in resolved) {
|
|
580
580
|
continue;
|
|
581
581
|
}
|
|
582
582
|
tokens.push(resolved);
|
|
583
|
-
|
|
584
|
-
break;
|
|
583
|
+
matches += 1;
|
|
585
584
|
}
|
|
586
|
-
if (
|
|
585
|
+
if (matches === 0) {
|
|
587
586
|
return {
|
|
588
587
|
success: false,
|
|
589
588
|
error: createError(
|