@leashmarket/core 0.1.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.
Files changed (106) hide show
  1. package/README.md +32 -0
  2. package/dist/agent/agent.d.ts +8 -0
  3. package/dist/agent/agent.d.ts.map +1 -0
  4. package/dist/agent/agent.js +14 -0
  5. package/dist/agent/agent.js.map +1 -0
  6. package/dist/agent/treasury-pda.d.ts +49 -0
  7. package/dist/agent/treasury-pda.d.ts.map +1 -0
  8. package/dist/agent/treasury-pda.js +55 -0
  9. package/dist/agent/treasury-pda.js.map +1 -0
  10. package/dist/explorer/index.d.ts +55 -0
  11. package/dist/explorer/index.d.ts.map +1 -0
  12. package/dist/explorer/index.js +95 -0
  13. package/dist/explorer/index.js.map +1 -0
  14. package/dist/fees/leash-fee.d.ts +248 -0
  15. package/dist/fees/leash-fee.d.ts.map +1 -0
  16. package/dist/fees/leash-fee.js +246 -0
  17. package/dist/fees/leash-fee.js.map +1 -0
  18. package/dist/format/index.d.ts +55 -0
  19. package/dist/format/index.d.ts.map +1 -0
  20. package/dist/format/index.js +130 -0
  21. package/dist/format/index.js.map +1 -0
  22. package/dist/index.d.ts +25 -0
  23. package/dist/index.d.ts.map +1 -0
  24. package/dist/index.js +25 -0
  25. package/dist/index.js.map +1 -0
  26. package/dist/policy/evaluate.d.ts +17 -0
  27. package/dist/policy/evaluate.d.ts.map +1 -0
  28. package/dist/policy/evaluate.js +46 -0
  29. package/dist/policy/evaluate.js.map +1 -0
  30. package/dist/policy/state.d.ts +8 -0
  31. package/dist/policy/state.d.ts.map +1 -0
  32. package/dist/policy/state.js +2 -0
  33. package/dist/policy/state.js.map +1 -0
  34. package/dist/receipt/build.d.ts +5 -0
  35. package/dist/receipt/build.d.ts.map +1 -0
  36. package/dist/receipt/build.js +9 -0
  37. package/dist/receipt/build.js.map +1 -0
  38. package/dist/receipt/hash.d.ts +15 -0
  39. package/dist/receipt/hash.d.ts.map +1 -0
  40. package/dist/receipt/hash.js +40 -0
  41. package/dist/receipt/hash.js.map +1 -0
  42. package/dist/receipt/verify.d.ts +10 -0
  43. package/dist/receipt/verify.d.ts.map +1 -0
  44. package/dist/receipt/verify.js +36 -0
  45. package/dist/receipt/verify.js.map +1 -0
  46. package/dist/tokens/index.d.ts +86 -0
  47. package/dist/tokens/index.d.ts.map +1 -0
  48. package/dist/tokens/index.js +163 -0
  49. package/dist/tokens/index.js.map +1 -0
  50. package/dist/treasury/balance.d.ts +3 -0
  51. package/dist/treasury/balance.d.ts.map +1 -0
  52. package/dist/treasury/balance.js +13 -0
  53. package/dist/treasury/balance.js.map +1 -0
  54. package/dist/treasury/inspect-token-account.d.ts +46 -0
  55. package/dist/treasury/inspect-token-account.d.ts.map +1 -0
  56. package/dist/treasury/inspect-token-account.js +67 -0
  57. package/dist/treasury/inspect-token-account.js.map +1 -0
  58. package/dist/treasury/list-balances.d.ts +57 -0
  59. package/dist/treasury/list-balances.d.ts.map +1 -0
  60. package/dist/treasury/list-balances.js +115 -0
  61. package/dist/treasury/list-balances.js.map +1 -0
  62. package/dist/treasury/pause.d.ts +32 -0
  63. package/dist/treasury/pause.d.ts.map +1 -0
  64. package/dist/treasury/pause.js +40 -0
  65. package/dist/treasury/pause.js.map +1 -0
  66. package/dist/treasury/withdraw.d.ts +13 -0
  67. package/dist/treasury/withdraw.d.ts.map +1 -0
  68. package/dist/treasury/withdraw.js +4 -0
  69. package/dist/treasury/withdraw.js.map +1 -0
  70. package/dist/wallet/index.d.ts +3 -0
  71. package/dist/wallet/index.d.ts.map +1 -0
  72. package/dist/wallet/index.js +2 -0
  73. package/dist/wallet/index.js.map +1 -0
  74. package/dist/x402/client.d.ts +72 -0
  75. package/dist/x402/client.d.ts.map +1 -0
  76. package/dist/x402/client.js +94 -0
  77. package/dist/x402/client.js.map +1 -0
  78. package/dist/x402/delegate-scheme.d.ts +107 -0
  79. package/dist/x402/delegate-scheme.d.ts.map +1 -0
  80. package/dist/x402/delegate-scheme.js +268 -0
  81. package/dist/x402/delegate-scheme.js.map +1 -0
  82. package/dist/x402/discovery.d.ts +110 -0
  83. package/dist/x402/discovery.d.ts.map +1 -0
  84. package/dist/x402/discovery.js +213 -0
  85. package/dist/x402/discovery.js.map +1 -0
  86. package/dist/x402/envelope.d.ts +65 -0
  87. package/dist/x402/envelope.d.ts.map +1 -0
  88. package/dist/x402/envelope.js +67 -0
  89. package/dist/x402/envelope.js.map +1 -0
  90. package/dist/x402/facilitator.d.ts +45 -0
  91. package/dist/x402/facilitator.d.ts.map +1 -0
  92. package/dist/x402/facilitator.js +61 -0
  93. package/dist/x402/facilitator.js.map +1 -0
  94. package/dist/x402/headers.d.ts +51 -0
  95. package/dist/x402/headers.d.ts.map +1 -0
  96. package/dist/x402/headers.js +84 -0
  97. package/dist/x402/headers.js.map +1 -0
  98. package/dist/x402/parse.d.ts +20 -0
  99. package/dist/x402/parse.d.ts.map +1 -0
  100. package/dist/x402/parse.js +31 -0
  101. package/dist/x402/parse.js.map +1 -0
  102. package/dist/x402/webhook.d.ts +48 -0
  103. package/dist/x402/webhook.d.ts.map +1 -0
  104. package/dist/x402/webhook.js +88 -0
  105. package/dist/x402/webhook.js.map +1 -0
  106. package/package.json +46 -0
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Enumerate every SPL token an owner holds (legacy SPL + Token-2022),
3
+ * enrich with metadata from the {@link KNOWN_TOKENS} registry, and pin
4
+ * stablecoin entries with zero balance when missing so UI surfaces always
5
+ * render them.
6
+ *
7
+ * This consolidates logic that previously lived in
8
+ * `apps/playground/app/api/agents/balance/route.ts` and
9
+ * `apps/playground/app/api/wallet/balance/route.ts`. Calling it from the SDK means
10
+ * any future Leash surface (CLI, mobile, downstream agents) gets the same
11
+ * canonical balance shape without re-implementing the RPC plumbing.
12
+ */
13
+ import { KNOWN_TOKENS, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID, lookupToken, pinnedMints, } from '../tokens/index.js';
14
+ /**
15
+ * Enumerate balances. Tolerates RPC failures on either token program — if
16
+ * Token-2022 calls fail (some RPCs lag here), the function still returns
17
+ * legacy SPL balances rather than throwing.
18
+ */
19
+ export async function listSplBalances(opts) {
20
+ const pin = opts.pinKnownStables ?? true;
21
+ const [lamports, splTokens, spl22Tokens] = await Promise.all([
22
+ fetchLamports(opts.rpcUrl, opts.owner),
23
+ fetchTokensForProgram(opts.rpcUrl, opts.owner, TOKEN_PROGRAM_ID, 'spl-token', opts.network).catch(() => []),
24
+ fetchTokensForProgram(opts.rpcUrl, opts.owner, TOKEN_2022_PROGRAM_ID, 'spl-token-2022', opts.network).catch(() => []),
25
+ ]);
26
+ const held = [...splTokens, ...spl22Tokens];
27
+ const heldByMint = new Map(held.map((t) => [t.mint, t]));
28
+ if (pin) {
29
+ for (const mint of pinnedMints(opts.network)) {
30
+ if (!heldByMint.has(mint)) {
31
+ const meta = lookupToken(mint, opts.network);
32
+ if (meta)
33
+ heldByMint.set(mint, emptyBalance(meta));
34
+ }
35
+ }
36
+ }
37
+ const tokens = [...heldByMint.values()].sort((a, b) => {
38
+ if (a.known !== b.known)
39
+ return a.known ? -1 : 1;
40
+ if (a.ui !== b.ui)
41
+ return b.ui - a.ui;
42
+ return (a.symbol ?? a.mint).localeCompare(b.symbol ?? b.mint);
43
+ });
44
+ return {
45
+ owner: opts.owner,
46
+ network: opts.network,
47
+ sol: Number(lamports) / 1_000_000_000,
48
+ lamports: lamports.toString(),
49
+ tokens,
50
+ };
51
+ }
52
+ async function rpc(rpcUrl, method, params) {
53
+ // `cache: 'no-store'` is a browser/Next.js RequestInit extension; we cast
54
+ // to keep the SDK consumable from server-only code (Node 20+) without
55
+ // pulling in DOM lib types.
56
+ const res = await fetch(rpcUrl, {
57
+ method: 'POST',
58
+ headers: { 'content-type': 'application/json' },
59
+ body: JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }),
60
+ cache: 'no-store',
61
+ });
62
+ if (!res.ok)
63
+ throw new Error(`RPC ${method} HTTP ${res.status}`);
64
+ const json = (await res.json());
65
+ if (json.error)
66
+ throw new Error(`RPC ${method}: ${json.error.message}`);
67
+ if (json.result === undefined)
68
+ throw new Error(`RPC ${method}: empty result`);
69
+ return json.result;
70
+ }
71
+ async function fetchLamports(rpcUrl, owner) {
72
+ const result = await rpc(rpcUrl, 'getBalance', [
73
+ owner,
74
+ { commitment: 'confirmed' },
75
+ ]);
76
+ return BigInt(result.value);
77
+ }
78
+ async function fetchTokensForProgram(rpcUrl, owner, programId, programLabel, network) {
79
+ const result = await rpc(rpcUrl, 'getTokenAccountsByOwner', [
80
+ owner,
81
+ { programId },
82
+ { encoding: 'jsonParsed' },
83
+ ]);
84
+ return result.value.map((acc) => {
85
+ const info = acc.account.data.parsed.info;
86
+ const known = lookupToken(info.mint, network);
87
+ return {
88
+ mint: info.mint,
89
+ symbol: known?.symbol ?? null,
90
+ name: known?.name ?? null,
91
+ decimals: info.tokenAmount.decimals,
92
+ amount: info.tokenAmount.amount,
93
+ ui: info.tokenAmount.uiAmount ??
94
+ Number(info.tokenAmount.amount) / 10 ** info.tokenAmount.decimals,
95
+ program: programLabel,
96
+ known: !!known,
97
+ };
98
+ });
99
+ }
100
+ function emptyBalance(meta) {
101
+ return {
102
+ mint: meta.mint,
103
+ symbol: meta.symbol,
104
+ name: meta.name,
105
+ decimals: meta.decimals,
106
+ amount: '0',
107
+ ui: 0,
108
+ program: meta.program,
109
+ known: true,
110
+ };
111
+ }
112
+ // Re-export for convenience so consumers don't import a parallel symbol from
113
+ // '../tokens/index.js' just to render labels.
114
+ export { KNOWN_TOKENS };
115
+ //# sourceMappingURL=list-balances.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"list-balances.js","sourceRoot":"","sources":["../../src/treasury/list-balances.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EACL,YAAY,EACZ,qBAAqB,EACrB,gBAAgB,EAChB,WAAW,EACX,WAAW,GAGZ,MAAM,oBAAoB,CAAC;AAyC5B;;;;GAIG;AACH,MAAM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AC7B,IAAI,EAAE,KAAK,EAAE,IAAI,IAAI,IAAI;YACzB,QAAQ,EAAE,IAAI,CAAC,WAAW,CAAC,QAAQ;YACnC,MAAM,EAAE,IAAI,CAAC,WAAW,CAAC,MAAM;YAC/B,EAAE,EACA,IAAI,CAAC,WAAW,CAAC,QAAQ;gBACzB,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,WAAW,CAAC,QAAQ;YACnE,OAAO,EAAE,YAAY;YACrB,KAAK,EAAE,CAAC,CAAC,KAAK;SACf,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,YAAY,CAAC,IAAgB;IACpC,OAAO;QACL,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,IAAI,EAAE,IAAI,CAAC,IAAI;QACf,QAAQ,EAAE,IAAI,CAAC,QAAQ;QACvB,MAAM,EAAE,GAAG;QACX,EAAE,EAAE,CAAC;QACL,OAAO,EAAE,IAAI,CAAC,OAAO;QACrB,KAAK,EAAE,IAAI;KACZ,CAAC;AACJ,CAAC;AAED,6EAA6E;AAC7E,8CAA8C;AAC9C,OAAO,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Kill-switch helpers (env + on-chain).
3
+ *
4
+ * The on-chain mechanism (per LEASH plan §7.5) is a `paused: bool` stored in
5
+ * the AppData plugin on the Core asset, owner-only writable via
6
+ * `updatePluginV1`. Reading that plugin requires an mpl-core-aware resolver
7
+ * (provided by the runner / app), so this module exposes a small generic
8
+ * cached wrapper plus the env breaker.
9
+ */
10
+ export type PauseState = {
11
+ paused: boolean;
12
+ source: 'env' | 'onchain' | 'cache';
13
+ };
14
+ export declare function readPauseFromEnv(): boolean;
15
+ export type PauseResolverOptions = {
16
+ /** Async function returning the current on-chain `paused` flag for the asset. */
17
+ fetchOnchainPaused?: () => Promise<boolean>;
18
+ /** Cache TTL in milliseconds (default 5000 — keeps RPC pressure low per plan). */
19
+ cacheTtlMs?: number;
20
+ /** Clock override for tests. */
21
+ now?: () => number;
22
+ };
23
+ /**
24
+ * Returns a function the runner can call before any outbound HTTP. Honors
25
+ * env breaker first (returns immediately), otherwise consults the on-chain
26
+ * resolver with a TTL cache. Failures fall back to the last-known value if
27
+ * present, else `false` (fail-open is intentional for v0.1: an RPC outage
28
+ * should not silently stall the agent — operators can set `LEASH_KILL=1` for
29
+ * hard stop).
30
+ */
31
+ export declare function createPauseResolver(opts?: PauseResolverOptions): () => Promise<PauseState>;
32
+ //# sourceMappingURL=pause.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pause.d.ts","sourceRoot":"","sources":["../../src/treasury/pause.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,MAAM,UAAU,GAAG;IAAE,MAAM,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,KAAK,GAAG,SAAS,GAAG,OAAO,CAAA;CAAE,CAAC;AAElF,wBAAgB,gBAAgB,IAAI,OAAO,CAE1C;AAED,MAAM,MAAM,oBAAoB,GAAG;IACjC,iFAAiF;IACjF,kBAAkB,CAAC,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,CAAC;IAC5C,kFAAkF;IAClF,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,gCAAgC;IAChC,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,GAAE,oBAAyB,GAAG,MAAM,OAAO,CAAC,UAAU,CAAC,CA2B9F"}
@@ -0,0 +1,40 @@
1
+ export function readPauseFromEnv() {
2
+ return process.env.LEASH_KILL === '1';
3
+ }
4
+ /**
5
+ * Returns a function the runner can call before any outbound HTTP. Honors
6
+ * env breaker first (returns immediately), otherwise consults the on-chain
7
+ * resolver with a TTL cache. Failures fall back to the last-known value if
8
+ * present, else `false` (fail-open is intentional for v0.1: an RPC outage
9
+ * should not silently stall the agent — operators can set `LEASH_KILL=1` for
10
+ * hard stop).
11
+ */
12
+ export function createPauseResolver(opts = {}) {
13
+ const ttl = opts.cacheTtlMs ?? 5000;
14
+ const now = opts.now ?? Date.now;
15
+ let last = null;
16
+ return async function resolve() {
17
+ if (readPauseFromEnv()) {
18
+ return { paused: true, source: 'env' };
19
+ }
20
+ if (!opts.fetchOnchainPaused) {
21
+ return { paused: false, source: 'onchain' };
22
+ }
23
+ const t = now();
24
+ if (last && t - last.ts < ttl) {
25
+ return { paused: last.value, source: 'cache' };
26
+ }
27
+ try {
28
+ const value = await opts.fetchOnchainPaused();
29
+ last = { value, ts: t };
30
+ return { paused: value, source: 'onchain' };
31
+ }
32
+ catch {
33
+ if (last) {
34
+ return { paused: last.value, source: 'cache' };
35
+ }
36
+ return { paused: false, source: 'onchain' };
37
+ }
38
+ };
39
+ }
40
+ //# sourceMappingURL=pause.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pause.js","sourceRoot":"","sources":["../../src/treasury/pause.ts"],"names":[],"mappings":"AAWA,MAAM,UAAU,gBAAgB;IAC9B,OAAO,OAAO,CAAC,GAAG,CAAC,UAAU,KAAK,GAAG,CAAC;AACxC,CAAC;AAWD;;;;;;;GAOG;AACH,MAAM,UAAU,mBAAmB,CAAC,OAA6B,EAAE;IACjE,MAAM,GAAG,GAAG,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC;IACpC,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC;IACjC,IAAI,IAAI,GAA0C,IAAI,CAAC;IAEvD,OAAO,KAAK,UAAU,OAAO;QAC3B,IAAI,gBAAgB,EAAE,EAAE,CAAC;YACvB,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;QACzC,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,kBAAkB,EAAE,CAAC;YAC7B,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;QAC9C,CAAC;QACD,MAAM,CAAC,GAAG,GAAG,EAAE,CAAC;QAChB,IAAI,IAAI,IAAI,CAAC,GAAG,IAAI,CAAC,EAAE,GAAG,GAAG,EAAE,CAAC;YAC9B,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;QACjD,CAAC;QACD,IAAI,CAAC;YACH,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,kBAAkB,EAAE,CAAC;YAC9C,IAAI,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,EAAE,CAAC;YACxB,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;QAC9C,CAAC;QAAC,MAAM,CAAC;YACP,IAAI,IAAI,EAAE,CAAC;gBACT,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;YACjD,CAAC;YACD,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;QAC9C,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { PublicKey } from '@metaplex-foundation/umi';
2
+ /**
3
+ * v0.1 stub: full MPL Core `Execute` + SPL transfer composition ships in a follow-up PR.
4
+ * Callers should use Umi + mpl-core instructions; this export reserves the API surface.
5
+ */
6
+ export type WithdrawParams = {
7
+ asset: PublicKey;
8
+ mint: PublicKey;
9
+ amount: bigint;
10
+ destination: PublicKey;
11
+ };
12
+ export declare function describeWithdraw(_p: WithdrawParams): string;
13
+ //# sourceMappingURL=withdraw.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withdraw.d.ts","sourceRoot":"","sources":["../../src/treasury/withdraw.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAE1D;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,EAAE,SAAS,CAAC;IACjB,IAAI,EAAE,SAAS,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE,SAAS,CAAC;CACxB,CAAC;AAEF,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,cAAc,GAAG,MAAM,CAE3D"}
@@ -0,0 +1,4 @@
1
+ export function describeWithdraw(_p) {
2
+ return 'withdraw: use Umi mpl-core Execute with SPL transferChecked from Asset Signer PDA';
3
+ }
4
+ //# sourceMappingURL=withdraw.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"withdraw.js","sourceRoot":"","sources":["../../src/treasury/withdraw.ts"],"names":[],"mappings":"AAaA,MAAM,UAAU,gBAAgB,CAAC,EAAkB;IACjD,OAAO,mFAAmF,CAAC;AAC7F,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { Signer } from '@metaplex-foundation/umi';
2
+ export type Wallet = Signer;
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/wallet/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAEvD,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/wallet/index.ts"],"names":[],"mappings":""}
@@ -0,0 +1,72 @@
1
+ import { x402Client } from '@x402/core/client';
2
+ import type { Network } from '@x402/core/types';
3
+ import { wrapFetchWithPayment } from '@x402/fetch';
4
+ import { ExactSvmScheme } from '@x402/svm';
5
+ import type { ClientSvmSigner } from '@x402/svm';
6
+ import { LeashDelegateExactSvmScheme, LeashExactSvmScheme } from './delegate-scheme.js';
7
+ import type { Address } from '@solana/kit';
8
+ export type LeashFetch = (input: string | URL | Request, init?: RequestInit) => Promise<Response>;
9
+ export type LeashX402Network = 'solana-mainnet' | 'solana-devnet' | 'solana-testnet';
10
+ export declare function caip2ForNetwork(network: LeashX402Network): Network;
11
+ /**
12
+ * Inverse of {@link caip2ForNetwork} — turn a CAIP-2 chain id (the form
13
+ * x402 facilitators emit on the wire, e.g. `solana:EtWTRABZaYq6...`) into a
14
+ * friendly Leash slug (`solana-devnet`). Falls back to the original input
15
+ * for unknown chains so receipts written by older code still round-trip.
16
+ *
17
+ * Used everywhere a receipt's `price.network` is constructed from a payment-
18
+ * required header so explorers display `solana-devnet` instead of the raw
19
+ * `solana:<genesis>` blob.
20
+ */
21
+ export declare function networkFromCaip2(input: string | null | undefined): string | null;
22
+ export type CreateSvmBuyerClientOptions = {
23
+ signer: ClientSvmSigner;
24
+ /**
25
+ * Networks to register on the underlying x402Client. Defaults to all three
26
+ * Solana clusters; only the ones the seller's `paymentRequirements` advertise
27
+ * will actually be exercised.
28
+ */
29
+ networks?: LeashX402Network[];
30
+ /** Optional custom RPC for transaction simulation/fetches. */
31
+ rpcUrl?: string;
32
+ /**
33
+ * If set, payments use {@link LeashDelegateExactSvmScheme} with this account
34
+ * as the source of funds and `signer` as the SPL delegate authority. Use this
35
+ * to spend from an agent treasury PDA's USDC ATA — the executive (Privy
36
+ * embedded wallet) signs but the funds debit from the agent's PDA.
37
+ *
38
+ * If omitted, the default `ExactSvmScheme` is used (signer also owns funds).
39
+ */
40
+ sourceTokenAccount?: Address | string;
41
+ /**
42
+ * SPL mint address the buyer prefers to settle in. When the seller offers
43
+ * multiple `accepts[]` (e.g. USDC + USDG), the underlying `x402Client`
44
+ * selector picks the entry whose `asset` matches this mint. Falls back to
45
+ * the first compatible entry when no preferred match exists, so a request
46
+ * never fails just because the buyer's preference is unavailable.
47
+ */
48
+ preferredAsset?: string;
49
+ };
50
+ /**
51
+ * Build a paid `fetch` for the buyer side of x402 on Solana.
52
+ *
53
+ * Wraps `globalThis.fetch` with `@x402/fetch`'s `wrapFetchWithPayment` and
54
+ * registers `ExactSvmScheme` against the provided signer. The returned
55
+ * function is a drop-in replacement for `fetch`: the first 402 response
56
+ * triggers a real SPL `TransferChecked` payment, and the request is replayed
57
+ * with `X-PAYMENT` attached.
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * import { createKeyPairSignerFromBytes } from '@solana/kit';
62
+ * import { createSvmBuyerFetch } from '@leashmarket/core';
63
+ *
64
+ * const signer = await createKeyPairSignerFromBytes(secret);
65
+ * const paidFetch = createSvmBuyerFetch({ signer, networks: ['solana-devnet'] });
66
+ * const res = await paidFetch('https://merchant.example/api/data');
67
+ * ```
68
+ */
69
+ export declare function createSvmBuyerFetch(opts: CreateSvmBuyerClientOptions): LeashFetch;
70
+ export { wrapFetchWithPayment, x402Client, ExactSvmScheme, LeashDelegateExactSvmScheme, LeashExactSvmScheme, };
71
+ export type { ClientSvmSigner };
72
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/x402/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAE3C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,EAAE,2BAA2B,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AACxF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAE3C,MAAM,MAAM,UAAU,GAAG,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAAE,IAAI,CAAC,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAElG,MAAM,MAAM,gBAAgB,GAAG,gBAAgB,GAAG,eAAe,GAAG,gBAAgB,CAAC;AAQrF,wBAAgB,eAAe,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAElE;AAED;;;;;;;;;GASG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,IAAI,CAWhF;AAED,MAAM,MAAM,2BAA2B,GAAG;IACxC,MAAM,EAAE,eAAe,CAAC;IACxB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,gBAAgB,EAAE,CAAC;IAC9B,8DAA8D;IAC9D,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;;;;;;OAOG;IACH,kBAAkB,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IACtC;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,2BAA2B,GAAG,UAAU,CAmCjF;AAED,OAAO,EACL,oBAAoB,EACpB,UAAU,EACV,cAAc,EACd,2BAA2B,EAC3B,mBAAmB,GACpB,CAAC;AACF,YAAY,EAAE,eAAe,EAAE,CAAC"}
@@ -0,0 +1,94 @@
1
+ import { x402Client } from '@x402/core/client';
2
+ import { wrapFetchWithPayment } from '@x402/fetch';
3
+ import { ExactSvmScheme } from '@x402/svm';
4
+ import { SOLANA_DEVNET_CAIP2, SOLANA_MAINNET_CAIP2, SOLANA_TESTNET_CAIP2 } from '@x402/svm';
5
+ import { LeashDelegateExactSvmScheme, LeashExactSvmScheme } from './delegate-scheme.js';
6
+ const NETWORK_TO_CAIP2 = {
7
+ 'solana-mainnet': SOLANA_MAINNET_CAIP2,
8
+ 'solana-devnet': SOLANA_DEVNET_CAIP2,
9
+ 'solana-testnet': SOLANA_TESTNET_CAIP2,
10
+ };
11
+ export function caip2ForNetwork(network) {
12
+ return NETWORK_TO_CAIP2[network];
13
+ }
14
+ /**
15
+ * Inverse of {@link caip2ForNetwork} — turn a CAIP-2 chain id (the form
16
+ * x402 facilitators emit on the wire, e.g. `solana:EtWTRABZaYq6...`) into a
17
+ * friendly Leash slug (`solana-devnet`). Falls back to the original input
18
+ * for unknown chains so receipts written by older code still round-trip.
19
+ *
20
+ * Used everywhere a receipt's `price.network` is constructed from a payment-
21
+ * required header so explorers display `solana-devnet` instead of the raw
22
+ * `solana:<genesis>` blob.
23
+ */
24
+ export function networkFromCaip2(input) {
25
+ if (!input)
26
+ return null;
27
+ const lower = input.toLowerCase();
28
+ if (lower === 'solana-mainnet' || lower === 'solana-devnet' || lower === 'solana-testnet') {
29
+ return lower;
30
+ }
31
+ // Match by genesis-hash prefix (CAIP-2 truncates to 32 chars).
32
+ if (lower.startsWith('solana:5eykt4u'))
33
+ return 'solana-mainnet';
34
+ if (lower.startsWith('solana:etwtrabz'))
35
+ return 'solana-devnet';
36
+ if (lower.startsWith('solana:4uhcvjyu'))
37
+ return 'solana-testnet';
38
+ return input;
39
+ }
40
+ /**
41
+ * Build a paid `fetch` for the buyer side of x402 on Solana.
42
+ *
43
+ * Wraps `globalThis.fetch` with `@x402/fetch`'s `wrapFetchWithPayment` and
44
+ * registers `ExactSvmScheme` against the provided signer. The returned
45
+ * function is a drop-in replacement for `fetch`: the first 402 response
46
+ * triggers a real SPL `TransferChecked` payment, and the request is replayed
47
+ * with `X-PAYMENT` attached.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * import { createKeyPairSignerFromBytes } from '@solana/kit';
52
+ * import { createSvmBuyerFetch } from '@leashmarket/core';
53
+ *
54
+ * const signer = await createKeyPairSignerFromBytes(secret);
55
+ * const paidFetch = createSvmBuyerFetch({ signer, networks: ['solana-devnet'] });
56
+ * const res = await paidFetch('https://merchant.example/api/data');
57
+ * ```
58
+ */
59
+ export function createSvmBuyerFetch(opts) {
60
+ const networks = opts.networks ?? ['solana-mainnet', 'solana-devnet', 'solana-testnet'];
61
+ const preferredAsset = opts.preferredAsset?.trim();
62
+ // The default selector picks `paymentRequirements[0]`. We override it to
63
+ // honour the buyer's preferred mint when the seller advertises multiple
64
+ // `accepts[]` (e.g. a USDC link that also accepts USDG / USDT).
65
+ const client = preferredAsset
66
+ ? new x402Client((_v, reqs) => {
67
+ const match = reqs.find((r) => r.asset === preferredAsset);
68
+ if (match)
69
+ return match;
70
+ const offered = Array.from(new Set(reqs.map((r) => r.asset).filter(Boolean))).join(', ');
71
+ throw new Error(`preferred_asset_unavailable: requested ${preferredAsset}; seller offers ${offered || '<none>'}`);
72
+ })
73
+ : new x402Client();
74
+ for (const n of networks) {
75
+ // Both Leash schemes append the protocol fee leg when the seller's
76
+ // `paymentRequirements.extra['leash.fee']` is present and degrade
77
+ // to the upstream wire shape otherwise — so callers don't need to
78
+ // branch on whether the seller is using a Leash facilitator.
79
+ const scheme = opts.sourceTokenAccount
80
+ ? new LeashDelegateExactSvmScheme({
81
+ signer: opts.signer,
82
+ sourceTokenAccount: opts.sourceTokenAccount,
83
+ ...(opts.rpcUrl ? { rpcUrl: opts.rpcUrl } : {}),
84
+ })
85
+ : new LeashExactSvmScheme({
86
+ signer: opts.signer,
87
+ ...(opts.rpcUrl ? { rpcUrl: opts.rpcUrl } : {}),
88
+ });
89
+ client.register(NETWORK_TO_CAIP2[n], scheme);
90
+ }
91
+ return wrapFetchWithPayment(globalThis.fetch, client);
92
+ }
93
+ export { wrapFetchWithPayment, x402Client, ExactSvmScheme, LeashDelegateExactSvmScheme, LeashExactSvmScheme, };
94
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/x402/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;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AC,CAAC,EAAE,CAAC;aAChD,CAAC,CAAC;QACP,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;IAC/C,CAAC;IACD,OAAO,oBAAoB,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;AACxD,CAAC;AAED,OAAO,EACL,oBAAoB,EACpB,UAAU,EACV,cAAc,EACd,2BAA2B,EAC3B,mBAAmB,GACpB,CAAC"}
@@ -0,0 +1,107 @@
1
+ /**
2
+ * x402 SVM payment scheme variants used by Leash buyers. Both schemes
3
+ * are wire-compatible with the upstream `@x402/svm` Exact scheme but
4
+ * append the **Leash protocol fee leg** when the seller's
5
+ * `paymentRequirements.extra['leash.fee']` block is present.
6
+ *
7
+ * Two variants are exported:
8
+ *
9
+ * - {@link LeashExactSvmScheme} — the buyer signs as the **owner** of
10
+ * the source ATA. Drop-in replacement for the vanilla
11
+ * `@x402/svm` `ExactSvmScheme`; the only difference is the optional
12
+ * fee leg.
13
+ *
14
+ * - {@link LeashDelegateExactSvmScheme} — the source ATA is provided
15
+ * externally (e.g. an agent treasury PDA's USDC ATA) and the buyer
16
+ * signs as the **SPL delegate** of that account. Used by Leash
17
+ * agents whose Privy embedded wallet is the delegate of a treasury
18
+ * it doesn't own.
19
+ *
20
+ * Transaction shape (with fee leg + auto-provisioning of destination ATAs):
21
+ *
22
+ * ix[0] SetComputeUnitLimit
23
+ * ix[1] SetComputeUnitPrice
24
+ * ix[2] CreateAssociatedTokenAccountIdempotent (seller `payTo` ATA; payer = facilitator)
25
+ * ix[3] CreateAssociatedTokenAccountIdempotent (fee vault ATA; payer = facilitator)
26
+ * ix[4] TransferChecked (seller leg, amount = paymentRequirements.amount)
27
+ * ix[5] TransferChecked (fee leg, amount = extra['leash.fee'].feeAtomic)
28
+ * ix[6] Memo
29
+ *
30
+ * Idempotent creates are always emitted so first-time settlements on a
31
+ * fresh mint (e.g. devnet USDG) succeed without an out-of-band ATA-init.
32
+ * The facilitator skips any leading idempotent ATA creates whose target
33
+ * matches the seller-`payTo` or fee-vault ATA.
34
+ *
35
+ * When `extra['leash.fee']` is absent (vanilla x402 seller, no Leash
36
+ * facilitator), the fee leg is skipped and the transaction degrades to
37
+ * the upstream 4-instruction shape: `[setLimit, setPrice, transfer, memo]`.
38
+ *
39
+ * Both legs share the same authority + source ATA, so a single signature
40
+ * covers both. Atomicity is guaranteed by Solana's all-or-nothing
41
+ * transaction semantics — either the seller AND treasury get paid, or
42
+ * neither does.
43
+ */
44
+ import { type Address } from '@solana/kit';
45
+ import type { ClientSvmSigner } from '@x402/svm';
46
+ import type { PaymentPayload, PaymentRequirements, SchemeNetworkClient } from '@x402/core/types';
47
+ export type LeashDelegateExactSvmSchemeOptions = {
48
+ /** Authority that will sign the TransferChecked. Must be the SPL delegate of `sourceTokenAccount`. */
49
+ signer: ClientSvmSigner;
50
+ /**
51
+ * The token account that funds the transfer (e.g. the agent treasury's
52
+ * USDC ATA). Funds debit from here, not from the signer's wallet.
53
+ */
54
+ sourceTokenAccount: Address | string;
55
+ /** Optional RPC endpoint override forwarded to `@x402/svm` helpers. */
56
+ rpcUrl?: string;
57
+ };
58
+ export type LeashExactSvmSchemeOptions = {
59
+ /** Owner-as-signer. Source ATA is derived from `signer.address` + `asset`. */
60
+ signer: ClientSvmSigner;
61
+ /** Optional RPC endpoint override forwarded to `@x402/svm` helpers. */
62
+ rpcUrl?: string;
63
+ };
64
+ /**
65
+ * SVM client implementation of the x402 "exact" scheme that signs as a
66
+ * **delegate** of an externally owned source ATA. Wire-compatible with
67
+ * the vanilla `ExactSvmScheme` plus the Leash protocol fee leg.
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * import { x402Client } from '@x402/core/client';
72
+ * import { LeashDelegateExactSvmScheme } from '@leashmarket/core';
73
+ *
74
+ * const client = new x402Client();
75
+ * client.register('solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1XkXr8aBmpfk5', new LeashDelegateExactSvmScheme({
76
+ * signer: privySigner,
77
+ * sourceTokenAccount: agentUsdcAta,
78
+ * }));
79
+ * ```
80
+ */
81
+ export declare class LeashDelegateExactSvmScheme implements SchemeNetworkClient {
82
+ readonly scheme = "exact";
83
+ private readonly signer;
84
+ private readonly sourceTokenAccount;
85
+ private readonly rpcUrl?;
86
+ constructor(options: LeashDelegateExactSvmSchemeOptions);
87
+ createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements): Promise<Pick<PaymentPayload, 'x402Version' | 'payload'>>;
88
+ }
89
+ /**
90
+ * SVM client implementation of the x402 "exact" scheme where the buyer
91
+ * signs as the **owner** of the source ATA (i.e. their own wallet).
92
+ * Replaces the upstream `@x402/svm` `ExactSvmScheme` for Leash buyers
93
+ * who want the Leash protocol fee leg appended whenever the seller
94
+ * advertises one.
95
+ *
96
+ * Falls back to the upstream wire shape when no fee block is present,
97
+ * so this scheme can be used unconditionally — calling code does not
98
+ * need to branch based on the seller's facilitator.
99
+ */
100
+ export declare class LeashExactSvmScheme implements SchemeNetworkClient {
101
+ readonly scheme = "exact";
102
+ private readonly signer;
103
+ private readonly rpcUrl?;
104
+ constructor(options: LeashExactSvmSchemeOptions);
105
+ createPaymentPayload(x402Version: number, paymentRequirements: PaymentRequirements): Promise<Pick<PaymentPayload, 'x402Version' | 'payload'>>;
106
+ }
107
+ //# sourceMappingURL=delegate-scheme.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"delegate-scheme.d.ts","sourceRoot":"","sources":["../../src/x402/delegate-scheme.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,OAAO,EAAwB,KAAK,OAAO,EAA0B,MAAM,aAAa,CAAC;AAwBzF,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,KAAK,EAAE,cAAc,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAejG,MAAM,MA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