@bosonprotocol/x402-server 0.1.0-alpha-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/LICENSE +201 -0
- package/README.md +65 -0
- package/dist/cjs/challenge/index.d.ts +64 -0
- package/dist/cjs/challenge/index.js +96 -0
- package/dist/cjs/challenge/index.js.map +1 -0
- package/dist/cjs/client-tnrpqVMW.d.ts +36 -0
- package/dist/cjs/config-CBr9qMps.d.ts +419 -0
- package/dist/cjs/facilitator/index.d.ts +26 -0
- package/dist/cjs/facilitator/index.js +136 -0
- package/dist/cjs/facilitator/index.js.map +1 -0
- package/dist/cjs/handlers/index.d.ts +267 -0
- package/dist/cjs/handlers/index.js +1182 -0
- package/dist/cjs/handlers/index.js.map +1 -0
- package/dist/cjs/index.d.ts +72 -0
- package/dist/cjs/index.js +1643 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/onchain/index.d.ts +73 -0
- package/dist/cjs/onchain/index.js +87 -0
- package/dist/cjs/onchain/index.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/validate/index.d.ts +67 -0
- package/dist/cjs/validate/index.js +429 -0
- package/dist/cjs/validate/index.js.map +1 -0
- package/dist/esm/challenge/index.js +3 -0
- package/dist/esm/challenge/index.js.map +1 -0
- package/dist/esm/chunk-4NQ3VKC3.js +84 -0
- package/dist/esm/chunk-4NQ3VKC3.js.map +1 -0
- package/dist/esm/chunk-CGVXQX5V.js +426 -0
- package/dist/esm/chunk-CGVXQX5V.js.map +1 -0
- package/dist/esm/chunk-DJMCSCBE.js +3 -0
- package/dist/esm/chunk-DJMCSCBE.js.map +1 -0
- package/dist/esm/chunk-EAKQ4EFZ.js +124 -0
- package/dist/esm/chunk-EAKQ4EFZ.js.map +1 -0
- package/dist/esm/chunk-GBPU373M.js +93 -0
- package/dist/esm/chunk-GBPU373M.js.map +1 -0
- package/dist/esm/chunk-PU5Y7FZG.js +14 -0
- package/dist/esm/chunk-PU5Y7FZG.js.map +1 -0
- package/dist/esm/chunk-WU7QJ7YP.js +3 -0
- package/dist/esm/chunk-WU7QJ7YP.js.map +1 -0
- package/dist/esm/chunk-Y5HFLCAT.js +657 -0
- package/dist/esm/chunk-Y5HFLCAT.js.map +1 -0
- package/dist/esm/facilitator/index.js +4 -0
- package/dist/esm/facilitator/index.js.map +1 -0
- package/dist/esm/handlers/index.js +6 -0
- package/dist/esm/handlers/index.js.map +1 -0
- package/dist/esm/index.js +261 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/onchain/index.js +4 -0
- package/dist/esm/onchain/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/validate/index.js +4 -0
- package/dist/esm/validate/index.js.map +1 -0
- package/package.json +88 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { EscrowPaymentRequirements, Address } from '@bosonprotocol/x402-core/schemes/escrow';
|
|
2
|
+
import { EscrowNextActions, ExchangeState, DisputeState, ChannelRegistry } from '@bosonprotocol/x402-actions';
|
|
3
|
+
import { ExchangeReader } from '../onchain/index.js';
|
|
4
|
+
import { F as FacilitatorClient } from '../client-tnrpqVMW.js';
|
|
5
|
+
import { X as X402bServerConfig, F as FulfillmentRecoveryEntry, b as CoreSdkReadAdapter } from '../config-CBr9qMps.js';
|
|
6
|
+
import { ExchangeActionId } from '@bosonprotocol/x402-core/state-machine';
|
|
7
|
+
import { Hex } from 'viem';
|
|
8
|
+
import '@bosonprotocol/x402-facilitator';
|
|
9
|
+
import 'zod';
|
|
10
|
+
import '@bosonprotocol/core-sdk';
|
|
11
|
+
|
|
12
|
+
type HandlerStatus = 200 | 400 | 402 | 404 | 409 | 500 | 502;
|
|
13
|
+
type HandlerResult<TBody> = {
|
|
14
|
+
ok: true;
|
|
15
|
+
status: 200;
|
|
16
|
+
body: TBody & {
|
|
17
|
+
nextActions: EscrowNextActions;
|
|
18
|
+
};
|
|
19
|
+
} | {
|
|
20
|
+
ok: false;
|
|
21
|
+
status: Exclude<HandlerStatus, 200>;
|
|
22
|
+
body: HandlerErrorBody;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Result shape for handlers whose success body does NOT carry a
|
|
26
|
+
* `nextActions` envelope — used by entity-keyed actions
|
|
27
|
+
* (`boson-withdrawFunds`) and the read-only `available-funds`
|
|
28
|
+
* endpoint, neither of which advance the exchange state machine.
|
|
29
|
+
*/
|
|
30
|
+
type PlainHandlerResult<TBody> = {
|
|
31
|
+
ok: true;
|
|
32
|
+
status: 200;
|
|
33
|
+
body: TBody;
|
|
34
|
+
} | {
|
|
35
|
+
ok: false;
|
|
36
|
+
status: Exclude<HandlerStatus, 200>;
|
|
37
|
+
body: HandlerErrorBody;
|
|
38
|
+
};
|
|
39
|
+
interface HandlerErrorBody {
|
|
40
|
+
/** Stable identifier — caller branches on this rather than the human-readable `reason`. */
|
|
41
|
+
code: string;
|
|
42
|
+
reason: string;
|
|
43
|
+
/** Optional rich detail — validator field/expected/got, facilitator code, etc. */
|
|
44
|
+
details?: unknown;
|
|
45
|
+
}
|
|
46
|
+
interface HandlerWarning {
|
|
47
|
+
/** Stable identifier — caller branches on this rather than the human-readable `reason`. */
|
|
48
|
+
code: string;
|
|
49
|
+
reason: string;
|
|
50
|
+
/** Optional rich detail — tx hash, exchange id, deferred operation, etc. */
|
|
51
|
+
details?: unknown;
|
|
52
|
+
}
|
|
53
|
+
declare function handlerOk<TBody>(body: TBody & {
|
|
54
|
+
nextActions: EscrowNextActions;
|
|
55
|
+
}): HandlerResult<TBody>;
|
|
56
|
+
declare function plainHandlerOk<TBody>(body: TBody): PlainHandlerResult<TBody>;
|
|
57
|
+
declare function handlerErr(status: Exclude<HandlerStatus, 200>, code: string, reason: string, details?: unknown): HandlerResult<never> & PlainHandlerResult<never>;
|
|
58
|
+
|
|
59
|
+
interface CommitHandlerInput {
|
|
60
|
+
/** Raw `X-PAYMENT` header value (base64'd JSON). */
|
|
61
|
+
paymentHeader: string | undefined | null;
|
|
62
|
+
/** The 402 `PaymentRequirements` the buyer is responding to. */
|
|
63
|
+
requirements: EscrowPaymentRequirements;
|
|
64
|
+
}
|
|
65
|
+
interface CommitHandlerContext {
|
|
66
|
+
config: X402bServerConfig;
|
|
67
|
+
facilitator: FacilitatorClient;
|
|
68
|
+
exchangeReader: ExchangeReader;
|
|
69
|
+
fulfillmentRecoveryStore: Map<string, FulfillmentRecoveryEntry>;
|
|
70
|
+
/**
|
|
71
|
+
* Per-exchange fulfillment option policy. Flow A writes the ids
|
|
72
|
+
* advertised by the original requirements so the redeem-time choice
|
|
73
|
+
* is constrained to the offer's own channel set.
|
|
74
|
+
*/
|
|
75
|
+
exchangeFulfillmentOptionStore: Map<string, readonly string[]>;
|
|
76
|
+
}
|
|
77
|
+
interface CommitOk {
|
|
78
|
+
exchangeId: string;
|
|
79
|
+
txHash: string;
|
|
80
|
+
/**
|
|
81
|
+
* Non-fatal post-settle conditions. Today only Flow B uses this slot —
|
|
82
|
+
* the on-chain redeem may have succeeded while the configured channel
|
|
83
|
+
* adapter's `onCommit(...)` failed (the buyer's funds and voucher are
|
|
84
|
+
* already gone; the seller's host needs to recover the delivery target
|
|
85
|
+
* out-of-band). The exchange state is the wire-format source of truth;
|
|
86
|
+
* warnings are advisory.
|
|
87
|
+
*/
|
|
88
|
+
warnings?: HandlerWarning[];
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Flow A — `boson-createOfferAndCommit`. Settles via facilitator,
|
|
92
|
+
* expects the resulting exchange in `COMMITTED`, returns 200 with
|
|
93
|
+
* `nextActions` advertising the legal post-COMMITTED transitions.
|
|
94
|
+
*/
|
|
95
|
+
declare function handleCommit(input: CommitHandlerInput, ctx: CommitHandlerContext): Promise<HandlerResult<CommitOk>>;
|
|
96
|
+
/**
|
|
97
|
+
* Flow B — `boson-createOfferCommitAndRedeem`. Same pipeline as
|
|
98
|
+
* `handleCommit` but verifies the exchange reached `REDEEMED`.
|
|
99
|
+
*/
|
|
100
|
+
declare function handleCommitAndRedeem(input: CommitHandlerInput, ctx: CommitHandlerContext): Promise<HandlerResult<CommitOk>>;
|
|
101
|
+
|
|
102
|
+
/** Per-action inputs accepted by every post-commit convenience handler. */
|
|
103
|
+
interface PerformActionInput {
|
|
104
|
+
exchangeId: string;
|
|
105
|
+
/** ABI-encoded `BosonMetaTx` tuple — see `encodeSignedPayload` in `@bosonprotocol/x402-facilitator`. */
|
|
106
|
+
signedPayload: Hex;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Redeem-time variant of `PerformActionInput`. Carries the buyer's
|
|
110
|
+
* `fulfillment` selection for Flow A — `data` is the delivery target
|
|
111
|
+
* the redeem-time channel adapter persists. Required when the
|
|
112
|
+
* original 402 advertised `fulfillment.required = true`; omitted
|
|
113
|
+
* otherwise.
|
|
114
|
+
*/
|
|
115
|
+
interface RedeemHandlerInput extends PerformActionInput {
|
|
116
|
+
fulfillment?: {
|
|
117
|
+
option: string;
|
|
118
|
+
data: Record<string, unknown> | null;
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
interface PerformActionContext {
|
|
122
|
+
config: X402bServerConfig;
|
|
123
|
+
facilitator: FacilitatorClient;
|
|
124
|
+
exchangeReader: ExchangeReader;
|
|
125
|
+
}
|
|
126
|
+
interface RedeemHandlerContext extends PerformActionContext {
|
|
127
|
+
exchangeFulfillmentOptionStore: Map<string, readonly string[]>;
|
|
128
|
+
fulfillmentRecoveryStore: Map<string, FulfillmentRecoveryEntry>;
|
|
129
|
+
}
|
|
130
|
+
interface PerformActionOk {
|
|
131
|
+
txHash: string;
|
|
132
|
+
warnings?: HandlerWarning[];
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Generic exchange-keyed post-commit handler — wired from each of the
|
|
136
|
+
* per-action wrappers below. Entity-keyed actions (e.g. `withdrawFunds`)
|
|
137
|
+
* have their own handler in `./withdraw-funds.ts`.
|
|
138
|
+
*/
|
|
139
|
+
declare function handlePerformAction(action: ExchangeActionId, input: PerformActionInput, ctx: PerformActionContext): Promise<HandlerResult<PerformActionOk>>;
|
|
140
|
+
/**
|
|
141
|
+
* Redeem handler. Validates the buyer's `fulfillment` selection (if
|
|
142
|
+
* present) against the offer's advertised option set and the host's
|
|
143
|
+
* channel registry, runs the channel's `validate` up-front, then
|
|
144
|
+
* forwards to the facilitator. The corresponding
|
|
145
|
+
* `onCommit(exchangeId, data)` upsert is deferred until *after* the
|
|
146
|
+
* facilitator + state verification confirm the exchange reached
|
|
147
|
+
* `REDEEMED`, so a failed redeem leaves the stored delivery target
|
|
148
|
+
* unchanged. The voucher NFT is transferable; whichever wallet signs
|
|
149
|
+
* `boson-redeem` supplies the delivery data — it's the redeemer's
|
|
150
|
+
* choice end-to-end.
|
|
151
|
+
*/
|
|
152
|
+
declare function handleRedeem(input: RedeemHandlerInput, ctx: RedeemHandlerContext): Promise<HandlerResult<PerformActionOk>>;
|
|
153
|
+
/** Per-action sugar — preserves the action id at the type level. */
|
|
154
|
+
declare const handleComplete: (input: PerformActionInput, ctx: PerformActionContext) => Promise<HandlerResult<PerformActionOk>>;
|
|
155
|
+
declare const handleDisputeRaise: (input: PerformActionInput, ctx: PerformActionContext) => Promise<HandlerResult<PerformActionOk>>;
|
|
156
|
+
declare const handleDisputeResolve: (input: PerformActionInput, ctx: PerformActionContext) => Promise<HandlerResult<PerformActionOk>>;
|
|
157
|
+
declare const handleDisputeRetract: (input: PerformActionInput, ctx: PerformActionContext) => Promise<HandlerResult<PerformActionOk>>;
|
|
158
|
+
declare const handleDisputeEscalate: (input: PerformActionInput, ctx: PerformActionContext) => Promise<HandlerResult<PerformActionOk>>;
|
|
159
|
+
|
|
160
|
+
interface WithdrawFundsBaseInput {
|
|
161
|
+
/** ABI-encoded `BosonMetaTx` tuple — see `encodeSignedPayload` in `@bosonprotocol/x402-evm/codec`. */
|
|
162
|
+
signedPayload: Hex;
|
|
163
|
+
}
|
|
164
|
+
type WithdrawFundsInput = WithdrawFundsBaseInput & ({
|
|
165
|
+
entityId: string;
|
|
166
|
+
} | {
|
|
167
|
+
address: string;
|
|
168
|
+
role?: "buyer" | "seller";
|
|
169
|
+
});
|
|
170
|
+
interface WithdrawFundsContext {
|
|
171
|
+
config: X402bServerConfig;
|
|
172
|
+
facilitator: FacilitatorClient;
|
|
173
|
+
coreSdkRead: CoreSdkReadAdapter;
|
|
174
|
+
}
|
|
175
|
+
interface WithdrawFundsOk {
|
|
176
|
+
txHash: string;
|
|
177
|
+
entityId: string;
|
|
178
|
+
role?: "buyer" | "seller";
|
|
179
|
+
}
|
|
180
|
+
declare function handleWithdrawFunds(input: WithdrawFundsInput, ctx: WithdrawFundsContext): Promise<PlainHandlerResult<WithdrawFundsOk>>;
|
|
181
|
+
|
|
182
|
+
interface AvailableFundsEntry {
|
|
183
|
+
tokenAddress: Address;
|
|
184
|
+
tokenSymbol: string;
|
|
185
|
+
tokenName: string;
|
|
186
|
+
decimals: number;
|
|
187
|
+
availableAmount: string;
|
|
188
|
+
}
|
|
189
|
+
interface AvailableFundsBody {
|
|
190
|
+
entityId: string;
|
|
191
|
+
/** Present when the caller looked up by `address`; omitted when looked up by `entityId`. */
|
|
192
|
+
role?: "buyer" | "seller";
|
|
193
|
+
funds: AvailableFundsEntry[];
|
|
194
|
+
}
|
|
195
|
+
type AvailableFundsQuery = {
|
|
196
|
+
entityId: string;
|
|
197
|
+
} | {
|
|
198
|
+
address: string;
|
|
199
|
+
role?: "buyer" | "seller";
|
|
200
|
+
};
|
|
201
|
+
interface AvailableFundsContext {
|
|
202
|
+
coreSdkRead: CoreSdkReadAdapter;
|
|
203
|
+
}
|
|
204
|
+
declare function handleGetAvailableFunds(query: AvailableFundsQuery, ctx: AvailableFundsContext): Promise<PlainHandlerResult<AvailableFundsBody>>;
|
|
205
|
+
|
|
206
|
+
interface ResolveEntityInput {
|
|
207
|
+
address: string;
|
|
208
|
+
role?: "buyer" | "seller";
|
|
209
|
+
}
|
|
210
|
+
interface ResolveEntityOk {
|
|
211
|
+
ok: true;
|
|
212
|
+
entityId: string;
|
|
213
|
+
role: "buyer" | "seller";
|
|
214
|
+
}
|
|
215
|
+
type ResolveEntityError = {
|
|
216
|
+
ok: false;
|
|
217
|
+
code: "NOT_FOUND";
|
|
218
|
+
reason: string;
|
|
219
|
+
} | {
|
|
220
|
+
ok: false;
|
|
221
|
+
code: "AMBIGUOUS";
|
|
222
|
+
reason: string;
|
|
223
|
+
/** Seller ids matching the address (one or many). Absent when no sellers matched. */
|
|
224
|
+
sellerIds?: string[];
|
|
225
|
+
/** Buyer ids matching the address (one or many). Absent when no buyers matched. */
|
|
226
|
+
buyerIds?: string[];
|
|
227
|
+
} | {
|
|
228
|
+
ok: false;
|
|
229
|
+
code: "SUBGRAPH_FAILURE";
|
|
230
|
+
reason: string;
|
|
231
|
+
};
|
|
232
|
+
type ResolveEntityResult = ResolveEntityOk | ResolveEntityError;
|
|
233
|
+
declare function resolveEntityId(coreSdk: CoreSdkReadAdapter, input: ResolveEntityInput): Promise<ResolveEntityResult>;
|
|
234
|
+
|
|
235
|
+
/** Boson account `entityId` — uint256 in decimal-string form, no leading zeros. */
|
|
236
|
+
declare const DECIMAL_UINT_RE: RegExp;
|
|
237
|
+
/** 20-byte EVM address, 0x-prefixed, any letter case (we normalise downstream). */
|
|
238
|
+
declare const ADDRESS_RE: RegExp;
|
|
239
|
+
/**
|
|
240
|
+
* Hex-string check for `signedPayload`. Requires `0x` followed by an
|
|
241
|
+
* *even* number of hex digits so the body decodes to whole bytes —
|
|
242
|
+
* odd-length payloads like `0xabc` would surface as
|
|
243
|
+
* `signedPayload decode failed: …` deep in the facilitator pipeline,
|
|
244
|
+
* which is a less precise 502 than the adapter-level 400 this regex
|
|
245
|
+
* catches. Stricter than core's `HEX_BYTES` (which permits odd
|
|
246
|
+
* lengths) so we keep this one local.
|
|
247
|
+
*/
|
|
248
|
+
declare const HEX_BYTES_RE: RegExp;
|
|
249
|
+
|
|
250
|
+
type EmitNextActionsInput = {
|
|
251
|
+
exchangeId: string;
|
|
252
|
+
} & ({
|
|
253
|
+
exchangeState: Exclude<ExchangeState, typeof ExchangeState.DISPUTED>;
|
|
254
|
+
disputeState?: never;
|
|
255
|
+
} | {
|
|
256
|
+
exchangeState: typeof ExchangeState.DISPUTED;
|
|
257
|
+
disputeState: DisputeState;
|
|
258
|
+
});
|
|
259
|
+
/**
|
|
260
|
+
* Build the `nextActions` envelope a handler attaches to its 200 body.
|
|
261
|
+
* Pure wrapper; the logic beyond `deriveNextActions` is the
|
|
262
|
+
* `DISPUTED → disputeState required` narrowing and the optional
|
|
263
|
+
* facilitator-endpoint stamp.
|
|
264
|
+
*/
|
|
265
|
+
declare function emitNextActions(input: EmitNextActionsInput, registry: ChannelRegistry, facilitatorUrl?: string): EscrowNextActions;
|
|
266
|
+
|
|
267
|
+
export { ADDRESS_RE, type AvailableFundsBody, type AvailableFundsContext, type AvailableFundsEntry, type AvailableFundsQuery, type CommitHandlerContext, type CommitHandlerInput, type CommitOk, DECIMAL_UINT_RE, type EmitNextActionsInput, HEX_BYTES_RE, type HandlerErrorBody, type HandlerResult, type HandlerStatus, type HandlerWarning, type PerformActionContext, type PerformActionInput, type PerformActionOk, type PlainHandlerResult, type RedeemHandlerContext, type RedeemHandlerInput, type ResolveEntityError, type ResolveEntityInput, type ResolveEntityOk, type ResolveEntityResult, type WithdrawFundsContext, type WithdrawFundsInput, type WithdrawFundsOk, emitNextActions, handleCommit, handleCommitAndRedeem, handleComplete, handleDisputeEscalate, handleDisputeRaise, handleDisputeResolve, handleDisputeRetract, handleGetAvailableFunds, handlePerformAction, handleRedeem, handleWithdrawFunds, handlerErr, handlerOk, plainHandlerOk, resolveEntityId };
|