@faremeter/payment-solana 0.10.0 → 0.10.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/dist/src/exact/facilitator.d.ts +7 -1
- package/dist/src/exact/facilitator.d.ts.map +1 -1
- package/dist/src/exact/facilitator.js +3 -2
- package/dist/src/exact/verify.d.ts +2 -1
- package/dist/src/exact/verify.d.ts.map +1 -1
- package/dist/src/exact/verify.js +58 -17
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
|
@@ -7,6 +7,11 @@ export declare const PaymentRequirementsExtra: import("arktype/internal/methods/
|
|
|
7
7
|
decimals?: number;
|
|
8
8
|
recentBlockhash?: string;
|
|
9
9
|
}, {}>;
|
|
10
|
+
interface FacilitatorOptions {
|
|
11
|
+
maxRetries?: number;
|
|
12
|
+
retryDelayMs?: number;
|
|
13
|
+
maxPriorityFee?: number;
|
|
14
|
+
}
|
|
10
15
|
export declare const PaymentPayload: import("arktype/internal/methods/object.ts").ObjectType<{
|
|
11
16
|
transaction: (In: string) => import("arktype").Out<Readonly<{
|
|
12
17
|
messageBytes: import("@solana/transactions").TransactionMessageBytes;
|
|
@@ -14,5 +19,6 @@ export declare const PaymentPayload: import("arktype/internal/methods/object.ts"
|
|
|
14
19
|
}>>;
|
|
15
20
|
}, {}>;
|
|
16
21
|
export declare function transactionErrorToString(t: TransactionError): string;
|
|
17
|
-
export declare const createFacilitatorHandler: (network: string, rpc: Rpc<SolanaRpcApi>, feePayerKeypair: Keypair, mint: PublicKey,
|
|
22
|
+
export declare const createFacilitatorHandler: (network: string, rpc: Rpc<SolanaRpcApi>, feePayerKeypair: Keypair, mint: PublicKey, config?: FacilitatorOptions) => FacilitatorHandler;
|
|
23
|
+
export {};
|
|
18
24
|
//# sourceMappingURL=facilitator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"facilitator.d.ts","sourceRoot":"","sources":["../../../src/exact/facilitator.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAGvE,OAAO,EAML,KAAK,GAAG,EACR,KAAK,YAAY,EAClB,MAAM,aAAa,CAAC;AAOrB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAM1D,eAAO,MAAM,wBAAwB;;;;MAInC,CAAC;
|
|
1
|
+
{"version":3,"file":"facilitator.d.ts","sourceRoot":"","sources":["../../../src/exact/facilitator.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAGvE,OAAO,EAML,KAAK,GAAG,EACR,KAAK,YAAY,EAClB,MAAM,aAAa,CAAC;AAOrB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC1D,OAAO,EAAE,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAM1D,eAAO,MAAM,wBAAwB;;;;MAInC,CAAC;AAEH,UAAU,kBAAkB;IAC1B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,MAAM,CAAC;IAGtB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB;AAmBD,eAAO,MAAM,cAAc;;;;;MAEzB,CAAC;AAEH,wBAAgB,wBAAwB,CAAC,CAAC,EAAE,gBAAgB,UAY3D;AAiDD,eAAO,MAAM,wBAAwB,GACnC,SAAS,MAAM,EACf,KAAK,GAAG,CAAC,YAAY,CAAC,EACtB,iBAAiB,OAAO,EACxB,MAAM,SAAS,EACf,SAAS,kBAAkB,KAC1B,kBA+HF,CAAC"}
|
|
@@ -72,8 +72,9 @@ const sendTransaction = async (rpc, signedTransaction, maxRetries, retryDelayMs)
|
|
|
72
72
|
}
|
|
73
73
|
return { success: false, error: "Transaction confirmation timeout" };
|
|
74
74
|
};
|
|
75
|
-
export const createFacilitatorHandler = (network, rpc, feePayerKeypair, mint,
|
|
75
|
+
export const createFacilitatorHandler = (network, rpc, feePayerKeypair, mint, config) => {
|
|
76
76
|
const { matchTuple, matchTupleAndAsset } = generateMatcher(network, mint.toBase58());
|
|
77
|
+
const { maxRetries = 30, retryDelayMs = 1000, maxPriorityFee = 100_000, } = config ?? {};
|
|
77
78
|
const getSupported = () => {
|
|
78
79
|
return lookupX402Network(network).map((network) => Promise.resolve({
|
|
79
80
|
x402Version: 1,
|
|
@@ -120,7 +121,7 @@ export const createFacilitatorHandler = (network, rpc, feePayerKeypair, mint, ma
|
|
|
120
121
|
throw new Error("Failed to get compiled transaction message", { cause });
|
|
121
122
|
}
|
|
122
123
|
try {
|
|
123
|
-
if (!(await isValidTransaction(transactionMessage, requirements))) {
|
|
124
|
+
if (!(await isValidTransaction(transactionMessage, requirements, feePayerKeypair.publicKey, maxPriorityFee))) {
|
|
124
125
|
logger.error("Invalid transaction");
|
|
125
126
|
return errorResponse("Invalid transaction");
|
|
126
127
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { x402PaymentRequirements } from "@faremeter/types/x402";
|
|
2
2
|
import { type CompilableTransactionMessage } from "@solana/kit";
|
|
3
|
-
|
|
3
|
+
import type { PublicKey } from "@solana/web3.js";
|
|
4
|
+
export declare function isValidTransaction(transactionMessage: CompilableTransactionMessage, paymentRequirements: x402PaymentRequirements, facilitatorAddress: PublicKey, maxPriorityFee?: number): Promise<boolean>;
|
|
4
5
|
//# sourceMappingURL=verify.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../../../src/exact/verify.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AAYrE,OAAO,EAEL,KAAK,4BAA4B,EAElC,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"verify.d.ts","sourceRoot":"","sources":["../../../src/exact/verify.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,uBAAuB,CAAC;AAYrE,OAAO,EAEL,KAAK,4BAA4B,EAElC,MAAM,aAAa,CAAC;AAGrB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AA2GjD,wBAAsB,kBAAkB,CACtC,kBAAkB,EAAE,4BAA4B,EAChD,mBAAmB,EAAE,uBAAuB,EAC5C,kBAAkB,EAAE,SAAS,EAC7B,cAAc,CAAC,EAAE,MAAM,GACtB,OAAO,CAAC,OAAO,CAAC,CAiGlB"}
|
package/dist/src/exact/verify.js
CHANGED
|
@@ -3,37 +3,41 @@ import { parseSetComputeUnitLimitInstruction, parseSetComputeUnitPriceInstructio
|
|
|
3
3
|
import { findAssociatedTokenPda, parseCreateAssociatedTokenInstruction, parseTransferCheckedInstruction, TOKEN_PROGRAM_ADDRESS, } from "@solana-program/token";
|
|
4
4
|
import { address, } from "@solana/kit";
|
|
5
5
|
import { PaymentRequirementsExtra } from "./facilitator.js";
|
|
6
|
+
import { logger } from "./logger.js";
|
|
6
7
|
function verifyComputeUnitLimitInstruction(instruction) {
|
|
7
8
|
if (!instruction.data) {
|
|
8
|
-
return false;
|
|
9
|
+
return { valid: false };
|
|
9
10
|
}
|
|
10
11
|
try {
|
|
11
|
-
parseSetComputeUnitLimitInstruction({
|
|
12
|
+
const parsed = parseSetComputeUnitLimitInstruction({
|
|
12
13
|
programAddress: instruction.programAddress,
|
|
13
14
|
data: new Uint8Array(instruction.data),
|
|
14
15
|
});
|
|
15
|
-
return true;
|
|
16
|
+
return { valid: true, units: parsed.data.units };
|
|
16
17
|
}
|
|
17
18
|
catch {
|
|
18
|
-
return false;
|
|
19
|
+
return { valid: false };
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
22
|
function verifyComputeUnitPriceInstruction(instruction) {
|
|
22
23
|
if (!instruction.data) {
|
|
23
|
-
return false;
|
|
24
|
+
return { valid: false };
|
|
24
25
|
}
|
|
25
26
|
try {
|
|
26
|
-
parseSetComputeUnitPriceInstruction({
|
|
27
|
+
const parsed = parseSetComputeUnitPriceInstruction({
|
|
27
28
|
programAddress: instruction.programAddress,
|
|
28
29
|
data: new Uint8Array(instruction.data),
|
|
29
30
|
});
|
|
30
|
-
return true;
|
|
31
|
+
return { valid: true, microLamports: parsed.data.microLamports };
|
|
31
32
|
}
|
|
32
33
|
catch {
|
|
33
|
-
return false;
|
|
34
|
+
return { valid: false };
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
|
-
|
|
37
|
+
function calculatePriorityFee(units, microLamports) {
|
|
38
|
+
return (units * Number(microLamports)) / 1_000_000;
|
|
39
|
+
}
|
|
40
|
+
async function verifyTransferInstruction(instruction, paymentRequirements, destination, facilitatorAddress) {
|
|
37
41
|
if (!instruction.data || !instruction.accounts) {
|
|
38
42
|
return false;
|
|
39
43
|
}
|
|
@@ -48,6 +52,19 @@ async function verifyTransferInstruction(instruction, paymentRequirements, desti
|
|
|
48
52
|
catch {
|
|
49
53
|
return false;
|
|
50
54
|
}
|
|
55
|
+
if (transfer.accounts.authority.address === facilitatorAddress) {
|
|
56
|
+
logger.error("Dropping transfer where the transfer authority is the facilitator");
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
const [facilitatorATA] = await findAssociatedTokenPda({
|
|
60
|
+
mint: address(paymentRequirements.asset),
|
|
61
|
+
owner: address(facilitatorAddress),
|
|
62
|
+
tokenProgram: TOKEN_PROGRAM_ADDRESS,
|
|
63
|
+
});
|
|
64
|
+
if (transfer.accounts.source.address === facilitatorATA) {
|
|
65
|
+
logger.error("Dropping transfer where the source is the facilitator");
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
51
68
|
return (transfer.data.amount === BigInt(paymentRequirements.maxAmountRequired) &&
|
|
52
69
|
transfer.accounts.mint.address === paymentRequirements.asset &&
|
|
53
70
|
transfer.accounts.destination.address === destination);
|
|
@@ -68,7 +85,7 @@ function verifyCreateATAInstruction(instruction) {
|
|
|
68
85
|
return false;
|
|
69
86
|
}
|
|
70
87
|
}
|
|
71
|
-
export async function isValidTransaction(transactionMessage, paymentRequirements) {
|
|
88
|
+
export async function isValidTransaction(transactionMessage, paymentRequirements, facilitatorAddress, maxPriorityFee) {
|
|
72
89
|
const extra = PaymentRequirementsExtra(paymentRequirements.extra);
|
|
73
90
|
if (isValidationError(extra)) {
|
|
74
91
|
throw new Error("feePayer is required");
|
|
@@ -88,19 +105,43 @@ export async function isValidTransaction(transactionMessage, paymentRequirements
|
|
|
88
105
|
if (!ix0 || !ix1 || !ix2) {
|
|
89
106
|
return false;
|
|
90
107
|
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
108
|
+
const limitResult = verifyComputeUnitLimitInstruction(ix0);
|
|
109
|
+
const priceResult = verifyComputeUnitPriceInstruction(ix1);
|
|
110
|
+
if (!limitResult.valid || !priceResult.valid) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
if (maxPriorityFee !== undefined &&
|
|
114
|
+
limitResult.units !== undefined &&
|
|
115
|
+
priceResult.microLamports !== undefined) {
|
|
116
|
+
const priorityFee = calculatePriorityFee(limitResult.units, priceResult.microLamports);
|
|
117
|
+
if (priorityFee > maxPriorityFee) {
|
|
118
|
+
logger.error(`Priority fee ${priorityFee} exceeds maximum ${maxPriorityFee}`);
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return await verifyTransferInstruction(ix2, paymentRequirements, destination, facilitatorAddress.toBase58());
|
|
94
123
|
}
|
|
95
124
|
else if (instructions.length === 4) {
|
|
96
125
|
const [ix0, ix1, ix2, ix3] = instructions;
|
|
97
126
|
if (!ix0 || !ix1 || !ix2 || !ix3) {
|
|
98
127
|
return false;
|
|
99
128
|
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
129
|
+
const limitResult = verifyComputeUnitLimitInstruction(ix0);
|
|
130
|
+
const priceResult = verifyComputeUnitPriceInstruction(ix1);
|
|
131
|
+
if (!limitResult.valid || !priceResult.valid) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
if (maxPriorityFee !== undefined &&
|
|
135
|
+
limitResult.units !== undefined &&
|
|
136
|
+
priceResult.microLamports !== undefined) {
|
|
137
|
+
const priorityFee = calculatePriorityFee(limitResult.units, priceResult.microLamports);
|
|
138
|
+
if (priorityFee > maxPriorityFee) {
|
|
139
|
+
logger.error(`Priority fee ${priorityFee} exceeds maximum ${maxPriorityFee}`);
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return (verifyCreateATAInstruction(ix2) &&
|
|
144
|
+
(await verifyTransferInstruction(ix3, paymentRequirements, destination, facilitatorAddress.toBase58())));
|
|
104
145
|
}
|
|
105
146
|
return false;
|
|
106
147
|
}
|