@agent402-dev/pay-server 0.2.0 → 0.4.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/README.md +38 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +27 -0
- package/dist/gate.d.ts +8 -0
- package/dist/gate.js +21 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/receipt.d.ts +37 -0
- package/dist/receipt.js +50 -0
- package/dist/register.d.ts +3 -0
- package/dist/register.js +54 -0
- package/dist/registration.d.ts +29 -0
- package/dist/registration.js +13 -0
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -77,6 +77,44 @@ export default {
|
|
|
77
77
|
```
|
|
78
78
|
Rate stores: `memoryRateStore()` (default, tests), `fileRateStore(path)` from `@agent402-dev/pay-server/rates-file` (Node), or your own `RateStore` (for example a Cloudflare Durable Object; see the [Workers example](https://github.com/agent402-dev/agent402-toolkit/tree/main/packages/pay-server/examples/cloudflare-worker)). A Node example is in [`examples/node-server`](https://github.com/agent402-dev/agent402-toolkit/tree/main/packages/pay-server/examples/node-server).
|
|
79
79
|
|
|
80
|
+
## Signed receipts (optional)
|
|
81
|
+
After a settled payment the gate can add an `X-Agent402-Receipt` header: a small JSON record, signed with Ed25519, that binds the request (method, path, body hash), the hash of the response body you delivered, and the payment (network, asset, amount, recipient, transaction). Buyers can keep it as evidence for disputes and settlement reports; `@agent402-dev/pay-mcp` checks it automatically.
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { receiptSignerFromBytes } from '@agent402-dev/pay-server';
|
|
85
|
+
|
|
86
|
+
const signer = await receiptSignerFromBytes(receiptKeyBytes); // 64-byte Solana CLI key file, or a 32-byte seed
|
|
87
|
+
const gate = createPaymentGate({ /* ... */ receipts: { signer } });
|
|
88
|
+
```
|
|
89
|
+
- Use a key made only for receipts (for example `solana-keygen new -o receipt-key.json`), never the key of your receiving wallet. The gate never needs the receiving wallet's key.
|
|
90
|
+
- Publish the receipt key's public key (`signer.publicKey`) so buyers can tell that receipts come from you.
|
|
91
|
+
- If signing fails, the paid resource is still delivered, without a receipt.
|
|
92
|
+
|
|
93
|
+
## Seller registration and locks (optional)
|
|
94
|
+
Buyers using `@agent402-dev/pay-mcp` can require sellers to have an on-chain lock. To be eligible, lock tokens (PREQ, USDC or wrapped SOL) with [Jupiter Lock](https://lock.jup.ag/) to your own address with **no cancel and no recipient-change permission**, then serve a signed registration at `https://<your host>/.well-known/agent402.json`:
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { receiptSignerFromBytes, signRegistration } from '@agent402-dev/pay-server';
|
|
98
|
+
|
|
99
|
+
const lockKey = await receiptSignerFromBytes(lockRecipientKeyBytes); // offline; the key that receives the lock
|
|
100
|
+
const doc = await signRegistration({ host: 'api.example.com', payTo: ['YOUR_RECEIVING_ADDRESS'], locks: [{ protocol: 'jupiter-lock', account: 'LOCK_ESCROW_ADDRESS' }] }, [lockKey]);
|
|
101
|
+
// Serve JSON.stringify(doc) at /.well-known/agent402.json
|
|
102
|
+
```
|
|
103
|
+
Or from the command line (keys are read on your computer and never sent):
|
|
104
|
+
```bash
|
|
105
|
+
npx -y @agent402-dev/pay-server register --host api.example.com --pay-to <receiving address> \
|
|
106
|
+
--lock jupiter-lock:<lock escrow address> --key <lock recipient key file> --out agent402.json
|
|
107
|
+
```
|
|
108
|
+
- Sign offline and serve only the resulting document. The gate never needs the lock key or the receiving wallet's key.
|
|
109
|
+
- Use a lock received by a regular key. A lock received by a multisig vault cannot sign the registration yet.
|
|
110
|
+
- The format is the same for every asset. A lock is a commitment signal only; nothing can be confiscated.
|
|
111
|
+
|
|
112
|
+
## Data
|
|
113
|
+
- No telemetry and no analytics.
|
|
114
|
+
- It contacts only the facilitators you configure (`/supported`, `/verify`, `/settle`) and the RPC you pass to `rpcChain` (settlement checks, receiving token accounts, pool prices for the PREQ rate).
|
|
115
|
+
- Replay protection keeps payment identifiers in the ledger you configure (in memory by default). It stores no other buyer data.
|
|
116
|
+
- It writes nothing to the console except the A402 (v1) deprecation warning when you enable A402.
|
|
117
|
+
|
|
80
118
|
## License
|
|
81
119
|
Apache-2.0. See [LICENSE](https://github.com/agent402-dev/agent402-toolkit/blob/main/LICENSE) and [NOTICE](https://github.com/agent402-dev/agent402-toolkit/blob/main/NOTICE).
|
|
82
120
|
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// @agent402-dev/pay-server command line: register (signed seller registration). The library itself is used from code (createPaymentGate).
|
|
3
|
+
import { writeFileSync } from 'node:fs';
|
|
4
|
+
import { buildRegistration, REGISTER_HELP } from './register.js';
|
|
5
|
+
const [cmd, ...args] = process.argv.slice(2);
|
|
6
|
+
if (cmd === 'register') {
|
|
7
|
+
try {
|
|
8
|
+
const doc = await buildRegistration(args);
|
|
9
|
+
const json = JSON.stringify(doc, null, 2) + '\n';
|
|
10
|
+
const i = args.indexOf('--out');
|
|
11
|
+
if (i >= 0 && args[i + 1]) {
|
|
12
|
+
writeFileSync(args[i + 1], json, { flag: 'wx' });
|
|
13
|
+
console.error(`Wrote ${args[i + 1]}. Serve it at /.well-known/agent402.json`);
|
|
14
|
+
}
|
|
15
|
+
else
|
|
16
|
+
process.stdout.write(json);
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
console.error(e.code === 'EEXIST' ? 'The --out file already exists. It was not changed.' : e.message);
|
|
20
|
+
process.exitCode = 1;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
console.log(REGISTER_HELP);
|
|
25
|
+
if (cmd && cmd !== 'help' && cmd !== '--help')
|
|
26
|
+
process.exitCode = 2;
|
|
27
|
+
}
|
package/dist/gate.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { type FacilitatorClient } from '@x402/core/server';
|
|
|
2
2
|
import type { PaymentRequirements } from '@x402/core/types';
|
|
3
3
|
import { type ChainReader } from './chain.js';
|
|
4
4
|
import { type DailyRate } from './pricing.js';
|
|
5
|
+
import { type ReceiptSigner } from './receipt.js';
|
|
5
6
|
import { type Asset, type X402Ledger } from './x402.js';
|
|
6
7
|
/** A402 is deprecated (v1) */
|
|
7
8
|
export type AssetSymbol = 'USDC' | 'PREQ' | 'A402';
|
|
@@ -82,6 +83,13 @@ export interface GateConfig {
|
|
|
82
83
|
now?: () => number;
|
|
83
84
|
/** Retry intervals for the on-chain recheck (default 1, 2, 4 s) */
|
|
84
85
|
confirmDelaysMs?: number[];
|
|
86
|
+
/**
|
|
87
|
+
* Signed receipts (optional): after a settled payment, adds the X-Agent402-Receipt header signed with this key.
|
|
88
|
+
* Use a key made only for receipts (receiptSignerFromBytes), never the receiving wallet's key
|
|
89
|
+
*/
|
|
90
|
+
receipts?: {
|
|
91
|
+
signer: ReceiptSigner;
|
|
92
|
+
};
|
|
85
93
|
}
|
|
86
94
|
export interface ReceivingAccount {
|
|
87
95
|
asset: AssetSymbol;
|
package/dist/gate.js
CHANGED
|
@@ -9,6 +9,7 @@ import { decodePaymentSignatureHeader, encodePaymentRequiredHeader } from '@x402
|
|
|
9
9
|
import { HTTPFacilitatorClient } from '@x402/core/server';
|
|
10
10
|
import { ataOf } from './chain.js';
|
|
11
11
|
import { tokenAmountFor } from './pricing.js';
|
|
12
|
+
import { RECEIPT_HEADER, sha256Hex, signReceipt } from './receipt.js';
|
|
12
13
|
import { A402_DEPRECATION_WARNING, PREQ_DECIMALS, PREQ_MAINNET_MINT, PREQ_TOKEN_PROGRAM } from './preq.js';
|
|
13
14
|
import { buildRequirements, chooseFacilitator, memoryX402Ledger, paymentResponseHeader, settleVerified, verifyPaymentHeader, } from './x402.js';
|
|
14
15
|
/**
|
|
@@ -199,6 +200,8 @@ export function createPaymentGate(c) {
|
|
|
199
200
|
}
|
|
200
201
|
// 2) Handler (time limit). No settlement on failure, timeout, or a 4xx/5xx response
|
|
201
202
|
const payment = v.payment;
|
|
203
|
+
// Copy of the request for the receipt's request body hash (the handler may read the original body)
|
|
204
|
+
const reqCopy = c.receipts && req.body ? req.clone() : null;
|
|
202
205
|
const info = { asset: payment.accepted.asset, amount: payment.accepted.amount, payTo: payment.accepted.payTo, network: payment.accepted.network };
|
|
203
206
|
let res;
|
|
204
207
|
let timer;
|
|
@@ -234,7 +237,24 @@ export function createPaymentGate(c) {
|
|
|
234
237
|
h.set('payment-response', paymentResponseHeader(s.settlement));
|
|
235
238
|
h.set('x-payment-confirmation', s.confirmation);
|
|
236
239
|
h.set('access-control-expose-headers', 'PAYMENT-RESPONSE, X-Payment-Confirmation');
|
|
237
|
-
|
|
240
|
+
if (!c.receipts)
|
|
241
|
+
return new Response(res.body, { status: res.status, headers: h });
|
|
242
|
+
const body = await res.arrayBuffer();
|
|
243
|
+
try {
|
|
244
|
+
const reqBody = reqCopy ? await reqCopy.arrayBuffer() : null;
|
|
245
|
+
const token = await signReceipt(c.receipts.signer, {
|
|
246
|
+
issuedAt: new Date(now()).toISOString(),
|
|
247
|
+
request: { method: req.method, path: `${url.pathname}${url.search}`, bodySha256: reqBody && reqBody.byteLength ? await sha256Hex(reqBody) : null },
|
|
248
|
+
response: { status: res.status, bodySha256: await sha256Hex(body) },
|
|
249
|
+
payment: { network: info.network, asset: info.asset, amount: info.amount, payTo: info.payTo, transaction: s.settlement.transaction || null },
|
|
250
|
+
});
|
|
251
|
+
h.set(RECEIPT_HEADER, token);
|
|
252
|
+
h.set('access-control-expose-headers', 'PAYMENT-RESPONSE, X-Payment-Confirmation, X-Agent402-Receipt');
|
|
253
|
+
}
|
|
254
|
+
catch {
|
|
255
|
+
// Receipts are optional: the paid resource is still delivered without one
|
|
256
|
+
}
|
|
257
|
+
return new Response(body, { status: res.status, headers: h });
|
|
238
258
|
};
|
|
239
259
|
}
|
|
240
260
|
return { withPayment, quote, checkReceivingAccounts };
|
package/dist/index.d.ts
CHANGED
|
@@ -3,4 +3,6 @@ export { PREQ_MAINNET_MINT, PREQ_MAINNET_POOL, A402_DEPRECATION_WARNING } from '
|
|
|
3
3
|
export { rpcChain, receivedBy, type ChainReader, type ChainTx, type AccountFetcher } from './chain.js';
|
|
4
4
|
export { createRateBook, memoryRateStore, type RateBook, type RateStore } from './rates.js';
|
|
5
5
|
export { DEFAULT_GUARDS, fixRate, samplePrice, tokenAmountFor, PREQ_POOL_TARGET, A402_POOL_TARGET, type DailyRate, type PoolTarget, type PriceSample, type RateGuards, } from './pricing.js';
|
|
6
|
+
export { receiptSignerFromBytes, signReceipt, RECEIPT_HEADER, type Receipt, type ReceiptSigner } from './receipt.js';
|
|
7
|
+
export { signRegistration, REGISTRATION_PATH, type RegistrationDocument, type RegistrationPayload } from './registration.js';
|
|
6
8
|
export { memoryX402Ledger, ledgerClaim, SETTLE_UNKNOWN_EXPIRY_MS, type X402Ledger, type LedgerEntry, type Asset } from './x402.js';
|
package/dist/index.js
CHANGED
|
@@ -5,4 +5,6 @@ export { PREQ_MAINNET_MINT, PREQ_MAINNET_POOL, A402_DEPRECATION_WARNING } from '
|
|
|
5
5
|
export { rpcChain, receivedBy } from './chain.js';
|
|
6
6
|
export { createRateBook, memoryRateStore } from './rates.js';
|
|
7
7
|
export { DEFAULT_GUARDS, fixRate, samplePrice, tokenAmountFor, PREQ_POOL_TARGET, A402_POOL_TARGET, } from './pricing.js';
|
|
8
|
+
export { receiptSignerFromBytes, signReceipt, RECEIPT_HEADER } from './receipt.js';
|
|
9
|
+
export { signRegistration, REGISTRATION_PATH } from './registration.js';
|
|
8
10
|
export { memoryX402Ledger, ledgerClaim, SETTLE_UNKNOWN_EXPIRY_MS } from './x402.js';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export declare const RECEIPT_HEADER = "x-agent402-receipt";
|
|
2
|
+
export declare const RECEIPT_KIND = "agent402.x402-receipt";
|
|
3
|
+
export interface Receipt {
|
|
4
|
+
v: 1;
|
|
5
|
+
kind: typeof RECEIPT_KIND;
|
|
6
|
+
issuedAt: string;
|
|
7
|
+
/** path is the URL path and query as the server received it */
|
|
8
|
+
request: {
|
|
9
|
+
method: string;
|
|
10
|
+
path: string;
|
|
11
|
+
bodySha256: string | null;
|
|
12
|
+
};
|
|
13
|
+
response: {
|
|
14
|
+
status: number;
|
|
15
|
+
bodySha256: string;
|
|
16
|
+
};
|
|
17
|
+
payment: {
|
|
18
|
+
network: string;
|
|
19
|
+
asset: string;
|
|
20
|
+
amount: string;
|
|
21
|
+
payTo: string;
|
|
22
|
+
transaction: string | null;
|
|
23
|
+
};
|
|
24
|
+
/** Base58 Ed25519 public key of the receipt key */
|
|
25
|
+
signer: string;
|
|
26
|
+
}
|
|
27
|
+
export interface ReceiptSigner {
|
|
28
|
+
publicKey: string;
|
|
29
|
+
sign(data: Uint8Array): Promise<Uint8Array>;
|
|
30
|
+
}
|
|
31
|
+
export declare function b64url(u: Uint8Array): string;
|
|
32
|
+
export declare function b64urlDecode(s: string): Uint8Array;
|
|
33
|
+
export declare function sha256Hex(data: ArrayBuffer | Uint8Array): Promise<string>;
|
|
34
|
+
/** Receipt key from a Solana CLI key file (64 bytes: seed + public key) or a 32-byte seed */
|
|
35
|
+
export declare function receiptSignerFromBytes(bytes: Uint8Array): Promise<ReceiptSigner>;
|
|
36
|
+
/** Signs a receipt → header value "<base64url JSON>.<base64url signature>" (the signature covers the exact JSON bytes) */
|
|
37
|
+
export declare function signReceipt(signer: ReceiptSigner, r: Omit<Receipt, 'v' | 'kind' | 'signer'>): Promise<string>;
|
package/dist/receipt.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Signed receipts (optional): after a settled payment the seller signs a small JSON record that binds
|
|
2
|
+
// the request, the hash of the response body and the payment. Buyers can keep it as evidence for disputes and settlement reports.
|
|
3
|
+
// Ed25519 through WebCrypto (Node 20+, Cloudflare Workers). Use a key made only for receipts, never the receiving wallet's key.
|
|
4
|
+
import { PublicKey } from '@solana/web3.js';
|
|
5
|
+
export const RECEIPT_HEADER = 'x-agent402-receipt';
|
|
6
|
+
export const RECEIPT_KIND = 'agent402.x402-receipt';
|
|
7
|
+
const PKCS8_ED25519_PREFIX = [0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x04, 0x22, 0x04, 0x20];
|
|
8
|
+
export function b64url(u) {
|
|
9
|
+
let s = '';
|
|
10
|
+
for (const b of u)
|
|
11
|
+
s += String.fromCharCode(b);
|
|
12
|
+
return btoa(s).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
13
|
+
}
|
|
14
|
+
export function b64urlDecode(s) {
|
|
15
|
+
const b = atob(s.replace(/-/g, '+').replace(/_/g, '/') + '='.repeat((4 - (s.length % 4)) % 4));
|
|
16
|
+
return Uint8Array.from(b, (c) => c.charCodeAt(0));
|
|
17
|
+
}
|
|
18
|
+
export async function sha256Hex(data) {
|
|
19
|
+
const d = new Uint8Array(await crypto.subtle.digest('SHA-256', data));
|
|
20
|
+
return Array.from(d, (b) => b.toString(16).padStart(2, '0')).join('');
|
|
21
|
+
}
|
|
22
|
+
/** Receipt key from a Solana CLI key file (64 bytes: seed + public key) or a 32-byte seed */
|
|
23
|
+
export async function receiptSignerFromBytes(bytes) {
|
|
24
|
+
if (bytes.length !== 64 && bytes.length !== 32)
|
|
25
|
+
throw new Error('receipt key must be 64 bytes (Solana CLI key file) or a 32-byte seed');
|
|
26
|
+
const pkcs8 = new Uint8Array(48);
|
|
27
|
+
pkcs8.set(PKCS8_ED25519_PREFIX);
|
|
28
|
+
pkcs8.set(bytes.subarray(0, 32), 16);
|
|
29
|
+
try {
|
|
30
|
+
// Exportable copy only to read the public key; the signing key is not exportable
|
|
31
|
+
const tmp = await crypto.subtle.importKey('pkcs8', pkcs8, { name: 'Ed25519' }, true, ['sign']);
|
|
32
|
+
const pub = b64urlDecode((await crypto.subtle.exportKey('jwk', tmp)).x);
|
|
33
|
+
if (bytes.length === 64 && b64url(pub) !== b64url(bytes.subarray(32)))
|
|
34
|
+
throw new Error('receipt key: the public key does not match the secret');
|
|
35
|
+
const key = await crypto.subtle.importKey('pkcs8', pkcs8, { name: 'Ed25519' }, false, ['sign']);
|
|
36
|
+
return {
|
|
37
|
+
publicKey: new PublicKey(pub).toBase58(),
|
|
38
|
+
async sign(data) { return new Uint8Array(await crypto.subtle.sign('Ed25519', key, data)); },
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
finally {
|
|
42
|
+
pkcs8.fill(0);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** Signs a receipt → header value "<base64url JSON>.<base64url signature>" (the signature covers the exact JSON bytes) */
|
|
46
|
+
export async function signReceipt(signer, r) {
|
|
47
|
+
const receipt = { v: 1, kind: RECEIPT_KIND, ...r, signer: signer.publicKey };
|
|
48
|
+
const bytes = new TextEncoder().encode(JSON.stringify(receipt));
|
|
49
|
+
return `${b64url(bytes)}.${b64url(await signer.sign(bytes))}`;
|
|
50
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type RegistrationDocument } from './registration.js';
|
|
2
|
+
export declare const REGISTER_HELP = "agent402-pay-server register \u2014 make a signed seller registration (asset-neutral)\n\n --host <host> host buyers call, e.g. api.example.com (required)\n --pay-to <address>[,<address>] receiving addresses (required)\n --lock <protocol>:<account> a lock account; protocol jupiter-lock or streamflow (repeat for more)\n --key <file> Solana CLI key file of a lock recipient; signs the registration (repeat for more)\n --receipt-signer <public key> receipt key buyers should expect (optional, repeatable)\n --expires-days <n> expiry in days (optional)\n --out <file> write here (never overwrites); default: print to the terminal\n\nServe the output at https://<host>/.well-known/agent402.json. Keys stay on this computer.";
|
|
3
|
+
export declare function buildRegistration(argv: string[], now?: Date, read?: (p: string) => string): Promise<RegistrationDocument>;
|
package/dist/register.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// `agent402-pay-server register`: makes the signed seller registration (/.well-known/agent402.json) from key files on this computer.
|
|
2
|
+
// Keys are read locally and never sent anywhere. Sign with the key that receives each lock (not the receiving wallet's key, unless it is the same).
|
|
3
|
+
import { readFileSync } from 'node:fs';
|
|
4
|
+
import { receiptSignerFromBytes } from './receipt.js';
|
|
5
|
+
import { signRegistration } from './registration.js';
|
|
6
|
+
export const REGISTER_HELP = `agent402-pay-server register — make a signed seller registration (asset-neutral)
|
|
7
|
+
|
|
8
|
+
--host <host> host buyers call, e.g. api.example.com (required)
|
|
9
|
+
--pay-to <address>[,<address>] receiving addresses (required)
|
|
10
|
+
--lock <protocol>:<account> a lock account; protocol jupiter-lock or streamflow (repeat for more)
|
|
11
|
+
--key <file> Solana CLI key file of a lock recipient; signs the registration (repeat for more)
|
|
12
|
+
--receipt-signer <public key> receipt key buyers should expect (optional, repeatable)
|
|
13
|
+
--expires-days <n> expiry in days (optional)
|
|
14
|
+
--out <file> write here (never overwrites); default: print to the terminal
|
|
15
|
+
|
|
16
|
+
Serve the output at https://<host>/.well-known/agent402.json. Keys stay on this computer.`;
|
|
17
|
+
const BASE58 = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
|
|
18
|
+
export async function buildRegistration(argv, now = new Date(), read = (p) => readFileSync(p, 'utf8')) {
|
|
19
|
+
const all = (name) => argv.flatMap((a, i) => (a === name && argv[i + 1] ? [argv[i + 1]] : []));
|
|
20
|
+
const one = (name) => all(name)[0];
|
|
21
|
+
const host = one('--host');
|
|
22
|
+
if (!host || /[/:]/.test(host.replace(/:\d+$/, '')))
|
|
23
|
+
throw new Error('--host is required (a host name such as api.example.com, no scheme or path)');
|
|
24
|
+
const payTo = (one('--pay-to') ?? '').split(',').map((s) => s.trim()).filter(Boolean);
|
|
25
|
+
if (!payTo.length || payTo.some((a) => !BASE58.test(a)))
|
|
26
|
+
throw new Error('--pay-to needs one or more Solana addresses');
|
|
27
|
+
const locks = all('--lock').map((l) => {
|
|
28
|
+
const [protocol, account] = l.split(':');
|
|
29
|
+
if ((protocol !== 'jupiter-lock' && protocol !== 'streamflow') || !BASE58.test(account ?? ''))
|
|
30
|
+
throw new Error(`--lock ${l}: use jupiter-lock:<account> or streamflow:<account>`);
|
|
31
|
+
return { protocol, account };
|
|
32
|
+
});
|
|
33
|
+
const keyFiles = all('--key');
|
|
34
|
+
if (locks.length && !keyFiles.length)
|
|
35
|
+
throw new Error('--key is required: the lock recipient must sign, or buyers will not count the lock');
|
|
36
|
+
const signers = await Promise.all(keyFiles.map(async (f) => {
|
|
37
|
+
const bytes = Uint8Array.from(JSON.parse(read(f)));
|
|
38
|
+
try {
|
|
39
|
+
return await receiptSignerFromBytes(bytes);
|
|
40
|
+
}
|
|
41
|
+
finally {
|
|
42
|
+
bytes.fill(0);
|
|
43
|
+
}
|
|
44
|
+
}));
|
|
45
|
+
const receiptSigners = all('--receipt-signer');
|
|
46
|
+
if (receiptSigners.some((a) => !BASE58.test(a)))
|
|
47
|
+
throw new Error('--receipt-signer needs a base58 public key');
|
|
48
|
+
const days = Number(one('--expires-days'));
|
|
49
|
+
return signRegistration({
|
|
50
|
+
host, payTo, locks, issuedAt: now.toISOString(),
|
|
51
|
+
...(receiptSigners.length ? { receiptSigners } : {}),
|
|
52
|
+
...(days > 0 ? { expiresAt: new Date(now.getTime() + days * 86_400_000).toISOString() } : {}),
|
|
53
|
+
}, signers);
|
|
54
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type ReceiptSigner } from './receipt.js';
|
|
2
|
+
export declare const REGISTRATION_PATH = "/.well-known/agent402.json";
|
|
3
|
+
export declare const REGISTRATION_KIND = "agent402.seller-registration";
|
|
4
|
+
export interface RegistrationPayload {
|
|
5
|
+
v: 1;
|
|
6
|
+
kind: typeof REGISTRATION_KIND;
|
|
7
|
+
/** Host buyers call (e.g. api.example.com), lowercase */
|
|
8
|
+
host: string;
|
|
9
|
+
payTo: string[];
|
|
10
|
+
locks: {
|
|
11
|
+
protocol: 'jupiter-lock' | 'streamflow';
|
|
12
|
+
account: string;
|
|
13
|
+
}[];
|
|
14
|
+
receiptSigners?: string[];
|
|
15
|
+
issuedAt: string;
|
|
16
|
+
expiresAt?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface RegistrationDocument {
|
|
19
|
+
v: 1;
|
|
20
|
+
payload: string;
|
|
21
|
+
signatures: {
|
|
22
|
+
signer: string;
|
|
23
|
+
sig: string;
|
|
24
|
+
}[];
|
|
25
|
+
}
|
|
26
|
+
/** Signs a registration with each given key (usually the recipient key of every lock) */
|
|
27
|
+
export declare function signRegistration(p: Omit<RegistrationPayload, 'v' | 'kind' | 'issuedAt'> & {
|
|
28
|
+
issuedAt?: string;
|
|
29
|
+
}, signers: ReceiptSigner[]): Promise<RegistrationDocument>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Seller registration (Agent402 standard, asset-neutral): the JSON document a seller serves at https://<host>/.well-known/agent402.json
|
|
2
|
+
// It lists the receiving addresses and lock accounts, and each lock's recipient key signs it (so nobody can claim someone else's lock).
|
|
3
|
+
// @agent402-dev/pay-mcp reads it when a buyer requires a seller lock. Signing happens offline with the lock recipient's key; the gate never needs it.
|
|
4
|
+
import { b64url } from './receipt.js';
|
|
5
|
+
export const REGISTRATION_PATH = '/.well-known/agent402.json';
|
|
6
|
+
export const REGISTRATION_KIND = 'agent402.seller-registration';
|
|
7
|
+
/** Signs a registration with each given key (usually the recipient key of every lock) */
|
|
8
|
+
export async function signRegistration(p, signers) {
|
|
9
|
+
const payload = { v: 1, kind: REGISTRATION_KIND, ...p, host: p.host.toLowerCase(), issuedAt: p.issuedAt ?? new Date().toISOString() };
|
|
10
|
+
const bytes = new TextEncoder().encode(JSON.stringify(payload));
|
|
11
|
+
const signatures = await Promise.all(signers.map(async (s) => ({ signer: s.publicKey, sig: b64url(await s.sign(bytes)) })));
|
|
12
|
+
return { v: 1, payload: b64url(bytes), signatures };
|
|
13
|
+
}
|
package/package.json
CHANGED
|
@@ -8,6 +8,9 @@
|
|
|
8
8
|
"token-2022",
|
|
9
9
|
"payments"
|
|
10
10
|
],
|
|
11
|
+
"bin": {
|
|
12
|
+
"agent402-pay-server": "./dist/cli.js"
|
|
13
|
+
},
|
|
11
14
|
"main": "./dist/index.js",
|
|
12
15
|
"types": "./dist/index.d.ts",
|
|
13
16
|
"exports": {
|
|
@@ -25,7 +28,7 @@
|
|
|
25
28
|
"@x402/core": "2.31.0",
|
|
26
29
|
"@x402/svm": "2.31.0"
|
|
27
30
|
},
|
|
28
|
-
"version": "0.
|
|
31
|
+
"version": "0.4.0",
|
|
29
32
|
"author": "Agent402",
|
|
30
33
|
"license": "Apache-2.0",
|
|
31
34
|
"type": "module",
|