@agent-shield/sdk 0.4.1 → 0.5.2
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 +191 -0
- package/README.md +10 -19
- package/dist/idl-json.d.ts +3 -3
- package/dist/idl-json.js +3 -3
- package/dist/idl-json.js.map +1 -1
- package/dist/idl.d.ts +3 -3
- package/dist/idl.d.ts.map +1 -1
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +46 -2
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -2
- package/dist/types.js.map +1 -1
- package/dist/wrapper/engine.d.ts +17 -0
- package/dist/wrapper/engine.d.ts.map +1 -0
- package/dist/wrapper/engine.js +45 -0
- package/dist/wrapper/engine.js.map +1 -0
- package/dist/wrapper/errors.d.ts +10 -0
- package/dist/wrapper/errors.d.ts.map +1 -0
- package/dist/wrapper/errors.js +20 -0
- package/dist/wrapper/errors.js.map +1 -0
- package/dist/wrapper/harden.d.ts +119 -0
- package/dist/wrapper/harden.d.ts.map +1 -0
- package/dist/wrapper/harden.js +399 -0
- package/dist/wrapper/harden.js.map +1 -0
- package/dist/wrapper/index.d.ts +17 -0
- package/dist/wrapper/index.d.ts.map +1 -0
- package/dist/wrapper/index.js +60 -0
- package/dist/wrapper/index.js.map +1 -0
- package/dist/wrapper/inspector.d.ts +22 -0
- package/dist/wrapper/inspector.d.ts.map +1 -0
- package/dist/wrapper/inspector.js +175 -0
- package/dist/wrapper/inspector.js.map +1 -0
- package/dist/wrapper/policies.d.ts +78 -0
- package/dist/wrapper/policies.d.ts.map +1 -0
- package/dist/wrapper/policies.js +108 -0
- package/dist/wrapper/policies.js.map +1 -0
- package/dist/wrapper/registry.d.ts +24 -0
- package/dist/wrapper/registry.d.ts.map +1 -0
- package/dist/wrapper/registry.js +43 -0
- package/dist/wrapper/registry.js.map +1 -0
- package/dist/wrapper/shield.d.ts +86 -0
- package/dist/wrapper/shield.d.ts.map +1 -0
- package/dist/wrapper/shield.js +236 -0
- package/dist/wrapper/shield.js.map +1 -0
- package/dist/wrapper/state.d.ts +3 -0
- package/dist/wrapper/state.d.ts.map +1 -0
- package/dist/wrapper/state.js +6 -0
- package/dist/wrapper/state.js.map +1 -0
- package/dist/wrapper/x402.d.ts +153 -0
- package/dist/wrapper/x402.d.ts.map +1 -0
- package/dist/wrapper/x402.js +342 -0
- package/dist/wrapper/x402.js.map +1 -0
- package/package.json +6 -4
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.isTeeWallet = isTeeWallet;
|
|
37
|
+
exports.shield = shield;
|
|
38
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
39
|
+
const policies_1 = require("./policies");
|
|
40
|
+
const inspector_1 = require("./inspector");
|
|
41
|
+
const engine_1 = require("./engine");
|
|
42
|
+
const errors_1 = require("./errors");
|
|
43
|
+
const state_1 = require("./state");
|
|
44
|
+
const registry_1 = require("./registry");
|
|
45
|
+
/**
|
|
46
|
+
* Type guard to detect TEE-backed wallets at runtime.
|
|
47
|
+
*/
|
|
48
|
+
function isTeeWallet(wallet) {
|
|
49
|
+
return "provider" in wallet && typeof wallet.provider === "string";
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Wrap any wallet with client-side spending controls.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* import { shieldWallet } from '@agent-shield/sdk';
|
|
57
|
+
*
|
|
58
|
+
* const protectedWallet = shieldWallet(wallet, { maxSpend: '500 USDC/day' });
|
|
59
|
+
* const agent = new SolanaAgentKit(protectedWallet, RPC_URL, config);
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* With no config, secure defaults are applied:
|
|
63
|
+
* - 1000 USDC/day, 1000 USDT/day, 10 SOL/day spending caps
|
|
64
|
+
* - Unknown programs blocked
|
|
65
|
+
* - 60 transactions/hour rate limit
|
|
66
|
+
* @internal Use shieldWallet() instead
|
|
67
|
+
*/
|
|
68
|
+
function shield(wallet, policies, options) {
|
|
69
|
+
let resolved = (0, policies_1.resolvePolicies)(policies);
|
|
70
|
+
const connection = options?.connection;
|
|
71
|
+
const state = new state_1.ShieldState(options?.storage);
|
|
72
|
+
const onDenied = options?.onDenied;
|
|
73
|
+
const onApproved = options?.onApproved;
|
|
74
|
+
const onPolicyUpdate = options?.onPolicyUpdate;
|
|
75
|
+
const onPause = options?.onPause;
|
|
76
|
+
const onResume = options?.onResume;
|
|
77
|
+
let paused = false;
|
|
78
|
+
const shielded = {
|
|
79
|
+
publicKey: wallet.publicKey,
|
|
80
|
+
innerWallet: wallet,
|
|
81
|
+
shieldState: state,
|
|
82
|
+
isHardened: false,
|
|
83
|
+
get resolvedPolicies() {
|
|
84
|
+
return resolved;
|
|
85
|
+
},
|
|
86
|
+
get isPaused() {
|
|
87
|
+
return paused;
|
|
88
|
+
},
|
|
89
|
+
async signTransaction(tx) {
|
|
90
|
+
if (paused) {
|
|
91
|
+
return wallet.signTransaction(tx);
|
|
92
|
+
}
|
|
93
|
+
// Resolve ALTs for VersionedTransactions when connection is available
|
|
94
|
+
let lookupTableAccounts;
|
|
95
|
+
if (connection &&
|
|
96
|
+
tx instanceof web3_js_1.VersionedTransaction &&
|
|
97
|
+
tx.message.addressTableLookups.length > 0) {
|
|
98
|
+
lookupTableAccounts = await (0, inspector_1.resolveTransactionAddressLookupTables)(tx, connection);
|
|
99
|
+
}
|
|
100
|
+
const analysis = (0, inspector_1.analyzeTransaction)(tx, wallet.publicKey, lookupTableAccounts);
|
|
101
|
+
const violations = (0, engine_1.evaluatePolicy)(analysis, resolved, state);
|
|
102
|
+
if (violations.length > 0) {
|
|
103
|
+
const error = new errors_1.ShieldDeniedError(violations);
|
|
104
|
+
onDenied?.(error);
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
// Policy passed — sign with underlying wallet
|
|
108
|
+
const signed = await wallet.signTransaction(tx);
|
|
109
|
+
// Record the spend and transaction
|
|
110
|
+
(0, engine_1.recordTransaction)(analysis, state);
|
|
111
|
+
onApproved?.(null);
|
|
112
|
+
return signed;
|
|
113
|
+
},
|
|
114
|
+
async signAllTransactions(txs) {
|
|
115
|
+
if (paused) {
|
|
116
|
+
if (wallet.signAllTransactions) {
|
|
117
|
+
return wallet.signAllTransactions(txs);
|
|
118
|
+
}
|
|
119
|
+
return Promise.all(txs.map((tx) => wallet.signTransaction(tx)));
|
|
120
|
+
}
|
|
121
|
+
// Resolve ALTs for any VersionedTransactions in the batch,
|
|
122
|
+
// caching resolved ALTs across the batch to avoid redundant RPCs.
|
|
123
|
+
const altCache = new Map();
|
|
124
|
+
const analyses = [];
|
|
125
|
+
for (const tx of txs) {
|
|
126
|
+
let lookupTableAccounts;
|
|
127
|
+
if (connection &&
|
|
128
|
+
tx instanceof web3_js_1.VersionedTransaction &&
|
|
129
|
+
tx.message.addressTableLookups.length > 0) {
|
|
130
|
+
const cached = [];
|
|
131
|
+
let hasMissing = false;
|
|
132
|
+
for (const lookup of tx.message.addressTableLookups) {
|
|
133
|
+
const key = lookup.accountKey.toBase58();
|
|
134
|
+
const existing = altCache.get(key);
|
|
135
|
+
if (existing) {
|
|
136
|
+
cached.push(existing);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
hasMissing = true;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (hasMissing) {
|
|
143
|
+
const fetched = await (0, inspector_1.resolveTransactionAddressLookupTables)(tx, connection);
|
|
144
|
+
for (const alt of fetched) {
|
|
145
|
+
altCache.set(alt.key.toBase58(), alt);
|
|
146
|
+
}
|
|
147
|
+
// Rebuild from cache to get all ALTs in order
|
|
148
|
+
cached.length = 0;
|
|
149
|
+
for (const lookup of tx.message.addressTableLookups) {
|
|
150
|
+
const alt = altCache.get(lookup.accountKey.toBase58());
|
|
151
|
+
if (alt)
|
|
152
|
+
cached.push(alt);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
lookupTableAccounts = cached;
|
|
156
|
+
}
|
|
157
|
+
analyses.push((0, inspector_1.analyzeTransaction)(tx, wallet.publicKey, lookupTableAccounts));
|
|
158
|
+
}
|
|
159
|
+
const cp = state.checkpoint();
|
|
160
|
+
try {
|
|
161
|
+
for (const analysis of analyses) {
|
|
162
|
+
const violations = (0, engine_1.evaluatePolicy)(analysis, resolved, state);
|
|
163
|
+
if (violations.length > 0) {
|
|
164
|
+
throw new errors_1.ShieldDeniedError(violations);
|
|
165
|
+
}
|
|
166
|
+
// Record into state so next tx sees cumulative spend
|
|
167
|
+
(0, engine_1.recordTransaction)(analysis, state);
|
|
168
|
+
}
|
|
169
|
+
// All passed — sign with underlying wallet
|
|
170
|
+
let signed;
|
|
171
|
+
if (wallet.signAllTransactions) {
|
|
172
|
+
signed = await wallet.signAllTransactions(txs);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
signed = await Promise.all(txs.map((tx) => wallet.signTransaction(tx)));
|
|
176
|
+
}
|
|
177
|
+
onApproved?.(null);
|
|
178
|
+
return signed;
|
|
179
|
+
}
|
|
180
|
+
catch (err) {
|
|
181
|
+
// Rollback phantom spends on ANY error (policy denial or signing failure)
|
|
182
|
+
state.rollback(cp);
|
|
183
|
+
// Fire callbacks AFTER rollback so state is consistent when handler reads it
|
|
184
|
+
if (err instanceof errors_1.ShieldDeniedError) {
|
|
185
|
+
onDenied?.(err);
|
|
186
|
+
}
|
|
187
|
+
throw err;
|
|
188
|
+
}
|
|
189
|
+
},
|
|
190
|
+
updatePolicies(newPolicies) {
|
|
191
|
+
resolved = (0, policies_1.resolvePolicies)(newPolicies);
|
|
192
|
+
onPolicyUpdate?.(newPolicies);
|
|
193
|
+
},
|
|
194
|
+
resetState() {
|
|
195
|
+
state.reset();
|
|
196
|
+
},
|
|
197
|
+
pause() {
|
|
198
|
+
paused = true;
|
|
199
|
+
onPause?.();
|
|
200
|
+
},
|
|
201
|
+
resume() {
|
|
202
|
+
paused = false;
|
|
203
|
+
onResume?.();
|
|
204
|
+
},
|
|
205
|
+
getSpendingSummary() {
|
|
206
|
+
const tokens = resolved.spendLimits.map((limit) => {
|
|
207
|
+
const spent = state.getSpendInWindow(limit.mint, limit.windowMs ?? 86400000);
|
|
208
|
+
const remaining = limit.amount > spent ? limit.amount - spent : BigInt(0);
|
|
209
|
+
const tokenInfo = (0, registry_1.getTokenInfo)(limit.mint);
|
|
210
|
+
return {
|
|
211
|
+
mint: limit.mint,
|
|
212
|
+
symbol: tokenInfo?.symbol,
|
|
213
|
+
spent,
|
|
214
|
+
limit: limit.amount,
|
|
215
|
+
remaining,
|
|
216
|
+
windowMs: limit.windowMs ?? 86400000,
|
|
217
|
+
};
|
|
218
|
+
});
|
|
219
|
+
const txCount = state.getTransactionCountInWindow(resolved.rateLimit.windowMs);
|
|
220
|
+
const rateLimit = {
|
|
221
|
+
count: txCount,
|
|
222
|
+
limit: resolved.rateLimit.maxTransactions,
|
|
223
|
+
remaining: Math.max(0, resolved.rateLimit.maxTransactions - txCount),
|
|
224
|
+
windowMs: resolved.rateLimit.windowMs,
|
|
225
|
+
};
|
|
226
|
+
return { tokens, rateLimit, isPaused: paused };
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
// Wire up x402 fetch support (lazy-loaded)
|
|
230
|
+
shielded.fetch = async (url, init) => {
|
|
231
|
+
const { shieldedFetch } = await Promise.resolve().then(() => __importStar(require("./x402")));
|
|
232
|
+
return shieldedFetch(shielded, url, { ...init, connection });
|
|
233
|
+
};
|
|
234
|
+
return shielded;
|
|
235
|
+
}
|
|
236
|
+
//# sourceMappingURL=shield.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"shield.js","sourceRoot":"","sources":["../../src/wrapper/shield.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+CA,kCAEC;AAgED,wBA0NC;AA3UD,6CAMyB;AACzB,yCAKoB;AACpB,2CAGqB;AACrB,qCAA6D;AAC7D,qCAA6C;AAC7C,mCAAqD;AACrD,yCAA0C;AAwB1C;;GAEG;AACH,SAAgB,WAAW,CAAC,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E,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,QAAQ,CAAC,SAAS,CAAC,eAAe,GAAG,OAAO,CAAC;gBACpE,QAAQ,EAAE,QAAQ,CAAC,SAAS,CAAC,QAAQ;aACtC,CAAC;YAEF,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC;QACjD,CAAC;KACF,CAAC;IAEF,2CAA2C;IAC3C,QAAQ,CAAC,KAAK,GAAG,KAAK,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QACnC,MAAM,EAAE,aAAa,EAAE,GAAG,wDAAa,QAAQ,GAAC,CAAC;QACjD,OAAO,aAAa,CAAC,QAAQ,EAAE,GAAG,EAAE,EAAE,GAAG,IAAI,EAAE,UAAU,EAAE,CAAC,CAAC;IAC/D,CAAC,CAAC;IAEF,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../../src/wrapper/state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ShieldState = void 0;
|
|
4
|
+
var core_1 = require("@agent-shield/core");
|
|
5
|
+
Object.defineProperty(exports, "ShieldState", { enumerable: true, get: function () { return core_1.ShieldState; } });
|
|
6
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.js","sourceRoot":"","sources":["../../src/wrapper/state.ts"],"names":[],"mappings":";;;AAAA,2CAAiD;AAAxC,mGAAA,WAAW,OAAA"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* x402 — HTTP 402 Payment Required support for shielded wallets.
|
|
3
|
+
*
|
|
4
|
+
* Implements the x402 V2 protocol (coinbase/x402) for machine-to-machine
|
|
5
|
+
* crypto payments. The client signs a payment, retries with PAYMENT-SIGNATURE
|
|
6
|
+
* header, and the API server settles via a facilitator.
|
|
7
|
+
*
|
|
8
|
+
* @see https://x402.org
|
|
9
|
+
*/
|
|
10
|
+
import { PublicKey, TransactionInstruction, Connection } from "@solana/web3.js";
|
|
11
|
+
import type { ShieldedWallet } from "./shield";
|
|
12
|
+
import type { ResolvedPolicies } from "./policies";
|
|
13
|
+
import type { ShieldState } from "./state";
|
|
14
|
+
/**
|
|
15
|
+
* x402 V2 PaymentRequired — decoded from PAYMENT-REQUIRED header.
|
|
16
|
+
* Mirrors @x402/core/types PaymentRequired.
|
|
17
|
+
*/
|
|
18
|
+
export interface PaymentRequired {
|
|
19
|
+
x402Version: number;
|
|
20
|
+
error?: string;
|
|
21
|
+
resource: ResourceInfo;
|
|
22
|
+
accepts: PaymentRequirements[];
|
|
23
|
+
extensions?: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
/** A single payment option offered by the server. */
|
|
26
|
+
export interface PaymentRequirements {
|
|
27
|
+
scheme: string;
|
|
28
|
+
network: string;
|
|
29
|
+
asset: string;
|
|
30
|
+
amount: string;
|
|
31
|
+
payTo: string;
|
|
32
|
+
maxTimeoutSeconds: number;
|
|
33
|
+
extra: Record<string, unknown>;
|
|
34
|
+
}
|
|
35
|
+
/** Resource metadata embedded in PaymentRequired. */
|
|
36
|
+
export interface ResourceInfo {
|
|
37
|
+
url: string;
|
|
38
|
+
description: string;
|
|
39
|
+
mimeType: string;
|
|
40
|
+
}
|
|
41
|
+
/** The signed payment payload sent back to the server. */
|
|
42
|
+
export interface PaymentPayload {
|
|
43
|
+
x402Version: number;
|
|
44
|
+
resource: ResourceInfo;
|
|
45
|
+
accepted: PaymentRequirements;
|
|
46
|
+
payload: Record<string, unknown>;
|
|
47
|
+
extensions?: Record<string, unknown>;
|
|
48
|
+
}
|
|
49
|
+
/** Settlement response decoded from PAYMENT-RESPONSE header. */
|
|
50
|
+
export interface SettleResponse {
|
|
51
|
+
success: boolean;
|
|
52
|
+
transaction?: string;
|
|
53
|
+
network?: string;
|
|
54
|
+
payer?: string;
|
|
55
|
+
errorReason?: string;
|
|
56
|
+
}
|
|
57
|
+
/** Options for shieldedFetch(). */
|
|
58
|
+
export interface ShieldedFetchOptions extends RequestInit {
|
|
59
|
+
/** Solana RPC connection for blockhash + ATA resolution. */
|
|
60
|
+
connection?: Connection;
|
|
61
|
+
/** If true, evaluate policies but don't pay. */
|
|
62
|
+
dryRun?: boolean;
|
|
63
|
+
/** Max payment in token base units — reject if server asks more. */
|
|
64
|
+
maxPayment?: string;
|
|
65
|
+
}
|
|
66
|
+
/** Extended response with x402 payment metadata. */
|
|
67
|
+
export interface ShieldedFetchResponse extends Response {
|
|
68
|
+
x402?: X402PaymentResult;
|
|
69
|
+
}
|
|
70
|
+
/** Payment metadata attached to a ShieldedFetchResponse. */
|
|
71
|
+
export interface X402PaymentResult {
|
|
72
|
+
paid: boolean;
|
|
73
|
+
amountPaid: string;
|
|
74
|
+
asset: string;
|
|
75
|
+
payTo: string;
|
|
76
|
+
settlement?: SettleResponse;
|
|
77
|
+
}
|
|
78
|
+
export declare class X402ParseError extends Error {
|
|
79
|
+
constructor(message: string);
|
|
80
|
+
}
|
|
81
|
+
export declare class X402PaymentError extends Error {
|
|
82
|
+
constructor(message: string);
|
|
83
|
+
}
|
|
84
|
+
export declare class X402UnsupportedError extends Error {
|
|
85
|
+
constructor(message: string);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Decode a base64-encoded PAYMENT-REQUIRED header value.
|
|
89
|
+
*/
|
|
90
|
+
export declare function decodePaymentRequiredHeader(header: string): PaymentRequired;
|
|
91
|
+
/**
|
|
92
|
+
* Encode a PaymentPayload as a base64 string for PAYMENT-SIGNATURE header.
|
|
93
|
+
*/
|
|
94
|
+
export declare function encodePaymentSignatureHeader(payload: PaymentPayload): string;
|
|
95
|
+
/**
|
|
96
|
+
* Decode a base64-encoded PAYMENT-RESPONSE header value.
|
|
97
|
+
*/
|
|
98
|
+
export declare function decodePaymentResponseHeader(header: string): SettleResponse;
|
|
99
|
+
/**
|
|
100
|
+
* Select a Solana-compatible payment option from the accepts array.
|
|
101
|
+
*
|
|
102
|
+
* @param paymentRequired The decoded PAYMENT-REQUIRED payload
|
|
103
|
+
* @param allowedTokens Optional set of token mint addresses to accept
|
|
104
|
+
* @returns The first matching PaymentRequirements, or throws
|
|
105
|
+
*/
|
|
106
|
+
export declare function selectPaymentOption(paymentRequired: PaymentRequired, allowedTokens?: Set<string>): PaymentRequirements;
|
|
107
|
+
/**
|
|
108
|
+
* Evaluate an x402 payment against shield policies without recording spend.
|
|
109
|
+
*
|
|
110
|
+
* Creates a synthetic TransactionAnalysis from the payment requirements
|
|
111
|
+
* and runs it through the existing policy engine.
|
|
112
|
+
*/
|
|
113
|
+
export declare function evaluateX402Payment(selected: PaymentRequirements, policies: ResolvedPolicies, state: ShieldState): import("./errors").PolicyViolation[];
|
|
114
|
+
/**
|
|
115
|
+
* Build an SPL TransferChecked instruction for an x402 payment.
|
|
116
|
+
*/
|
|
117
|
+
export declare function buildX402TransferInstruction(params: {
|
|
118
|
+
from: PublicKey;
|
|
119
|
+
payTo: PublicKey;
|
|
120
|
+
asset: PublicKey;
|
|
121
|
+
amount: bigint;
|
|
122
|
+
decimals: number;
|
|
123
|
+
}): TransactionInstruction;
|
|
124
|
+
/**
|
|
125
|
+
* Encode a signed transaction into a full x402 V2 PaymentPayload.
|
|
126
|
+
*/
|
|
127
|
+
export declare function encodeX402Payload(signedTx: Uint8Array, resource: ResourceInfo, accepted: PaymentRequirements): string;
|
|
128
|
+
/**
|
|
129
|
+
* Fetch a URL with automatic x402 payment support.
|
|
130
|
+
*
|
|
131
|
+
* Flow:
|
|
132
|
+
* 1. Make the initial HTTP request
|
|
133
|
+
* 2. If 402, parse PAYMENT-REQUIRED header
|
|
134
|
+
* 3. Select a Solana payment option
|
|
135
|
+
* 4. Evaluate against shield policies (fast deny)
|
|
136
|
+
* 5. Build, sign, and encode payment transaction
|
|
137
|
+
* 6. Retry with PAYMENT-SIGNATURE header
|
|
138
|
+
* 7. Return response with x402 metadata
|
|
139
|
+
*
|
|
140
|
+
* The client NEVER settles — the API server calls the facilitator.
|
|
141
|
+
*/
|
|
142
|
+
export declare function shieldedFetch(wallet: ShieldedWallet, url: string | URL, options?: ShieldedFetchOptions): Promise<ShieldedFetchResponse>;
|
|
143
|
+
/**
|
|
144
|
+
* Create a wallet-bound fetch function with automatic x402 payment support.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```typescript
|
|
148
|
+
* const fetch = createShieldedFetchForWallet(shieldedWallet, { connection });
|
|
149
|
+
* const res = await fetch('https://api.example.com/paid-endpoint');
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
export declare function createShieldedFetchForWallet(wallet: ShieldedWallet, defaults?: Omit<ShieldedFetchOptions, "body" | "method">): (url: string | URL, init?: RequestInit) => Promise<ShieldedFetchResponse>;
|
|
153
|
+
//# sourceMappingURL=x402.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"x402.d.ts","sourceRoot":"","sources":["../../src/wrapper/x402.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EACL,SAAS,EAET,sBAAsB,EACtB,UAAU,EACX,MAAM,iBAAiB,CAAC;AAMzB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAC/C,OAAO,KAAK,EAAE,gBAAgB,EAAuB,MAAM,YAAY,CAAC;AAGxE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAO3C;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,CAAC,EAAE,MAAM,CA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