@oobe-protocol-labs/synapse-sap-sdk 0.6.3 → 0.8.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/dist/cjs/constants/seeds.js +9 -0
- package/dist/cjs/constants/seeds.js.map +1 -1
- package/dist/cjs/core/client.js +55 -0
- package/dist/cjs/core/client.js.map +1 -1
- package/dist/cjs/idl/synapse_agent_sap.json +8571 -4106
- package/dist/cjs/index.js +29 -3
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/modules/escrow-v2.js +220 -0
- package/dist/cjs/modules/escrow-v2.js.map +1 -0
- package/dist/cjs/modules/escrow.js +4 -0
- package/dist/cjs/modules/escrow.js.map +1 -1
- package/dist/cjs/modules/index.js +9 -1
- package/dist/cjs/modules/index.js.map +1 -1
- package/dist/cjs/modules/receipt.js +144 -0
- package/dist/cjs/modules/receipt.js.map +1 -0
- package/dist/cjs/modules/staking.js +94 -0
- package/dist/cjs/modules/staking.js.map +1 -0
- package/dist/cjs/modules/subscription.js +96 -0
- package/dist/cjs/modules/subscription.js.map +1 -0
- package/dist/cjs/pda/index.js +166 -1
- package/dist/cjs/pda/index.js.map +1 -1
- package/dist/cjs/registries/x402.js +88 -51
- package/dist/cjs/registries/x402.js.map +1 -1
- package/dist/cjs/types/enums.js +96 -1
- package/dist/cjs/types/enums.js.map +1 -1
- package/dist/cjs/types/index.js +6 -1
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/types/instructions.js.map +1 -1
- package/dist/cjs/utils/escrow-validation.js +219 -0
- package/dist/cjs/utils/escrow-validation.js.map +1 -0
- package/dist/cjs/utils/index.js +12 -1
- package/dist/cjs/utils/index.js.map +1 -1
- package/dist/cjs/utils/merchant-validator.js +246 -0
- package/dist/cjs/utils/merchant-validator.js.map +1 -0
- package/dist/cjs/utils/x402-direct.js +231 -0
- package/dist/cjs/utils/x402-direct.js.map +1 -0
- package/dist/esm/constants/seeds.js +9 -0
- package/dist/esm/constants/seeds.js.map +1 -1
- package/dist/esm/core/client.js +55 -0
- package/dist/esm/core/client.js.map +1 -1
- package/dist/esm/idl/synapse_agent_sap.json +8571 -4106
- package/dist/esm/index.js +5 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/modules/escrow-v2.js +216 -0
- package/dist/esm/modules/escrow-v2.js.map +1 -0
- package/dist/esm/modules/escrow.js +4 -0
- package/dist/esm/modules/escrow.js.map +1 -1
- package/dist/esm/modules/index.js +4 -0
- package/dist/esm/modules/index.js.map +1 -1
- package/dist/esm/modules/receipt.js +140 -0
- package/dist/esm/modules/receipt.js.map +1 -0
- package/dist/esm/modules/staking.js +90 -0
- package/dist/esm/modules/staking.js.map +1 -0
- package/dist/esm/modules/subscription.js +92 -0
- package/dist/esm/modules/subscription.js.map +1 -0
- package/dist/esm/pda/index.js +157 -0
- package/dist/esm/pda/index.js.map +1 -1
- package/dist/esm/registries/x402.js +89 -52
- package/dist/esm/registries/x402.js.map +1 -1
- package/dist/esm/types/enums.js +95 -0
- package/dist/esm/types/enums.js.map +1 -1
- package/dist/esm/types/index.js +1 -1
- package/dist/esm/types/index.js.map +1 -1
- package/dist/esm/types/instructions.js.map +1 -1
- package/dist/esm/utils/escrow-validation.js +212 -0
- package/dist/esm/utils/escrow-validation.js.map +1 -0
- package/dist/esm/utils/index.js +4 -0
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/esm/utils/merchant-validator.js +241 -0
- package/dist/esm/utils/merchant-validator.js.map +1 -0
- package/dist/esm/utils/x402-direct.js +228 -0
- package/dist/esm/utils/x402-direct.js.map +1 -0
- package/dist/types/constants/seeds.d.ts +9 -0
- package/dist/types/constants/seeds.d.ts.map +1 -1
- package/dist/types/core/client.d.ts +43 -0
- package/dist/types/core/client.d.ts.map +1 -1
- package/dist/types/index.d.ts +6 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/modules/escrow-v2.d.ts +58 -0
- package/dist/types/modules/escrow-v2.d.ts.map +1 -0
- package/dist/types/modules/escrow.d.ts +4 -0
- package/dist/types/modules/escrow.d.ts.map +1 -1
- package/dist/types/modules/index.d.ts +4 -0
- package/dist/types/modules/index.d.ts.map +1 -1
- package/dist/types/modules/receipt.d.ts +77 -0
- package/dist/types/modules/receipt.d.ts.map +1 -0
- package/dist/types/modules/staking.d.ts +32 -0
- package/dist/types/modules/staking.d.ts.map +1 -0
- package/dist/types/modules/subscription.d.ts +33 -0
- package/dist/types/modules/subscription.d.ts.map +1 -0
- package/dist/types/pda/index.d.ts +114 -0
- package/dist/types/pda/index.d.ts.map +1 -1
- package/dist/types/plugin/schemas.d.ts +2 -2
- package/dist/types/registries/x402.d.ts +14 -12
- package/dist/types/registries/x402.d.ts.map +1 -1
- package/dist/types/types/accounts.d.ts +201 -1
- package/dist/types/types/accounts.d.ts.map +1 -1
- package/dist/types/types/enums.d.ts +115 -0
- package/dist/types/types/enums.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +4 -4
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/types/instructions.d.ts +34 -0
- package/dist/types/types/instructions.d.ts.map +1 -1
- package/dist/types/utils/escrow-validation.d.ts +145 -0
- package/dist/types/utils/escrow-validation.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +6 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/merchant-validator.d.ts +176 -0
- package/dist/types/utils/merchant-validator.d.ts.map +1 -0
- package/dist/types/utils/x402-direct.d.ts +114 -0
- package/dist/types/utils/x402-direct.d.ts.map +1 -0
- package/package.json +2 -2
- package/src/constants/seeds.ts +9 -0
- package/src/core/client.ts +59 -0
- package/src/idl/synapse_agent_sap.json +8571 -4106
- package/src/index.ts +54 -0
- package/src/modules/escrow-v2.ts +375 -0
- package/src/modules/escrow.ts +4 -0
- package/src/modules/index.ts +4 -0
- package/src/modules/receipt.ts +207 -0
- package/src/modules/staking.ts +122 -0
- package/src/modules/subscription.ts +147 -0
- package/src/pda/index.ts +228 -0
- package/src/registries/x402.ts +108 -69
- package/src/types/accounts.ts +241 -1
- package/src/types/enums.ts +119 -0
- package/src/types/index.ts +20 -0
- package/src/types/instructions.ts +40 -0
- package/src/utils/escrow-validation.ts +301 -0
- package/src/utils/index.ts +28 -0
- package/src/utils/merchant-validator.ts +359 -0
- package/src/utils/x402-direct.ts +370 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module utils/escrow-validation
|
|
4
|
+
* @description Server-side escrow validation pipeline.
|
|
5
|
+
*
|
|
6
|
+
* Provides typed helpers to validate escrow state before settlement
|
|
7
|
+
* and to build the correct SPL `AccountMeta[]` for token escrows.
|
|
8
|
+
*
|
|
9
|
+
* @category Utils
|
|
10
|
+
* @since v0.6.4
|
|
11
|
+
*/
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.MissingEscrowAtaError = void 0;
|
|
14
|
+
exports.validateEscrowState = validateEscrowState;
|
|
15
|
+
exports.attachSplAccounts = attachSplAccounts;
|
|
16
|
+
exports.toAccountMetas = toAccountMetas;
|
|
17
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
18
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
19
|
+
const rpc_strategy_1 = require("./rpc-strategy");
|
|
20
|
+
const pda_1 = require("../pda");
|
|
21
|
+
const errors_1 = require("../errors");
|
|
22
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
23
|
+
// Error
|
|
24
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
25
|
+
/**
|
|
26
|
+
* @name MissingEscrowAtaError
|
|
27
|
+
* @description Thrown when an SPL escrow operation is missing required
|
|
28
|
+
* Associated Token Accounts.
|
|
29
|
+
* @category Errors
|
|
30
|
+
* @since v0.6.4
|
|
31
|
+
*/
|
|
32
|
+
class MissingEscrowAtaError extends errors_1.SapError {
|
|
33
|
+
/** The ATA address that is missing. */
|
|
34
|
+
ataAddress;
|
|
35
|
+
/** Which side is missing: depositor or escrow. */
|
|
36
|
+
side;
|
|
37
|
+
constructor(ataAddress, side) {
|
|
38
|
+
super(`Missing ${side} ATA: ${ataAddress}. ` +
|
|
39
|
+
`Settlement mode is Escrow/SPL but the Associated Token Account does not exist. ` +
|
|
40
|
+
`The ${side} must create the ATA before escrow operations.`, "SAP_MISSING_ESCROW_ATA");
|
|
41
|
+
this.name = "MissingEscrowAtaError";
|
|
42
|
+
this.ataAddress = ataAddress;
|
|
43
|
+
this.side = side;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.MissingEscrowAtaError = MissingEscrowAtaError;
|
|
47
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
48
|
+
// Validation
|
|
49
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
50
|
+
/** Standard SPL Token program ID. */
|
|
51
|
+
const TOKEN_PROGRAM_ID_STR = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
|
|
52
|
+
/**
|
|
53
|
+
* @name validateEscrowState
|
|
54
|
+
* @description Validates that an escrow is in a correct state for settlement.
|
|
55
|
+
*
|
|
56
|
+
* Checks:
|
|
57
|
+
* - Escrow PDA exists on-chain
|
|
58
|
+
* - If SPL: depositor ATA exists, escrow ATA exists, token mint matches
|
|
59
|
+
* - Balance >= requested settlement amount
|
|
60
|
+
* - Escrow is not expired
|
|
61
|
+
* - Max calls not exceeded
|
|
62
|
+
*
|
|
63
|
+
* @param connection - Solana RPC connection.
|
|
64
|
+
* @param agentWallet - The agent's wallet public key.
|
|
65
|
+
* @param depositorWallet - The depositor's wallet public key.
|
|
66
|
+
* @param fetchEscrow - Callback to fetch escrow data (avoids coupling to SapProgram).
|
|
67
|
+
* @param opts
|
|
68
|
+
* @param opts.callsToSettle - Number of calls to validate affordability for.
|
|
69
|
+
*
|
|
70
|
+
* @returns A detailed {@link EscrowValidationResult}.
|
|
71
|
+
*
|
|
72
|
+
* @category Utils
|
|
73
|
+
* @since v0.6.4
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```ts
|
|
77
|
+
* const result = await validateEscrowState(
|
|
78
|
+
* connection,
|
|
79
|
+
* agentWallet,
|
|
80
|
+
* depositorWallet,
|
|
81
|
+
* (pda) => client.escrow.fetchByPda(pda),
|
|
82
|
+
* { callsToSettle: 5 },
|
|
83
|
+
* );
|
|
84
|
+
*
|
|
85
|
+
* if (!result.valid) {
|
|
86
|
+
* console.error("Escrow validation failed:", result.errors);
|
|
87
|
+
* }
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
async function validateEscrowState(connection, agentWallet, depositorWallet, fetchEscrow, opts) {
|
|
91
|
+
const [agentPda] = (0, pda_1.deriveAgent)(agentWallet);
|
|
92
|
+
const [escrowPda] = (0, pda_1.deriveEscrow)(agentPda, depositorWallet);
|
|
93
|
+
const errors = [];
|
|
94
|
+
// 1. Fetch escrow
|
|
95
|
+
const escrow = await fetchEscrow(escrowPda);
|
|
96
|
+
if (!escrow) {
|
|
97
|
+
return {
|
|
98
|
+
valid: false,
|
|
99
|
+
escrow: null,
|
|
100
|
+
escrowPda,
|
|
101
|
+
agentPda,
|
|
102
|
+
isSplEscrow: false,
|
|
103
|
+
splAccounts: [],
|
|
104
|
+
errors: [`Escrow not found at ${escrowPda.toBase58()}`],
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
// 2. Check expiry
|
|
108
|
+
if (escrow.expiresAt.gt(new anchor_1.BN(0))) {
|
|
109
|
+
const now = Math.floor(Date.now() / 1000);
|
|
110
|
+
if (escrow.expiresAt.lt(new anchor_1.BN(now))) {
|
|
111
|
+
errors.push(`Escrow expired at ${escrow.expiresAt.toString()}`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
// 3. Check balance / calls
|
|
115
|
+
const callsToSettle = opts?.callsToSettle ?? 1;
|
|
116
|
+
const costForCalls = escrow.pricePerCall.mul(new anchor_1.BN(callsToSettle));
|
|
117
|
+
if (escrow.balance.lt(costForCalls)) {
|
|
118
|
+
errors.push(`Insufficient balance: ${escrow.balance.toString()} < ${costForCalls.toString()} (${callsToSettle} calls × ${escrow.pricePerCall.toString()})`);
|
|
119
|
+
}
|
|
120
|
+
// 4. Check max calls
|
|
121
|
+
if (escrow.maxCalls.gt(new anchor_1.BN(0))) {
|
|
122
|
+
const remaining = escrow.maxCalls.sub(escrow.totalCallsSettled);
|
|
123
|
+
if (remaining.lt(new anchor_1.BN(callsToSettle))) {
|
|
124
|
+
errors.push(`Max calls exceeded: ${remaining.toString()} remaining but needs ${callsToSettle}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// 5. Determine if SPL
|
|
128
|
+
const isSplEscrow = escrow.tokenMint !== null &&
|
|
129
|
+
escrow.tokenMint.toBase58() !== "11111111111111111111111111111111";
|
|
130
|
+
// 6. Build SPL accounts (if SPL escrow)
|
|
131
|
+
const splAccounts = [];
|
|
132
|
+
if (isSplEscrow) {
|
|
133
|
+
const mint = escrow.tokenMint;
|
|
134
|
+
const depositorAta = (0, rpc_strategy_1.findATA)(depositorWallet, mint);
|
|
135
|
+
const escrowAta = (0, rpc_strategy_1.findATA)(escrowPda, mint);
|
|
136
|
+
// Verify depositor ATA exists
|
|
137
|
+
const depositorAtaInfo = await connection.getAccountInfo(depositorAta);
|
|
138
|
+
if (!depositorAtaInfo) {
|
|
139
|
+
errors.push(`Depositor ATA does not exist: ${depositorAta.toBase58()}`);
|
|
140
|
+
}
|
|
141
|
+
// Verify escrow ATA exists
|
|
142
|
+
const escrowAtaInfo = await connection.getAccountInfo(escrowAta);
|
|
143
|
+
if (!escrowAtaInfo) {
|
|
144
|
+
errors.push(`Escrow ATA does not exist: ${escrowAta.toBase58()}`);
|
|
145
|
+
}
|
|
146
|
+
// Verify mint matches
|
|
147
|
+
if (depositorAtaInfo) {
|
|
148
|
+
// SPL token account data: bytes 0-32 = mint
|
|
149
|
+
const ataMint = depositorAtaInfo.data.subarray(0, 32);
|
|
150
|
+
if (Buffer.from(ataMint).toString("hex") !== mint.toBuffer().toString("hex")) {
|
|
151
|
+
errors.push(`Depositor ATA mint mismatch: expected ${mint.toBase58()}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
splAccounts.push({ kind: "depositorAta", pubkey: depositorAta, writable: true }, { kind: "escrowAta", pubkey: escrowAta, writable: true }, { kind: "tokenMint", pubkey: mint, writable: false }, { kind: "tokenProgram", pubkey: new web3_js_1.PublicKey(TOKEN_PROGRAM_ID_STR), writable: false });
|
|
155
|
+
}
|
|
156
|
+
return {
|
|
157
|
+
valid: errors.length === 0,
|
|
158
|
+
escrow,
|
|
159
|
+
escrowPda,
|
|
160
|
+
agentPda,
|
|
161
|
+
isSplEscrow,
|
|
162
|
+
splAccounts,
|
|
163
|
+
errors,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* @name attachSplAccounts
|
|
168
|
+
* @description Build the typed `SplAccountMeta[]` for an SPL token escrow operation.
|
|
169
|
+
* Does NOT validate existence — use {@link validateEscrowState} for full validation.
|
|
170
|
+
*
|
|
171
|
+
* @param escrowPda - The escrow PDA address.
|
|
172
|
+
* @param depositorWallet - The depositor's wallet public key.
|
|
173
|
+
* @param tokenMint - The SPL token mint.
|
|
174
|
+
* @returns An array of typed {@link SplAccountMeta} for SPL escrow operations.
|
|
175
|
+
*
|
|
176
|
+
* @category Utils
|
|
177
|
+
* @since v0.6.4
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* const splMetas = attachSplAccounts(escrowPda, depositorWallet, usdcMint);
|
|
182
|
+
*
|
|
183
|
+
* // Convert to Anchor-compatible AccountMeta[]
|
|
184
|
+
* const accountMetas = splMetas.map(m => ({
|
|
185
|
+
* pubkey: m.pubkey,
|
|
186
|
+
* isWritable: m.writable,
|
|
187
|
+
* isSigner: false,
|
|
188
|
+
* }));
|
|
189
|
+
*
|
|
190
|
+
* await client.escrow.settle(depositor, calls, hash, accountMetas);
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
function attachSplAccounts(escrowPda, depositorWallet, tokenMint) {
|
|
194
|
+
return [
|
|
195
|
+
{ kind: "depositorAta", pubkey: (0, rpc_strategy_1.findATA)(depositorWallet, tokenMint), writable: true },
|
|
196
|
+
{ kind: "escrowAta", pubkey: (0, rpc_strategy_1.findATA)(escrowPda, tokenMint), writable: true },
|
|
197
|
+
{ kind: "tokenMint", pubkey: tokenMint, writable: false },
|
|
198
|
+
{ kind: "tokenProgram", pubkey: new web3_js_1.PublicKey(TOKEN_PROGRAM_ID_STR), writable: false },
|
|
199
|
+
];
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* @name toAccountMetas
|
|
203
|
+
* @description Convert typed {@link SplAccountMeta} to Anchor-compatible
|
|
204
|
+
* `AccountMeta[]` for use with `.remainingAccounts()`.
|
|
205
|
+
*
|
|
206
|
+
* @param splMetas - Array of typed SPL account metas.
|
|
207
|
+
* @returns `AccountMeta[]` compatible with Anchor's `remainingAccounts`.
|
|
208
|
+
*
|
|
209
|
+
* @category Utils
|
|
210
|
+
* @since v0.6.4
|
|
211
|
+
*/
|
|
212
|
+
function toAccountMetas(splMetas) {
|
|
213
|
+
return splMetas.map((m) => ({
|
|
214
|
+
pubkey: m.pubkey,
|
|
215
|
+
isWritable: m.writable,
|
|
216
|
+
isSigner: false,
|
|
217
|
+
}));
|
|
218
|
+
}
|
|
219
|
+
//# sourceMappingURL=escrow-validation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"escrow-validation.js","sourceRoot":"","sources":["../../../src/utils/escrow-validation.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;AAkIH,kDAsGC;AA6BD,8CAWC;AAaD,wCAMC;AAjSD,6CAIyB;AACzB,8CAAuC;AACvC,iDAAyC;AACzC,gCAAmD;AACnD,sCAAqC;AA6CrC,sEAAsE;AACtE,SAAS;AACT,sEAAsE;AAEtE;;;;;;GAMG;A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,QAA0B;IACvD,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QAC1B,MAAM,EAAE,CAAC,CAAC,MAAM;QAChB,UAAU,EAAE,CAAC,CAAC,QAAQ;QACtB,QAAQ,EAAE,KAAK;KAChB,CAAC,CAAC,CAAC;AACN,CAAC"}
|
package/dist/cjs/utils/index.js
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.DEFAULT_BATCH_SETTLE_COMPUTE_UNITS = exports.DEFAULT_SETTLE_COMPUTE_UNITS = exports.DEFAULT_SETTLE_PRIORITY_FEE = exports.FAST_BATCH_SETTLE_OPTIONS = exports.FAST_SETTLE_OPTIONS = exports.buildRpcOptions = exports.buildPriorityFeeIxs = exports.validateOrThrow = exports.createCallArgsSchema = exports.createRegisterAgentSchema = exports.createPreparePaymentSchema = exports.createAgentManifestSchema = exports.createToolManifestEntrySchema = exports.createHealthCheckSchema = exports.createEndpointDescriptorSchema = exports.createEnvSchema = exports.extractAnchorErrorCode = exports.classifyAnchorError = exports.findATA = exports.createDualConnection = exports.getFallbackRpcUrl = exports.getRpcUrl = exports.validateAgentEndpoints = exports.validateHealthCheck = exports.validateEndpointDescriptor = exports.validateEndpoint = exports.isKnownNetwork = exports.getNetworkClusterName = exports.getNetworkGenesisHash = exports.isNetworkEquivalent = exports.normalizeNetworkId = exports.serializeValue = exports.serializeAccount = exports.assert = exports.hashToArray = exports.sha256 = void 0;
|
|
18
|
+
exports.getX402DirectPayments = exports.parseX402Headers = exports.SapMerchantValidator = exports.MissingEscrowAtaError = exports.toAccountMetas = exports.attachSplAccounts = exports.validateEscrowState = exports.DEFAULT_BATCH_SETTLE_COMPUTE_UNITS = exports.DEFAULT_SETTLE_COMPUTE_UNITS = exports.DEFAULT_SETTLE_PRIORITY_FEE = exports.FAST_BATCH_SETTLE_OPTIONS = exports.FAST_SETTLE_OPTIONS = exports.buildRpcOptions = exports.buildPriorityFeeIxs = exports.validateOrThrow = exports.createCallArgsSchema = exports.createRegisterAgentSchema = exports.createPreparePaymentSchema = exports.createAgentManifestSchema = exports.createToolManifestEntrySchema = exports.createHealthCheckSchema = exports.createEndpointDescriptorSchema = exports.createEnvSchema = exports.extractAnchorErrorCode = exports.classifyAnchorError = exports.findATA = exports.createDualConnection = exports.getFallbackRpcUrl = exports.getRpcUrl = exports.validateAgentEndpoints = exports.validateHealthCheck = exports.validateEndpointDescriptor = exports.validateEndpoint = exports.isKnownNetwork = exports.getNetworkClusterName = exports.getNetworkGenesisHash = exports.isNetworkEquivalent = exports.normalizeNetworkId = exports.serializeValue = exports.serializeAccount = exports.assert = exports.hashToArray = exports.sha256 = void 0;
|
|
19
19
|
var hash_1 = require("./hash");
|
|
20
20
|
Object.defineProperty(exports, "sha256", { enumerable: true, get: function () { return hash_1.sha256; } });
|
|
21
21
|
Object.defineProperty(exports, "hashToArray", { enumerable: true, get: function () { return hash_1.hashToArray; } });
|
|
@@ -62,4 +62,15 @@ Object.defineProperty(exports, "FAST_BATCH_SETTLE_OPTIONS", { enumerable: true,
|
|
|
62
62
|
Object.defineProperty(exports, "DEFAULT_SETTLE_PRIORITY_FEE", { enumerable: true, get: function () { return priority_fee_1.DEFAULT_SETTLE_PRIORITY_FEE; } });
|
|
63
63
|
Object.defineProperty(exports, "DEFAULT_SETTLE_COMPUTE_UNITS", { enumerable: true, get: function () { return priority_fee_1.DEFAULT_SETTLE_COMPUTE_UNITS; } });
|
|
64
64
|
Object.defineProperty(exports, "DEFAULT_BATCH_SETTLE_COMPUTE_UNITS", { enumerable: true, get: function () { return priority_fee_1.DEFAULT_BATCH_SETTLE_COMPUTE_UNITS; } });
|
|
65
|
+
// ── v0.6.4 Escrow Validation & Merchant Middleware ──
|
|
66
|
+
var escrow_validation_1 = require("./escrow-validation");
|
|
67
|
+
Object.defineProperty(exports, "validateEscrowState", { enumerable: true, get: function () { return escrow_validation_1.validateEscrowState; } });
|
|
68
|
+
Object.defineProperty(exports, "attachSplAccounts", { enumerable: true, get: function () { return escrow_validation_1.attachSplAccounts; } });
|
|
69
|
+
Object.defineProperty(exports, "toAccountMetas", { enumerable: true, get: function () { return escrow_validation_1.toAccountMetas; } });
|
|
70
|
+
Object.defineProperty(exports, "MissingEscrowAtaError", { enumerable: true, get: function () { return escrow_validation_1.MissingEscrowAtaError; } });
|
|
71
|
+
var merchant_validator_1 = require("./merchant-validator");
|
|
72
|
+
Object.defineProperty(exports, "SapMerchantValidator", { enumerable: true, get: function () { return merchant_validator_1.SapMerchantValidator; } });
|
|
73
|
+
Object.defineProperty(exports, "parseX402Headers", { enumerable: true, get: function () { return merchant_validator_1.parseX402Headers; } });
|
|
74
|
+
var x402_direct_1 = require("./x402-direct");
|
|
75
|
+
Object.defineProperty(exports, "getX402DirectPayments", { enumerable: true, get: function () { return x402_direct_1.getX402DirectPayments; } });
|
|
65
76
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,+BAA6C;AAApC,8FAAA,MAAM,OAAA;AAAE,mGAAA,WAAW,OAAA;AAC5B,2CAAsC;AAA7B,oGAAA,MAAM,OAAA;AACf,iDAAmE;AAA1D,iHAAA,gBAAgB,OAAA;AAAE,+GAAA,cAAc,OAAA;AAEzC,uDAAuD;AACvD,2DAM8B;AAL5B,wHAAA,kBAAkB,OAAA;AAClB,yHAAA,mBAAmB,OAAA;AACnB,2HAAA,qBAAqB,OAAA;AACrB,2HAAA,qBAAqB,OAAA;AACrB,oHAAA,cAAc,OAAA;AAGhB,2DAK8B;AAJ5B,sHAAA,gBAAgB,OAAA;AAChB,gIAAA,0BAA0B,OAAA;AAC1B,yHAAA,mBAAmB,OAAA;AACnB,4HAAA,sBAAsB,OAAA;AA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AAA;AAIxB,+CAOwB;AANtB,yGAAA,SAAS,OAAA;AACT,iHAAA,iBAAiB,OAAA;AACjB,oHAAA,oBAAoB,OAAA;AACpB,uGAAA,OAAO,OAAA;AACP,mHAAA,mBAAmB,OAAA;AACnB,sHAAA,sBAAsB,OAAA;AAIxB,qCAUmB;AATjB,0GAAA,eAAe,OAAA;AACf,yHAAA,8BAA8B,OAAA;AAC9B,kHAAA,uBAAuB,OAAA;AACvB,wHAAA,6BAA6B,OAAA;AAC7B,oHAAA,yBAAyB,OAAA;AACzB,qHAAA,0BAA0B,OAAA;AAC1B,oHAAA,yBAAyB,OAAA;AACzB,+GAAA,oBAAoB,OAAA;AACpB,0GAAA,eAAe,OAAA;AAGjB,uDAAuD;AACvD,+CAQwB;AAPtB,mHAAA,mBAAmB,OAAA;AACnB,+GAAA,eAAe,OAAA;AACf,mHAAA,mBAAmB,OAAA;AACnB,yHAAA,yBAAyB,OAAA;AACzB,2HAAA,2BAA2B,OAAA;AAC3B,4HAAA,4BAA4B,OAAA;AAC5B,kIAAA,kCAAkC,OAAA;AAOpC,wDAAwD;AACxD,yDAK6B;AAJ3B,wHAAA,mBAAmB,OAAA;AACnB,sHAAA,iBAAiB,OAAA;AACjB,mHAAA,cAAc,OAAA;AACd,0HAAA,qBAAqB,OAAA;AAOvB,2DAG8B;AAF5B,0HAAA,oBAAoB,OAAA;AACpB,sHAAA,gBAAgB,OAAA;AAOlB,6CAAsD;AAA7C,oHAAA,qBAAqB,OAAA"}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module utils/merchant-validator
|
|
4
|
+
* @description Standard Synapse merchant middleware for x402 settlement.
|
|
5
|
+
*
|
|
6
|
+
* Reads `X-Payment-*` headers from incoming HTTP requests, validates
|
|
7
|
+
* the escrow on-chain, auto-generates the correct `AccountMeta[]`,
|
|
8
|
+
* and throws explicit errors (e.g. {@link MissingEscrowAtaError})
|
|
9
|
+
* instead of letting the program return a generic crash.
|
|
10
|
+
*
|
|
11
|
+
* Designed for agents like Syra/Invoica that receive x402 payments.
|
|
12
|
+
*
|
|
13
|
+
* @category Utils
|
|
14
|
+
* @since v0.6.4
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.SapMerchantValidator = void 0;
|
|
18
|
+
exports.parseX402Headers = parseX402Headers;
|
|
19
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
20
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
21
|
+
const escrow_validation_1 = require("./escrow-validation");
|
|
22
|
+
const errors_1 = require("../errors");
|
|
23
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
24
|
+
// Header parsing
|
|
25
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
26
|
+
/** Required x402 headers. */
|
|
27
|
+
const REQUIRED_HEADERS = [
|
|
28
|
+
"X-Payment-Protocol",
|
|
29
|
+
"X-Payment-Escrow",
|
|
30
|
+
"X-Payment-Agent",
|
|
31
|
+
"X-Payment-Depositor",
|
|
32
|
+
"X-Payment-MaxCalls",
|
|
33
|
+
"X-Payment-PricePerCall",
|
|
34
|
+
"X-Payment-Program",
|
|
35
|
+
"X-Payment-Network",
|
|
36
|
+
];
|
|
37
|
+
/**
|
|
38
|
+
* @name parseX402Headers
|
|
39
|
+
* @description Parse and validate x402 headers from an HTTP request.
|
|
40
|
+
*
|
|
41
|
+
* @param headers - HTTP headers object (case-insensitive key lookup).
|
|
42
|
+
* @returns Parsed x402 headers.
|
|
43
|
+
* @throws {SapValidationError} If required headers are missing or malformed.
|
|
44
|
+
*
|
|
45
|
+
* @category Utils
|
|
46
|
+
* @since v0.6.4
|
|
47
|
+
*/
|
|
48
|
+
function parseX402Headers(headers) {
|
|
49
|
+
// Normalize to case-insensitive
|
|
50
|
+
const normalized = new Map();
|
|
51
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
52
|
+
const val = Array.isArray(value) ? value[0] : value;
|
|
53
|
+
if (val !== undefined) {
|
|
54
|
+
normalized.set(key.toLowerCase(), val);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// Validate required headers present
|
|
58
|
+
const missing = [];
|
|
59
|
+
for (const h of REQUIRED_HEADERS) {
|
|
60
|
+
if (!normalized.has(h.toLowerCase())) {
|
|
61
|
+
missing.push(h);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
if (missing.length > 0) {
|
|
65
|
+
throw new errors_1.SapValidationError(`Missing required x402 headers: ${missing.join(", ")}`, "x402-headers");
|
|
66
|
+
}
|
|
67
|
+
const get = (key) => normalized.get(key.toLowerCase());
|
|
68
|
+
// Validate protocol
|
|
69
|
+
const protocol = get("X-Payment-Protocol");
|
|
70
|
+
if (protocol !== "SAP-x402") {
|
|
71
|
+
throw new errors_1.SapValidationError(`Invalid X-Payment-Protocol: "${protocol}" (expected "SAP-x402")`, "X-Payment-Protocol");
|
|
72
|
+
}
|
|
73
|
+
// Parse PublicKeys
|
|
74
|
+
let escrowPda;
|
|
75
|
+
let agentPda;
|
|
76
|
+
let depositorWallet;
|
|
77
|
+
let programId;
|
|
78
|
+
try {
|
|
79
|
+
escrowPda = new web3_js_1.PublicKey(get("X-Payment-Escrow"));
|
|
80
|
+
agentPda = new web3_js_1.PublicKey(get("X-Payment-Agent"));
|
|
81
|
+
depositorWallet = new web3_js_1.PublicKey(get("X-Payment-Depositor"));
|
|
82
|
+
programId = new web3_js_1.PublicKey(get("X-Payment-Program"));
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
throw new errors_1.SapValidationError("Malformed public key in x402 headers", "x402-headers");
|
|
86
|
+
}
|
|
87
|
+
// Parse numeric values
|
|
88
|
+
const maxCallsStr = get("X-Payment-MaxCalls");
|
|
89
|
+
const pricePerCallStr = get("X-Payment-PricePerCall");
|
|
90
|
+
let maxCalls;
|
|
91
|
+
let pricePerCall;
|
|
92
|
+
try {
|
|
93
|
+
maxCalls = new anchor_1.BN(maxCallsStr);
|
|
94
|
+
pricePerCall = new anchor_1.BN(pricePerCallStr);
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
throw new errors_1.SapValidationError("Invalid numeric value in X-Payment-MaxCalls or X-Payment-PricePerCall", "x402-headers");
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
protocol,
|
|
101
|
+
escrowPda,
|
|
102
|
+
agentPda,
|
|
103
|
+
depositorWallet,
|
|
104
|
+
maxCalls,
|
|
105
|
+
pricePerCall,
|
|
106
|
+
programId,
|
|
107
|
+
network: get("X-Payment-Network"),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
111
|
+
// Merchant Validator
|
|
112
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
113
|
+
/**
|
|
114
|
+
* @name SapMerchantValidator
|
|
115
|
+
* @description Standard Synapse merchant middleware for x402 payment validation.
|
|
116
|
+
*
|
|
117
|
+
* Reads `X-Payment-*` headers, validates escrow state on-chain, generates
|
|
118
|
+
* correct `AccountMeta[]` for SPL token escrows, and throws explicit errors
|
|
119
|
+
* (e.g. {@link MissingEscrowAtaError}) when ATA accounts are missing.
|
|
120
|
+
*
|
|
121
|
+
* @category Utils
|
|
122
|
+
* @since v0.6.4
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* const validator = new SapMerchantValidator(connection, fetchEscrow);
|
|
127
|
+
*
|
|
128
|
+
* // Express.js integration
|
|
129
|
+
* app.post("/api/v1/chat", async (req, res) => {
|
|
130
|
+
* try {
|
|
131
|
+
* const validation = await validator.validateRequest(req.headers, {
|
|
132
|
+
* callsToSettle: 1,
|
|
133
|
+
* });
|
|
134
|
+
*
|
|
135
|
+
* if (!validation.valid) {
|
|
136
|
+
* return res.status(402).json({ errors: validation.errors });
|
|
137
|
+
* }
|
|
138
|
+
*
|
|
139
|
+
* // Process request...
|
|
140
|
+
*
|
|
141
|
+
* // Settle payment using pre-built account metas
|
|
142
|
+
* await client.escrow.settle(
|
|
143
|
+
* validation.headers.depositorWallet,
|
|
144
|
+
* 1,
|
|
145
|
+
* serviceHash,
|
|
146
|
+
* validation.accountMetas,
|
|
147
|
+
* );
|
|
148
|
+
*
|
|
149
|
+
* res.json({ result: "..." });
|
|
150
|
+
* } catch (err) {
|
|
151
|
+
* if (err instanceof MissingEscrowAtaError) {
|
|
152
|
+
* return res.status(402).json({
|
|
153
|
+
* error: err.message,
|
|
154
|
+
* side: err.side,
|
|
155
|
+
* ata: err.ataAddress,
|
|
156
|
+
* });
|
|
157
|
+
* }
|
|
158
|
+
* throw err;
|
|
159
|
+
* }
|
|
160
|
+
* });
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
class SapMerchantValidator {
|
|
164
|
+
connection;
|
|
165
|
+
fetchEscrow;
|
|
166
|
+
/**
|
|
167
|
+
* @param connection - Solana RPC connection.
|
|
168
|
+
* @param fetchEscrow - Callback to fetch escrow account data by PDA.
|
|
169
|
+
* Typically `(pda) => client.escrow.fetchByPda(pda).catch(() => null)`.
|
|
170
|
+
*/
|
|
171
|
+
constructor(connection, fetchEscrow) {
|
|
172
|
+
this.connection = connection;
|
|
173
|
+
this.fetchEscrow = fetchEscrow;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* @name validateRequest
|
|
177
|
+
* @description Full validation pipeline for an incoming x402 request.
|
|
178
|
+
*
|
|
179
|
+
* Steps:
|
|
180
|
+
* 1. Parse `X-Payment-*` headers
|
|
181
|
+
* 2. Fetch escrow on-chain
|
|
182
|
+
* 3. Validate escrow state (balance, expiry, max calls)
|
|
183
|
+
* 4. If SPL escrow: validate ATAs exist and mint matches
|
|
184
|
+
* 5. Build `AccountMeta[]` for settlement TX
|
|
185
|
+
*
|
|
186
|
+
* @param headers - HTTP headers from the incoming request.
|
|
187
|
+
* @param opts
|
|
188
|
+
* @param opts.callsToSettle - Number of calls to validate affordability for (default: 1).
|
|
189
|
+
* @param opts.throwOnMissingAta - Throw {@link MissingEscrowAtaError} instead of returning errors (default: true).
|
|
190
|
+
*
|
|
191
|
+
* @returns A complete {@link MerchantValidationResult}.
|
|
192
|
+
*
|
|
193
|
+
* @throws {SapValidationError} If headers are missing or malformed.
|
|
194
|
+
* @throws {MissingEscrowAtaError} If SPL ATAs are missing and `throwOnMissingAta` is true.
|
|
195
|
+
*
|
|
196
|
+
* @category Utils
|
|
197
|
+
* @since v0.6.4
|
|
198
|
+
*/
|
|
199
|
+
async validateRequest(headers, opts) {
|
|
200
|
+
// 1. Parse headers
|
|
201
|
+
const parsed = parseX402Headers(headers);
|
|
202
|
+
// 2. Validate escrow state
|
|
203
|
+
const escrowValidation = await (0, escrow_validation_1.validateEscrowState)(this.connection, parsed.agentPda, // agentWallet derived inside validate
|
|
204
|
+
parsed.depositorWallet, this.fetchEscrow, { callsToSettle: opts?.callsToSettle ?? 1 });
|
|
205
|
+
// 3. Check for ATA errors and optionally throw
|
|
206
|
+
const throwOnMissingAta = opts?.throwOnMissingAta !== false;
|
|
207
|
+
if (throwOnMissingAta && escrowValidation.isSplEscrow) {
|
|
208
|
+
for (const error of escrowValidation.errors) {
|
|
209
|
+
if (error.includes("Depositor ATA does not exist")) {
|
|
210
|
+
const ataAddr = error.split(": ")[1] ?? "unknown";
|
|
211
|
+
throw new escrow_validation_1.MissingEscrowAtaError(ataAddr, "depositor");
|
|
212
|
+
}
|
|
213
|
+
if (error.includes("Escrow ATA does not exist")) {
|
|
214
|
+
const ataAddr = error.split(": ")[1] ?? "unknown";
|
|
215
|
+
throw new escrow_validation_1.MissingEscrowAtaError(ataAddr, "escrow");
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
// 4. Build account metas
|
|
220
|
+
const accountMetas = (0, escrow_validation_1.toAccountMetas)(escrowValidation.splAccounts);
|
|
221
|
+
return {
|
|
222
|
+
valid: escrowValidation.valid,
|
|
223
|
+
headers: parsed,
|
|
224
|
+
escrowValidation,
|
|
225
|
+
accountMetas,
|
|
226
|
+
errors: escrowValidation.errors,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* @name validateEscrow
|
|
231
|
+
* @description Validate escrow from pre-parsed headers (convenience method).
|
|
232
|
+
* Call this when you've already parsed the headers yourself.
|
|
233
|
+
*
|
|
234
|
+
* @param headers - Pre-parsed x402 headers.
|
|
235
|
+
* @param opts
|
|
236
|
+
* @returns The escrow validation result with pre-built account metas.
|
|
237
|
+
*
|
|
238
|
+
* @category Utils
|
|
239
|
+
* @since v0.6.4
|
|
240
|
+
*/
|
|
241
|
+
async validateEscrow(headers, opts) {
|
|
242
|
+
return (0, escrow_validation_1.validateEscrowState)(this.connection, headers.agentPda, headers.depositorWallet, this.fetchEscrow, opts);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
exports.SapMerchantValidator = SapMerchantValidator;
|
|
246
|
+
//# sourceMappingURL=merchant-validator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"merchant-validator.js","sourceRoot":"","sources":["../../../src/utils/merchant-validator.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;GAaG;;;AA+FH,4CA+EC;AAvKD,6CAAkD;AAClD,8CAAuC;AACvC,2DAI6B;AAI7B,sCAA+C;AAmD/C,sEAAsE;AACtE,kBAAkB;AAClB,sEAAsE;AAEtE,6BAA6B;AAC7B,MAAM,gBAAgB,GAAG;IACvB,oBAAoB;IACpB,kBAAkB;IAClB,iBAAiB;IACjB,qBAAqB;IACrB,oBAAoB;IACpB,wBAAwB;IACxB,mBAAmB;IACnB,mBAAmB;CACX,CAAC;AAEX;;;;;;;;;;GAUG;AACH,SAAgB,gBAAgB,CAC9B,OAAsD;IAEtD,gCAAgC;IAChC,MAAM,UAAU,GAAG,IAAI,GAAG,EAAkB,CAAC;IAC7C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CA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