aipp-node 1.2.0 → 1.2.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.
@@ -0,0 +1,75 @@
1
+ export interface AippConfig {
2
+ apiKey: string;
3
+ baseUrl?: string;
4
+ }
5
+ export interface ChargeParams {
6
+ amountSats?: number;
7
+ amountUsd?: number;
8
+ memo?: string;
9
+ protocol?: 'L402' | 'x402';
10
+ }
11
+ export interface ChargeResponse {
12
+ payment_hash: string;
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;
20
+ }
21
+ export interface ChargeStatus {
22
+ paid: boolean;
23
+ status: 'pending' | 'settled';
24
+ preimage: string | null;
25
+ }
26
+ export interface AippErrorResponse {
27
+ error: string;
28
+ code?: string;
29
+ }
30
+ export interface PayoutResponse {
31
+ message: string;
32
+ amount_sats?: number;
33
+ amount_usd?: number;
34
+ }
35
+ export interface ReceiptCompliance {
36
+ regulation: string;
37
+ note: string;
38
+ }
39
+ export interface ReceiptFinancials {
40
+ currency: string;
41
+ total_amount: number;
42
+ merchant_amount: number;
43
+ platform_fee: number;
44
+ }
45
+ export interface ReceiptPaymentDetails {
46
+ protocol: string;
47
+ proof: string | null;
48
+ merchant_destination: string | null;
49
+ }
50
+ /** EU AI Act Article 26 compliant receipt for a settled invoice */
51
+ export interface ReceiptResponse {
52
+ receipt_id: string;
53
+ transaction_id: string;
54
+ date: string;
55
+ status: string;
56
+ compliance: ReceiptCompliance;
57
+ payment_details: ReceiptPaymentDetails;
58
+ financials: ReceiptFinancials;
59
+ }
60
+ export interface MarketplaceTool {
61
+ name: string;
62
+ description: string;
63
+ priceUsdt: number;
64
+ }
65
+ /** PaidMCP.dev compatible manifest for listing on AI agent marketplaces */
66
+ export interface MarketplaceManifest {
67
+ id: string;
68
+ name: string;
69
+ tagline: string;
70
+ description: string;
71
+ endpoint: string;
72
+ chains: string[];
73
+ tools: MarketplaceTool[];
74
+ tags: string[];
75
+ }
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,32 +1,32 @@
1
- {
2
- "name": "aipp-node",
3
- "version": "1.2.0",
4
- "description": "Official Node.js SDK for AIPP - The Lightning Network Split-Payment Gateway",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "types": "dist/index.d.ts",
8
- "scripts": {
9
- "build": "tsup src/index.ts --format cjs,esm --dts",
10
- "test": "vitest run"
11
- },
12
- "keywords": [
13
- "aipp",
14
- "bitcoin",
15
- "lightning",
16
- "payments",
17
- "l402",
18
- "ai"
19
- ],
20
- "author": "AIPP",
21
- "license": "MIT",
22
- "devDependencies": {
23
- "@types/node": "^20.0.0",
24
- "tsup": "^8.0.0",
25
- "typescript": "^5.0.0",
26
- "vitest": "^1.0.0"
27
- },
28
- "repository": {
29
- "type": "git",
30
- "url": "https://github.com/aippde/aipp-key.git"
31
- }
32
- }
1
+ {
2
+ "name": "aipp-node",
3
+ "version": "1.2.1",
4
+ "description": "Official Node.js SDK for AIPP - The Lightning Network Split-Payment Gateway",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "scripts": {
9
+ "build": "tsup src/index.ts --format cjs,esm --dts",
10
+ "test": "vitest run"
11
+ },
12
+ "keywords": [
13
+ "aipp",
14
+ "bitcoin",
15
+ "lightning",
16
+ "payments",
17
+ "l402",
18
+ "ai"
19
+ ],
20
+ "author": "AIPP",
21
+ "license": "MIT",
22
+ "devDependencies": {
23
+ "@types/node": "^20.0.0",
24
+ "tsup": "^8.0.0",
25
+ "typescript": "^5.0.0",
26
+ "vitest": "^1.0.0"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/aippde/aipp-key.git"
31
+ }
32
+ }
package/src/client.ts CHANGED
@@ -1,78 +1,101 @@
1
- import { AippConfig, ChargeParams, ChargeResponse, ChargeStatus, AippErrorResponse } from './types';
2
-
3
- export class Aipp {
4
- private apiKey: string;
5
- private baseUrl: string;
6
-
7
- constructor(config: AippConfig) {
8
- if (!config.apiKey) {
9
- throw new Error('AIPP: apiKey is required');
10
- }
11
- this.apiKey = config.apiKey;
12
- this.baseUrl = config.baseUrl || 'https://aipp.dev';
13
- }
14
-
15
- private async request<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
16
- const url = `${this.baseUrl}${endpoint}`;
17
- const headers = {
18
- 'Content-Type': 'application/json',
19
- 'X-Api-Key': this.apiKey,
20
- ...options.headers,
21
- };
22
-
23
- const response = await fetch(url, { ...options, headers });
24
-
25
- if (!response.ok) {
26
- let errorData: AippErrorResponse;
27
- try {
28
- errorData = await response.json();
29
- } catch (err) {
30
- throw new Error(`AIPP API Error: ${response.status} ${response.statusText}`);
31
- }
32
- throw new Error(`AIPP API Error: ${errorData.error || response.statusText}`);
33
- }
34
-
35
- return response.json() as Promise<T>;
36
- }
37
-
38
- /**
39
- * Creates a new Invoice (either L402 or x402)
40
- */
41
- async createCharge(params: ChargeParams): Promise<ChargeResponse> {
42
- if (!params.amountSats && !params.amountUsd) {
43
- throw new Error('AIPP: Either amountSats or amountUsd is required');
44
- }
45
-
46
- const body: any = { memo: params.memo };
47
- if (params.amountSats) body.amount_sats = params.amountSats;
48
- if (params.amountUsd) body.amount_usd = params.amountUsd;
49
- if (params.protocol) body.protocol = params.protocol;
50
-
51
- return this.request<ChargeResponse>('/invoice/create', {
52
- method: 'POST',
53
- body: JSON.stringify(body),
54
- });
55
- }
56
-
57
- /**
58
- * Checks the status of an existing charge
59
- */
60
- async getCharge(paymentHash: string, txHash?: string): Promise<ChargeStatus> {
61
- if (!paymentHash) {
62
- throw new Error('AIPP: paymentHash is required');
63
- }
64
- const query = txHash ? `?tx_hash=${txHash}` : '';
65
- return this.request<ChargeStatus>(`/invoice/status/${paymentHash}${query}`, {
66
- method: 'GET',
67
- });
68
- }
69
-
70
- /**
71
- * Triggers a manual withdrawal of your merchant balance
72
- */
73
- async payout(): Promise<import('./types').PayoutResponse> {
74
- return this.request<import('./types').PayoutResponse>('/merchant/payout', {
75
- method: 'POST',
76
- });
77
- }
78
- }
1
+ import { AippConfig, ChargeParams, ChargeResponse, ChargeStatus, AippErrorResponse, ReceiptResponse, MarketplaceManifest } from './types';
2
+
3
+ export class Aipp {
4
+ private apiKey: string;
5
+ private baseUrl: string;
6
+
7
+ constructor(config: AippConfig) {
8
+ if (!config.apiKey) {
9
+ throw new Error('AIPP: apiKey is required');
10
+ }
11
+ this.apiKey = config.apiKey;
12
+ this.baseUrl = config.baseUrl || 'https://aipp.dev';
13
+ }
14
+
15
+ private async request<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
16
+ const url = `${this.baseUrl}${endpoint}`;
17
+ const headers = {
18
+ 'Content-Type': 'application/json',
19
+ 'X-Api-Key': this.apiKey,
20
+ ...options.headers,
21
+ };
22
+
23
+ const response = await fetch(url, { ...options, headers });
24
+
25
+ if (!response.ok) {
26
+ let errorData: AippErrorResponse;
27
+ try {
28
+ errorData = await response.json();
29
+ } catch (err) {
30
+ throw new Error(`AIPP API Error: ${response.status} ${response.statusText}`);
31
+ }
32
+ throw new Error(`AIPP API Error: ${errorData.error || response.statusText}`);
33
+ }
34
+
35
+ return response.json() as Promise<T>;
36
+ }
37
+
38
+ /**
39
+ * Creates a new Invoice (either L402 or x402)
40
+ */
41
+ async createCharge(params: ChargeParams): Promise<ChargeResponse> {
42
+ if (!params.amountSats && !params.amountUsd) {
43
+ throw new Error('AIPP: Either amountSats or amountUsd is required');
44
+ }
45
+
46
+ const body: any = { memo: params.memo };
47
+ if (params.amountSats) body.amount_sats = params.amountSats;
48
+ if (params.amountUsd) body.amount_usd = params.amountUsd;
49
+ if (params.protocol) body.protocol = params.protocol;
50
+
51
+ return this.request<ChargeResponse>('/invoice/create', {
52
+ method: 'POST',
53
+ body: JSON.stringify(body),
54
+ });
55
+ }
56
+
57
+ /**
58
+ * Checks the status of an existing charge
59
+ */
60
+ async getCharge(paymentHash: string, txHash?: string): Promise<ChargeStatus> {
61
+ if (!paymentHash) {
62
+ throw new Error('AIPP: paymentHash is required');
63
+ }
64
+ const query = txHash ? `?tx_hash=${txHash}` : '';
65
+ return this.request<ChargeStatus>(`/invoice/status/${paymentHash}${query}`, {
66
+ method: 'GET',
67
+ });
68
+ }
69
+
70
+ /**
71
+ * Triggers a manual withdrawal of your merchant balance
72
+ */
73
+ async payout(): Promise<import('./types').PayoutResponse> {
74
+ return this.request<import('./types').PayoutResponse>('/merchant/payout', {
75
+ method: 'POST',
76
+ });
77
+ }
78
+
79
+ /**
80
+ * Retrieves an EU AI Act Article 26 compliant receipt for a settled invoice.
81
+ * Only available for invoices with status = 'settled'.
82
+ */
83
+ async getReceipt(paymentHash: string): Promise<ReceiptResponse> {
84
+ if (!paymentHash) {
85
+ throw new Error('AIPP: paymentHash is required');
86
+ }
87
+ return this.request<ReceiptResponse>(`/invoice/receipt/${paymentHash}`, {
88
+ method: 'GET',
89
+ });
90
+ }
91
+
92
+ /**
93
+ * Returns the PaidMCP.dev compatible marketplace manifest for this merchant.
94
+ * Use this JSON to list your AIPP-protected endpoints on AI agent directories.
95
+ */
96
+ async getMarketplaceManifest(): Promise<MarketplaceManifest> {
97
+ return this.request<MarketplaceManifest>('/paidmcp.json', {
98
+ method: 'GET',
99
+ });
100
+ }
101
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- export * from './client';
2
- export * from './types';
3
- export * from './middleware';
1
+ export * from './client';
2
+ export * from './types';
3
+ export * from './middleware';
package/src/jwt.ts CHANGED
@@ -1,48 +1,48 @@
1
- import crypto from 'crypto';
2
-
3
- function base64urlEncode(str: string | Buffer): string {
4
- const buf = Buffer.isBuffer(str) ? str : Buffer.from(str);
5
- return buf.toString('base64url');
6
- }
7
-
8
- function base64urlDecode(str: string): string {
9
- return Buffer.from(str, 'base64url').toString('utf8');
10
- }
11
-
12
- export function signJwt(payload: any, secret: string): string {
13
- const header = { alg: 'HS256', typ: 'JWT' };
14
- const encodedHeader = base64urlEncode(JSON.stringify(header));
15
- const encodedPayload = base64urlEncode(JSON.stringify(payload));
16
- const data = `${encodedHeader}.${encodedPayload}`;
17
-
18
- const signature = crypto.createHmac('sha256', secret).update(data).digest('base64url');
19
- return `${data}.${signature}`;
20
- }
21
-
22
- export function verifyJwt(token: string, secret: string): any {
23
- const parts = token.split('.');
24
- if (parts.length !== 3) {
25
- throw new Error('Invalid JWT format');
26
- }
27
-
28
- const [encodedHeader, encodedPayload, signature] = parts;
29
- const data = `${encodedHeader}.${encodedPayload}`;
30
-
31
- const expectedSignature = crypto.createHmac('sha256', secret).update(data).digest('base64url');
32
-
33
- // Constant time comparison to prevent timing attacks
34
- const signatureBuffer = Buffer.from(signature);
35
- const expectedBuffer = Buffer.from(expectedSignature);
36
-
37
- if (signatureBuffer.length !== expectedBuffer.length || !crypto.timingSafeEqual(signatureBuffer, expectedBuffer)) {
38
- throw new Error('Invalid JWT signature');
39
- }
40
-
41
- const payload = JSON.parse(base64urlDecode(encodedPayload));
42
-
43
- if (payload.exp && Date.now() >= payload.exp * 1000) {
44
- throw new Error('JWT expired');
45
- }
46
-
47
- return payload;
48
- }
1
+ import crypto from 'crypto';
2
+
3
+ function base64urlEncode(str: string | Buffer): string {
4
+ const buf = Buffer.isBuffer(str) ? str : Buffer.from(str);
5
+ return buf.toString('base64url');
6
+ }
7
+
8
+ function base64urlDecode(str: string): string {
9
+ return Buffer.from(str, 'base64url').toString('utf8');
10
+ }
11
+
12
+ export function signJwt(payload: any, secret: string): string {
13
+ const header = { alg: 'HS256', typ: 'JWT' };
14
+ const encodedHeader = base64urlEncode(JSON.stringify(header));
15
+ const encodedPayload = base64urlEncode(JSON.stringify(payload));
16
+ const data = `${encodedHeader}.${encodedPayload}`;
17
+
18
+ const signature = crypto.createHmac('sha256', secret).update(data).digest('base64url');
19
+ return `${data}.${signature}`;
20
+ }
21
+
22
+ export function verifyJwt(token: string, secret: string): any {
23
+ const parts = token.split('.');
24
+ if (parts.length !== 3) {
25
+ throw new Error('Invalid JWT format');
26
+ }
27
+
28
+ const [encodedHeader, encodedPayload, signature] = parts;
29
+ const data = `${encodedHeader}.${encodedPayload}`;
30
+
31
+ const expectedSignature = crypto.createHmac('sha256', secret).update(data).digest('base64url');
32
+
33
+ // Constant time comparison to prevent timing attacks
34
+ const signatureBuffer = Buffer.from(signature);
35
+ const expectedBuffer = Buffer.from(expectedSignature);
36
+
37
+ if (signatureBuffer.length !== expectedBuffer.length || !crypto.timingSafeEqual(signatureBuffer, expectedBuffer)) {
38
+ throw new Error('Invalid JWT signature');
39
+ }
40
+
41
+ const payload = JSON.parse(base64urlDecode(encodedPayload));
42
+
43
+ if (payload.exp && Date.now() >= payload.exp * 1000) {
44
+ throw new Error('JWT expired');
45
+ }
46
+
47
+ return payload;
48
+ }