@alleyboss/micropay-solana-x402-paywall 2.3.0 → 2.3.1
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 +35 -1
- package/dist/agent/index.cjs +359 -0
- package/dist/agent/index.cjs.map +1 -0
- package/dist/agent/index.d.cts +212 -0
- package/dist/agent/index.d.ts +212 -0
- package/dist/agent/index.js +348 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/index.cjs +299 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +291 -2
- package/dist/index.js.map +1 -1
- package/dist/priority-fees-C-OH4Trr.d.cts +50 -0
- package/dist/priority-fees-C-OH4Trr.d.ts +50 -0
- package/dist/solana/index.d.cts +4 -49
- package/dist/solana/index.d.ts +4 -49
- package/package.json +11 -1
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { Connection, Keypair } from '@solana/web3.js';
|
|
2
|
+
import { P as PriorityFeeConfig } from '../priority-fees-C-OH4Trr.cjs';
|
|
3
|
+
import { a as SessionConfig, S as SessionData } from '../session-D2IoWAWV.cjs';
|
|
4
|
+
|
|
5
|
+
/** Parameters for executing an agent payment */
|
|
6
|
+
interface ExecuteAgentPaymentParams {
|
|
7
|
+
/** Solana connection */
|
|
8
|
+
connection: Connection;
|
|
9
|
+
/** Agent's keypair (server-side only!) */
|
|
10
|
+
agentKeypair: Keypair;
|
|
11
|
+
/** Recipient wallet address (base58) */
|
|
12
|
+
recipientAddress: string;
|
|
13
|
+
/** Amount to send in lamports */
|
|
14
|
+
amountLamports: bigint;
|
|
15
|
+
/** Optional memo for the transaction */
|
|
16
|
+
memo?: string;
|
|
17
|
+
/** Optional priority fee configuration */
|
|
18
|
+
priorityFee?: PriorityFeeConfig;
|
|
19
|
+
/** Timeout for confirmation in ms (default: 60000) */
|
|
20
|
+
confirmationTimeout?: number;
|
|
21
|
+
}
|
|
22
|
+
/** Result of an agent payment execution */
|
|
23
|
+
interface AgentPaymentResult {
|
|
24
|
+
/** Whether the payment was successful */
|
|
25
|
+
success: boolean;
|
|
26
|
+
/** Transaction signature (if successful) */
|
|
27
|
+
signature?: string;
|
|
28
|
+
/** Error message (if failed) */
|
|
29
|
+
error?: string;
|
|
30
|
+
/** Block time when confirmed (Unix timestamp) */
|
|
31
|
+
confirmedAt?: number;
|
|
32
|
+
/** Slot number */
|
|
33
|
+
slot?: number;
|
|
34
|
+
/** Amount sent in lamports */
|
|
35
|
+
amountLamports?: bigint;
|
|
36
|
+
/** Amount sent in SOL */
|
|
37
|
+
amountSol?: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Execute an autonomous SOL payment from the agent's wallet
|
|
41
|
+
*
|
|
42
|
+
* This is the core utility for AI agents to pay for x402-protected resources.
|
|
43
|
+
* The agent keypair must be loaded server-side only (never exposed to client).
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* import { executeAgentPayment } from '@alleyboss/micropay-solana-x402-paywall/agent';
|
|
48
|
+
* import { Keypair, Connection } from '@solana/web3.js';
|
|
49
|
+
* import bs58 from 'bs58';
|
|
50
|
+
*
|
|
51
|
+
* // Load agent keypair from environment (server-side only!)
|
|
52
|
+
* const agentKeypair = Keypair.fromSecretKey(
|
|
53
|
+
* bs58.decode(process.env.AGENT_KEYPAIR_SECRET!)
|
|
54
|
+
* );
|
|
55
|
+
*
|
|
56
|
+
* const connection = new Connection('https://api.devnet.solana.com');
|
|
57
|
+
*
|
|
58
|
+
* const result = await executeAgentPayment({
|
|
59
|
+
* connection,
|
|
60
|
+
* agentKeypair,
|
|
61
|
+
* recipientAddress: 'RecipientWallet...',
|
|
62
|
+
* amountLamports: 20_000_000n, // 0.02 SOL
|
|
63
|
+
* priorityFee: { enabled: true, microLamports: 5000 },
|
|
64
|
+
* });
|
|
65
|
+
*
|
|
66
|
+
* if (result.success) {
|
|
67
|
+
* console.log('Payment sent:', result.signature);
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function executeAgentPayment(params: ExecuteAgentPaymentParams): Promise<AgentPaymentResult>;
|
|
72
|
+
/**
|
|
73
|
+
* Get the agent wallet's SOL balance
|
|
74
|
+
*/
|
|
75
|
+
declare function getAgentBalance(connection: Connection, agentKeypair: Keypair): Promise<{
|
|
76
|
+
balance: bigint;
|
|
77
|
+
balanceSol: number;
|
|
78
|
+
}>;
|
|
79
|
+
/**
|
|
80
|
+
* Check if agent has sufficient balance for a payment
|
|
81
|
+
*/
|
|
82
|
+
declare function hasAgentSufficientBalance(connection: Connection, agentKeypair: Keypair, requiredLamports: bigint): Promise<{
|
|
83
|
+
sufficient: boolean;
|
|
84
|
+
balance: bigint;
|
|
85
|
+
required: bigint;
|
|
86
|
+
}>;
|
|
87
|
+
/**
|
|
88
|
+
* Create a Keypair from a base58-encoded secret key string
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* const keypair = keypairFromBase58(process.env.AGENT_KEYPAIR_SECRET!);
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
declare function keypairFromBase58(base58Secret: string): Keypair;
|
|
96
|
+
/**
|
|
97
|
+
* Create a new random Keypair for agent use
|
|
98
|
+
* Returns both the keypair and its base58-encoded secret for storage
|
|
99
|
+
*/
|
|
100
|
+
declare function generateAgentKeypair(): {
|
|
101
|
+
keypair: Keypair;
|
|
102
|
+
secretBase58: string;
|
|
103
|
+
publicKey: string;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** Credit claims stored in session JWT */
|
|
107
|
+
interface CreditSessionClaims {
|
|
108
|
+
/** Number of remaining credits */
|
|
109
|
+
credits: number;
|
|
110
|
+
/** Unix timestamp when bundle expires (optional) */
|
|
111
|
+
bundleExpiry?: number;
|
|
112
|
+
/** Bundle type identifier (e.g., 'starter', 'pro') */
|
|
113
|
+
bundleType?: string;
|
|
114
|
+
}
|
|
115
|
+
/** Extended session data with credit information */
|
|
116
|
+
type CreditSessionData = SessionData & CreditSessionClaims;
|
|
117
|
+
/** Configuration for credit sessions */
|
|
118
|
+
interface CreditSessionConfig extends SessionConfig {
|
|
119
|
+
/** Initial number of credits */
|
|
120
|
+
initialCredits: number;
|
|
121
|
+
/** Bundle expiry in hours (optional, defaults to session duration) */
|
|
122
|
+
bundleExpiryHours?: number;
|
|
123
|
+
/** Bundle type identifier */
|
|
124
|
+
bundleType?: string;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Create a new credit session after bundle purchase
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* import { createCreditSession } from '@alleyboss/micropay-solana-x402-paywall/agent';
|
|
132
|
+
*
|
|
133
|
+
* const { token, session } = await createCreditSession(
|
|
134
|
+
* walletAddress,
|
|
135
|
+
* 'bundle-purchase',
|
|
136
|
+
* {
|
|
137
|
+
* secret: process.env.SESSION_SECRET!,
|
|
138
|
+
* durationHours: 24 * 30, // 30 days
|
|
139
|
+
* initialCredits: 10,
|
|
140
|
+
* bundleType: 'starter',
|
|
141
|
+
* }
|
|
142
|
+
* );
|
|
143
|
+
*
|
|
144
|
+
* // Set as cookie
|
|
145
|
+
* response.cookies.set('x402_credits', token, { httpOnly: true });
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
declare function createCreditSession(walletAddress: string, purchaseId: string, config: CreditSessionConfig): Promise<{
|
|
149
|
+
token: string;
|
|
150
|
+
session: CreditSessionData;
|
|
151
|
+
}>;
|
|
152
|
+
/** Result of validating a credit session */
|
|
153
|
+
interface CreditValidation {
|
|
154
|
+
valid: boolean;
|
|
155
|
+
session?: CreditSessionData;
|
|
156
|
+
reason?: string;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Validate a credit session token
|
|
160
|
+
*/
|
|
161
|
+
declare function validateCreditSession(token: string, secret: string): Promise<CreditValidation>;
|
|
162
|
+
/** Result of using a credit */
|
|
163
|
+
interface UseCreditResult {
|
|
164
|
+
/** Whether the credit was successfully used */
|
|
165
|
+
success: boolean;
|
|
166
|
+
/** Remaining credits after use */
|
|
167
|
+
remainingCredits: number;
|
|
168
|
+
/** New token with decremented credits (if successful) */
|
|
169
|
+
newToken?: string;
|
|
170
|
+
/** Error message (if failed) */
|
|
171
|
+
error?: string;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Use one credit from the session
|
|
175
|
+
* Returns a new token with decremented credit count
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* import { useCredit } from '@alleyboss/micropay-solana-x402-paywall/agent';
|
|
180
|
+
*
|
|
181
|
+
* const result = await useCredit(token, process.env.SESSION_SECRET!);
|
|
182
|
+
*
|
|
183
|
+
* if (result.success) {
|
|
184
|
+
* console.log(`Credit used. ${result.remainingCredits} remaining`);
|
|
185
|
+
* // Update cookie with new token
|
|
186
|
+
* response.cookies.set('x402_credits', result.newToken!);
|
|
187
|
+
* } else {
|
|
188
|
+
* console.log('No credits:', result.error);
|
|
189
|
+
* }
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
declare function useCredit(token: string, secret: string, creditsToUse?: number): Promise<UseCreditResult>;
|
|
193
|
+
/**
|
|
194
|
+
* Add credits to an existing session
|
|
195
|
+
* Useful for top-ups or rewards
|
|
196
|
+
*/
|
|
197
|
+
declare function addCredits(token: string, secret: string, creditsToAdd: number): Promise<{
|
|
198
|
+
success: boolean;
|
|
199
|
+
newToken?: string;
|
|
200
|
+
totalCredits?: number;
|
|
201
|
+
error?: string;
|
|
202
|
+
}>;
|
|
203
|
+
/**
|
|
204
|
+
* Get remaining credits from a session token (quick check without full validation)
|
|
205
|
+
*/
|
|
206
|
+
declare function getRemainingCredits(token: string, secret: string): Promise<{
|
|
207
|
+
credits: number;
|
|
208
|
+
valid: boolean;
|
|
209
|
+
bundleExpiry?: number;
|
|
210
|
+
}>;
|
|
211
|
+
|
|
212
|
+
export { type AgentPaymentResult, type CreditSessionClaims, type CreditSessionConfig, type CreditSessionData, type CreditValidation, type ExecuteAgentPaymentParams, type UseCreditResult, addCredits, createCreditSession, executeAgentPayment, generateAgentKeypair, getAgentBalance, getRemainingCredits, hasAgentSufficientBalance, keypairFromBase58, useCredit, validateCreditSession };
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { Connection, Keypair } from '@solana/web3.js';
|
|
2
|
+
import { P as PriorityFeeConfig } from '../priority-fees-C-OH4Trr.js';
|
|
3
|
+
import { a as SessionConfig, S as SessionData } from '../session-D2IoWAWV.js';
|
|
4
|
+
|
|
5
|
+
/** Parameters for executing an agent payment */
|
|
6
|
+
interface ExecuteAgentPaymentParams {
|
|
7
|
+
/** Solana connection */
|
|
8
|
+
connection: Connection;
|
|
9
|
+
/** Agent's keypair (server-side only!) */
|
|
10
|
+
agentKeypair: Keypair;
|
|
11
|
+
/** Recipient wallet address (base58) */
|
|
12
|
+
recipientAddress: string;
|
|
13
|
+
/** Amount to send in lamports */
|
|
14
|
+
amountLamports: bigint;
|
|
15
|
+
/** Optional memo for the transaction */
|
|
16
|
+
memo?: string;
|
|
17
|
+
/** Optional priority fee configuration */
|
|
18
|
+
priorityFee?: PriorityFeeConfig;
|
|
19
|
+
/** Timeout for confirmation in ms (default: 60000) */
|
|
20
|
+
confirmationTimeout?: number;
|
|
21
|
+
}
|
|
22
|
+
/** Result of an agent payment execution */
|
|
23
|
+
interface AgentPaymentResult {
|
|
24
|
+
/** Whether the payment was successful */
|
|
25
|
+
success: boolean;
|
|
26
|
+
/** Transaction signature (if successful) */
|
|
27
|
+
signature?: string;
|
|
28
|
+
/** Error message (if failed) */
|
|
29
|
+
error?: string;
|
|
30
|
+
/** Block time when confirmed (Unix timestamp) */
|
|
31
|
+
confirmedAt?: number;
|
|
32
|
+
/** Slot number */
|
|
33
|
+
slot?: number;
|
|
34
|
+
/** Amount sent in lamports */
|
|
35
|
+
amountLamports?: bigint;
|
|
36
|
+
/** Amount sent in SOL */
|
|
37
|
+
amountSol?: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Execute an autonomous SOL payment from the agent's wallet
|
|
41
|
+
*
|
|
42
|
+
* This is the core utility for AI agents to pay for x402-protected resources.
|
|
43
|
+
* The agent keypair must be loaded server-side only (never exposed to client).
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* import { executeAgentPayment } from '@alleyboss/micropay-solana-x402-paywall/agent';
|
|
48
|
+
* import { Keypair, Connection } from '@solana/web3.js';
|
|
49
|
+
* import bs58 from 'bs58';
|
|
50
|
+
*
|
|
51
|
+
* // Load agent keypair from environment (server-side only!)
|
|
52
|
+
* const agentKeypair = Keypair.fromSecretKey(
|
|
53
|
+
* bs58.decode(process.env.AGENT_KEYPAIR_SECRET!)
|
|
54
|
+
* );
|
|
55
|
+
*
|
|
56
|
+
* const connection = new Connection('https://api.devnet.solana.com');
|
|
57
|
+
*
|
|
58
|
+
* const result = await executeAgentPayment({
|
|
59
|
+
* connection,
|
|
60
|
+
* agentKeypair,
|
|
61
|
+
* recipientAddress: 'RecipientWallet...',
|
|
62
|
+
* amountLamports: 20_000_000n, // 0.02 SOL
|
|
63
|
+
* priorityFee: { enabled: true, microLamports: 5000 },
|
|
64
|
+
* });
|
|
65
|
+
*
|
|
66
|
+
* if (result.success) {
|
|
67
|
+
* console.log('Payment sent:', result.signature);
|
|
68
|
+
* }
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function executeAgentPayment(params: ExecuteAgentPaymentParams): Promise<AgentPaymentResult>;
|
|
72
|
+
/**
|
|
73
|
+
* Get the agent wallet's SOL balance
|
|
74
|
+
*/
|
|
75
|
+
declare function getAgentBalance(connection: Connection, agentKeypair: Keypair): Promise<{
|
|
76
|
+
balance: bigint;
|
|
77
|
+
balanceSol: number;
|
|
78
|
+
}>;
|
|
79
|
+
/**
|
|
80
|
+
* Check if agent has sufficient balance for a payment
|
|
81
|
+
*/
|
|
82
|
+
declare function hasAgentSufficientBalance(connection: Connection, agentKeypair: Keypair, requiredLamports: bigint): Promise<{
|
|
83
|
+
sufficient: boolean;
|
|
84
|
+
balance: bigint;
|
|
85
|
+
required: bigint;
|
|
86
|
+
}>;
|
|
87
|
+
/**
|
|
88
|
+
* Create a Keypair from a base58-encoded secret key string
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* const keypair = keypairFromBase58(process.env.AGENT_KEYPAIR_SECRET!);
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
declare function keypairFromBase58(base58Secret: string): Keypair;
|
|
96
|
+
/**
|
|
97
|
+
* Create a new random Keypair for agent use
|
|
98
|
+
* Returns both the keypair and its base58-encoded secret for storage
|
|
99
|
+
*/
|
|
100
|
+
declare function generateAgentKeypair(): {
|
|
101
|
+
keypair: Keypair;
|
|
102
|
+
secretBase58: string;
|
|
103
|
+
publicKey: string;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** Credit claims stored in session JWT */
|
|
107
|
+
interface CreditSessionClaims {
|
|
108
|
+
/** Number of remaining credits */
|
|
109
|
+
credits: number;
|
|
110
|
+
/** Unix timestamp when bundle expires (optional) */
|
|
111
|
+
bundleExpiry?: number;
|
|
112
|
+
/** Bundle type identifier (e.g., 'starter', 'pro') */
|
|
113
|
+
bundleType?: string;
|
|
114
|
+
}
|
|
115
|
+
/** Extended session data with credit information */
|
|
116
|
+
type CreditSessionData = SessionData & CreditSessionClaims;
|
|
117
|
+
/** Configuration for credit sessions */
|
|
118
|
+
interface CreditSessionConfig extends SessionConfig {
|
|
119
|
+
/** Initial number of credits */
|
|
120
|
+
initialCredits: number;
|
|
121
|
+
/** Bundle expiry in hours (optional, defaults to session duration) */
|
|
122
|
+
bundleExpiryHours?: number;
|
|
123
|
+
/** Bundle type identifier */
|
|
124
|
+
bundleType?: string;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Create a new credit session after bundle purchase
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* import { createCreditSession } from '@alleyboss/micropay-solana-x402-paywall/agent';
|
|
132
|
+
*
|
|
133
|
+
* const { token, session } = await createCreditSession(
|
|
134
|
+
* walletAddress,
|
|
135
|
+
* 'bundle-purchase',
|
|
136
|
+
* {
|
|
137
|
+
* secret: process.env.SESSION_SECRET!,
|
|
138
|
+
* durationHours: 24 * 30, // 30 days
|
|
139
|
+
* initialCredits: 10,
|
|
140
|
+
* bundleType: 'starter',
|
|
141
|
+
* }
|
|
142
|
+
* );
|
|
143
|
+
*
|
|
144
|
+
* // Set as cookie
|
|
145
|
+
* response.cookies.set('x402_credits', token, { httpOnly: true });
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
declare function createCreditSession(walletAddress: string, purchaseId: string, config: CreditSessionConfig): Promise<{
|
|
149
|
+
token: string;
|
|
150
|
+
session: CreditSessionData;
|
|
151
|
+
}>;
|
|
152
|
+
/** Result of validating a credit session */
|
|
153
|
+
interface CreditValidation {
|
|
154
|
+
valid: boolean;
|
|
155
|
+
session?: CreditSessionData;
|
|
156
|
+
reason?: string;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Validate a credit session token
|
|
160
|
+
*/
|
|
161
|
+
declare function validateCreditSession(token: string, secret: string): Promise<CreditValidation>;
|
|
162
|
+
/** Result of using a credit */
|
|
163
|
+
interface UseCreditResult {
|
|
164
|
+
/** Whether the credit was successfully used */
|
|
165
|
+
success: boolean;
|
|
166
|
+
/** Remaining credits after use */
|
|
167
|
+
remainingCredits: number;
|
|
168
|
+
/** New token with decremented credits (if successful) */
|
|
169
|
+
newToken?: string;
|
|
170
|
+
/** Error message (if failed) */
|
|
171
|
+
error?: string;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Use one credit from the session
|
|
175
|
+
* Returns a new token with decremented credit count
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* import { useCredit } from '@alleyboss/micropay-solana-x402-paywall/agent';
|
|
180
|
+
*
|
|
181
|
+
* const result = await useCredit(token, process.env.SESSION_SECRET!);
|
|
182
|
+
*
|
|
183
|
+
* if (result.success) {
|
|
184
|
+
* console.log(`Credit used. ${result.remainingCredits} remaining`);
|
|
185
|
+
* // Update cookie with new token
|
|
186
|
+
* response.cookies.set('x402_credits', result.newToken!);
|
|
187
|
+
* } else {
|
|
188
|
+
* console.log('No credits:', result.error);
|
|
189
|
+
* }
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
declare function useCredit(token: string, secret: string, creditsToUse?: number): Promise<UseCreditResult>;
|
|
193
|
+
/**
|
|
194
|
+
* Add credits to an existing session
|
|
195
|
+
* Useful for top-ups or rewards
|
|
196
|
+
*/
|
|
197
|
+
declare function addCredits(token: string, secret: string, creditsToAdd: number): Promise<{
|
|
198
|
+
success: boolean;
|
|
199
|
+
newToken?: string;
|
|
200
|
+
totalCredits?: number;
|
|
201
|
+
error?: string;
|
|
202
|
+
}>;
|
|
203
|
+
/**
|
|
204
|
+
* Get remaining credits from a session token (quick check without full validation)
|
|
205
|
+
*/
|
|
206
|
+
declare function getRemainingCredits(token: string, secret: string): Promise<{
|
|
207
|
+
credits: number;
|
|
208
|
+
valid: boolean;
|
|
209
|
+
bundleExpiry?: number;
|
|
210
|
+
}>;
|
|
211
|
+
|
|
212
|
+
export { type AgentPaymentResult, type CreditSessionClaims, type CreditSessionConfig, type CreditSessionData, type CreditValidation, type ExecuteAgentPaymentParams, type UseCreditResult, addCredits, createCreditSession, executeAgentPayment, generateAgentKeypair, getAgentBalance, getRemainingCredits, hasAgentSufficientBalance, keypairFromBase58, useCredit, validateCreditSession };
|