mppx 0.19.0 → 0.20.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.
Files changed (47) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/Errors.d.ts +29 -0
  3. package/dist/Errors.js +27 -2
  4. package/dist/cli/cli.js +96 -96
  5. package/dist/cli/plugins/index.js +12 -12
  6. package/dist/extensions/identity/erc8004/Constants.d.ts +20 -0
  7. package/dist/extensions/identity/erc8004/Constants.js +20 -0
  8. package/dist/extensions/identity/erc8004/Types.d.ts +11 -0
  9. package/dist/extensions/identity/erc8004/Types.js +2 -0
  10. package/dist/extensions/identity/erc8004/client/index.d.ts +33 -0
  11. package/dist/extensions/identity/erc8004/client/index.js +33 -0
  12. package/dist/extensions/identity/erc8004/index.d.ts +5 -0
  13. package/dist/extensions/identity/erc8004/index.js +4 -0
  14. package/dist/extensions/identity/erc8004/internal/Agent.d.ts +21 -0
  15. package/dist/extensions/identity/erc8004/internal/Agent.js +24 -0
  16. package/dist/extensions/identity/erc8004/server/index.d.ts +44 -0
  17. package/dist/extensions/identity/erc8004/server/index.js +178 -0
  18. package/dist/extensions/identity/index.d.ts +2 -0
  19. package/dist/extensions/identity/index.js +2 -0
  20. package/dist/extensions/identity/poppy/Constants.d.ts +26 -0
  21. package/dist/extensions/identity/poppy/Constants.js +26 -0
  22. package/dist/extensions/identity/poppy/Types.d.ts +19 -0
  23. package/dist/extensions/identity/poppy/Types.js +2 -0
  24. package/dist/extensions/identity/poppy/client/index.d.ts +69 -0
  25. package/dist/extensions/identity/poppy/client/index.js +81 -0
  26. package/dist/extensions/identity/poppy/index.d.ts +7 -0
  27. package/dist/extensions/identity/poppy/index.js +7 -0
  28. package/dist/extensions/identity/poppy/internal/Jws.d.ts +12 -0
  29. package/dist/extensions/identity/poppy/internal/Jws.js +78 -0
  30. package/dist/extensions/identity/poppy/server/index.d.ts +40 -0
  31. package/dist/extensions/identity/poppy/server/index.js +181 -0
  32. package/dist/identity/Headers.d.ts +1 -1
  33. package/dist/identity/Headers.js +3 -0
  34. package/dist/internal/version.d.ts +2 -2
  35. package/dist/internal/version.js +1 -1
  36. package/dist/proxy/Proxy.js +47 -9
  37. package/dist/tempo/client/Charge.js +7 -4
  38. package/dist/tempo/internal/simulation.d.ts +33 -0
  39. package/dist/tempo/internal/simulation.js +80 -0
  40. package/dist/tempo/server/internal/html.gen.js +1 -1
  41. package/dist/tempo/session/client/ChannelOps.js +5 -2
  42. package/dist/tempo/session/server/CredentialVerification.d.ts +2 -0
  43. package/dist/tempo/session/server/CredentialVerification.js +44 -0
  44. package/dist/tempo/session/server/MeteredStream.d.ts +2 -0
  45. package/dist/tempo/session/server/MeteredStream.js +11 -0
  46. package/dist/tempo/session/server/Sse.js +9 -4
  47. package/package.json +9 -1
@@ -0,0 +1,20 @@
1
+ /** ERC-8004 values used by the agent identity provider. */
2
+ export declare const Constants: {
3
+ /** Identity type advertised in a 402's `Identity-Required` header. */
4
+ readonly type: 'erc8004';
5
+ /** Chain the default registry is on: Tempo mainnet. */
6
+ readonly chainId: 4217;
7
+ /** The ERC-8004 Identity Registry, at the same address on every chain that deploys it. */
8
+ readonly identityRegistry: '0x8004A169FB4a3325136EB29fA0ceB6D2e539a432';
9
+ /** Default RPC for Tempo mainnet. */
10
+ readonly rpcUrl: 'https://rpc.tempo.xyz';
11
+ /** Carries the agent's ID: `eip155:<chainId>:<registry>:<agentId>`. */
12
+ readonly idHeader: 'Agent-Id';
13
+ /** Carries `<created>.<nonce>.<signature>`: the agent wallet's signature of the request. */
14
+ readonly signatureHeader: 'Agent-Signature';
15
+ /** How long, in seconds, a request signature stays valid. */
16
+ readonly maxAge: 300;
17
+ /** Allowed clock skew, in seconds. */
18
+ readonly clockTolerance: 60;
19
+ };
20
+ //# sourceMappingURL=Constants.d.ts.map
@@ -0,0 +1,20 @@
1
+ /** ERC-8004 values used by the agent identity provider. */
2
+ export const Constants = {
3
+ /** Identity type advertised in a 402's `Identity-Required` header. */
4
+ type: 'erc8004',
5
+ /** Chain the default registry is on: Tempo mainnet. */
6
+ chainId: 4217,
7
+ /** The ERC-8004 Identity Registry, at the same address on every chain that deploys it. */
8
+ identityRegistry: '0x8004A169FB4a3325136EB29fA0ceB6D2e539a432',
9
+ /** Default RPC for Tempo mainnet. */
10
+ rpcUrl: 'https://rpc.tempo.xyz',
11
+ /** Carries the agent's ID: `eip155:<chainId>:<registry>:<agentId>`. */
12
+ idHeader: 'Agent-Id',
13
+ /** Carries `<created>.<nonce>.<signature>`: the agent wallet's signature of the request. */
14
+ signatureHeader: 'Agent-Signature',
15
+ /** How long, in seconds, a request signature stays valid. */
16
+ maxAge: 300,
17
+ /** Allowed clock skew, in seconds. */
18
+ clockTolerance: 60,
19
+ };
20
+ //# sourceMappingURL=Constants.js.map
@@ -0,0 +1,11 @@
1
+ import type { Address } from 'viem';
2
+ /** An ERC-8004 agent whose wallet signed the request or paid. */
3
+ export type Agent = {
4
+ /** The agent's global ID: `eip155:<chainId>:<registry>:<agentId>`. */
5
+ readonly id: string;
6
+ /** The agent's token ID in the Identity Registry. */
7
+ readonly agentId: bigint;
8
+ /** The agent's wallet, from the registry's `getAgentWallet`, checksummed. */
9
+ readonly wallet: Address;
10
+ };
11
+ //# sourceMappingURL=Types.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=Types.js.map
@@ -0,0 +1,33 @@
1
+ import type { Address, Hex } from 'viem';
2
+ import type * as Identity from '../../../../identity/Types.js';
3
+ /**
4
+ * Creates a prover that signs each request as an ERC-8004 agent, with the agent's wallet.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * const mppx = Mppx.create({
9
+ * identity: [Erc8004.Client.prover({ account, agentId: 42n, scope: ['https://api.example'] })],
10
+ * methods: [tempo({ account })],
11
+ * })
12
+ * ```
13
+ */
14
+ export declare function prover(config: prover.Config): Identity.Prover;
15
+ export declare namespace prover {
16
+ type Config = {
17
+ /** The agent's wallet, such as a viem `privateKeyToAccount` account. It also pays. */
18
+ readonly account: {
19
+ readonly signMessage: (parameters: {
20
+ message: string;
21
+ }) => Promise<Hex>;
22
+ };
23
+ /** The agent's token ID in the Identity Registry. */
24
+ readonly agentId: bigint | number;
25
+ /** Origins that receive the agent's signature, or `'*'` for any origin. */
26
+ readonly scope: Identity.Prover['scope'];
27
+ /** The registry's chain. @default 4217 (Tempo mainnet) */
28
+ readonly chainId?: number | undefined;
29
+ /** The Identity Registry. @default '0x8004A169FB4a3325136EB29fA0ceB6D2e539a432' */
30
+ readonly registry?: Address | undefined;
31
+ };
32
+ }
33
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,33 @@
1
+ import { Constants } from '../Constants.js';
2
+ import * as Agent from '../internal/Agent.js';
3
+ /**
4
+ * Creates a prover that signs each request as an ERC-8004 agent, with the agent's wallet.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * const mppx = Mppx.create({
9
+ * identity: [Erc8004.Client.prover({ account, agentId: 42n, scope: ['https://api.example'] })],
10
+ * methods: [tempo({ account })],
11
+ * })
12
+ * ```
13
+ */
14
+ export function prover(config) {
15
+ const id = Agent.format({
16
+ agentId: BigInt(config.agentId),
17
+ chainId: config.chainId ?? Constants.chainId,
18
+ registry: config.registry ?? Constants.identityRegistry,
19
+ });
20
+ return {
21
+ scope: config.scope,
22
+ async prove(request, { created, nonce }) {
23
+ const signature = await config.account.signMessage({
24
+ message: Agent.message({ created, id, nonce, request }),
25
+ });
26
+ const headers = new Headers(request.headers);
27
+ headers.set(Constants.idHeader, id);
28
+ headers.set(Constants.signatureHeader, `${created}.${nonce}.${signature}`);
29
+ return new Request(request, { headers });
30
+ },
31
+ };
32
+ }
33
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,5 @@
1
+ export * as Client from './client/index.js';
2
+ export { Constants } from './Constants.js';
3
+ export * as Server from './server/index.js';
4
+ export type * from './Types.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,4 @@
1
+ export * as Client from './client/index.js';
2
+ export { Constants } from './Constants.js';
3
+ export * as Server from './server/index.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,21 @@
1
+ import { type Address } from 'viem';
2
+ /** Formats an agent's global ID: `eip155:<chainId>:<registry>:<agentId>`. */
3
+ export declare function format(agent: {
4
+ chainId: number;
5
+ registry: Address;
6
+ agentId: bigint;
7
+ }): string;
8
+ /** Parses an agent's global ID, or returns `undefined` when it is malformed. */
9
+ export declare function parse(value: string): {
10
+ chainId: number;
11
+ registry: `0x${string}`;
12
+ agentId: bigint;
13
+ } | undefined;
14
+ /** The message an agent's wallet signs (EIP-191) for one request attempt. */
15
+ export declare function message(parameters: {
16
+ id: string;
17
+ request: Request;
18
+ created: number;
19
+ nonce: string;
20
+ }): string;
21
+ //# sourceMappingURL=Agent.d.ts.map
@@ -0,0 +1,24 @@
1
+ import { getAddress, isAddress } from 'viem';
2
+ /** Formats an agent's global ID: `eip155:<chainId>:<registry>:<agentId>`. */
3
+ export function format(agent) {
4
+ return `eip155:${agent.chainId}:${getAddress(agent.registry)}:${agent.agentId}`;
5
+ }
6
+ /** Parses an agent's global ID, or returns `undefined` when it is malformed. */
7
+ export function parse(value) {
8
+ const [, chainId, registry, agentId] = /^eip155:(\d{1,15}):(0x[0-9a-fA-F]{40}):(\d{1,78})$/.exec(value) ?? [];
9
+ if (!chainId || !registry || !agentId || !isAddress(registry, { strict: false }))
10
+ return undefined;
11
+ return { chainId: Number(chainId), registry: getAddress(registry), agentId: BigInt(agentId) };
12
+ }
13
+ /** The message an agent's wallet signs (EIP-191) for one request attempt. */
14
+ export function message(parameters) {
15
+ const url = new URL(parameters.request.url);
16
+ return [
17
+ 'mppx:erc8004',
18
+ parameters.id,
19
+ `${parameters.request.method} ${url.origin}${url.pathname}${url.search}`,
20
+ parameters.created,
21
+ parameters.nonce,
22
+ ].join('\n');
23
+ }
24
+ //# sourceMappingURL=Agent.js.map
@@ -0,0 +1,44 @@
1
+ import { type Address, type Client } from 'viem';
2
+ import type * as Identity from '../../../../identity/Types.js';
3
+ import * as Store from '../../../../Store.js';
4
+ import type * as Types from '../Types.js';
5
+ /**
6
+ * Creates an ERC-8004 agent verifier for one API.
7
+ *
8
+ * - `verifier` requires a request signed by the agent's wallet, as the Identity Registry's
9
+ * `getAgentWallet` names it: an EOA signature, or ERC-1271 for a contract or configurable account.
10
+ * - `paymentCheck` accepts a `tempo` charge the agent's wallet signed (a transaction or proof), so
11
+ * the payment itself proves the agent. For anything else (no Credential, transaction hashes,
12
+ * sessions, Zones, other methods) it requires the signature.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * const agents = Erc8004.Server.create({ store })
17
+ * mppx.charge({ amount: '0.01', identity: { request: [agents.verifier], payment: agents.paymentCheck } })
18
+ * ```
19
+ */
20
+ export declare function create(config?: create.Config): Erc8004;
21
+ export declare namespace create {
22
+ type Config = {
23
+ /** Decides whether a verified agent may call the API, such as by its ID or reputation. */
24
+ readonly allow?: ((agent: Types.Agent) => boolean | Promise<boolean>) | undefined;
25
+ /** The registry's chain. Payments must settle on it too. @default 4217 (Tempo mainnet) */
26
+ readonly chainId?: number | undefined;
27
+ /** A viem client for the registry's chain. @default an HTTP client for `rpcUrl` */
28
+ readonly client?: Client | undefined;
29
+ /** The Identity Registry. @default '0x8004A169FB4a3325136EB29fA0ceB6D2e539a432' */
30
+ readonly registry?: Address | undefined;
31
+ /** RPC URL of the registry's chain. @default 'https://rpc.tempo.xyz' */
32
+ readonly rpcUrl?: string | undefined;
33
+ /** Accepts each request signature once. Without it, a signature can be replayed for six minutes. */
34
+ readonly store?: Store.AtomicStore | undefined;
35
+ };
36
+ }
37
+ /** An ERC-8004 agent verifier from {@link create}. */
38
+ export type Erc8004 = {
39
+ /** A request-stage verifier: requires a request signed by the agent's wallet, before any 402. */
40
+ readonly verifier: Identity.Verifier<Types.Agent>;
41
+ /** A payment check: requires that the agent's wallet paid. */
42
+ readonly paymentCheck: Identity.PaymentCheck<Types.Agent>;
43
+ };
44
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,178 @@
1
+ import { createClient, getAddress, hashMessage, http, isAddressEqual, recoverMessageAddress, zeroAddress, } from 'viem';
2
+ import { readContract } from 'viem/actions';
3
+ import * as Store from '../../../../Store.js';
4
+ import { Constants } from '../Constants.js';
5
+ import * as Agent from '../internal/Agent.js';
6
+ const abi = [
7
+ {
8
+ type: 'function',
9
+ name: 'getAgentWallet',
10
+ stateMutability: 'view',
11
+ inputs: [{ name: 'agentId', type: 'uint256' }],
12
+ outputs: [{ name: '', type: 'address' }],
13
+ },
14
+ {
15
+ type: 'function',
16
+ name: 'isValidSignature',
17
+ stateMutability: 'view',
18
+ inputs: [
19
+ { name: 'hash', type: 'bytes32' },
20
+ { name: 'signature', type: 'bytes' },
21
+ ],
22
+ outputs: [{ name: '', type: 'bytes4' }],
23
+ },
24
+ ];
25
+ /**
26
+ * Creates an ERC-8004 agent verifier for one API.
27
+ *
28
+ * - `verifier` requires a request signed by the agent's wallet, as the Identity Registry's
29
+ * `getAgentWallet` names it: an EOA signature, or ERC-1271 for a contract or configurable account.
30
+ * - `paymentCheck` accepts a `tempo` charge the agent's wallet signed (a transaction or proof), so
31
+ * the payment itself proves the agent. For anything else (no Credential, transaction hashes,
32
+ * sessions, Zones, other methods) it requires the signature.
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * const agents = Erc8004.Server.create({ store })
37
+ * mppx.charge({ amount: '0.01', identity: { request: [agents.verifier], payment: agents.paymentCheck } })
38
+ * ```
39
+ */
40
+ export function create(config = {}) {
41
+ const chainId = config.chainId ?? Constants.chainId;
42
+ const registry = getAddress(config.registry ?? Constants.identityRegistry);
43
+ if (!config.client && !config.rpcUrl && chainId !== Constants.chainId)
44
+ throw new TypeError('Pass `client` or `rpcUrl` for a chain other than Tempo mainnet.');
45
+ const client = config.client ?? createClient({ transport: http(config.rpcUrl ?? Constants.rpcUrl) });
46
+ const store = config.store;
47
+ // One lookup per request, so the verifier and payment check share it and spend a nonce once.
48
+ const agents = new WeakMap();
49
+ const signed = new WeakMap();
50
+ const invalid = (reason) => ({ status: 'invalid', reason });
51
+ /** Reads the request's agent and its wallet from the registry. */
52
+ async function lookup(request) {
53
+ const id = request.headers.get(Constants.idHeader);
54
+ if (!id)
55
+ return { status: 'missing', reason: 'Sign the request as an ERC-8004 agent.' };
56
+ const agent = Agent.parse(id);
57
+ if (!agent || agent.chainId !== chainId || !isAddressEqual(agent.registry, registry))
58
+ return invalid(`The agent must be in the Identity Registry ${registry} on chain ${chainId}.`);
59
+ let wallet;
60
+ try {
61
+ wallet = await readContract(client, {
62
+ abi,
63
+ address: registry,
64
+ args: [agent.agentId],
65
+ functionName: 'getAgentWallet',
66
+ });
67
+ }
68
+ catch {
69
+ return { status: 'unavailable', reason: 'The Identity Registry is unavailable.' };
70
+ }
71
+ if (isAddressEqual(wallet, zeroAddress))
72
+ return { status: 'forbidden', reason: 'The agent has no wallet.' };
73
+ return {
74
+ status: 'valid',
75
+ value: { agentId: agent.agentId, id: Agent.format(agent), wallet: getAddress(wallet) },
76
+ };
77
+ }
78
+ /** Checks that the agent's wallet signed this request. */
79
+ async function verifySignature(request) {
80
+ const result = await getAgent(request);
81
+ if (result.status !== 'valid')
82
+ return result;
83
+ const [created = '', nonce = '', signature = '', ...rest] = (request.headers.get(Constants.signatureHeader) ?? '').split('.');
84
+ if (rest.length > 0 ||
85
+ !/^\d{1,12}$/.test(created) ||
86
+ !nonce ||
87
+ !/^0x[0-9a-fA-F]+$/.test(signature))
88
+ return invalid('The agent signature is missing or malformed.');
89
+ const now = Math.floor(Date.now() / 1_000);
90
+ const expires = Number(created) + Constants.maxAge + Constants.clockTolerance;
91
+ if (Number(created) > now + Constants.clockTolerance || expires < now)
92
+ return invalid('The agent signature has expired.');
93
+ const message = Agent.message({
94
+ created: Number(created),
95
+ id: request.headers.get(Constants.idHeader),
96
+ nonce,
97
+ request,
98
+ });
99
+ if (!(await signedBy(result.value.wallet, message, signature)))
100
+ return invalid("The agent's wallet did not sign the request.");
101
+ if (store) {
102
+ const key = `mppx:erc8004:nonce:${hashMessage(`${result.value.id}:${nonce}`)}`;
103
+ const claimed = await Promise.resolve()
104
+ .then(() => Store.tryClaim(store, key, expires * 1_000))
105
+ .catch(() => null);
106
+ if (claimed === null)
107
+ return { status: 'unavailable', reason: 'The replay store is unavailable.' };
108
+ if (!claimed)
109
+ return invalid('The agent signature was already used.');
110
+ }
111
+ return result;
112
+ }
113
+ /** Returns whether `wallet` signed `message`: by EOA recovery, else by ERC-1271. */
114
+ async function signedBy(wallet, message, signature) {
115
+ const recovered = await recoverMessageAddress({ message, signature }).catch(() => undefined);
116
+ if (recovered && isAddressEqual(recovered, wallet))
117
+ return true;
118
+ const magic = await readContract(client, {
119
+ abi,
120
+ address: wallet,
121
+ args: [hashMessage(message), signature],
122
+ functionName: 'isValidSignature',
123
+ }).catch(() => undefined);
124
+ return magic === '0x1626ba7e';
125
+ }
126
+ function getAgent(request) {
127
+ let result = agents.get(request);
128
+ if (!result)
129
+ agents.set(request, (result = lookup(request)));
130
+ return result;
131
+ }
132
+ function getSigned(request) {
133
+ let result = signed.get(request);
134
+ if (!result)
135
+ signed.set(request, (result = verifySignature(request)));
136
+ return result;
137
+ }
138
+ /** Runs `allow` on a verified agent. */
139
+ async function allowed(result) {
140
+ if (result.status !== 'valid' || !config.allow || (await config.allow(result.value)))
141
+ return result;
142
+ return { status: 'forbidden', reason: 'This agent may not call this API.' };
143
+ }
144
+ return {
145
+ verifier: { verify: async (request) => allowed(await getSigned(request)) },
146
+ paymentCheck: {
147
+ requires: [{ type: Constants.type }],
148
+ async verify({ challenge, credential, request }) {
149
+ // Only a tempo charge the source signed binds it to the payer: a transaction or a proof.
150
+ // A hash credential doesn't (the charge's validateSender can accept another payer), and
151
+ // neither do sessions, Zones or other methods, so they need the agent's signature instead.
152
+ const type = credential?.payload?.type;
153
+ if (!credential ||
154
+ challenge.method !== 'tempo' ||
155
+ challenge.intent !== 'charge' ||
156
+ (type !== 'transaction' && type !== 'proof'))
157
+ return allowed(await getSigned(request));
158
+ const agent = await getAgent(request);
159
+ if (agent.status !== 'valid')
160
+ return agent;
161
+ // The source is a did:pkh:eip155:<chainId>:<address>.
162
+ const parts = credential.source?.split(':') ?? [];
163
+ const [did, pkh, namespace, chain, address = ''] = parts;
164
+ if (parts.length !== 5 ||
165
+ did !== 'did' ||
166
+ pkh !== 'pkh' ||
167
+ namespace !== 'eip155' ||
168
+ chain !== String(chainId) ||
169
+ !/^0x[0-9a-fA-F]{40}$/.test(address))
170
+ return invalid(`The payment must name its source account on chain ${chainId}.`);
171
+ if (!isAddressEqual(address, agent.value.wallet))
172
+ return { status: 'forbidden', reason: "The payment is not from the agent's wallet." };
173
+ return allowed(agent);
174
+ },
175
+ },
176
+ };
177
+ }
178
+ //# sourceMappingURL=index.js.map
@@ -2,6 +2,8 @@
2
2
  export * as Attestation from './attestation/index.js';
3
3
  /** Email address verification with a one-time code. */
4
4
  export * as EmailOtp from './email-otp/index.js';
5
+ /** ERC-8004 agents: requests signed, and payments made, by an agent's registered wallet. */
6
+ export * as Erc8004 from './erc8004/index.js';
5
7
  /** Link agent identity: attestation tokens and Link-signed claims. */
6
8
  export * as Link from './link/index.js';
7
9
  /** Persona KYC: a payment check that requires an approved Persona inquiry. */
@@ -2,6 +2,8 @@
2
2
  export * as Attestation from './attestation/index.js';
3
3
  /** Email address verification with a one-time code. */
4
4
  export * as EmailOtp from './email-otp/index.js';
5
+ /** ERC-8004 agents: requests signed, and payments made, by an agent's registered wallet. */
6
+ export * as Erc8004 from './erc8004/index.js';
5
7
  /** Link agent identity: attestation tokens and Link-signed claims. */
6
8
  export * as Link from './link/index.js';
7
9
  /** Persona KYC: a payment check that requires an approved Persona inquiry. */
@@ -0,0 +1,26 @@
1
+ /** Poppy (Personal Agent Protocol 1.0) values. */
2
+ export declare const Constants: {
3
+ /** Header that carries the Session JWT on every request. */
4
+ readonly sessionHeader: 'Poppy-Session';
5
+ /** Header that names the protocol version. */
6
+ readonly versionHeader: 'Poppy-Version';
7
+ /** Protocol version this adapter speaks. */
8
+ readonly version: '1.0';
9
+ /** `typ` of a Session JWT. */
10
+ readonly sessionType: 'poppy-session+jwt';
11
+ /** `typ` of a delegation JWT. */
12
+ readonly delegationType: 'poppy-delegation+jwt';
13
+ /** Longest a delegation may last, in seconds (spec 5.2). */
14
+ readonly maxDelegationLifetime: number;
15
+ /** Default delegation lifetime, in seconds. */
16
+ readonly defaultDelegationLifetime: number;
17
+ /** Default window, in seconds, around a Session JWT's `iat`. */
18
+ readonly maxAge: 60;
19
+ /** Seconds a client's metadata and keys are cached. */
20
+ readonly keyCacheTtl: 300;
21
+ /** Seconds before a failed load or an unknown key `kid` loads a client's keys again. */
22
+ readonly keyRetryAfter: 30;
23
+ /** Seconds a client's last loaded keys keep verifying while reloads fail. */
24
+ readonly keyStaleTtl: number;
25
+ };
26
+ //# sourceMappingURL=Constants.d.ts.map
@@ -0,0 +1,26 @@
1
+ /** Poppy (Personal Agent Protocol 1.0) values. */
2
+ export const Constants = {
3
+ /** Header that carries the Session JWT on every request. */
4
+ sessionHeader: 'Poppy-Session',
5
+ /** Header that names the protocol version. */
6
+ versionHeader: 'Poppy-Version',
7
+ /** Protocol version this adapter speaks. */
8
+ version: '1.0',
9
+ /** `typ` of a Session JWT. */
10
+ sessionType: 'poppy-session+jwt',
11
+ /** `typ` of a delegation JWT. */
12
+ delegationType: 'poppy-delegation+jwt',
13
+ /** Longest a delegation may last, in seconds (spec 5.2). */
14
+ maxDelegationLifetime: 24 * 60 * 60,
15
+ /** Default delegation lifetime, in seconds. */
16
+ defaultDelegationLifetime: 60 * 60,
17
+ /** Default window, in seconds, around a Session JWT's `iat`. */
18
+ maxAge: 60,
19
+ /** Seconds a client's metadata and keys are cached. */
20
+ keyCacheTtl: 300,
21
+ /** Seconds before a failed load or an unknown key `kid` loads a client's keys again. */
22
+ keyRetryAfter: 30,
23
+ /** Seconds a client's last loaded keys keep verifying while reloads fail. */
24
+ keyStaleTtl: 60 * 60,
25
+ };
26
+ //# sourceMappingURL=Constants.js.map
@@ -0,0 +1,19 @@
1
+ /** A verified Poppy Session: who is calling, for whom, through which agent. */
2
+ export type Session = {
3
+ /** The Personal Agent's `client_id`: the URL of its client metadata document. */
4
+ readonly clientId: string;
5
+ /** The pseudonymous User ID the agent assigns this User for this Company. */
6
+ readonly userId: string;
7
+ /** The Session ID. */
8
+ readonly sessionId: string;
9
+ /** The optional Agent ID, when the Personal Agent runs several agents for the User. */
10
+ readonly agentId?: string | undefined;
11
+ };
12
+ /** An ES256 delegated key that signs Session JWTs, used for one User ID only (spec 5.2). */
13
+ export type DelegatedKey = {
14
+ /** The public key as a JWK. */
15
+ readonly jwk: JsonWebKey;
16
+ /** The private key that signs each Session JWT. */
17
+ readonly privateKey: CryptoKey;
18
+ };
19
+ //# sourceMappingURL=Types.d.ts.map
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=Types.js.map
@@ -0,0 +1,69 @@
1
+ import type * as Identity from '../../../../identity/Types.js';
2
+ import type { MaybePromise } from '../../../../internal/types.js';
3
+ import type * as Types from '../Types.js';
4
+ /**
5
+ * Creates a prover that sends a Poppy `Poppy-Session` header with each request to one Company.
6
+ *
7
+ * A Session JWT is signed fresh for every attempt, including the paid retry, so it names the
8
+ * request's method and URL and, when the request carries an OAuth access token, binds to it with
9
+ * `ath`. The Payment Credential is not an access token: it is never bound.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * const mppx = Mppx.create({
14
+ * identity: [
15
+ * Poppy.Client.prover({
16
+ * audience: 'https://api.example.com',
17
+ * clientId: 'https://agent.example/agent.json',
18
+ * delegation: () => delegations.current(),
19
+ * key: delegatedKey,
20
+ * scope: ['https://api.example.com'],
21
+ * userId: 'usr_Q7c1vK',
22
+ * }),
23
+ * ],
24
+ * methods: [tempo({ account })],
25
+ * })
26
+ * ```
27
+ */
28
+ export declare function prover(config: prover.Config): Identity.Prover;
29
+ export declare namespace prover {
30
+ type Config = {
31
+ /** The Company's identifier: its OAuth issuer, or `https://{organization.domain}`. */
32
+ readonly audience: string;
33
+ /** The Personal Agent's `client_id`, the HTTPS URL of its client metadata document. */
34
+ readonly clientId: string;
35
+ /** Returns a current delegation for `key`, signed by a key in the agent's `jwks_uri`. */
36
+ readonly delegation: () => MaybePromise<string>;
37
+ /** The delegated key that signs Session JWTs. Use it for this User ID only. */
38
+ readonly key: Types.DelegatedKey;
39
+ /** Origins of this Company that receive the header. */
40
+ readonly scope: Identity.Prover['scope'];
41
+ /** The User ID for this Company: stable, opaque and not derived from personal information. */
42
+ readonly userId: string;
43
+ /** The optional Agent ID, for a Personal Agent that runs several agents for the User. */
44
+ readonly agentId?: string | undefined;
45
+ /** The Session ID. @default a random ID per prover */
46
+ readonly sessionId?: string | undefined;
47
+ };
48
+ }
49
+ /**
50
+ * Signs a delegation (spec 5.2): a short-lived JWT in which a key from the agent's `jwks_uri`
51
+ * vouches for a delegated key. It names no User or Company.
52
+ */
53
+ export declare function delegate(parameters: delegate.Parameters): Promise<string>;
54
+ export declare namespace delegate {
55
+ type Parameters = {
56
+ /** The Personal Agent's `client_id`. */
57
+ readonly clientId: string;
58
+ /** The ES256 key from `jwks_uri` that signs the delegation, with its `kid`. */
59
+ readonly key: {
60
+ readonly kid: string;
61
+ readonly privateKey: CryptoKey;
62
+ };
63
+ /** The delegated public key. */
64
+ readonly publicKey: JsonWebKey;
65
+ /** Lifetime in seconds, at most 24 hours. @default 3600 */
66
+ readonly expiresIn?: number | undefined;
67
+ };
68
+ }
69
+ //# sourceMappingURL=index.d.ts.map