x402-svm-mantle 2.1.1-mantle
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/README.md +200 -0
- package/dist/cjs/exact/client/index.d.ts +37 -0
- package/dist/cjs/exact/client/index.js +281 -0
- package/dist/cjs/exact/client/index.js.map +1 -0
- package/dist/cjs/exact/facilitator/index.d.ts +110 -0
- package/dist/cjs/exact/facilitator/index.js +714 -0
- package/dist/cjs/exact/facilitator/index.js.map +1 -0
- package/dist/cjs/exact/server/index.d.ts +87 -0
- package/dist/cjs/exact/server/index.js +209 -0
- package/dist/cjs/exact/server/index.js.map +1 -0
- package/dist/cjs/exact/v1/client/index.d.ts +33 -0
- package/dist/cjs/exact/v1/client/index.js +169 -0
- package/dist/cjs/exact/v1/client/index.js.map +1 -0
- package/dist/cjs/exact/v1/facilitator/index.d.ts +71 -0
- package/dist/cjs/exact/v1/facilitator/index.js +384 -0
- package/dist/cjs/exact/v1/facilitator/index.js.map +1 -0
- package/dist/cjs/index.d.ts +118 -0
- package/dist/cjs/index.js +433 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/scheme-CkL_b_xl.d.ts +29 -0
- package/dist/cjs/scheme-MoBRXFM8.d.ts +29 -0
- package/dist/cjs/signer-BMkbhFYE.d.ts +123 -0
- package/dist/cjs/v1/index.d.ts +12 -0
- package/dist/cjs/v1/index.js +180 -0
- package/dist/cjs/v1/index.js.map +1 -0
- package/dist/esm/chunk-M7NDDMX7.mjs +111 -0
- package/dist/esm/chunk-M7NDDMX7.mjs.map +1 -0
- package/dist/esm/chunk-PHFXLBH6.mjs +157 -0
- package/dist/esm/chunk-PHFXLBH6.mjs.map +1 -0
- package/dist/esm/chunk-TA6OPUHK.mjs +108 -0
- package/dist/esm/chunk-TA6OPUHK.mjs.map +1 -0
- package/dist/esm/chunk-WRDCPYFH.mjs +339 -0
- package/dist/esm/chunk-WRDCPYFH.mjs.map +1 -0
- package/dist/esm/chunk-WWACQNRQ.mjs +7 -0
- package/dist/esm/chunk-WWACQNRQ.mjs.map +1 -0
- package/dist/esm/exact/client/index.d.mts +37 -0
- package/dist/esm/exact/client/index.mjs +36 -0
- package/dist/esm/exact/client/index.mjs.map +1 -0
- package/dist/esm/exact/facilitator/index.d.mts +110 -0
- package/dist/esm/exact/facilitator/index.mjs +350 -0
- package/dist/esm/exact/facilitator/index.mjs.map +1 -0
- package/dist/esm/exact/server/index.d.mts +87 -0
- package/dist/esm/exact/server/index.mjs +129 -0
- package/dist/esm/exact/server/index.mjs.map +1 -0
- package/dist/esm/exact/v1/client/index.d.mts +33 -0
- package/dist/esm/exact/v1/client/index.mjs +8 -0
- package/dist/esm/exact/v1/client/index.mjs.map +1 -0
- package/dist/esm/exact/v1/facilitator/index.d.mts +71 -0
- package/dist/esm/exact/v1/facilitator/index.mjs +8 -0
- package/dist/esm/exact/v1/facilitator/index.mjs.map +1 -0
- package/dist/esm/index.d.mts +118 -0
- package/dist/esm/index.mjs +198 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/scheme-VFikrqtD.d.mts +29 -0
- package/dist/esm/signer-BMkbhFYE.d.mts +123 -0
- package/dist/esm/v1/index.d.mts +12 -0
- package/dist/esm/v1/index.mjs +13 -0
- package/dist/esm/v1/index.mjs.map +1 -0
- package/package.json +130 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/exact/v1/facilitator/scheme.ts"],"sourcesContent":["import {\n COMPUTE_BUDGET_PROGRAM_ADDRESS,\n parseSetComputeUnitLimitInstruction,\n parseSetComputeUnitPriceInstruction,\n} from \"@solana-program/compute-budget\";\nimport {\n parseTransferCheckedInstruction as parseTransferCheckedInstructionToken,\n TOKEN_PROGRAM_ADDRESS,\n} from \"@solana-program/token\";\nimport {\n findAssociatedTokenPda,\n parseTransferCheckedInstruction as parseTransferCheckedInstruction2022,\n TOKEN_2022_PROGRAM_ADDRESS,\n} from \"@solana-program/token-2022\";\nimport {\n decompileTransactionMessage,\n getCompiledTransactionMessageDecoder,\n type Address,\n} from \"@solana/kit\";\nimport type {\n PaymentPayload,\n PaymentRequirements,\n SchemeNetworkFacilitator,\n SettleResponse,\n VerifyResponse,\n} from \"x402-core-mantle/types\";\nimport type { PaymentPayloadV1, PaymentRequirementsV1 } from \"x402-core-mantle/types/v1\";\nimport { MAX_COMPUTE_UNIT_PRICE_MICROLAMPORTS } from \"../../../constants\";\nimport type { FacilitatorSvmSigner } from \"../../../signer\";\nimport type { ExactSvmPayloadV1 } from \"../../../types\";\nimport { decodeTransactionFromPayload, getTokenPayerFromTransaction } from \"../../../utils\";\n\n/**\n * SVM facilitator implementation for the Exact payment scheme (V1).\n */\nexport class ExactSvmSchemeV1 implements SchemeNetworkFacilitator {\n readonly scheme = \"exact\";\n readonly caipFamily = \"solana:*\";\n\n /**\n * Creates a new ExactSvmFacilitatorV1 instance.\n *\n * @param signer - The SVM RPC client for facilitator operations\n * @returns ExactSvmFacilitatorV1 instance\n */\n constructor(private readonly signer: FacilitatorSvmSigner) {}\n\n /**\n * Get mechanism-specific extra data for the supported kinds endpoint.\n * For SVM, this includes a randomly selected fee payer address.\n * Random selection distributes load across multiple signers.\n *\n * @param _ - The network identifier (unused for SVM)\n * @returns Extra data with feePayer address\n */\n getExtra(_: string): Record<string, unknown> | undefined {\n // Randomly select from available signers to distribute load\n const addresses = this.signer.getAddresses();\n const randomIndex = Math.floor(Math.random() * addresses.length);\n\n return {\n feePayer: addresses[randomIndex],\n };\n }\n\n /**\n * Get signer addresses used by this facilitator.\n * For SVM, returns all available fee payer addresses.\n *\n * @param _ - The network identifier (unused for SVM)\n * @returns Array of fee payer addresses\n */\n getSigners(_: string): string[] {\n return [...this.signer.getAddresses()];\n }\n\n /**\n * Verifies a payment payload (V1).\n *\n * @param payload - The payment payload to verify\n * @param requirements - The payment requirements\n * @returns Promise resolving to verification response\n */\n async verify(\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<VerifyResponse> {\n const requirementsV1 = requirements as unknown as PaymentRequirementsV1;\n const payloadV1 = payload as unknown as PaymentPayloadV1;\n const exactSvmPayload = payload.payload as ExactSvmPayloadV1;\n\n // Step 1: Validate Payment Requirements\n if (payloadV1.scheme !== \"exact\" || requirements.scheme !== \"exact\") {\n return {\n isValid: false,\n invalidReason: \"unsupported_scheme\",\n payer: \"\",\n };\n }\n\n if (payloadV1.network !== requirements.network) {\n return {\n isValid: false,\n invalidReason: \"network_mismatch\",\n payer: \"\",\n };\n }\n\n if (!requirementsV1.extra?.feePayer || typeof requirementsV1.extra.feePayer !== \"string\") {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_missing_fee_payer\",\n payer: \"\",\n };\n }\n\n // Verify that the requested feePayer is managed by this facilitator\n const signerAddresses = this.signer.getAddresses().map(addr => addr.toString());\n if (!signerAddresses.includes(requirementsV1.extra.feePayer)) {\n return {\n isValid: false,\n invalidReason: \"fee_payer_not_managed_by_facilitator\",\n payer: \"\",\n };\n }\n\n // Step 2: Parse and Validate Transaction Structure\n let transaction;\n try {\n transaction = decodeTransactionFromPayload(exactSvmPayload);\n } catch {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_transaction_could_not_be_decoded\",\n payer: \"\",\n };\n }\n\n const compiled = getCompiledTransactionMessageDecoder().decode(transaction.messageBytes);\n const decompiled = decompileTransactionMessage(compiled);\n const instructions = decompiled.instructions ?? [];\n\n // 3 instructions: ComputeLimit + ComputePrice + TransferChecked\n if (instructions.length !== 3) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_transaction_instructions_length\",\n payer: \"\",\n };\n }\n\n // Step 3: Verify Compute Budget Instructions\n try {\n this.verifyComputeLimitInstruction(instructions[0] as never);\n this.verifyComputePriceInstruction(instructions[1] as never);\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n return {\n isValid: false,\n invalidReason: errorMessage,\n payer: \"\",\n };\n }\n\n const payer = getTokenPayerFromTransaction(transaction);\n if (!payer) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_no_transfer_instruction\",\n payer: \"\",\n };\n }\n\n // Step 4: Verify Transfer Instruction\n const transferIx = instructions[2];\n const programAddress = transferIx.programAddress.toString();\n\n if (\n programAddress !== TOKEN_PROGRAM_ADDRESS.toString() &&\n programAddress !== TOKEN_2022_PROGRAM_ADDRESS.toString()\n ) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_no_transfer_instruction\",\n payer,\n };\n }\n\n // Parse the transfer instruction using the appropriate library helper\n let parsedTransfer;\n try {\n if (programAddress === TOKEN_PROGRAM_ADDRESS.toString()) {\n parsedTransfer = parseTransferCheckedInstructionToken(transferIx as never);\n } else {\n parsedTransfer = parseTransferCheckedInstruction2022(transferIx as never);\n }\n } catch {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_no_transfer_instruction\",\n payer,\n };\n }\n\n // Verify that the facilitator's signers are not transferring their own funds\n // SECURITY: Prevent facilitator from signing away their own tokens\n const authorityAddress = parsedTransfer.accounts.authority.address.toString();\n if (signerAddresses.includes(authorityAddress)) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_transaction_fee_payer_transferring_funds\",\n payer,\n };\n }\n\n // Verify mint address matches requirements\n const mintAddress = parsedTransfer.accounts.mint.address.toString();\n if (mintAddress !== requirements.asset) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_mint_mismatch\",\n payer,\n };\n }\n\n // Verify destination ATA matches expected ATA for payTo address\n const destATA = parsedTransfer.accounts.destination.address.toString();\n try {\n const [expectedDestATA] = await findAssociatedTokenPda({\n mint: requirements.asset as Address,\n owner: requirements.payTo as Address,\n tokenProgram:\n programAddress === TOKEN_PROGRAM_ADDRESS.toString()\n ? (TOKEN_PROGRAM_ADDRESS as Address)\n : (TOKEN_2022_PROGRAM_ADDRESS as Address),\n });\n\n if (destATA !== expectedDestATA.toString()) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_recipient_mismatch\",\n payer,\n };\n }\n } catch {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_recipient_mismatch\",\n payer,\n };\n }\n\n // Verify transfer amount meets requirements\n const amount = parsedTransfer.data.amount;\n if (amount < BigInt(requirementsV1.maxAmountRequired)) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_amount_insufficient\",\n payer,\n };\n }\n\n // Step 5: Sign and Simulate Transaction\n // CRITICAL: Simulation proves transaction will succeed (catches insufficient balance, invalid accounts, etc)\n try {\n const feePayer = requirementsV1.extra.feePayer as Address;\n\n // Sign transaction with the feePayer's signer\n const fullySignedTransaction = await this.signer.signTransaction(\n exactSvmPayload.transaction,\n feePayer,\n requirements.network,\n );\n\n // Simulate to verify transaction would succeed\n await this.signer.simulateTransaction(fullySignedTransaction, requirements.network);\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n return {\n isValid: false,\n invalidReason: `transaction_simulation_failed: ${errorMessage}`,\n payer,\n };\n }\n\n return {\n isValid: true,\n invalidReason: undefined,\n payer,\n };\n }\n\n /**\n * Settles a payment by submitting the transaction (V1).\n * Ensures the correct signer is used based on the feePayer specified in requirements.\n *\n * @param payload - The payment payload to settle\n * @param requirements - The payment requirements\n * @returns Promise resolving to settlement response\n */\n async settle(\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<SettleResponse> {\n const payloadV1 = payload as unknown as PaymentPayloadV1;\n const exactSvmPayload = payload.payload as ExactSvmPayloadV1;\n\n const valid = await this.verify(payload, requirements);\n if (!valid.isValid) {\n return {\n success: false,\n network: payloadV1.network,\n transaction: \"\",\n errorReason: valid.invalidReason ?? \"verification_failed\",\n payer: valid.payer || \"\",\n };\n }\n\n try {\n // Extract feePayer from requirements (already validated in verify)\n const feePayer = requirements.extra.feePayer as Address;\n\n // Sign transaction with the feePayer's signer\n const fullySignedTransaction = await this.signer.signTransaction(\n exactSvmPayload.transaction,\n feePayer,\n requirements.network,\n );\n\n // Send transaction to network\n const signature = await this.signer.sendTransaction(\n fullySignedTransaction,\n requirements.network,\n );\n\n // Wait for confirmation\n await this.signer.confirmTransaction(signature, requirements.network);\n\n return {\n success: true,\n transaction: signature,\n network: payloadV1.network,\n payer: valid.payer,\n };\n } catch (error) {\n console.error(\"Failed to settle transaction:\", error);\n return {\n success: false,\n errorReason: \"transaction_failed\",\n transaction: \"\",\n network: payloadV1.network,\n payer: valid.payer || \"\",\n };\n }\n }\n\n /**\n * Verify compute limit instruction\n *\n * @param instruction - The compute limit instruction\n * @param instruction.programAddress - Program address\n * @param instruction.data - Instruction data bytes\n */\n private verifyComputeLimitInstruction(instruction: {\n programAddress: Address;\n data?: Readonly<Uint8Array>;\n }): void {\n const programAddress = instruction.programAddress.toString();\n\n if (\n programAddress !== COMPUTE_BUDGET_PROGRAM_ADDRESS.toString() ||\n !instruction.data ||\n instruction.data[0] !== 2\n ) {\n throw new Error(\n \"invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction\",\n );\n }\n\n try {\n parseSetComputeUnitLimitInstruction(instruction as never);\n } catch {\n throw new Error(\n \"invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction\",\n );\n }\n }\n\n /**\n * Verify compute price instruction\n *\n * @param instruction - The compute price instruction\n * @param instruction.programAddress - Program address\n * @param instruction.data - Instruction data bytes\n */\n private verifyComputePriceInstruction(instruction: {\n programAddress: Address;\n data?: Readonly<Uint8Array>;\n }): void {\n const programAddress = instruction.programAddress.toString();\n\n if (\n programAddress !== COMPUTE_BUDGET_PROGRAM_ADDRESS.toString() ||\n !instruction.data ||\n instruction.data[0] !== 3\n ) {\n throw new Error(\n \"invalid_exact_svm_payload_transaction_instructions_compute_price_instruction\",\n );\n }\n\n try {\n const parsedInstruction = parseSetComputeUnitPriceInstruction(instruction as never);\n\n // Check if price exceeds maximum (5 lamports per compute unit)\n if (\n (parsedInstruction as unknown as { microLamports: bigint }).microLamports >\n BigInt(MAX_COMPUTE_UNIT_PRICE_MICROLAMPORTS)\n ) {\n throw new Error(\n \"invalid_exact_svm_payload_transaction_instructions_compute_price_instruction_too_high\",\n );\n }\n } catch (error) {\n if (error instanceof Error && error.message.includes(\"too_high\")) {\n throw error;\n }\n throw new Error(\n \"invalid_exact_svm_payload_transaction_instructions_compute_price_instruction\",\n );\n }\n }\n}\n"],"mappings":";;;;;;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE,mCAAmC;AAAA,EACnC;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA,mCAAmC;AAAA,EACnC;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AAiBA,IAAM,mBAAN,MAA2D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUhE,YAA6B,QAA8B;AAA9B;AAT7B,SAAS,SAAS;AAClB,SAAS,aAAa;AAAA,EAQsC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAU5D,SAAS,GAAgD;AAEvD,UAAM,YAAY,KAAK,OAAO,aAAa;AAC3C,UAAM,cAAc,KAAK,MAAM,KAAK,OAAO,IAAI,UAAU,MAAM;AAE/D,WAAO;AAAA,MACL,UAAU,UAAU,WAAW;AAAA,IACjC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,WAAW,GAAqB;AAC9B,WAAO,CAAC,GAAG,KAAK,OAAO,aAAa,CAAC;AAAA,EACvC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,OACJ,SACA,cACyB;AACzB,UAAM,iBAAiB;AACvB,UAAM,YAAY;AAClB,UAAM,kBAAkB,QAAQ;AAGhC,QAAI,UAAU,WAAW,WAAW,aAAa,WAAW,SAAS;AACnE,aAAO;AAAA,QACL,SAAS;AAAA,QACT,eAAe;AAAA,QACf,OAAO;AAAA,MACT;AAAA,IACF;AAEA,QAAI,UAAU,YAAY,aAAa,SAAS;AAC9C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ACF;AACF;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/v1/index.ts"],"sourcesContent":["/**\n * V1 exports for the SVM mechanism\n */\nexport { ExactSvmSchemeV1 } from \"../exact/v1\";\n\nexport const NETWORKS: string[] = [\"solana\", \"solana-devnet\", \"solana-testnet\"];\n"],"mappings":";AAKO,IAAM,WAAqB,CAAC,UAAU,iBAAiB,gBAAgB;","names":[]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export { E as ExactSvmScheme } from '../../scheme-VFikrqtD.mjs';
|
|
2
|
+
import { x402Client, SelectPaymentRequirements, PaymentPolicy } from 'x402-core-mantle/client';
|
|
3
|
+
import { Network } from 'x402-core-mantle/types';
|
|
4
|
+
import { C as ClientSvmSigner } from '../../signer-BMkbhFYE.mjs';
|
|
5
|
+
import '@solana/kit';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Configuration options for registering SVM schemes to an x402Client
|
|
9
|
+
*/
|
|
10
|
+
interface SvmClientConfig {
|
|
11
|
+
/**
|
|
12
|
+
* The SVM signer to use for creating payment payloads
|
|
13
|
+
*/
|
|
14
|
+
signer: ClientSvmSigner;
|
|
15
|
+
/**
|
|
16
|
+
* Optional payment requirements selector function
|
|
17
|
+
*/
|
|
18
|
+
paymentRequirementsSelector?: SelectPaymentRequirements;
|
|
19
|
+
/**
|
|
20
|
+
* Optional policies to apply to the client
|
|
21
|
+
*/
|
|
22
|
+
policies?: PaymentPolicy[];
|
|
23
|
+
/**
|
|
24
|
+
* Optional specific networks to register
|
|
25
|
+
*/
|
|
26
|
+
networks?: Network[];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Registers SVM payment schemes to an existing x402Client instance.
|
|
30
|
+
*
|
|
31
|
+
* @param client - The x402Client instance to register schemes to
|
|
32
|
+
* @param config - Configuration for SVM client registration
|
|
33
|
+
* @returns The client instance for chaining
|
|
34
|
+
*/
|
|
35
|
+
declare function registerExactSvmScheme(client: x402Client, config: SvmClientConfig): x402Client;
|
|
36
|
+
|
|
37
|
+
export { type SvmClientConfig, registerExactSvmScheme };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ExactSvmScheme
|
|
3
|
+
} from "../../chunk-TA6OPUHK.mjs";
|
|
4
|
+
import {
|
|
5
|
+
NETWORKS
|
|
6
|
+
} from "../../chunk-WWACQNRQ.mjs";
|
|
7
|
+
import {
|
|
8
|
+
ExactSvmSchemeV1
|
|
9
|
+
} from "../../chunk-M7NDDMX7.mjs";
|
|
10
|
+
import "../../chunk-WRDCPYFH.mjs";
|
|
11
|
+
import "../../chunk-PHFXLBH6.mjs";
|
|
12
|
+
|
|
13
|
+
// src/exact/client/register.ts
|
|
14
|
+
function registerExactSvmScheme(client, config) {
|
|
15
|
+
if (config.networks && config.networks.length > 0) {
|
|
16
|
+
config.networks.forEach((network) => {
|
|
17
|
+
client.register(network, new ExactSvmScheme(config.signer));
|
|
18
|
+
});
|
|
19
|
+
} else {
|
|
20
|
+
client.register("solana:*", new ExactSvmScheme(config.signer));
|
|
21
|
+
}
|
|
22
|
+
NETWORKS.forEach((network) => {
|
|
23
|
+
client.registerV1(network, new ExactSvmSchemeV1(config.signer));
|
|
24
|
+
});
|
|
25
|
+
if (config.policies) {
|
|
26
|
+
config.policies.forEach((policy) => {
|
|
27
|
+
client.registerPolicy(policy);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return client;
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
ExactSvmScheme,
|
|
34
|
+
registerExactSvmScheme
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/exact/client/register.ts"],"sourcesContent":["import { x402Client, SelectPaymentRequirements, PaymentPolicy } from \"x402-core-mantle/client\";\nimport { Network } from \"x402-core-mantle/types\";\nimport { ClientSvmSigner } from \"../../signer\";\nimport { ExactSvmScheme } from \"./scheme\";\nimport { ExactSvmSchemeV1 } from \"../v1/client/scheme\";\nimport { NETWORKS } from \"../../v1\";\n\n/**\n * Configuration options for registering SVM schemes to an x402Client\n */\nexport interface SvmClientConfig {\n /**\n * The SVM signer to use for creating payment payloads\n */\n signer: ClientSvmSigner;\n\n /**\n * Optional payment requirements selector function\n */\n paymentRequirementsSelector?: SelectPaymentRequirements;\n\n /**\n * Optional policies to apply to the client\n */\n policies?: PaymentPolicy[];\n\n /**\n * Optional specific networks to register\n */\n networks?: Network[];\n}\n\n/**\n * Registers SVM payment schemes to an existing x402Client instance.\n *\n * @param client - The x402Client instance to register schemes to\n * @param config - Configuration for SVM client registration\n * @returns The client instance for chaining\n */\nexport function registerExactSvmScheme(client: x402Client, config: SvmClientConfig): x402Client {\n // Register V2 scheme\n if (config.networks && config.networks.length > 0) {\n config.networks.forEach(network => {\n client.register(network, new ExactSvmScheme(config.signer));\n });\n } else {\n client.register(\"solana:*\", new ExactSvmScheme(config.signer));\n }\n\n // Register all V1 networks\n NETWORKS.forEach(network => {\n client.registerV1(network as Network, new ExactSvmSchemeV1(config.signer));\n });\n\n if (config.policies) {\n config.policies.forEach(policy => {\n client.registerPolicy(policy);\n });\n }\n\n return client;\n}\n"],"mappings":";;;;;;;;;;;;;AAuCO,SAAS,uBAAuB,QAAoB,QAAqC;AAE9F,MAAI,OAAO,YAAY,OAAO,SAAS,SAAS,GAAG;AACjD,WAAO,SAAS,QAAQ,aAAW;AACjC,aAAO,SAAS,SAAS,IAAI,eAAe,OAAO,MAAM,CAAC;AAAA,IAC5D,CAAC;AAAA,EACH,OAAO;AACL,WAAO,SAAS,YAAY,IAAI,eAAe,OAAO,MAAM,CAAC;AAAA,EAC/D;AAGA,WAAS,QAAQ,aAAW;AAC1B,WAAO,WAAW,SAAoB,IAAI,iBAAiB,OAAO,MAAM,CAAC;AAAA,EAC3E,CAAC;AAED,MAAI,OAAO,UAAU;AACnB,WAAO,SAAS,QAAQ,YAAU;AAChC,aAAO,eAAe,MAAM;AAAA,IAC9B,CAAC;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { SchemeNetworkFacilitator, PaymentPayload, PaymentRequirements, VerifyResponse, SettleResponse, Network } from 'x402-core-mantle/types';
|
|
2
|
+
import { F as FacilitatorSvmSigner } from '../../signer-BMkbhFYE.mjs';
|
|
3
|
+
import { x402Facilitator } from 'x402-core-mantle/facilitator';
|
|
4
|
+
import '@solana/kit';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* SVM facilitator implementation for the Exact payment scheme.
|
|
8
|
+
*/
|
|
9
|
+
declare class ExactSvmScheme implements SchemeNetworkFacilitator {
|
|
10
|
+
private readonly signer;
|
|
11
|
+
readonly scheme = "exact";
|
|
12
|
+
readonly caipFamily = "solana:*";
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new ExactSvmFacilitator instance.
|
|
15
|
+
*
|
|
16
|
+
* @param signer - The SVM RPC client for facilitator operations
|
|
17
|
+
* @returns ExactSvmFacilitator instance
|
|
18
|
+
*/
|
|
19
|
+
constructor(signer: FacilitatorSvmSigner);
|
|
20
|
+
/**
|
|
21
|
+
* Get mechanism-specific extra data for the supported kinds endpoint.
|
|
22
|
+
* For SVM, this includes a randomly selected fee payer address.
|
|
23
|
+
* Random selection distributes load across multiple signers.
|
|
24
|
+
*
|
|
25
|
+
* @param _ - The network identifier (unused for SVM)
|
|
26
|
+
* @returns Extra data with feePayer address
|
|
27
|
+
*/
|
|
28
|
+
getExtra(_: string): Record<string, unknown> | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Get signer addresses used by this facilitator.
|
|
31
|
+
* For SVM, returns all available fee payer addresses.
|
|
32
|
+
*
|
|
33
|
+
* @param _ - The network identifier (unused for SVM)
|
|
34
|
+
* @returns Array of fee payer addresses
|
|
35
|
+
*/
|
|
36
|
+
getSigners(_: string): string[];
|
|
37
|
+
/**
|
|
38
|
+
* Verifies a payment payload.
|
|
39
|
+
*
|
|
40
|
+
* @param payload - The payment payload to verify
|
|
41
|
+
* @param requirements - The payment requirements
|
|
42
|
+
* @returns Promise resolving to verification response
|
|
43
|
+
*/
|
|
44
|
+
verify(payload: PaymentPayload, requirements: PaymentRequirements): Promise<VerifyResponse>;
|
|
45
|
+
/**
|
|
46
|
+
* Settles a payment by submitting the transaction.
|
|
47
|
+
* Ensures the correct signer is used based on the feePayer specified in requirements.
|
|
48
|
+
*
|
|
49
|
+
* @param payload - The payment payload to settle
|
|
50
|
+
* @param requirements - The payment requirements
|
|
51
|
+
* @returns Promise resolving to settlement response
|
|
52
|
+
*/
|
|
53
|
+
settle(payload: PaymentPayload, requirements: PaymentRequirements): Promise<SettleResponse>;
|
|
54
|
+
/**
|
|
55
|
+
* Verify that the compute limit instruction is valid.
|
|
56
|
+
*
|
|
57
|
+
* @param instruction - The compute limit instruction
|
|
58
|
+
* @param instruction.programAddress - Program address
|
|
59
|
+
* @param instruction.data - Instruction data bytes
|
|
60
|
+
*/
|
|
61
|
+
private verifyComputeLimitInstruction;
|
|
62
|
+
/**
|
|
63
|
+
* Verify that the compute price instruction is valid.
|
|
64
|
+
*
|
|
65
|
+
* @param instruction - The compute price instruction
|
|
66
|
+
* @param instruction.programAddress - Program address
|
|
67
|
+
* @param instruction.data - Instruction data bytes
|
|
68
|
+
*/
|
|
69
|
+
private verifyComputePriceInstruction;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Configuration options for registering SVM schemes to an x402Facilitator
|
|
74
|
+
*/
|
|
75
|
+
interface SvmFacilitatorConfig {
|
|
76
|
+
/**
|
|
77
|
+
* The SVM signer for facilitator operations
|
|
78
|
+
*/
|
|
79
|
+
signer: FacilitatorSvmSigner;
|
|
80
|
+
/**
|
|
81
|
+
* Networks to register (single network or array of networks)
|
|
82
|
+
* Examples: "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1", ["solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1", "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"]
|
|
83
|
+
*/
|
|
84
|
+
networks: Network | Network[];
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Registers SVM payment schemes to an existing x402Facilitator instance.
|
|
88
|
+
*
|
|
89
|
+
* @param facilitator - The x402Facilitator instance to register schemes to
|
|
90
|
+
* @param config - Configuration for SVM facilitator registration
|
|
91
|
+
* @returns The facilitator instance for chaining
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```typescript
|
|
95
|
+
* // Single network
|
|
96
|
+
* registerExactSvmScheme(facilitator, {
|
|
97
|
+
* signer: svmSigner,
|
|
98
|
+
* networks: "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1" // Devnet
|
|
99
|
+
* });
|
|
100
|
+
*
|
|
101
|
+
* // Multiple networks (will auto-derive solana:* pattern)
|
|
102
|
+
* registerExactSvmScheme(facilitator, {
|
|
103
|
+
* signer: svmSigner,
|
|
104
|
+
* networks: ["solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1", "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"]
|
|
105
|
+
* });
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
declare function registerExactSvmScheme(facilitator: x402Facilitator, config: SvmFacilitatorConfig): x402Facilitator;
|
|
109
|
+
|
|
110
|
+
export { ExactSvmScheme, type SvmFacilitatorConfig, registerExactSvmScheme };
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NETWORKS
|
|
3
|
+
} from "../../chunk-WWACQNRQ.mjs";
|
|
4
|
+
import "../../chunk-M7NDDMX7.mjs";
|
|
5
|
+
import {
|
|
6
|
+
ExactSvmSchemeV1
|
|
7
|
+
} from "../../chunk-WRDCPYFH.mjs";
|
|
8
|
+
import {
|
|
9
|
+
MAX_COMPUTE_UNIT_PRICE_MICROLAMPORTS,
|
|
10
|
+
decodeTransactionFromPayload,
|
|
11
|
+
getTokenPayerFromTransaction
|
|
12
|
+
} from "../../chunk-PHFXLBH6.mjs";
|
|
13
|
+
|
|
14
|
+
// src/exact/facilitator/scheme.ts
|
|
15
|
+
import {
|
|
16
|
+
COMPUTE_BUDGET_PROGRAM_ADDRESS,
|
|
17
|
+
parseSetComputeUnitLimitInstruction,
|
|
18
|
+
parseSetComputeUnitPriceInstruction
|
|
19
|
+
} from "@solana-program/compute-budget";
|
|
20
|
+
import {
|
|
21
|
+
parseTransferCheckedInstruction as parseTransferCheckedInstructionToken,
|
|
22
|
+
TOKEN_PROGRAM_ADDRESS
|
|
23
|
+
} from "@solana-program/token";
|
|
24
|
+
import {
|
|
25
|
+
findAssociatedTokenPda,
|
|
26
|
+
parseTransferCheckedInstruction as parseTransferCheckedInstruction2022,
|
|
27
|
+
TOKEN_2022_PROGRAM_ADDRESS
|
|
28
|
+
} from "@solana-program/token-2022";
|
|
29
|
+
import {
|
|
30
|
+
decompileTransactionMessage,
|
|
31
|
+
getCompiledTransactionMessageDecoder
|
|
32
|
+
} from "@solana/kit";
|
|
33
|
+
var ExactSvmScheme = class {
|
|
34
|
+
/**
|
|
35
|
+
* Creates a new ExactSvmFacilitator instance.
|
|
36
|
+
*
|
|
37
|
+
* @param signer - The SVM RPC client for facilitator operations
|
|
38
|
+
* @returns ExactSvmFacilitator instance
|
|
39
|
+
*/
|
|
40
|
+
constructor(signer) {
|
|
41
|
+
this.signer = signer;
|
|
42
|
+
this.scheme = "exact";
|
|
43
|
+
this.caipFamily = "solana:*";
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Get mechanism-specific extra data for the supported kinds endpoint.
|
|
47
|
+
* For SVM, this includes a randomly selected fee payer address.
|
|
48
|
+
* Random selection distributes load across multiple signers.
|
|
49
|
+
*
|
|
50
|
+
* @param _ - The network identifier (unused for SVM)
|
|
51
|
+
* @returns Extra data with feePayer address
|
|
52
|
+
*/
|
|
53
|
+
getExtra(_) {
|
|
54
|
+
const addresses = this.signer.getAddresses();
|
|
55
|
+
const randomIndex = Math.floor(Math.random() * addresses.length);
|
|
56
|
+
return {
|
|
57
|
+
feePayer: addresses[randomIndex]
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get signer addresses used by this facilitator.
|
|
62
|
+
* For SVM, returns all available fee payer addresses.
|
|
63
|
+
*
|
|
64
|
+
* @param _ - The network identifier (unused for SVM)
|
|
65
|
+
* @returns Array of fee payer addresses
|
|
66
|
+
*/
|
|
67
|
+
getSigners(_) {
|
|
68
|
+
return [...this.signer.getAddresses()];
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Verifies a payment payload.
|
|
72
|
+
*
|
|
73
|
+
* @param payload - The payment payload to verify
|
|
74
|
+
* @param requirements - The payment requirements
|
|
75
|
+
* @returns Promise resolving to verification response
|
|
76
|
+
*/
|
|
77
|
+
async verify(payload, requirements) {
|
|
78
|
+
const exactSvmPayload = payload.payload;
|
|
79
|
+
if (payload.accepted.scheme !== "exact" || requirements.scheme !== "exact") {
|
|
80
|
+
return {
|
|
81
|
+
isValid: false,
|
|
82
|
+
invalidReason: "unsupported_scheme",
|
|
83
|
+
payer: ""
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
if (payload.accepted.network !== requirements.network) {
|
|
87
|
+
return {
|
|
88
|
+
isValid: false,
|
|
89
|
+
invalidReason: "network_mismatch",
|
|
90
|
+
payer: ""
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
if (!requirements.extra?.feePayer || typeof requirements.extra.feePayer !== "string") {
|
|
94
|
+
return {
|
|
95
|
+
isValid: false,
|
|
96
|
+
invalidReason: "invalid_exact_svm_payload_missing_fee_payer",
|
|
97
|
+
payer: ""
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
const signerAddresses = this.signer.getAddresses().map((addr) => addr.toString());
|
|
101
|
+
if (!signerAddresses.includes(requirements.extra.feePayer)) {
|
|
102
|
+
return {
|
|
103
|
+
isValid: false,
|
|
104
|
+
invalidReason: "fee_payer_not_managed_by_facilitator",
|
|
105
|
+
payer: ""
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
let transaction;
|
|
109
|
+
try {
|
|
110
|
+
transaction = decodeTransactionFromPayload(exactSvmPayload);
|
|
111
|
+
} catch {
|
|
112
|
+
return {
|
|
113
|
+
isValid: false,
|
|
114
|
+
invalidReason: "invalid_exact_svm_payload_transaction_could_not_be_decoded",
|
|
115
|
+
payer: ""
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
const compiled = getCompiledTransactionMessageDecoder().decode(transaction.messageBytes);
|
|
119
|
+
const decompiled = decompileTransactionMessage(compiled);
|
|
120
|
+
const instructions = decompiled.instructions ?? [];
|
|
121
|
+
if (instructions.length !== 3) {
|
|
122
|
+
return {
|
|
123
|
+
isValid: false,
|
|
124
|
+
invalidReason: "invalid_exact_svm_payload_transaction_instructions_length",
|
|
125
|
+
payer: ""
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
try {
|
|
129
|
+
this.verifyComputeLimitInstruction(instructions[0]);
|
|
130
|
+
this.verifyComputePriceInstruction(instructions[1]);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
133
|
+
return {
|
|
134
|
+
isValid: false,
|
|
135
|
+
invalidReason: errorMessage,
|
|
136
|
+
payer: ""
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
const payer = getTokenPayerFromTransaction(transaction);
|
|
140
|
+
if (!payer) {
|
|
141
|
+
return {
|
|
142
|
+
isValid: false,
|
|
143
|
+
invalidReason: "invalid_exact_svm_payload_no_transfer_instruction",
|
|
144
|
+
payer: ""
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
const transferIx = instructions[2];
|
|
148
|
+
const programAddress = transferIx.programAddress.toString();
|
|
149
|
+
if (programAddress !== TOKEN_PROGRAM_ADDRESS.toString() && programAddress !== TOKEN_2022_PROGRAM_ADDRESS.toString()) {
|
|
150
|
+
return {
|
|
151
|
+
isValid: false,
|
|
152
|
+
invalidReason: "invalid_exact_svm_payload_no_transfer_instruction",
|
|
153
|
+
payer
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
let parsedTransfer;
|
|
157
|
+
try {
|
|
158
|
+
if (programAddress === TOKEN_PROGRAM_ADDRESS.toString()) {
|
|
159
|
+
parsedTransfer = parseTransferCheckedInstructionToken(transferIx);
|
|
160
|
+
} else {
|
|
161
|
+
parsedTransfer = parseTransferCheckedInstruction2022(transferIx);
|
|
162
|
+
}
|
|
163
|
+
} catch {
|
|
164
|
+
return {
|
|
165
|
+
isValid: false,
|
|
166
|
+
invalidReason: "invalid_exact_svm_payload_no_transfer_instruction",
|
|
167
|
+
payer
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
const authorityAddress = parsedTransfer.accounts.authority.address.toString();
|
|
171
|
+
if (signerAddresses.includes(authorityAddress)) {
|
|
172
|
+
return {
|
|
173
|
+
isValid: false,
|
|
174
|
+
invalidReason: "invalid_exact_svm_payload_transaction_fee_payer_transferring_funds",
|
|
175
|
+
payer
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
const mintAddress = parsedTransfer.accounts.mint.address.toString();
|
|
179
|
+
if (mintAddress !== requirements.asset) {
|
|
180
|
+
return {
|
|
181
|
+
isValid: false,
|
|
182
|
+
invalidReason: "invalid_exact_svm_payload_mint_mismatch",
|
|
183
|
+
payer
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
const destATA = parsedTransfer.accounts.destination.address.toString();
|
|
187
|
+
try {
|
|
188
|
+
const [expectedDestATA] = await findAssociatedTokenPda({
|
|
189
|
+
mint: requirements.asset,
|
|
190
|
+
owner: requirements.payTo,
|
|
191
|
+
tokenProgram: programAddress === TOKEN_PROGRAM_ADDRESS.toString() ? TOKEN_PROGRAM_ADDRESS : TOKEN_2022_PROGRAM_ADDRESS
|
|
192
|
+
});
|
|
193
|
+
if (destATA !== expectedDestATA.toString()) {
|
|
194
|
+
return {
|
|
195
|
+
isValid: false,
|
|
196
|
+
invalidReason: "invalid_exact_svm_payload_recipient_mismatch",
|
|
197
|
+
payer
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
} catch {
|
|
201
|
+
return {
|
|
202
|
+
isValid: false,
|
|
203
|
+
invalidReason: "invalid_exact_svm_payload_recipient_mismatch",
|
|
204
|
+
payer
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
const amount = parsedTransfer.data.amount;
|
|
208
|
+
if (amount < BigInt(requirements.amount)) {
|
|
209
|
+
return {
|
|
210
|
+
isValid: false,
|
|
211
|
+
invalidReason: "invalid_exact_svm_payload_amount_insufficient",
|
|
212
|
+
payer
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
const feePayer = requirements.extra.feePayer;
|
|
217
|
+
const fullySignedTransaction = await this.signer.signTransaction(
|
|
218
|
+
exactSvmPayload.transaction,
|
|
219
|
+
feePayer,
|
|
220
|
+
requirements.network
|
|
221
|
+
);
|
|
222
|
+
await this.signer.simulateTransaction(fullySignedTransaction, requirements.network);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
225
|
+
return {
|
|
226
|
+
isValid: false,
|
|
227
|
+
invalidReason: `transaction_simulation_failed: ${errorMessage}`,
|
|
228
|
+
payer
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
isValid: true,
|
|
233
|
+
invalidReason: void 0,
|
|
234
|
+
payer
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Settles a payment by submitting the transaction.
|
|
239
|
+
* Ensures the correct signer is used based on the feePayer specified in requirements.
|
|
240
|
+
*
|
|
241
|
+
* @param payload - The payment payload to settle
|
|
242
|
+
* @param requirements - The payment requirements
|
|
243
|
+
* @returns Promise resolving to settlement response
|
|
244
|
+
*/
|
|
245
|
+
async settle(payload, requirements) {
|
|
246
|
+
const exactSvmPayload = payload.payload;
|
|
247
|
+
const valid = await this.verify(payload, requirements);
|
|
248
|
+
if (!valid.isValid) {
|
|
249
|
+
return {
|
|
250
|
+
success: false,
|
|
251
|
+
network: payload.accepted.network,
|
|
252
|
+
transaction: "",
|
|
253
|
+
errorReason: valid.invalidReason ?? "verification_failed",
|
|
254
|
+
payer: valid.payer || ""
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
try {
|
|
258
|
+
const feePayer = requirements.extra.feePayer;
|
|
259
|
+
const fullySignedTransaction = await this.signer.signTransaction(
|
|
260
|
+
exactSvmPayload.transaction,
|
|
261
|
+
feePayer,
|
|
262
|
+
requirements.network
|
|
263
|
+
);
|
|
264
|
+
const signature = await this.signer.sendTransaction(
|
|
265
|
+
fullySignedTransaction,
|
|
266
|
+
requirements.network
|
|
267
|
+
);
|
|
268
|
+
await this.signer.confirmTransaction(signature, requirements.network);
|
|
269
|
+
return {
|
|
270
|
+
success: true,
|
|
271
|
+
transaction: signature,
|
|
272
|
+
network: payload.accepted.network,
|
|
273
|
+
payer: valid.payer
|
|
274
|
+
};
|
|
275
|
+
} catch (error) {
|
|
276
|
+
console.error("Failed to settle transaction:", error);
|
|
277
|
+
return {
|
|
278
|
+
success: false,
|
|
279
|
+
errorReason: "transaction_failed",
|
|
280
|
+
transaction: "",
|
|
281
|
+
network: payload.accepted.network,
|
|
282
|
+
payer: valid.payer || ""
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Verify that the compute limit instruction is valid.
|
|
288
|
+
*
|
|
289
|
+
* @param instruction - The compute limit instruction
|
|
290
|
+
* @param instruction.programAddress - Program address
|
|
291
|
+
* @param instruction.data - Instruction data bytes
|
|
292
|
+
*/
|
|
293
|
+
verifyComputeLimitInstruction(instruction) {
|
|
294
|
+
const programAddress = instruction.programAddress.toString();
|
|
295
|
+
if (programAddress !== COMPUTE_BUDGET_PROGRAM_ADDRESS.toString() || !instruction.data || instruction.data[0] !== 2) {
|
|
296
|
+
throw new Error(
|
|
297
|
+
"invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction"
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
try {
|
|
301
|
+
parseSetComputeUnitLimitInstruction(instruction);
|
|
302
|
+
} catch {
|
|
303
|
+
throw new Error(
|
|
304
|
+
"invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction"
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Verify that the compute price instruction is valid.
|
|
310
|
+
*
|
|
311
|
+
* @param instruction - The compute price instruction
|
|
312
|
+
* @param instruction.programAddress - Program address
|
|
313
|
+
* @param instruction.data - Instruction data bytes
|
|
314
|
+
*/
|
|
315
|
+
verifyComputePriceInstruction(instruction) {
|
|
316
|
+
const programAddress = instruction.programAddress.toString();
|
|
317
|
+
if (programAddress !== COMPUTE_BUDGET_PROGRAM_ADDRESS.toString() || !instruction.data || instruction.data[0] !== 3) {
|
|
318
|
+
throw new Error(
|
|
319
|
+
"invalid_exact_svm_payload_transaction_instructions_compute_price_instruction"
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
try {
|
|
323
|
+
const parsedInstruction = parseSetComputeUnitPriceInstruction(instruction);
|
|
324
|
+
if (parsedInstruction.microLamports > BigInt(MAX_COMPUTE_UNIT_PRICE_MICROLAMPORTS)) {
|
|
325
|
+
throw new Error(
|
|
326
|
+
"invalid_exact_svm_payload_transaction_instructions_compute_price_instruction_too_high"
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
} catch (error) {
|
|
330
|
+
if (error instanceof Error && error.message.includes("too_high")) {
|
|
331
|
+
throw error;
|
|
332
|
+
}
|
|
333
|
+
throw new Error(
|
|
334
|
+
"invalid_exact_svm_payload_transaction_instructions_compute_price_instruction"
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
// src/exact/facilitator/register.ts
|
|
341
|
+
function registerExactSvmScheme(facilitator, config) {
|
|
342
|
+
facilitator.register(config.networks, new ExactSvmScheme(config.signer));
|
|
343
|
+
facilitator.registerV1(NETWORKS, new ExactSvmSchemeV1(config.signer));
|
|
344
|
+
return facilitator;
|
|
345
|
+
}
|
|
346
|
+
export {
|
|
347
|
+
ExactSvmScheme,
|
|
348
|
+
registerExactSvmScheme
|
|
349
|
+
};
|
|
350
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/exact/facilitator/scheme.ts","../../../../src/exact/facilitator/register.ts"],"sourcesContent":["import {\n COMPUTE_BUDGET_PROGRAM_ADDRESS,\n parseSetComputeUnitLimitInstruction,\n parseSetComputeUnitPriceInstruction,\n} from \"@solana-program/compute-budget\";\nimport {\n parseTransferCheckedInstruction as parseTransferCheckedInstructionToken,\n TOKEN_PROGRAM_ADDRESS,\n} from \"@solana-program/token\";\nimport {\n findAssociatedTokenPda,\n parseTransferCheckedInstruction as parseTransferCheckedInstruction2022,\n TOKEN_2022_PROGRAM_ADDRESS,\n} from \"@solana-program/token-2022\";\nimport {\n decompileTransactionMessage,\n getCompiledTransactionMessageDecoder,\n type Address,\n} from \"@solana/kit\";\nimport type {\n PaymentPayload,\n PaymentRequirements,\n SchemeNetworkFacilitator,\n SettleResponse,\n VerifyResponse,\n} from \"x402-core-mantle/types\";\nimport { MAX_COMPUTE_UNIT_PRICE_MICROLAMPORTS } from \"../../constants\";\nimport type { FacilitatorSvmSigner } from \"../../signer\";\nimport type { ExactSvmPayloadV2 } from \"../../types\";\nimport { decodeTransactionFromPayload, getTokenPayerFromTransaction } from \"../../utils\";\n\n/**\n * SVM facilitator implementation for the Exact payment scheme.\n */\nexport class ExactSvmScheme implements SchemeNetworkFacilitator {\n readonly scheme = \"exact\";\n readonly caipFamily = \"solana:*\";\n\n /**\n * Creates a new ExactSvmFacilitator instance.\n *\n * @param signer - The SVM RPC client for facilitator operations\n * @returns ExactSvmFacilitator instance\n */\n constructor(private readonly signer: FacilitatorSvmSigner) {}\n\n /**\n * Get mechanism-specific extra data for the supported kinds endpoint.\n * For SVM, this includes a randomly selected fee payer address.\n * Random selection distributes load across multiple signers.\n *\n * @param _ - The network identifier (unused for SVM)\n * @returns Extra data with feePayer address\n */\n getExtra(_: string): Record<string, unknown> | undefined {\n // Randomly select from available signers to distribute load\n const addresses = this.signer.getAddresses();\n const randomIndex = Math.floor(Math.random() * addresses.length);\n\n return {\n feePayer: addresses[randomIndex],\n };\n }\n\n /**\n * Get signer addresses used by this facilitator.\n * For SVM, returns all available fee payer addresses.\n *\n * @param _ - The network identifier (unused for SVM)\n * @returns Array of fee payer addresses\n */\n getSigners(_: string): string[] {\n return [...this.signer.getAddresses()];\n }\n\n /**\n * Verifies a payment payload.\n *\n * @param payload - The payment payload to verify\n * @param requirements - The payment requirements\n * @returns Promise resolving to verification response\n */\n async verify(\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<VerifyResponse> {\n const exactSvmPayload = payload.payload as ExactSvmPayloadV2;\n\n // Step 1: Validate Payment Requirements\n if (payload.accepted.scheme !== \"exact\" || requirements.scheme !== \"exact\") {\n return {\n isValid: false,\n invalidReason: \"unsupported_scheme\",\n payer: \"\",\n };\n }\n\n if (payload.accepted.network !== requirements.network) {\n return {\n isValid: false,\n invalidReason: \"network_mismatch\",\n payer: \"\",\n };\n }\n\n if (!requirements.extra?.feePayer || typeof requirements.extra.feePayer !== \"string\") {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_missing_fee_payer\",\n payer: \"\",\n };\n }\n\n // Verify that the requested feePayer is managed by this facilitator\n const signerAddresses = this.signer.getAddresses().map(addr => addr.toString());\n if (!signerAddresses.includes(requirements.extra.feePayer)) {\n return {\n isValid: false,\n invalidReason: \"fee_payer_not_managed_by_facilitator\",\n payer: \"\",\n };\n }\n\n // Step 2: Parse and Validate Transaction Structure\n let transaction;\n try {\n transaction = decodeTransactionFromPayload(exactSvmPayload);\n } catch {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_transaction_could_not_be_decoded\",\n payer: \"\",\n };\n }\n\n const compiled = getCompiledTransactionMessageDecoder().decode(transaction.messageBytes);\n const decompiled = decompileTransactionMessage(compiled);\n const instructions = decompiled.instructions ?? [];\n\n // 3 instructions: ComputeLimit + ComputePrice + TransferChecked\n if (instructions.length !== 3) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_transaction_instructions_length\",\n payer: \"\",\n };\n }\n\n // Step 3: Verify Compute Budget Instructions\n try {\n this.verifyComputeLimitInstruction(instructions[0] as never);\n this.verifyComputePriceInstruction(instructions[1] as never);\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n return {\n isValid: false,\n invalidReason: errorMessage,\n payer: \"\",\n };\n }\n\n const payer = getTokenPayerFromTransaction(transaction);\n if (!payer) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_no_transfer_instruction\",\n payer: \"\",\n };\n }\n\n // Step 4: Verify Transfer Instruction\n const transferIx = instructions[2];\n const programAddress = transferIx.programAddress.toString();\n\n if (\n programAddress !== TOKEN_PROGRAM_ADDRESS.toString() &&\n programAddress !== TOKEN_2022_PROGRAM_ADDRESS.toString()\n ) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_no_transfer_instruction\",\n payer,\n };\n }\n\n // Parse the transfer instruction using the appropriate library helper\n let parsedTransfer;\n try {\n if (programAddress === TOKEN_PROGRAM_ADDRESS.toString()) {\n parsedTransfer = parseTransferCheckedInstructionToken(transferIx as never);\n } else {\n parsedTransfer = parseTransferCheckedInstruction2022(transferIx as never);\n }\n } catch {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_no_transfer_instruction\",\n payer,\n };\n }\n\n // Verify that the facilitator's signers are not transferring their own funds\n // SECURITY: Prevent facilitator from signing away their own tokens\n const authorityAddress = parsedTransfer.accounts.authority.address.toString();\n if (signerAddresses.includes(authorityAddress)) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_transaction_fee_payer_transferring_funds\",\n payer,\n };\n }\n\n // Verify mint address matches requirements\n const mintAddress = parsedTransfer.accounts.mint.address.toString();\n if (mintAddress !== requirements.asset) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_mint_mismatch\",\n payer,\n };\n }\n\n // Verify destination ATA matches expected ATA for payTo address\n const destATA = parsedTransfer.accounts.destination.address.toString();\n try {\n const [expectedDestATA] = await findAssociatedTokenPda({\n mint: requirements.asset as Address,\n owner: requirements.payTo as Address,\n tokenProgram:\n programAddress === TOKEN_PROGRAM_ADDRESS.toString()\n ? (TOKEN_PROGRAM_ADDRESS as Address)\n : (TOKEN_2022_PROGRAM_ADDRESS as Address),\n });\n\n if (destATA !== expectedDestATA.toString()) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_recipient_mismatch\",\n payer,\n };\n }\n } catch {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_recipient_mismatch\",\n payer,\n };\n }\n\n // Verify transfer amount meets requirements\n const amount = parsedTransfer.data.amount;\n if (amount < BigInt(requirements.amount)) {\n return {\n isValid: false,\n invalidReason: \"invalid_exact_svm_payload_amount_insufficient\",\n payer,\n };\n }\n\n // Step 5: Sign and Simulate Transaction\n // CRITICAL: Simulation proves transaction will succeed (catches insufficient balance, invalid accounts, etc)\n try {\n const feePayer = requirements.extra.feePayer as Address;\n\n // Sign transaction with the feePayer's signer\n const fullySignedTransaction = await this.signer.signTransaction(\n exactSvmPayload.transaction,\n feePayer,\n requirements.network,\n );\n\n // Simulate to verify transaction would succeed\n await this.signer.simulateTransaction(fullySignedTransaction, requirements.network);\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : String(error);\n return {\n isValid: false,\n invalidReason: `transaction_simulation_failed: ${errorMessage}`,\n payer,\n };\n }\n\n return {\n isValid: true,\n invalidReason: undefined,\n payer,\n };\n }\n\n /**\n * Settles a payment by submitting the transaction.\n * Ensures the correct signer is used based on the feePayer specified in requirements.\n *\n * @param payload - The payment payload to settle\n * @param requirements - The payment requirements\n * @returns Promise resolving to settlement response\n */\n async settle(\n payload: PaymentPayload,\n requirements: PaymentRequirements,\n ): Promise<SettleResponse> {\n const exactSvmPayload = payload.payload as ExactSvmPayloadV2;\n\n const valid = await this.verify(payload, requirements);\n if (!valid.isValid) {\n return {\n success: false,\n network: payload.accepted.network,\n transaction: \"\",\n errorReason: valid.invalidReason ?? \"verification_failed\",\n payer: valid.payer || \"\",\n };\n }\n\n try {\n // Extract feePayer from requirements (already validated in verify)\n const feePayer = requirements.extra.feePayer as Address;\n\n // Sign transaction with the feePayer's signer\n const fullySignedTransaction = await this.signer.signTransaction(\n exactSvmPayload.transaction,\n feePayer,\n requirements.network,\n );\n\n // Send transaction to network\n const signature = await this.signer.sendTransaction(\n fullySignedTransaction,\n requirements.network,\n );\n\n // Wait for confirmation\n await this.signer.confirmTransaction(signature, requirements.network);\n\n return {\n success: true,\n transaction: signature,\n network: payload.accepted.network,\n payer: valid.payer,\n };\n } catch (error) {\n console.error(\"Failed to settle transaction:\", error);\n return {\n success: false,\n errorReason: \"transaction_failed\",\n transaction: \"\",\n network: payload.accepted.network,\n payer: valid.payer || \"\",\n };\n }\n }\n\n /**\n * Verify that the compute limit instruction is valid.\n *\n * @param instruction - The compute limit instruction\n * @param instruction.programAddress - Program address\n * @param instruction.data - Instruction data bytes\n */\n private verifyComputeLimitInstruction(instruction: {\n programAddress: Address;\n data?: Readonly<Uint8Array>;\n }): void {\n const programAddress = instruction.programAddress.toString();\n\n if (\n programAddress !== COMPUTE_BUDGET_PROGRAM_ADDRESS.toString() ||\n !instruction.data ||\n instruction.data[0] !== 2 // discriminator for SetComputeUnitLimit\n ) {\n throw new Error(\n \"invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction\",\n );\n }\n\n try {\n parseSetComputeUnitLimitInstruction(instruction as never);\n } catch {\n throw new Error(\n \"invalid_exact_svm_payload_transaction_instructions_compute_limit_instruction\",\n );\n }\n }\n\n /**\n * Verify that the compute price instruction is valid.\n *\n * @param instruction - The compute price instruction\n * @param instruction.programAddress - Program address\n * @param instruction.data - Instruction data bytes\n */\n private verifyComputePriceInstruction(instruction: {\n programAddress: Address;\n data?: Readonly<Uint8Array>;\n }): void {\n const programAddress = instruction.programAddress.toString();\n\n if (\n programAddress !== COMPUTE_BUDGET_PROGRAM_ADDRESS.toString() ||\n !instruction.data ||\n instruction.data[0] !== 3 // discriminator for SetComputeUnitPrice\n ) {\n throw new Error(\n \"invalid_exact_svm_payload_transaction_instructions_compute_price_instruction\",\n );\n }\n\n try {\n const parsedInstruction = parseSetComputeUnitPriceInstruction(instruction as never);\n\n // Check if price exceeds maximum (5 lamports per compute unit)\n if (\n (parsedInstruction as unknown as { microLamports: bigint }).microLamports >\n BigInt(MAX_COMPUTE_UNIT_PRICE_MICROLAMPORTS)\n ) {\n throw new Error(\n \"invalid_exact_svm_payload_transaction_instructions_compute_price_instruction_too_high\",\n );\n }\n } catch (error) {\n if (error instanceof Error && error.message.includes(\"too_high\")) {\n throw error;\n }\n throw new Error(\n \"invalid_exact_svm_payload_transaction_instructions_compute_price_instruction\",\n );\n }\n }\n}\n","import { x402Facilitator } from \"x402-core-mantle/facilitator\";\nimport { Network } from \"x402-core-mantle/types\";\nimport { FacilitatorSvmSigner } from \"../../signer\";\nimport { ExactSvmScheme } from \"./scheme\";\nimport { ExactSvmSchemeV1 } from \"../v1/facilitator/scheme\";\nimport { NETWORKS } from \"../../v1\";\n\n/**\n * Configuration options for registering SVM schemes to an x402Facilitator\n */\nexport interface SvmFacilitatorConfig {\n /**\n * The SVM signer for facilitator operations\n */\n signer: FacilitatorSvmSigner;\n\n /**\n * Networks to register (single network or array of networks)\n * Examples: \"solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1\", [\"solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1\", \"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp\"]\n */\n networks: Network | Network[];\n}\n\n/**\n * Registers SVM payment schemes to an existing x402Facilitator instance.\n *\n * @param facilitator - The x402Facilitator instance to register schemes to\n * @param config - Configuration for SVM facilitator registration\n * @returns The facilitator instance for chaining\n *\n * @example\n * ```typescript\n * // Single network\n * registerExactSvmScheme(facilitator, {\n * signer: svmSigner,\n * networks: \"solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1\" // Devnet\n * });\n *\n * // Multiple networks (will auto-derive solana:* pattern)\n * registerExactSvmScheme(facilitator, {\n * signer: svmSigner,\n * networks: [\"solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1\", \"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp\"]\n * });\n * ```\n */\nexport function registerExactSvmScheme(\n facilitator: x402Facilitator,\n config: SvmFacilitatorConfig,\n): x402Facilitator {\n // Register V2 scheme with specified networks\n facilitator.register(config.networks, new ExactSvmScheme(config.signer));\n\n // Register all V1 networks\n facilitator.registerV1(NETWORKS as Network[], new ExactSvmSchemeV1(config.signer));\n\n return facilitator;\n}\n"],"mappings":";;;;;;;;;;;;;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE,mCAAmC;AAAA,EACnC;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA,mCAAmC;AAAA,EACnC;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AAgBA,IAAM,iBAAN,MAAyD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAU9D,YAA6B,QAA8B;AAA9B;AAT7B,SAAS,SAAS;AAClB,SAAS,aAAa;AAAA,EAQsC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAU5D,SAAS,GAAgD;AAEvD,UAAM,YAAY,KAAK,OAAO,aAAa;AAC3C,UAAM,c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