@unbrowse/sdk 6.17.0-preview.6
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 +21 -0
- package/README.md +156 -0
- package/dist/client.d.ts +120 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +379 -0
- package/dist/client.js.map +1 -0
- package/dist/contracts.d.ts +372 -0
- package/dist/contracts.d.ts.map +1 -0
- package/dist/contracts.js +2 -0
- package/dist/contracts.js.map +1 -0
- package/dist/errors.d.ts +45 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +72 -0
- package/dist/errors.js.map +1 -0
- package/dist/flex.d.ts +178 -0
- package/dist/flex.d.ts.map +1 -0
- package/dist/flex.js +299 -0
- package/dist/flex.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/runtime.d.ts +70 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +326 -0
- package/dist/runtime.js.map +1 -0
- package/dist/x402.d.ts +75 -0
- package/dist/x402.d.ts.map +1 -0
- package/dist/x402.js +29 -0
- package/dist/x402.js.map +1 -0
- package/package.json +46 -0
package/dist/flex.d.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flex payment primitives — typed shapes for the @faremeter/flex-solana
|
|
3
|
+
* escrow-and-session-key payment scheme. Day-4: real wiring against
|
|
4
|
+
* @faremeter/flex-solana@0.2.1 (peer + optional dep so SDK callers who only
|
|
5
|
+
* use free routes don't pull @solana/kit).
|
|
6
|
+
*
|
|
7
|
+
* Aligned with the package's exported wire shape:
|
|
8
|
+
* - `FlexPaymentPayload` (`@faremeter/flex-solana/types`) =
|
|
9
|
+
* { escrow, mint, maxAmount, authorizationId, expiresAtSlot,
|
|
10
|
+
* splits[], sessionKey, signature }
|
|
11
|
+
* - `FlexPaymentRequirementsExtra` (`@faremeter/flex-solana/types`) =
|
|
12
|
+
* { facilitator, supportedMints[], splits[], escrow?, minGracePeriodSlots? }
|
|
13
|
+
* - `SplitInput` (`@faremeter/flex-solana/authorization`) =
|
|
14
|
+
* { recipient: Address, bps: number }
|
|
15
|
+
*
|
|
16
|
+
* IMPORTANT (tree-shake invariant from Day 3): this file MUST NOT import
|
|
17
|
+
* @faremeter/flex-solana at top-level. All package references go through
|
|
18
|
+
* `await import("@faremeter/flex-solana")` inside function bodies so SDK
|
|
19
|
+
* callers who never sign Flex authorizations don't pull @solana/kit (~3MB).
|
|
20
|
+
*/
|
|
21
|
+
import type { PaymentRequiredError } from "./errors.js";
|
|
22
|
+
/** USDC SPL mint addresses. v6.16 defaults to mainnet; callers override per-env. */
|
|
23
|
+
export declare const USDC_MINT_MAINNET = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
|
|
24
|
+
export declare const USDC_MINT_DEVNET = "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU";
|
|
25
|
+
/**
|
|
26
|
+
* Unsigned Flex payment authorization. Matches the persistable subset of
|
|
27
|
+
* `FlexPaymentPayload` from @faremeter/flex-solana minus the wire-only
|
|
28
|
+
* `sessionKey` (added at signing time) and `signature` (added at signing
|
|
29
|
+
* time). All numeric fields are base10 strings so the shape survives JSON.
|
|
30
|
+
*/
|
|
31
|
+
export interface FlexAuthorization {
|
|
32
|
+
/** Base58 escrow PDA. */
|
|
33
|
+
escrow: string;
|
|
34
|
+
/** USDC mint address (base58). */
|
|
35
|
+
mint: string;
|
|
36
|
+
/** µ¢ atomic units the facilitator may draw, as base10 string. */
|
|
37
|
+
maxAmount: string;
|
|
38
|
+
/** Random u64 as base10 string, used for replay protection. */
|
|
39
|
+
authorizationId: string;
|
|
40
|
+
/** Slot height after which this authorization is invalid, as base10 string. */
|
|
41
|
+
expiresAtSlot: string;
|
|
42
|
+
/**
|
|
43
|
+
* Up to 5 splits whose bps sum to exactly 10000. Each recipient is a
|
|
44
|
+
* base58 token-account address; bps is an integer in [1, 10000].
|
|
45
|
+
* Matches `SplitInput` from @faremeter/flex-solana/authorization.d.ts.
|
|
46
|
+
*/
|
|
47
|
+
splits: Array<{
|
|
48
|
+
recipient: string;
|
|
49
|
+
bps: number;
|
|
50
|
+
}>;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Minimal contract a caller's Flex wallet must satisfy. The wallet owns
|
|
54
|
+
* the escrow PDA AND has registered a session key — both wire-time facts
|
|
55
|
+
* the SDK does not produce, only consume.
|
|
56
|
+
*/
|
|
57
|
+
export interface FlexWalletLike {
|
|
58
|
+
/** Wallet address that owns the escrow PDA (base58). */
|
|
59
|
+
address: string;
|
|
60
|
+
/** Registered session key address (base58). */
|
|
61
|
+
sessionKeyAddress: string;
|
|
62
|
+
/**
|
|
63
|
+
* Sign the authorization with the session key's Ed25519 secret. Returns
|
|
64
|
+
* the 64-byte signature as base64 — same shape the on-chain
|
|
65
|
+
* `createEd25519VerifyInstruction` expects.
|
|
66
|
+
*/
|
|
67
|
+
signFlexAuthorization(auth: FlexAuthorization): Promise<string>;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Opaque signer object — the caller's `@solana/kit`-compatible
|
|
71
|
+
* `TransactionSigner` for the escrow owner / depositor. We avoid pulling
|
|
72
|
+
* @solana/kit types here (tree-shake); the lazy-imported instruction
|
|
73
|
+
* builders accept this through structural typing.
|
|
74
|
+
*/
|
|
75
|
+
export type TransactionSignerOpaque = any;
|
|
76
|
+
/** Arguments for `fundEscrow` — builds + sends create-escrow + deposit in one tx. */
|
|
77
|
+
export interface FlexFundEscrowParams {
|
|
78
|
+
walletAddress: string;
|
|
79
|
+
facilitatorAddress: string;
|
|
80
|
+
amountUsdc: string;
|
|
81
|
+
refundTimeoutSlots?: number;
|
|
82
|
+
deadmanTimeoutSlots?: number;
|
|
83
|
+
/** USDC mint (defaults to mainnet USDC). */
|
|
84
|
+
mint?: string;
|
|
85
|
+
/**
|
|
86
|
+
* Optional caller-supplied `TransactionSigner` for the wallet. If
|
|
87
|
+
* omitted, `fundEscrow` rejects with a `requires_signer` error and
|
|
88
|
+
* recommends `buildEscrowCreationTx` instead.
|
|
89
|
+
*/
|
|
90
|
+
signer?: TransactionSignerOpaque;
|
|
91
|
+
/** Optional caller-supplied `@solana/kit` RPC + sender; if omitted we throw. */
|
|
92
|
+
rpc?: unknown;
|
|
93
|
+
rpcSubscriptions?: unknown;
|
|
94
|
+
}
|
|
95
|
+
/** Arguments for `registerSessionKey`. */
|
|
96
|
+
export interface FlexRegisterSessionKeyParams {
|
|
97
|
+
walletAddress: string;
|
|
98
|
+
escrowAddress: string;
|
|
99
|
+
sessionKeyAddress: string;
|
|
100
|
+
expiresAtSlot?: string;
|
|
101
|
+
revocationGracePeriodSlots?: number;
|
|
102
|
+
signer?: TransactionSignerOpaque;
|
|
103
|
+
rpc?: unknown;
|
|
104
|
+
}
|
|
105
|
+
/** Pure tx-build result — what the sender wrappers consume. */
|
|
106
|
+
export interface BuiltFlexTx {
|
|
107
|
+
/** Instructions to feed `@solana/kit`'s tx message builder. */
|
|
108
|
+
instructions: unknown[];
|
|
109
|
+
/**
|
|
110
|
+
* Addresses the user is putting at risk by signing this tx. Lets a wallet
|
|
111
|
+
* UI render a sane confirmation screen ("you are depositing X USDC into
|
|
112
|
+
* escrow Y, registering session key Z against facilitator F").
|
|
113
|
+
*/
|
|
114
|
+
accountsAtRisk: string[];
|
|
115
|
+
/** Programmatic hint about what this tx does. */
|
|
116
|
+
intent: "create_escrow_and_deposit" | "register_session_key";
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Build an unsigned `FlexAuthorization`. Validates splits sum, generates
|
|
120
|
+
* a random `authorizationId`, and returns the canonical JSON shape.
|
|
121
|
+
*
|
|
122
|
+
* Cited symbol: shape mirrors `FlexPaymentPayload` from
|
|
123
|
+
* `@faremeter/flex-solana/types` (minus wire-only `sessionKey`/`signature`).
|
|
124
|
+
*/
|
|
125
|
+
export declare function buildFlexAuthorization(opts: {
|
|
126
|
+
escrow: string;
|
|
127
|
+
mint: string;
|
|
128
|
+
maxAmount: string;
|
|
129
|
+
splits: FlexAuthorization["splits"];
|
|
130
|
+
expiresAtSlot: string;
|
|
131
|
+
}): Promise<FlexAuthorization>;
|
|
132
|
+
/**
|
|
133
|
+
* Settle a `PaymentRequiredError` whose `accepts[]` advertises a Flex
|
|
134
|
+
* scheme. Picks the first Flex-shaped requirement (one whose
|
|
135
|
+
* `extra.escrow` and `extra.splits` are populated — matches
|
|
136
|
+
* `FlexPaymentRequirementsExtra` from `@faremeter/flex-solana/types`),
|
|
137
|
+
* has the wallet sign, packs as `FlexPaymentPayload` inside an
|
|
138
|
+
* `X402PaymentPayload`, base64-encodes, and replays via
|
|
139
|
+
* `retry(paymentHeader)`.
|
|
140
|
+
*/
|
|
141
|
+
export declare function payAndRetryFlex<T>(error: PaymentRequiredError, wallet: FlexWalletLike, retry: (paymentHeader: string) => Promise<T>): Promise<T>;
|
|
142
|
+
/**
|
|
143
|
+
* Pure tx-builder for create-escrow + deposit (no signer required, no
|
|
144
|
+
* network call). Returns instructions a caller can sign + send with any
|
|
145
|
+
* `@solana/kit`-compatible runtime. Lazy-imports @faremeter/flex-solana
|
|
146
|
+
* so tree-shake is preserved.
|
|
147
|
+
*
|
|
148
|
+
* Cited symbols: `getCreateEscrowInstructionAsync`, `getDepositInstructionAsync`
|
|
149
|
+
* (both from `@faremeter/flex-solana` root export, per
|
|
150
|
+
* `/tmp/flex-probe/.../flex-solana/dist/src/index.d.ts:9`).
|
|
151
|
+
*/
|
|
152
|
+
export declare function buildEscrowCreationTx(params: FlexFundEscrowParams): Promise<BuiltFlexTx>;
|
|
153
|
+
/**
|
|
154
|
+
* Pure tx-builder for register-session-key (no signer required).
|
|
155
|
+
*
|
|
156
|
+
* Cited symbol: `getRegisterSessionKeyInstructionAsync` from
|
|
157
|
+
* `@faremeter/flex-solana` root export.
|
|
158
|
+
*/
|
|
159
|
+
export declare function buildSessionKeyRegistrationTx(params: FlexRegisterSessionKeyParams): Promise<BuiltFlexTx>;
|
|
160
|
+
/**
|
|
161
|
+
* Send a create-escrow + deposit transaction. Requires `params.signer`
|
|
162
|
+
* and `params.rpc` — both `@solana/kit` types we don't import at
|
|
163
|
+
* top-level (tree-shake). Without them, throws `requires_signer` and
|
|
164
|
+
* points at `buildEscrowCreationTx` for the pure-build path.
|
|
165
|
+
*/
|
|
166
|
+
export declare function fundEscrow(params: FlexFundEscrowParams): Promise<{
|
|
167
|
+
escrowAddress: string;
|
|
168
|
+
txSignature: string;
|
|
169
|
+
}>;
|
|
170
|
+
/**
|
|
171
|
+
* Send a register-session-key transaction. Thin sender wrapper around
|
|
172
|
+
* `buildSessionKeyRegistrationTx`. Same `requires_signer` contract as
|
|
173
|
+
* `fundEscrow`.
|
|
174
|
+
*/
|
|
175
|
+
export declare function registerSessionKey(params: FlexRegisterSessionKeyParams): Promise<{
|
|
176
|
+
txSignature: string;
|
|
177
|
+
}>;
|
|
178
|
+
//# sourceMappingURL=flex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flex.d.ts","sourceRoot":"","sources":["../src/flex.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAExD,oFAAoF;AACpF,eAAO,MAAM,iBAAiB,iDAAiD,CAAC;AAChF,eAAO,MAAM,gBAAgB,iDAAiD,CAAC;AAE/E;;;;;GAKG;AACH,MAAM,WAAW,iBAAiB;IAChC,yBAAyB;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,kCAAkC;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,kEAAkE;IAClE,SAAS,EAAE,MAAM,CAAC;IAClB,+DAA+D;IAC/D,eAAe,EAAE,MAAM,CAAC;IACxB,+EAA+E;IAC/E,aAAa,EAAE,MAAM,CAAC;I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,cAAc,EAAE,MAAM,EAAE,CAAC;IACzB,iDAAiD;IACjD,MAAM,EAAE,2BAA2B,GAAG,sBAAsB,CAAC;CAC9D;AAgCD;;;;;;GAMG;AACH,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE;IACJ,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,iBAAiB,CAAC,QAAQ,CAAC,CAAC;IACpC,aAAa,EAAE,MAAM,CAAC;CACvB,GACA,OAAO,CAAC,iBAAiB,CAAC,CAmB5B;AAED;;;;;;;;GAQG;AACH,wBAAsB,eAAe,CAAC,CAAC,EACrC,KAAK,EAAE,oBAAoB,EAC3B,MAAM,EAAE,cAAc,EACtB,KAAK,EAAE,CAAC,aAAa,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,GAC3C,OAAO,CAAC,CAAC,CAAC,CA6DZ;AAmBD;;;;;;;;;GASG;AACH,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,oBAAoB,GAC3B,OAAO,CAAC,WAAW,CAAC,CA2DtB;AAED;;;;;GAKG;AACH,wBAAsB,6BAA6B,CACjD,MAAM,EAAE,4BAA4B,GACnC,OAAO,CAAC,WAAW,CAAC,CAsCtB;AAED;;;;;GAKG;AACH,wBAAsB,UAAU,CAC9B,MAAM,EAAE,oBAAoB,GAC3B,OAAO,CAAC;IAAE,aAAa,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,CAAC,CAiBzD;AAED;;;;GAIG;AACH,wBAAsB,kBAAkB,CACtC,MAAM,EAAE,4BAA4B,GACnC,OAAO,CAAC;IAAE,WAAW,EAAE,MAAM,CAAA;CAAE,CAAC,CAclC"}
|
package/dist/flex.js
ADDED
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flex payment primitives — typed shapes for the @faremeter/flex-solana
|
|
3
|
+
* escrow-and-session-key payment scheme. Day-4: real wiring against
|
|
4
|
+
* @faremeter/flex-solana@0.2.1 (peer + optional dep so SDK callers who only
|
|
5
|
+
* use free routes don't pull @solana/kit).
|
|
6
|
+
*
|
|
7
|
+
* Aligned with the package's exported wire shape:
|
|
8
|
+
* - `FlexPaymentPayload` (`@faremeter/flex-solana/types`) =
|
|
9
|
+
* { escrow, mint, maxAmount, authorizationId, expiresAtSlot,
|
|
10
|
+
* splits[], sessionKey, signature }
|
|
11
|
+
* - `FlexPaymentRequirementsExtra` (`@faremeter/flex-solana/types`) =
|
|
12
|
+
* { facilitator, supportedMints[], splits[], escrow?, minGracePeriodSlots? }
|
|
13
|
+
* - `SplitInput` (`@faremeter/flex-solana/authorization`) =
|
|
14
|
+
* { recipient: Address, bps: number }
|
|
15
|
+
*
|
|
16
|
+
* IMPORTANT (tree-shake invariant from Day 3): this file MUST NOT import
|
|
17
|
+
* @faremeter/flex-solana at top-level. All package references go through
|
|
18
|
+
* `await import("@faremeter/flex-solana")` inside function bodies so SDK
|
|
19
|
+
* callers who never sign Flex authorizations don't pull @solana/kit (~3MB).
|
|
20
|
+
*/
|
|
21
|
+
/** USDC SPL mint addresses. v6.16 defaults to mainnet; callers override per-env. */
|
|
22
|
+
export const USDC_MINT_MAINNET = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
|
|
23
|
+
export const USDC_MINT_DEVNET = "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU";
|
|
24
|
+
function randomAuthorizationId() {
|
|
25
|
+
// 8 random bytes → bigint (u64) → base10 string. Matches
|
|
26
|
+
// `SerializePaymentAuthorizationArgs.authorizationId: bigint` at the boundary.
|
|
27
|
+
const bytes = new Uint8Array(8);
|
|
28
|
+
crypto.getRandomValues(bytes);
|
|
29
|
+
let n = 0n;
|
|
30
|
+
for (const b of bytes)
|
|
31
|
+
n = (n << 8n) | BigInt(b);
|
|
32
|
+
return n.toString(10);
|
|
33
|
+
}
|
|
34
|
+
function validateSplits(splits) {
|
|
35
|
+
if (!Array.isArray(splits) || splits.length === 0 || splits.length > 5) {
|
|
36
|
+
throw new Error(`flex: splits must be 1..5 entries, got ${splits?.length ?? 0}`);
|
|
37
|
+
}
|
|
38
|
+
const sum = splits.reduce((s, e) => s + e.bps, 0);
|
|
39
|
+
if (sum !== 10_000) {
|
|
40
|
+
throw new Error(`flex: splits bps must sum to 10000, got ${sum}`);
|
|
41
|
+
}
|
|
42
|
+
for (const s of splits) {
|
|
43
|
+
if (!s.recipient || typeof s.recipient !== "string") {
|
|
44
|
+
throw new Error("flex: split.recipient must be a base58 address");
|
|
45
|
+
}
|
|
46
|
+
if (!Number.isInteger(s.bps) || s.bps < 1 || s.bps > 10_000) {
|
|
47
|
+
throw new Error(`flex: split.bps must be int in [1,10000], got ${s.bps}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Build an unsigned `FlexAuthorization`. Validates splits sum, generates
|
|
53
|
+
* a random `authorizationId`, and returns the canonical JSON shape.
|
|
54
|
+
*
|
|
55
|
+
* Cited symbol: shape mirrors `FlexPaymentPayload` from
|
|
56
|
+
* `@faremeter/flex-solana/types` (minus wire-only `sessionKey`/`signature`).
|
|
57
|
+
*/
|
|
58
|
+
export async function buildFlexAuthorization(opts) {
|
|
59
|
+
if (!opts.escrow)
|
|
60
|
+
throw new Error("flex: escrow required");
|
|
61
|
+
if (!opts.mint)
|
|
62
|
+
throw new Error("flex: mint required");
|
|
63
|
+
if (!/^\d+$/.test(opts.maxAmount)) {
|
|
64
|
+
throw new Error("flex: maxAmount must be base10 string");
|
|
65
|
+
}
|
|
66
|
+
if (!/^\d+$/.test(opts.expiresAtSlot)) {
|
|
67
|
+
throw new Error("flex: expiresAtSlot must be base10 string");
|
|
68
|
+
}
|
|
69
|
+
validateSplits(opts.splits);
|
|
70
|
+
return {
|
|
71
|
+
escrow: opts.escrow,
|
|
72
|
+
mint: opts.mint,
|
|
73
|
+
maxAmount: opts.maxAmount,
|
|
74
|
+
authorizationId: randomAuthorizationId(),
|
|
75
|
+
expiresAtSlot: opts.expiresAtSlot,
|
|
76
|
+
splits: opts.splits,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Settle a `PaymentRequiredError` whose `accepts[]` advertises a Flex
|
|
81
|
+
* scheme. Picks the first Flex-shaped requirement (one whose
|
|
82
|
+
* `extra.escrow` and `extra.splits` are populated — matches
|
|
83
|
+
* `FlexPaymentRequirementsExtra` from `@faremeter/flex-solana/types`),
|
|
84
|
+
* has the wallet sign, packs as `FlexPaymentPayload` inside an
|
|
85
|
+
* `X402PaymentPayload`, base64-encodes, and replays via
|
|
86
|
+
* `retry(paymentHeader)`.
|
|
87
|
+
*/
|
|
88
|
+
export async function payAndRetryFlex(error, wallet, retry) {
|
|
89
|
+
const accepts = error.accepts ?? [];
|
|
90
|
+
// Pick the first requirement whose `extra` carries Flex-shape fields.
|
|
91
|
+
// Backend will set scheme="@faremeter/flex" but we don't gate on the
|
|
92
|
+
// scheme string — `extra` shape is the source of truth.
|
|
93
|
+
const requirement = accepts.find((r) => {
|
|
94
|
+
const extra = r.extra;
|
|
95
|
+
return !!extra?.escrow && Array.isArray(extra?.splits);
|
|
96
|
+
});
|
|
97
|
+
if (!requirement)
|
|
98
|
+
throw error;
|
|
99
|
+
const extra = requirement.extra;
|
|
100
|
+
// We need an expiresAtSlot. Prefer extra.expiresAtSlot; else derive
|
|
101
|
+
// from minGracePeriodSlots + a sentinel (caller can refine).
|
|
102
|
+
const expiresAtSlot = extra.expiresAtSlot ?? extra.minGracePeriodSlots ?? "0";
|
|
103
|
+
const auth = await buildFlexAuthorization({
|
|
104
|
+
escrow: extra.escrow,
|
|
105
|
+
mint: USDC_MINT_MAINNET,
|
|
106
|
+
maxAmount: requirement.maxAmountRequired,
|
|
107
|
+
splits: extra.splits,
|
|
108
|
+
expiresAtSlot,
|
|
109
|
+
});
|
|
110
|
+
const signature = await wallet.signFlexAuthorization(auth);
|
|
111
|
+
// Wire shape: `FlexPaymentPayload` from @faremeter/flex-solana/types.
|
|
112
|
+
// sessionKey + signature added here (signing-time fields).
|
|
113
|
+
const flexPayload = {
|
|
114
|
+
escrow: auth.escrow,
|
|
115
|
+
mint: auth.mint,
|
|
116
|
+
maxAmount: auth.maxAmount,
|
|
117
|
+
authorizationId: auth.authorizationId,
|
|
118
|
+
expiresAtSlot: auth.expiresAtSlot,
|
|
119
|
+
splits: auth.splits,
|
|
120
|
+
sessionKey: wallet.sessionKeyAddress,
|
|
121
|
+
signature,
|
|
122
|
+
};
|
|
123
|
+
// x402 envelope (`X402PaymentPayload` from `./x402.ts` = standard
|
|
124
|
+
// x402 wire format: {x402Version, scheme, network, payload}).
|
|
125
|
+
const envelope = {
|
|
126
|
+
x402Version: 1,
|
|
127
|
+
scheme: requirement.scheme,
|
|
128
|
+
network: requirement.network,
|
|
129
|
+
payload: flexPayload,
|
|
130
|
+
};
|
|
131
|
+
// X-PAYMENT header value: base64-encoded JSON of the envelope.
|
|
132
|
+
// Matches what the backend's Flex facilitator decodes.
|
|
133
|
+
const headerValue = base64UrlSafeEncodeJson(envelope);
|
|
134
|
+
return retry(headerValue);
|
|
135
|
+
}
|
|
136
|
+
function base64UrlSafeEncodeJson(obj) {
|
|
137
|
+
const json = JSON.stringify(obj);
|
|
138
|
+
// Cross-runtime base64: prefer Buffer (Node), else btoa (browser/edge).
|
|
139
|
+
// Standard x402 uses regular base64 (not url-safe), so we match that.
|
|
140
|
+
// deno-lint-ignore no-explicit-any
|
|
141
|
+
const g = globalThis;
|
|
142
|
+
if (g.Buffer?.from)
|
|
143
|
+
return g.Buffer.from(json, "utf8").toString("base64");
|
|
144
|
+
if (typeof g.btoa === "function") {
|
|
145
|
+
// btoa needs a binary string
|
|
146
|
+
const bytes = new TextEncoder().encode(json);
|
|
147
|
+
let bin = "";
|
|
148
|
+
for (const b of bytes)
|
|
149
|
+
bin += String.fromCharCode(b);
|
|
150
|
+
return g.btoa(bin);
|
|
151
|
+
}
|
|
152
|
+
throw new Error("flex: no base64 encoder available in this runtime");
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Pure tx-builder for create-escrow + deposit (no signer required, no
|
|
156
|
+
* network call). Returns instructions a caller can sign + send with any
|
|
157
|
+
* `@solana/kit`-compatible runtime. Lazy-imports @faremeter/flex-solana
|
|
158
|
+
* so tree-shake is preserved.
|
|
159
|
+
*
|
|
160
|
+
* Cited symbols: `getCreateEscrowInstructionAsync`, `getDepositInstructionAsync`
|
|
161
|
+
* (both from `@faremeter/flex-solana` root export, per
|
|
162
|
+
* `/tmp/flex-probe/.../flex-solana/dist/src/index.d.ts:9`).
|
|
163
|
+
*/
|
|
164
|
+
export async function buildEscrowCreationTx(params) {
|
|
165
|
+
if (!params.walletAddress)
|
|
166
|
+
throw new Error("flex: walletAddress required");
|
|
167
|
+
if (!params.facilitatorAddress) {
|
|
168
|
+
throw new Error("flex: facilitatorAddress required");
|
|
169
|
+
}
|
|
170
|
+
if (!/^\d+$/.test(params.amountUsdc)) {
|
|
171
|
+
throw new Error("flex: amountUsdc must be base10 atomic-unit string");
|
|
172
|
+
}
|
|
173
|
+
const mint = params.mint ?? USDC_MINT_MAINNET;
|
|
174
|
+
const refundTimeoutSlots = BigInt(params.refundTimeoutSlots ?? 150);
|
|
175
|
+
const deadmanTimeoutSlots = BigInt(params.deadmanTimeoutSlots ?? 432000);
|
|
176
|
+
const index = BigInt(Date.now()); // monotonic-ish nonce for PDA derivation
|
|
177
|
+
// Lazy import — tree-shake invariant.
|
|
178
|
+
const flex = await import("@faremeter/flex-solana");
|
|
179
|
+
if (!params.signer) {
|
|
180
|
+
// Pure-build mode: we can't produce a `TransactionSigner` from a string
|
|
181
|
+
// address. Caller must build the tx with their own kit-compatible signer.
|
|
182
|
+
// Return the intended shape so the caller can mirror this with their
|
|
183
|
+
// own `getCreateEscrowInstructionAsync` call.
|
|
184
|
+
return {
|
|
185
|
+
instructions: [],
|
|
186
|
+
accountsAtRisk: [
|
|
187
|
+
params.walletAddress,
|
|
188
|
+
params.facilitatorAddress,
|
|
189
|
+
mint,
|
|
190
|
+
],
|
|
191
|
+
intent: "create_escrow_and_deposit",
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
const createIx = await flex.getCreateEscrowInstructionAsync({
|
|
195
|
+
owner: params.signer,
|
|
196
|
+
index,
|
|
197
|
+
facilitator: params.facilitatorAddress,
|
|
198
|
+
refundTimeoutSlots,
|
|
199
|
+
deadmanTimeoutSlots,
|
|
200
|
+
maxSessionKeys: 5,
|
|
201
|
+
});
|
|
202
|
+
const depositIx = await flex.getDepositInstructionAsync({
|
|
203
|
+
depositor: params.signer,
|
|
204
|
+
escrow: (createIx.accounts[1].address),
|
|
205
|
+
mint: mint,
|
|
206
|
+
source: params.walletAddress, // caller should pass the wallet ATA
|
|
207
|
+
amount: BigInt(params.amountUsdc),
|
|
208
|
+
});
|
|
209
|
+
return {
|
|
210
|
+
instructions: [createIx, depositIx],
|
|
211
|
+
accountsAtRisk: [
|
|
212
|
+
params.walletAddress,
|
|
213
|
+
params.facilitatorAddress,
|
|
214
|
+
mint,
|
|
215
|
+
String(createIx.accounts[1].address ?? ""),
|
|
216
|
+
],
|
|
217
|
+
intent: "create_escrow_and_deposit",
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Pure tx-builder for register-session-key (no signer required).
|
|
222
|
+
*
|
|
223
|
+
* Cited symbol: `getRegisterSessionKeyInstructionAsync` from
|
|
224
|
+
* `@faremeter/flex-solana` root export.
|
|
225
|
+
*/
|
|
226
|
+
export async function buildSessionKeyRegistrationTx(params) {
|
|
227
|
+
if (!params.walletAddress)
|
|
228
|
+
throw new Error("flex: walletAddress required");
|
|
229
|
+
if (!params.escrowAddress)
|
|
230
|
+
throw new Error("flex: escrowAddress required");
|
|
231
|
+
if (!params.sessionKeyAddress) {
|
|
232
|
+
throw new Error("flex: sessionKeyAddress required");
|
|
233
|
+
}
|
|
234
|
+
const flex = await import("@faremeter/flex-solana");
|
|
235
|
+
if (!params.signer) {
|
|
236
|
+
return {
|
|
237
|
+
instructions: [],
|
|
238
|
+
accountsAtRisk: [
|
|
239
|
+
params.walletAddress,
|
|
240
|
+
params.escrowAddress,
|
|
241
|
+
params.sessionKeyAddress,
|
|
242
|
+
],
|
|
243
|
+
intent: "register_session_key",
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
const ix = await flex.getRegisterSessionKeyInstructionAsync({
|
|
247
|
+
owner: params.signer,
|
|
248
|
+
escrow: params.escrowAddress,
|
|
249
|
+
sessionKey: params.sessionKeyAddress,
|
|
250
|
+
expiresAtSlot: params.expiresAtSlot ? BigInt(params.expiresAtSlot) : null,
|
|
251
|
+
revocationGracePeriodSlots: BigInt(params.revocationGracePeriodSlots ?? 0),
|
|
252
|
+
});
|
|
253
|
+
return {
|
|
254
|
+
instructions: [ix],
|
|
255
|
+
accountsAtRisk: [
|
|
256
|
+
params.walletAddress,
|
|
257
|
+
params.escrowAddress,
|
|
258
|
+
params.sessionKeyAddress,
|
|
259
|
+
],
|
|
260
|
+
intent: "register_session_key",
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Send a create-escrow + deposit transaction. Requires `params.signer`
|
|
265
|
+
* and `params.rpc` — both `@solana/kit` types we don't import at
|
|
266
|
+
* top-level (tree-shake). Without them, throws `requires_signer` and
|
|
267
|
+
* points at `buildEscrowCreationTx` for the pure-build path.
|
|
268
|
+
*/
|
|
269
|
+
export async function fundEscrow(params) {
|
|
270
|
+
if (!params.signer || !params.rpc) {
|
|
271
|
+
throw new Error("flex.fundEscrow: requires_signer — caller must supply " +
|
|
272
|
+
"`signer` (TransactionSigner) and `rpc` (kit RPC client). For " +
|
|
273
|
+
"pure tx construction without sending, use `buildEscrowCreationTx`.");
|
|
274
|
+
}
|
|
275
|
+
const built = await buildEscrowCreationTx(params);
|
|
276
|
+
// Sender is a thin wrapper. Real impl: assemble tx message via
|
|
277
|
+
// @solana/kit, sign + send via the caller's rpc. v6.16 prelim:
|
|
278
|
+
// surface the structured failure so callers know where to plug in.
|
|
279
|
+
throw new Error("flex.fundEscrow: tx-send wiring is caller-supplied in v6.16-preview.0. " +
|
|
280
|
+
`Built ${built.instructions.length} instructions; send via your ` +
|
|
281
|
+
"@solana/kit pipeline. See `buildEscrowCreationTx` for the pure shape.");
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Send a register-session-key transaction. Thin sender wrapper around
|
|
285
|
+
* `buildSessionKeyRegistrationTx`. Same `requires_signer` contract as
|
|
286
|
+
* `fundEscrow`.
|
|
287
|
+
*/
|
|
288
|
+
export async function registerSessionKey(params) {
|
|
289
|
+
if (!params.signer || !params.rpc) {
|
|
290
|
+
throw new Error("flex.registerSessionKey: requires_signer — caller must supply " +
|
|
291
|
+
"`signer` (TransactionSigner) and `rpc` (kit RPC client). For " +
|
|
292
|
+
"pure tx construction without sending, use `buildSessionKeyRegistrationTx`.");
|
|
293
|
+
}
|
|
294
|
+
const built = await buildSessionKeyRegistrationTx(params);
|
|
295
|
+
throw new Error("flex.registerSessionKey: tx-send wiring is caller-supplied in v6.16-preview.0. " +
|
|
296
|
+
`Built ${built.instructions.length} instructions; send via your ` +
|
|
297
|
+
"@solana/kit pipeline. See `buildSessionKeyRegistrationTx` for the pure shape.");
|
|
298
|
+
}
|
|
299
|
+
//# sourceMappingURL=flex.js.map
|
package/dist/flex.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flex.js","sourceRoot":"","sources":["../src/flex.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAIH,oFAAoF;AACpF,MAAM,CAAC,MAAM,iBAAiB,GAAG,8CAA8C,CAAC;AAChF,MAAM,CAAC,MAAM,gBAAgB,GAAG,8CAA8C,CAAC;AAmG/E,SAAS,qBAAqB;IAC5B,yDAAyD;IACzD,+EA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export { PaymentRequiredError, RuntimeUnavailableError, SponsorExhaustedError, UnbrowseApiError, } from "./errors.js";
|
|
3
|
+
export { locateUnbrowseBinary, probeUnbrowseRuntime, spawnUnbrowseRuntime, } from "./runtime.js";
|
|
4
|
+
export type { RuntimeHandle, SpawnRuntimeOptions } from "./runtime.js";
|
|
5
|
+
export { payAndRetry } from "./x402.js";
|
|
6
|
+
export type { WalletLike, X402PaymentPayload, X402PaymentRequirement, } from "./x402.js";
|
|
7
|
+
export { buildEscrowCreationTx, buildFlexAuthorization, buildSessionKeyRegistrationTx, fundEscrow, payAndRetryFlex, registerSessionKey, USDC_MINT_DEVNET, USDC_MINT_MAINNET, } from "./flex.js";
|
|
8
|
+
export type { BuiltFlexTx, FlexAuthorization, FlexFundEscrowParams, FlexRegisterSessionKeyParams, FlexWalletLike, TransactionSignerOpaque, } from "./flex.js";
|
|
9
|
+
export type { AttributionLedger, AvailableEndpoint, CreatorTransaction, CreatorTransactionsResponse, Dashboard, DashboardContributions, DashboardEarnings, DashboardSpending, EndpointDescriptor, ExecuteInput, ExecuteResponse, ExecutionTrace, FeedbackInput, FeedbackResponse, HealthResponse, LoginInput, LoginResponse, OrchestrationTiming, ProjectionOptions, RequestOptions, ResolveInput, ResolveResponse, ResponseSchema, SearchDomainInput, SearchHit, SearchInput, SearchResponse, SkillManifest, StatsResponse, StealAuthInput, StealAuthResponse, UnbrowseClientOptions, WalletInfo, } from "./contracts.js";
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EACL,oBAAoB,EACpB,uBAAuB,EACvB,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,cAAc,CAAC;AACtB,YAAY,EAAE,aAAa,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,YAAY,EACV,UAAU,EACV,kBAAkB,EAClB,sBAAsB,GACvB,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,6BAA6B,EAC7B,UAAU,EACV,eAAe,EACf,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,WAAW,CAAC;AACnB,YAAY,EACV,WAAW,EACX,iBAAiB,EACjB,oBAAoB,EACpB,4BAA4B,EAC5B,cAAc,EACd,uBAAuB,GACxB,MAAM,WAAW,CAAC;AACnB,YAAY,EACV,iBAAiB,EACjB,iBAAiB,EACjB,kBAAkB,EAClB,2BAA2B,EAC3B,SAAS,EACT,sBAAsB,EACtB,iBAAiB,EACjB,iBAAiB,EACjB,kBAAkB,EAClB,YAAY,EACZ,eAAe,EACf,cAAc,EACd,aAAa,EACb,gBAAgB,EAChB,cAAc,EACd,UAAU,EACV,aAAa,EACb,mBAAmB,EACnB,iBAAiB,EACjB,cAAc,EACd,YAAY,EACZ,eAAe,EACf,cAAc,EACd,iBAAiB,EACjB,SAAS,EACT,WAAW,EACX,cAAc,EACd,aAAa,EACb,aAAa,EACb,cAAc,EACd,iBAAiB,EACjB,qBAAqB,EACrB,UAAU,GACX,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { Unbrowse } from "./client.js";
|
|
2
|
+
export { PaymentRequiredError, RuntimeUnavailableError, SponsorExhaustedError, UnbrowseApiError, } from "./errors.js";
|
|
3
|
+
export { locateUnbrowseBinary, probeUnbrowseRuntime, spawnUnbrowseRuntime, } from "./runtime.js";
|
|
4
|
+
export { payAndRetry } from "./x402.js";
|
|
5
|
+
export { buildEscrowCreationTx, buildFlexAuthorization, buildSessionKeyRegistrationTx, fundEscrow, payAndRetryFlex, registerSessionKey, USDC_MINT_DEVNET, USDC_MINT_MAINNET, } from "./flex.js";
|
|
6
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EACL,oBAAoB,EACpB,uBAAuB,EACvB,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAMxC,OAAO,EACL,qBAAqB,EACrB,sBAAsB,EACtB,6BAA6B,EAC7B,UAAU,EACV,eAAe,EACf,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local-binary lifecycle for `@unbrowse/sdk`.
|
|
3
|
+
*
|
|
4
|
+
* The SDK talks to a co-located `unbrowse` runtime over loopback HTTP.
|
|
5
|
+
* `runtime.ts` owns the contract for locating, probing, and spawning that
|
|
6
|
+
* runtime so `client.ts` can stay a pure HTTP transport.
|
|
7
|
+
*
|
|
8
|
+
* Tree-shaking note: heavy Node-only imports (`child_process`) are pulled
|
|
9
|
+
* in lazily inside `spawnUnbrowseRuntime`. Callers that only use
|
|
10
|
+
* `Unbrowse.connect` never pay for them.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Handle returned by a successful spawn. `owned` distinguishes processes the
|
|
14
|
+
* SDK forked (kill on `.close()`) from processes the SDK adopted via probe
|
|
15
|
+
* (leave running on `.close()`).
|
|
16
|
+
*/
|
|
17
|
+
export interface RuntimeHandle {
|
|
18
|
+
/** Base URL the running runtime listens on, e.g. "http://127.0.0.1:6969". */
|
|
19
|
+
baseUrl: string;
|
|
20
|
+
/** OS process id; -1 if the runtime was adopted via probe. */
|
|
21
|
+
pid: number;
|
|
22
|
+
/** Stops the runtime if `owned`; no-op otherwise. */
|
|
23
|
+
kill(): Promise<void>;
|
|
24
|
+
/** Resolves once the runtime answers `/health` with 200. */
|
|
25
|
+
ready: Promise<void>;
|
|
26
|
+
/** True iff this SDK started the process and is responsible for tearing it down. */
|
|
27
|
+
owned: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Options for `spawnUnbrowseRuntime`. Every field has a sensible default,
|
|
31
|
+
* so the zero-arg call is the documented happy path.
|
|
32
|
+
*/
|
|
33
|
+
export interface SpawnRuntimeOptions {
|
|
34
|
+
/** Preferred port; default 6969. If already alive, adopts that runtime. */
|
|
35
|
+
port?: number;
|
|
36
|
+
/** Working directory for the child; default = current process cwd. */
|
|
37
|
+
cwd?: string;
|
|
38
|
+
/** Extra env vars layered onto the child's inherited env. */
|
|
39
|
+
env?: Record<string, string>;
|
|
40
|
+
/** Explicit path to the binary; overrides `locateUnbrowseBinary`. */
|
|
41
|
+
binaryPath?: string;
|
|
42
|
+
/** Milliseconds to wait for `/health` readiness; default 10_000. */
|
|
43
|
+
readyTimeoutMs?: number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Probe an already-running runtime by hitting `/health`. Returns true iff
|
|
47
|
+
* the response status is 2xx. Never throws — connection refused, timeout,
|
|
48
|
+
* DNS failures, and non-2xx all resolve to `false`.
|
|
49
|
+
*/
|
|
50
|
+
export declare function probeUnbrowseRuntime(baseUrl: string, timeoutMs?: number): Promise<boolean>;
|
|
51
|
+
/**
|
|
52
|
+
* Locate the `unbrowse` binary by walking, in order:
|
|
53
|
+
* 1. `UNBROWSE_BIN` env var
|
|
54
|
+
* 2. `require.resolve('unbrowse/package.json')` -> derived `bin/unbrowse-wrapper.mjs`
|
|
55
|
+
* 3. `Bun.which('unbrowse')` if running under Bun
|
|
56
|
+
* Returns null when nothing is wired.
|
|
57
|
+
*/
|
|
58
|
+
export declare function locateUnbrowseBinary(): string | null;
|
|
59
|
+
/**
|
|
60
|
+
* Spawn (or adopt) a co-located `unbrowse` runtime and wait for it to
|
|
61
|
+
* answer `/health`. Returns a `RuntimeHandle` whose `.kill()` tears the
|
|
62
|
+
* child down. Throws `RuntimeUnavailableError` on failure modes.
|
|
63
|
+
*
|
|
64
|
+
* Concurrency: if two callers race on the same port, only one spawn is
|
|
65
|
+
* launched. The second caller awaits the first's promise and gets an
|
|
66
|
+
* adopted (`owned=false`) view of the same handle so it cannot
|
|
67
|
+
* double-kill the child.
|
|
68
|
+
*/
|
|
69
|
+
export declare function spawnUnbrowseRuntime(opts?: SpawnRuntimeOptions): Promise<RuntimeHandle>;
|
|
70
|
+
//# sourceMappingURL=runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAaH;;;;GAIG;AACH,MAAM,WAAW,aAAa;IAC5B,6EAA6E;IAC7E,OAAO,EAAE,MAAM,CAAC;IAChB,8DAA8D;IAC9D,GAAG,EAAE,MAAM,CAAC;IACZ,qDAAqD;IACrD,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACtB,4DAA4D;IAC5D,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACrB,oFAAoF;IACpF,KAAK,EAAE,OAAO,CAAC;CAChB;AAED;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IAClC,2EAA2E;IAC3E,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,sEAAsE;IACtE,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,6DAA6D;IAC7D,GAAG,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC7B,qEAAqE;IACrE,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,oEAAoE;IACpE,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAMD;;;;GAIG;AACH,wBAAsB,oBAAoB,CACxC,OAAO,EAAE,MAAM,EACf,SAAS,GAAE,MAAiC,GAC3C,OAAO,CAAC,OAAO,CAAC,CAYlB;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,GAAG,IAAI,CAoCpD;AA2BD;;;;;;;;;GASG;AACH,wBAAsB,oBAAoB,CACxC,IAAI,GAAE,mBAAwB,GAC7B,OAAO,CAAC,aAAa,CAAC,CA8BxB"}
|