@secondlayer/sdk 6.54.1 → 8.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secondlayer/sdk",
3
- "version": "6.54.1",
3
+ "version": "8.0.0",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,10 +28,6 @@
28
28
  "types": "./dist/subgraphs/index.d.ts",
29
29
  "import": "./dist/subgraphs/index.js"
30
30
  },
31
- "./x402": {
32
- "types": "./dist/x402.d.ts",
33
- "import": "./dist/x402.js"
34
- },
35
31
  "./sinks/core": {
36
32
  "types": "./dist/sinks/core.d.ts",
37
33
  "import": "./dist/sinks/core.js"
@@ -63,9 +59,9 @@
63
59
  "prepublishOnly": "bun run build"
64
60
  },
65
61
  "dependencies": {
66
- "@secondlayer/shared": "^7.11.0",
67
- "@secondlayer/stacks": "^3.2.0",
68
- "@secondlayer/subgraphs": "^3.26.3"
62
+ "@secondlayer/shared": "^9.0.0",
63
+ "@secondlayer/stacks": "^4.0.0",
64
+ "@secondlayer/subgraphs": "^4.0.1"
69
65
  },
70
66
  "devDependencies": {
71
67
  "@openpkg-ts/cli": "^0.11.3",
package/dist/x402.d.ts DELETED
@@ -1,160 +0,0 @@
1
- import { X402TokenSymbol } from "@secondlayer/shared/x402";
2
- import { LocalAccount } from "@secondlayer/stacks/accounts";
3
- import { StacksChain } from "@secondlayer/stacks/chains";
4
- /**
5
- * Client for the x402 pay-per-request rail. Turns a 402 challenge into a signed
6
- * `PAYMENT-SIGNATURE` and retries — gasless (the agent signs origin-only; the
7
- * facilitator sponsors the STX fee) and accountless (no key/Stripe/session).
8
- *
9
- * Three layers:
10
- * - `withX402(fetch, opts)` — drop-in fetch that auto-pays on 402.
11
- * - `createX402Client(opts)` — `.get/.post` returning `{ data, payment }`.
12
- * - `buildSignedX402Payment` / `readX402Challenge` — primitives for custom transports.
13
- */
14
- type X402Accept = {
15
- scheme: "exact";
16
- network: string;
17
- asset: string;
18
- /** Atomic units. */
19
- amount: string;
20
- payTo: string;
21
- maxTimeoutSeconds: number;
22
- extra: {
23
- nonce: string;
24
- };
25
- };
26
- type X402Challenge = {
27
- x402Version: number;
28
- accepts: X402Accept[];
29
- error?: string;
30
- };
31
- /** Decoded `PAYMENT-RESPONSE` settlement receipt. */
32
- type X402Receipt = {
33
- success: boolean;
34
- /** Settlement tier: `optimistic` (served on broadcast-accept, reconciled
35
- * async) or `confirmed` (canonical). Absent on older deployments. */
36
- state?: "optimistic" | "confirmed";
37
- txid: string;
38
- payer: string;
39
- network: string;
40
- };
41
- /** Bitcoin-native first — sBTC is the compelling micropay asset; USDCx the
42
- * dollar peg; STX the fallback. Override via `preferAssets`. */
43
- declare const DEFAULT_PREFER_ASSETS: X402TokenSymbol[];
44
- /** Default node for auto-resolving the payer's account nonce (`/v2/accounts`). */
45
- declare const DEFAULT_NONCE_NODE_URL = "https://api.hiro.so";
46
- /** Thrown when no offered asset is within the caller's spend guard / preferences. */
47
- declare class X402SpendGuardError extends Error {
48
- constructor(message: string);
49
- }
50
- /** Read the x402 challenge from a 402 response — prefer the wire header, fall
51
- * back to the JSON body. */
52
- declare function readX402Challenge(res: Response): Promise<X402Challenge | null>;
53
- /** Read the settlement receipt from a paid 200 response. */
54
- declare function readX402Receipt(res: Response): X402Receipt | null;
55
- type SelectOfferOptions = {
56
- preferAssets?: X402TokenSymbol[];
57
- maxAmountPerCall?: Partial<Record<X402TokenSymbol, bigint>>;
58
- };
59
- /** Choose an `accepts[]` entry by preference order, skipping any that exceed the
60
- * per-asset spend cap. Throws {@link X402SpendGuardError} if none qualify. */
61
- declare function selectOffer(challenge: X402Challenge, opts?: SelectOfferOptions): {
62
- accept: X402Accept;
63
- symbol: X402TokenSymbol;
64
- };
65
- /** Fetch the payer account's next nonce from a Stacks node (`/v2/accounts`). */
66
- declare function resolveAccountNonce(address: string, nodeUrl?: string): Promise<number>;
67
- type BuildSignedX402PaymentOptions = {
68
- challenge: X402Challenge;
69
- account: LocalAccount;
70
- /** The payer account's next nonce. */
71
- accountNonce: bigint | number | string;
72
- /** Preferred asset string (e.g. an sBTC/USDCx id). Defaults to the first offer. */
73
- asset?: string;
74
- chain?: StacksChain;
75
- };
76
- /** Build a signed, base64 `PAYMENT-SIGNATURE` header for one `accepts[]` entry. */
77
- declare function buildSignedX402Payment(opts: BuildSignedX402PaymentOptions): Promise<{
78
- header: string;
79
- accept: X402Accept;
80
- }>;
81
- type WithX402Options = SelectOfferOptions & {
82
- account: LocalAccount;
83
- /** Prepaid-credit token (PAYMENT-BALANCE) from a prior deposit — calls
84
- * debit the tab server-side instead of settling on-chain per call. */
85
- balanceToken?: string;
86
- /** Autonomous treasury policy: when the tab's remaining balance (read from
87
- * X-BALANCE-REMAINING-USD) drops below `whenBelow`, deposit `usd` more in
88
- * the background (one on-chain payment) and adopt the fresh token. */
89
- topUp?: {
90
- usd: number;
91
- whenBelow: number;
92
- };
93
- /** Override the payer nonce; auto-resolved from `nodeUrl` when omitted. */
94
- accountNonce?: bigint | number | string;
95
- /** Node for auto-nonce lookup. Defaults to {@link DEFAULT_NONCE_NODE_URL}. */
96
- nodeUrl?: string;
97
- chain?: StacksChain;
98
- /** Fired just before the paid retry (the call then blocks on confirmed-tier
99
- * settle, or returns near-instantly on an optimistic surface). */
100
- onSettling?: (info: {
101
- asset: string;
102
- amount: string;
103
- }) => void;
104
- /** Abort the paid retry after this long. */
105
- timeoutMs?: number;
106
- };
107
- type X402Fetch = (input: string | URL, init?: RequestInit) => Promise<Response>;
108
- /**
109
- * Wrap a `fetch` so requests transparently pay on 402: select an offer (by
110
- * `preferAssets` + `maxAmountPerCall`), resolve the nonce, sign origin-only, and
111
- * retry once with `PAYMENT-SIGNATURE`. Returns the final `Response` (read the
112
- * receipt with {@link readX402Receipt}).
113
- *
114
- * @example
115
- * const x402fetch = withX402(fetch, { account });
116
- * const res = await x402fetch("http://127.0.0.1:3800/v1/index/events?event_type=ft_transfer");
117
- */
118
- declare function withX402(baseFetch: typeof fetch, opts: WithX402Options): X402Fetch;
119
- type X402ClientOptions = WithX402Options & {
120
- /** Instance API. Defaults to {@link resolveBaseUrl}. */
121
- baseUrl?: string;
122
- /** Override the underlying fetch (tests). */
123
- fetch?: typeof fetch;
124
- };
125
- type X402Result<T = unknown> = {
126
- data: T;
127
- /** Settlement receipt, or null if the response carried none. */
128
- payment: X402Receipt | null;
129
- response: Response;
130
- };
131
- type X402Client = {
132
- get<T = unknown>(path: string, o?: {
133
- query?: Record<string, string>;
134
- }): Promise<X402Result<T>>;
135
- post<T = unknown>(path: string, o?: {
136
- body?: unknown;
137
- }): Promise<X402Result<T>>;
138
- };
139
- /**
140
- * A small client over {@link withX402}: `.get/.post` against `baseUrl`, returning
141
- * parsed JSON plus the settlement receipt.
142
- *
143
- * @example
144
- * const sl = createX402Client({ account, baseUrl: "http://127.0.0.1:3800" });
145
- * const { data, payment } = await sl.get("/v1/index/events", { query: { event_type: "ft_transfer" } });
146
- */
147
- declare function createX402Client(opts: X402ClientOptions): X402Client;
148
- /** Caller-supplied fetch that re-runs the request with the given extra headers. */
149
- type X402FetchFn = (extraHeaders: Record<string, string>) => Promise<Response>;
150
- type PayAndRetryOptions = Omit<WithX402Options, never> & {
151
- /** Required here (use `withX402` for auto-nonce). */
152
- accountNonce: bigint | number | string;
153
- };
154
- /**
155
- * Low-level: run a request via a caller-controlled fetch; if it 402s, pay and
156
- * retry once. Prefer {@link withX402} for the common case (it auto-resolves the
157
- * nonce + selects the asset). Kept for custom transports.
158
- */
159
- declare function payAndRetry(doFetch: X402FetchFn, opts: PayAndRetryOptions): Promise<Response>;
160
- export { withX402, selectOffer, resolveAccountNonce, readX402Receipt, readX402Challenge, payAndRetry, createX402Client, buildSignedX402Payment, X402SpendGuardError, X402Result, X402Receipt, X402FetchFn, X402Fetch, X402ClientOptions, X402Client, X402Challenge, X402Accept, WithX402Options, SelectOfferOptions, PayAndRetryOptions, DEFAULT_PREFER_ASSETS, DEFAULT_NONCE_NODE_URL, BuildSignedX402PaymentOptions };