aipp-node 1.1.0 → 1.2.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/index.d.mts CHANGED
@@ -6,16 +6,22 @@ interface ChargeParams {
6
6
  amountSats?: number;
7
7
  amountUsd?: number;
8
8
  memo?: string;
9
+ protocol?: 'L402' | 'x402';
9
10
  }
10
11
  interface ChargeResponse {
11
- payment_request: string;
12
12
  payment_hash: string;
13
- amount_sats: number;
13
+ protocol: 'L402' | 'x402';
14
+ amount_usd?: number;
15
+ pay_to?: string;
16
+ network?: string;
17
+ token?: string;
18
+ payment_request?: string;
19
+ amount_sats?: number;
14
20
  }
15
21
  interface ChargeStatus {
22
+ paid: boolean;
16
23
  status: 'pending' | 'settled';
17
- payment_hash: string;
18
- amount_sats: number;
24
+ preimage: string | null;
19
25
  }
20
26
  interface AippErrorResponse {
21
27
  error: string;
@@ -24,6 +30,7 @@ interface AippErrorResponse {
24
30
  interface PayoutResponse {
25
31
  message: string;
26
32
  amount_sats?: number;
33
+ amount_usd?: number;
27
34
  }
28
35
 
29
36
  declare class Aipp {
@@ -32,13 +39,13 @@ declare class Aipp {
32
39
  constructor(config: AippConfig);
33
40
  private request;
34
41
  /**
35
- * Creates a new Lightning Invoice
42
+ * Creates a new Invoice (either L402 or x402)
36
43
  */
37
44
  createCharge(params: ChargeParams): Promise<ChargeResponse>;
38
45
  /**
39
46
  * Checks the status of an existing charge
40
47
  */
41
- getCharge(paymentHash: string): Promise<ChargeStatus>;
48
+ getCharge(paymentHash: string, txHash?: string): Promise<ChargeStatus>;
42
49
  /**
43
50
  * Triggers a manual withdrawal of your merchant balance
44
51
  */
@@ -54,5 +61,11 @@ interface L402Options {
54
61
  expiresInSeconds?: number;
55
62
  }
56
63
  declare function l402Paywall(options: L402Options): (req: any, res: any, next: any) => Promise<any>;
64
+ interface X402Options {
65
+ client: Aipp;
66
+ resourceId: string;
67
+ amountUsd: number;
68
+ }
69
+ declare function x402Paywall(options: X402Options): (req: any, res: any, next: any) => Promise<any>;
57
70
 
58
- export { Aipp, type AippConfig, type AippErrorResponse, type ChargeParams, type ChargeResponse, type ChargeStatus, type L402Options, type PayoutResponse, l402Paywall };
71
+ export { Aipp, type AippConfig, type AippErrorResponse, type ChargeParams, type ChargeResponse, type ChargeStatus, type L402Options, type PayoutResponse, type X402Options, l402Paywall, x402Paywall };
package/dist/index.d.ts CHANGED
@@ -6,16 +6,22 @@ interface ChargeParams {
6
6
  amountSats?: number;
7
7
  amountUsd?: number;
8
8
  memo?: string;
9
+ protocol?: 'L402' | 'x402';
9
10
  }
10
11
  interface ChargeResponse {
11
- payment_request: string;
12
12
  payment_hash: string;
13
- amount_sats: number;
13
+ protocol: 'L402' | 'x402';
14
+ amount_usd?: number;
15
+ pay_to?: string;
16
+ network?: string;
17
+ token?: string;
18
+ payment_request?: string;
19
+ amount_sats?: number;
14
20
  }
15
21
  interface ChargeStatus {
22
+ paid: boolean;
16
23
  status: 'pending' | 'settled';
17
- payment_hash: string;
18
- amount_sats: number;
24
+ preimage: string | null;
19
25
  }
20
26
  interface AippErrorResponse {
21
27
  error: string;
@@ -24,6 +30,7 @@ interface AippErrorResponse {
24
30
  interface PayoutResponse {
25
31
  message: string;
26
32
  amount_sats?: number;
33
+ amount_usd?: number;
27
34
  }
28
35
 
29
36
  declare class Aipp {
@@ -32,13 +39,13 @@ declare class Aipp {
32
39
  constructor(config: AippConfig);
33
40
  private request;
34
41
  /**
35
- * Creates a new Lightning Invoice
42
+ * Creates a new Invoice (either L402 or x402)
36
43
  */
37
44
  createCharge(params: ChargeParams): Promise<ChargeResponse>;
38
45
  /**
39
46
  * Checks the status of an existing charge
40
47
  */
41
- getCharge(paymentHash: string): Promise<ChargeStatus>;
48
+ getCharge(paymentHash: string, txHash?: string): Promise<ChargeStatus>;
42
49
  /**
43
50
  * Triggers a manual withdrawal of your merchant balance
44
51
  */
@@ -54,5 +61,11 @@ interface L402Options {
54
61
  expiresInSeconds?: number;
55
62
  }
56
63
  declare function l402Paywall(options: L402Options): (req: any, res: any, next: any) => Promise<any>;
64
+ interface X402Options {
65
+ client: Aipp;
66
+ resourceId: string;
67
+ amountUsd: number;
68
+ }
69
+ declare function x402Paywall(options: X402Options): (req: any, res: any, next: any) => Promise<any>;
57
70
 
58
- export { Aipp, type AippConfig, type AippErrorResponse, type ChargeParams, type ChargeResponse, type ChargeStatus, type L402Options, type PayoutResponse, l402Paywall };
71
+ export { Aipp, type AippConfig, type AippErrorResponse, type ChargeParams, type ChargeResponse, type ChargeStatus, type L402Options, type PayoutResponse, type X402Options, l402Paywall, x402Paywall };
package/dist/index.js CHANGED
@@ -31,7 +31,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var index_exports = {};
32
32
  __export(index_exports, {
33
33
  Aipp: () => Aipp,
34
- l402Paywall: () => l402Paywall
34
+ l402Paywall: () => l402Paywall,
35
+ x402Paywall: () => x402Paywall
35
36
  });
36
37
  module.exports = __toCommonJS(index_exports);
37
38
 
@@ -66,7 +67,7 @@ var Aipp = class {
66
67
  return response.json();
67
68
  }
68
69
  /**
69
- * Creates a new Lightning Invoice
70
+ * Creates a new Invoice (either L402 or x402)
70
71
  */
71
72
  async createCharge(params) {
72
73
  if (!params.amountSats && !params.amountUsd) {
@@ -75,6 +76,7 @@ var Aipp = class {
75
76
  const body = { memo: params.memo };
76
77
  if (params.amountSats) body.amount_sats = params.amountSats;
77
78
  if (params.amountUsd) body.amount_usd = params.amountUsd;
79
+ if (params.protocol) body.protocol = params.protocol;
78
80
  return this.request("/invoice/create", {
79
81
  method: "POST",
80
82
  body: JSON.stringify(body)
@@ -83,11 +85,12 @@ var Aipp = class {
83
85
  /**
84
86
  * Checks the status of an existing charge
85
87
  */
86
- async getCharge(paymentHash) {
88
+ async getCharge(paymentHash, txHash) {
87
89
  if (!paymentHash) {
88
90
  throw new Error("AIPP: paymentHash is required");
89
91
  }
90
- return this.request(`/invoice/status/${paymentHash}`, {
92
+ const query = txHash ? `?tx_hash=${txHash}` : "";
93
+ return this.request(`/invoice/status/${paymentHash}${query}`, {
91
94
  method: "GET"
92
95
  });
93
96
  }
@@ -196,8 +199,67 @@ function l402Paywall(options) {
196
199
  }
197
200
  };
198
201
  }
202
+ function x402Paywall(options) {
203
+ return async (req, res, next) => {
204
+ const authHeader = req.headers.authorization || req.headers.Authorization;
205
+ let txHash = req.query.tx_hash || req.headers["payment-signature"] || req.headers["x-payment-signature"];
206
+ let paymentHash = req.query.payment_hash || req.headers["x-payment-hash"];
207
+ if (!txHash && authHeader && typeof authHeader === "string") {
208
+ if (authHeader.startsWith("Bearer ")) {
209
+ txHash = authHeader.substring(7).trim();
210
+ } else if (authHeader.startsWith("x402 ")) {
211
+ txHash = authHeader.substring(5).trim();
212
+ } else if (authHeader.startsWith("L402 ")) {
213
+ const parts = authHeader.substring(5).split(":");
214
+ if (parts.length === 2) {
215
+ paymentHash = parts[0];
216
+ txHash = parts[1];
217
+ }
218
+ }
219
+ }
220
+ if (paymentHash && txHash) {
221
+ try {
222
+ const chargeStatus = await options.client.getCharge(paymentHash, txHash);
223
+ if (chargeStatus.status === "settled") {
224
+ return next();
225
+ }
226
+ } catch (e) {
227
+ }
228
+ }
229
+ try {
230
+ const charge = await options.client.createCharge({
231
+ amountUsd: options.amountUsd,
232
+ protocol: "x402",
233
+ memo: `x402 Payment for ${options.resourceId}`
234
+ });
235
+ const challengeObj = {
236
+ scheme: "exact",
237
+ network: "base",
238
+ payTo: charge.pay_to || "",
239
+ price: options.amountUsd.toFixed(2),
240
+ token: charge.token || "",
241
+ payment_hash: charge.payment_hash
242
+ };
243
+ const challengeBase64 = Buffer.from(JSON.stringify(challengeObj), "utf8").toString("base64");
244
+ res.setHeader("PAYMENT-REQUIRED", challengeBase64);
245
+ res.status(402);
246
+ res.json({
247
+ error: "Payment Required",
248
+ code: "x402",
249
+ payment_hash: charge.payment_hash,
250
+ pay_to: challengeObj.payTo,
251
+ price: challengeObj.price,
252
+ token: challengeObj.token,
253
+ network: challengeObj.network
254
+ });
255
+ } catch (err) {
256
+ res.status(500).json({ error: "Failed to generate x402 challenge", details: err.message || err });
257
+ }
258
+ };
259
+ }
199
260
  // Annotate the CommonJS export names for ESM import in node:
200
261
  0 && (module.exports = {
201
262
  Aipp,
202
- l402Paywall
263
+ l402Paywall,
264
+ x402Paywall
203
265
  });
package/dist/index.mjs CHANGED
@@ -29,7 +29,7 @@ var Aipp = class {
29
29
  return response.json();
30
30
  }
31
31
  /**
32
- * Creates a new Lightning Invoice
32
+ * Creates a new Invoice (either L402 or x402)
33
33
  */
34
34
  async createCharge(params) {
35
35
  if (!params.amountSats && !params.amountUsd) {
@@ -38,6 +38,7 @@ var Aipp = class {
38
38
  const body = { memo: params.memo };
39
39
  if (params.amountSats) body.amount_sats = params.amountSats;
40
40
  if (params.amountUsd) body.amount_usd = params.amountUsd;
41
+ if (params.protocol) body.protocol = params.protocol;
41
42
  return this.request("/invoice/create", {
42
43
  method: "POST",
43
44
  body: JSON.stringify(body)
@@ -46,11 +47,12 @@ var Aipp = class {
46
47
  /**
47
48
  * Checks the status of an existing charge
48
49
  */
49
- async getCharge(paymentHash) {
50
+ async getCharge(paymentHash, txHash) {
50
51
  if (!paymentHash) {
51
52
  throw new Error("AIPP: paymentHash is required");
52
53
  }
53
- return this.request(`/invoice/status/${paymentHash}`, {
54
+ const query = txHash ? `?tx_hash=${txHash}` : "";
55
+ return this.request(`/invoice/status/${paymentHash}${query}`, {
54
56
  method: "GET"
55
57
  });
56
58
  }
@@ -159,7 +161,66 @@ function l402Paywall(options) {
159
161
  }
160
162
  };
161
163
  }
164
+ function x402Paywall(options) {
165
+ return async (req, res, next) => {
166
+ const authHeader = req.headers.authorization || req.headers.Authorization;
167
+ let txHash = req.query.tx_hash || req.headers["payment-signature"] || req.headers["x-payment-signature"];
168
+ let paymentHash = req.query.payment_hash || req.headers["x-payment-hash"];
169
+ if (!txHash && authHeader && typeof authHeader === "string") {
170
+ if (authHeader.startsWith("Bearer ")) {
171
+ txHash = authHeader.substring(7).trim();
172
+ } else if (authHeader.startsWith("x402 ")) {
173
+ txHash = authHeader.substring(5).trim();
174
+ } else if (authHeader.startsWith("L402 ")) {
175
+ const parts = authHeader.substring(5).split(":");
176
+ if (parts.length === 2) {
177
+ paymentHash = parts[0];
178
+ txHash = parts[1];
179
+ }
180
+ }
181
+ }
182
+ if (paymentHash && txHash) {
183
+ try {
184
+ const chargeStatus = await options.client.getCharge(paymentHash, txHash);
185
+ if (chargeStatus.status === "settled") {
186
+ return next();
187
+ }
188
+ } catch (e) {
189
+ }
190
+ }
191
+ try {
192
+ const charge = await options.client.createCharge({
193
+ amountUsd: options.amountUsd,
194
+ protocol: "x402",
195
+ memo: `x402 Payment for ${options.resourceId}`
196
+ });
197
+ const challengeObj = {
198
+ scheme: "exact",
199
+ network: "base",
200
+ payTo: charge.pay_to || "",
201
+ price: options.amountUsd.toFixed(2),
202
+ token: charge.token || "",
203
+ payment_hash: charge.payment_hash
204
+ };
205
+ const challengeBase64 = Buffer.from(JSON.stringify(challengeObj), "utf8").toString("base64");
206
+ res.setHeader("PAYMENT-REQUIRED", challengeBase64);
207
+ res.status(402);
208
+ res.json({
209
+ error: "Payment Required",
210
+ code: "x402",
211
+ payment_hash: charge.payment_hash,
212
+ pay_to: challengeObj.payTo,
213
+ price: challengeObj.price,
214
+ token: challengeObj.token,
215
+ network: challengeObj.network
216
+ });
217
+ } catch (err) {
218
+ res.status(500).json({ error: "Failed to generate x402 challenge", details: err.message || err });
219
+ }
220
+ };
221
+ }
162
222
  export {
163
223
  Aipp,
164
- l402Paywall
224
+ l402Paywall,
225
+ x402Paywall
165
226
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aipp-node",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Official Node.js SDK for AIPP - The Lightning Network Split-Payment Gateway",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
package/src/client.ts CHANGED
@@ -36,7 +36,7 @@ export class Aipp {
36
36
  }
37
37
 
38
38
  /**
39
- * Creates a new Lightning Invoice
39
+ * Creates a new Invoice (either L402 or x402)
40
40
  */
41
41
  async createCharge(params: ChargeParams): Promise<ChargeResponse> {
42
42
  if (!params.amountSats && !params.amountUsd) {
@@ -46,6 +46,7 @@ export class Aipp {
46
46
  const body: any = { memo: params.memo };
47
47
  if (params.amountSats) body.amount_sats = params.amountSats;
48
48
  if (params.amountUsd) body.amount_usd = params.amountUsd;
49
+ if (params.protocol) body.protocol = params.protocol;
49
50
 
50
51
  return this.request<ChargeResponse>('/invoice/create', {
51
52
  method: 'POST',
@@ -56,11 +57,12 @@ export class Aipp {
56
57
  /**
57
58
  * Checks the status of an existing charge
58
59
  */
59
- async getCharge(paymentHash: string): Promise<ChargeStatus> {
60
+ async getCharge(paymentHash: string, txHash?: string): Promise<ChargeStatus> {
60
61
  if (!paymentHash) {
61
62
  throw new Error('AIPP: paymentHash is required');
62
63
  }
63
- return this.request<ChargeStatus>(`/invoice/status/${paymentHash}`, {
64
+ const query = txHash ? `?tx_hash=${txHash}` : '';
65
+ return this.request<ChargeStatus>(`/invoice/status/${paymentHash}${query}`, {
64
66
  method: 'GET',
65
67
  });
66
68
  }
package/src/middleware.ts CHANGED
@@ -84,3 +84,76 @@ export function l402Paywall(options: L402Options) {
84
84
  }
85
85
  };
86
86
  }
87
+
88
+ export interface X402Options {
89
+ client: Aipp;
90
+ resourceId: string;
91
+ amountUsd: number;
92
+ }
93
+
94
+ export function x402Paywall(options: X402Options) {
95
+ return async (req: any, res: any, next: any) => {
96
+ const authHeader = req.headers.authorization || req.headers.Authorization;
97
+ let txHash = (req.query.tx_hash || req.headers['payment-signature'] || req.headers['x-payment-signature']) as string;
98
+ let paymentHash = (req.query.payment_hash || req.headers['x-payment-hash']) as string;
99
+
100
+ if (!txHash && authHeader && typeof authHeader === 'string') {
101
+ if (authHeader.startsWith('Bearer ')) {
102
+ txHash = authHeader.substring(7).trim();
103
+ } else if (authHeader.startsWith('x402 ')) {
104
+ txHash = authHeader.substring(5).trim();
105
+ } else if (authHeader.startsWith('L402 ')) {
106
+ // Fallback or double format check
107
+ const parts = authHeader.substring(5).split(':');
108
+ if (parts.length === 2) {
109
+ paymentHash = parts[0];
110
+ txHash = parts[1];
111
+ }
112
+ }
113
+ }
114
+
115
+ if (paymentHash && txHash) {
116
+ try {
117
+ const chargeStatus = await options.client.getCharge(paymentHash, txHash);
118
+ if (chargeStatus.status === 'settled') {
119
+ return next();
120
+ }
121
+ } catch (e) {
122
+ // Validation failed, proceed to challenge
123
+ }
124
+ }
125
+
126
+ try {
127
+ const charge = await options.client.createCharge({
128
+ amountUsd: options.amountUsd,
129
+ protocol: 'x402',
130
+ memo: `x402 Payment for ${options.resourceId}`
131
+ });
132
+
133
+ const challengeObj = {
134
+ scheme: 'exact',
135
+ network: 'base',
136
+ payTo: charge.pay_to || '',
137
+ price: options.amountUsd.toFixed(2),
138
+ token: charge.token || '',
139
+ payment_hash: charge.payment_hash
140
+ };
141
+
142
+ const challengeBase64 = Buffer.from(JSON.stringify(challengeObj), 'utf8').toString('base64');
143
+ res.setHeader('PAYMENT-REQUIRED', challengeBase64);
144
+
145
+ res.status(402);
146
+ res.json({
147
+ error: "Payment Required",
148
+ code: "x402",
149
+ payment_hash: charge.payment_hash,
150
+ pay_to: challengeObj.payTo,
151
+ price: challengeObj.price,
152
+ token: challengeObj.token,
153
+ network: challengeObj.network
154
+ });
155
+ } catch (err: any) {
156
+ res.status(500).json({ error: "Failed to generate x402 challenge", details: err.message || err });
157
+ }
158
+ };
159
+ }
package/src/types.ts CHANGED
@@ -7,18 +7,24 @@ export interface ChargeParams {
7
7
  amountSats?: number;
8
8
  amountUsd?: number;
9
9
  memo?: string;
10
+ protocol?: 'L402' | 'x402';
10
11
  }
11
12
 
12
13
  export interface ChargeResponse {
13
- payment_request: string;
14
14
  payment_hash: string;
15
- amount_sats: number;
15
+ protocol: 'L402' | 'x402';
16
+ amount_usd?: number;
17
+ pay_to?: string;
18
+ network?: string;
19
+ token?: string;
20
+ payment_request?: string; // For L402
21
+ amount_sats?: number; // For L402
16
22
  }
17
23
 
18
24
  export interface ChargeStatus {
25
+ paid: boolean;
19
26
  status: 'pending' | 'settled';
20
- payment_hash: string;
21
- amount_sats: number;
27
+ preimage: string | null;
22
28
  }
23
29
 
24
30
  export interface AippErrorResponse {
@@ -29,4 +35,5 @@ export interface AippErrorResponse {
29
35
  export interface PayoutResponse {
30
36
  message: string;
31
37
  amount_sats?: number;
38
+ amount_usd?: number;
32
39
  }