mppx 0.19.1 → 0.20.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/CHANGELOG.md +17 -0
- package/dist/cli/cli.js +3 -3
- package/dist/cli/plugins/index.js +1 -1
- package/dist/evm/server/Charge.js +104 -0
- package/dist/extensions/identity/attestation/Issuer.d.ts +91 -0
- package/dist/extensions/identity/attestation/Issuer.js +159 -0
- package/dist/extensions/identity/attestation/Token.js +7 -0
- package/dist/extensions/identity/attestation/Types.d.ts +31 -3
- package/dist/extensions/identity/attestation/index.d.ts +3 -1
- package/dist/extensions/identity/attestation/index.js +3 -1
- package/dist/extensions/identity/erc8004/Constants.d.ts +20 -0
- package/dist/extensions/identity/erc8004/Constants.js +20 -0
- package/dist/extensions/identity/erc8004/Types.d.ts +11 -0
- package/dist/extensions/identity/erc8004/Types.js +2 -0
- package/dist/extensions/identity/erc8004/client/index.d.ts +33 -0
- package/dist/extensions/identity/erc8004/client/index.js +33 -0
- package/dist/extensions/identity/erc8004/index.d.ts +5 -0
- package/dist/extensions/identity/erc8004/index.js +4 -0
- package/dist/extensions/identity/erc8004/internal/Agent.d.ts +21 -0
- package/dist/extensions/identity/erc8004/internal/Agent.js +24 -0
- package/dist/extensions/identity/erc8004/server/index.d.ts +44 -0
- package/dist/extensions/identity/erc8004/server/index.js +178 -0
- package/dist/extensions/identity/index.d.ts +2 -0
- package/dist/extensions/identity/index.js +2 -0
- package/dist/extensions/identity/tempo/Constants.d.ts +4 -0
- package/dist/extensions/identity/tempo/Constants.js +4 -0
- package/dist/extensions/identity/tempo/client/index.d.ts +63 -0
- package/dist/extensions/identity/tempo/client/index.js +151 -0
- package/dist/extensions/identity/tempo/index.d.ts +1 -0
- package/dist/extensions/identity/tempo/index.js +1 -0
- package/dist/extensions/identity/tempo/issuer/index.d.ts +83 -0
- package/dist/extensions/identity/tempo/issuer/index.js +183 -0
- package/dist/identity/Headers.d.ts +1 -1
- package/dist/identity/Headers.js +2 -0
- package/dist/internal/version.d.ts +2 -2
- package/dist/internal/version.js +1 -1
- package/dist/proxy/Proxy.js +47 -9
- package/dist/proxy/internal/Headers.js +1 -0
- package/dist/server/Mppx.js +28 -1
- package/dist/tempo/server/internal/html.gen.js +1 -1
- package/dist/tempo/session/server/CredentialVerification.d.ts +2 -0
- package/dist/tempo/session/server/CredentialVerification.js +44 -0
- package/dist/tempo/session/server/MeteredStream.d.ts +2 -0
- package/dist/tempo/session/server/MeteredStream.js +11 -0
- package/dist/tempo/session/server/Sse.js +9 -4
- package/dist/x402/Siwx.d.ts +270 -0
- package/dist/x402/Siwx.js +458 -0
- package/dist/x402/Types.d.ts +6 -6
- package/dist/x402/Types.js +1 -1
- package/dist/x402/index.d.ts +1 -0
- package/dist/x402/index.js +1 -0
- package/dist/x402/server/EvmCharge.d.ts +7 -0
- package/dist/x402/server/EvmCharge.js +14 -1
- package/package.json +5 -1
|
@@ -11,9 +11,16 @@ export type Claims = {
|
|
|
11
11
|
readonly iss: string;
|
|
12
12
|
/** Origin of the server the attestation is for, such as `https://api.example`. */
|
|
13
13
|
readonly aud: string;
|
|
14
|
-
/**
|
|
14
|
+
/**
|
|
15
|
+
* Attested subject: an email address or phone number, or the provider's stable identifier for
|
|
16
|
+
* an account (such as a GitHub user ID), never a handle that can change.
|
|
17
|
+
*/
|
|
15
18
|
readonly sub: string;
|
|
16
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* Attestation type: any structured-field token the issuer and server agree on, such as
|
|
21
|
+
* `email`, `phone`, `github`, `x` or `tempo-id`. One attestation states one fact; an issuer
|
|
22
|
+
* that knows several of a user's accounts signs one attestation of each type.
|
|
23
|
+
*/
|
|
17
24
|
readonly type: string;
|
|
18
25
|
/** Issued-at time, in Unix seconds. */
|
|
19
26
|
readonly iat: number;
|
|
@@ -21,7 +28,28 @@ export type Claims = {
|
|
|
21
28
|
readonly exp: number;
|
|
22
29
|
/** Unique attestation identifier. */
|
|
23
30
|
readonly jti?: string | undefined;
|
|
24
|
-
|
|
31
|
+
/**
|
|
32
|
+
* The issuer's identifier for the user behind the subject, the same across that issuer's
|
|
33
|
+
* attestations of every type, so a server can tell that an `email` and a `github`
|
|
34
|
+
* attestation from one issuer name the same user.
|
|
35
|
+
*/
|
|
36
|
+
readonly uid?: string | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Confirmation (RFC 7800): what the attestation is bound to, such as the key that must sign
|
|
39
|
+
* each request (`jkt`, an RFC 7638 thumbprint) or the account that pays (`addr`).
|
|
40
|
+
*/
|
|
41
|
+
readonly cnf?: Confirmation | undefined;
|
|
42
|
+
} & Readonly<Record<string, unknown>>;
|
|
43
|
+
/**
|
|
44
|
+
* What an attestation is bound to (RFC 7800 `cnf`). Issuers may add other members; a server
|
|
45
|
+
* enforces the ones it understands.
|
|
46
|
+
*/
|
|
47
|
+
export type Confirmation = {
|
|
48
|
+
/** RFC 7638 thumbprint of the key that must sign each request. */
|
|
49
|
+
readonly jkt?: string | undefined;
|
|
50
|
+
/** Address of the account that must pay, such as a Tempo Account. */
|
|
51
|
+
readonly addr?: string | undefined;
|
|
52
|
+
} & Readonly<Record<string, unknown>>;
|
|
25
53
|
/**
|
|
26
54
|
* Resolves the issuer's public key for an attestation's `kid`. Returns
|
|
27
55
|
* `undefined` for an unknown or malformed `kid`, and throws only when the keys
|
|
@@ -4,11 +4,13 @@
|
|
|
4
4
|
* to and sent in `Identity-Attestation`.
|
|
5
5
|
*
|
|
6
6
|
* Servers require one with `Server.verifier`, clients present and obtain them with
|
|
7
|
-
* `Client.prover`, and issuers sign them with `sign` and publish their keys as a JSON Web Key
|
|
7
|
+
* `Client.prover`, and issuers sign them with `Issuer.create` (or `sign`) and publish their keys as a JSON Web Key
|
|
8
8
|
* Set that `Jwks.resolver` reads.
|
|
9
9
|
*/
|
|
10
10
|
/** Client prover that presents and obtains attestations. */
|
|
11
11
|
export * as Client from './client/index.js';
|
|
12
|
+
/** Issuer that signs attestations and publishes its key. */
|
|
13
|
+
export * as Issuer from './Issuer.js';
|
|
12
14
|
/** Issuer JSON Web Key Set resolver. */
|
|
13
15
|
export * as Jwks from './Jwks.js';
|
|
14
16
|
/** Server verifier that requires an attestation. */
|
|
@@ -4,11 +4,13 @@
|
|
|
4
4
|
* to and sent in `Identity-Attestation`.
|
|
5
5
|
*
|
|
6
6
|
* Servers require one with `Server.verifier`, clients present and obtain them with
|
|
7
|
-
* `Client.prover`, and issuers sign them with `sign` and publish their keys as a JSON Web Key
|
|
7
|
+
* `Client.prover`, and issuers sign them with `Issuer.create` (or `sign`) and publish their keys as a JSON Web Key
|
|
8
8
|
* Set that `Jwks.resolver` reads.
|
|
9
9
|
*/
|
|
10
10
|
/** Client prover that presents and obtains attestations. */
|
|
11
11
|
export * as Client from './client/index.js';
|
|
12
|
+
/** Issuer that signs attestations and publishes its key. */
|
|
13
|
+
export * as Issuer from './Issuer.js';
|
|
12
14
|
/** Issuer JSON Web Key Set resolver. */
|
|
13
15
|
export * as Jwks from './Jwks.js';
|
|
14
16
|
/** Server verifier that requires an attestation. */
|
|
@@ -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,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,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. */
|
|
@@ -4,6 +4,10 @@ export declare const Constants: {
|
|
|
4
4
|
readonly type: 'tempo-id';
|
|
5
5
|
/** Tempo ID in production: the issuer of every access token. */
|
|
6
6
|
readonly issuer: 'https://id.tempo.xyz';
|
|
7
|
+
/** Path, relative to a Tempo ID attestation issuer's URL, that exchanges a token for an attestation. */
|
|
8
|
+
readonly attestPath: '/tempo-id/attest';
|
|
9
|
+
/** Attestation types a Tempo ID attestation issuer signs: the user's Tempo ID and verified email. */
|
|
10
|
+
readonly attestationTypes: readonly ('tempo-id' | 'email')[];
|
|
7
11
|
/** Header that carries the user's ID token, for their email address. */
|
|
8
12
|
readonly idTokenHeader: 'Tempo-Id-Token';
|
|
9
13
|
/** Seconds a DPoP proof is accepted after its `iat`. */
|
|
@@ -4,6 +4,10 @@ export const Constants = {
|
|
|
4
4
|
type: 'tempo-id',
|
|
5
5
|
/** Tempo ID in production: the issuer of every access token. */
|
|
6
6
|
issuer: 'https://id.tempo.xyz',
|
|
7
|
+
/** Path, relative to a Tempo ID attestation issuer's URL, that exchanges a token for an attestation. */
|
|
8
|
+
attestPath: '/tempo-id/attest',
|
|
9
|
+
/** Attestation types a Tempo ID attestation issuer signs: the user's Tempo ID and verified email. */
|
|
10
|
+
attestationTypes: ['tempo-id', 'email'],
|
|
7
11
|
/** Header that carries the user's ID token, for their email address. */
|
|
8
12
|
idTokenHeader: 'Tempo-Id-Token',
|
|
9
13
|
/** Seconds a DPoP proof is accepted after its `iat`. */
|
|
@@ -31,4 +31,67 @@ export declare namespace prover {
|
|
|
31
31
|
readonly scope: Identity.Prover['scope'];
|
|
32
32
|
};
|
|
33
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* Creates a prover that presents Tempo ID attestations, obtaining them from a Tempo ID
|
|
36
|
+
* attestation issuer (`TempoId.Issuer`) when a server asks: an `Attestation.Client.prover` whose
|
|
37
|
+
* `acquire` exchanges the user's access token (and ID token, for `email`) at the issuer.
|
|
38
|
+
*
|
|
39
|
+
* Unlike {@link prover}, the access token goes only to the issuer: servers get short-lived
|
|
40
|
+
* attestations bound to their origin, and verify them like any other issuer's.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const mppx = Mppx.create({
|
|
45
|
+
* identity: [
|
|
46
|
+
* TempoId.Client.attestations({
|
|
47
|
+
* accessToken: () => accounts.session.accessToken(),
|
|
48
|
+
* idToken: () => accounts.session.get()?.idToken,
|
|
49
|
+
* issuer: 'https://attest.example',
|
|
50
|
+
* scope: ['https://api.example'],
|
|
51
|
+
* }),
|
|
52
|
+
* ],
|
|
53
|
+
* methods: [tempo({ account })],
|
|
54
|
+
* })
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare function attestations(config: attestations.Config): Identity.Prover;
|
|
58
|
+
export declare namespace attestations {
|
|
59
|
+
type Config = Omit<attest.Parameters, 'audience' | 'type'> & {
|
|
60
|
+
/** Origins that may receive attestations and ask for new ones. */
|
|
61
|
+
readonly scope: Identity.Prover['scope'];
|
|
62
|
+
/** Attestation types to obtain. @default ['tempo-id', 'email'] */
|
|
63
|
+
readonly types?: readonly string[] | undefined;
|
|
64
|
+
/** Attestations already held, such as from storage. */
|
|
65
|
+
readonly attestations?: readonly string[] | undefined;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Exchanges a Tempo ID access token for one attestation at a Tempo ID attestation issuer.
|
|
70
|
+
* Resolves with `undefined` when the issuer refuses this user that type (403, such as `email`
|
|
71
|
+
* without a verified address), and throws on any other failure.
|
|
72
|
+
*/
|
|
73
|
+
export declare function attest(parameters: attest.Parameters): Promise<{
|
|
74
|
+
readonly attestation: string;
|
|
75
|
+
readonly expiresAt: Date;
|
|
76
|
+
} | undefined>;
|
|
77
|
+
export declare namespace attest {
|
|
78
|
+
type Parameters = {
|
|
79
|
+
/** The Tempo ID attestation issuer's URL. */
|
|
80
|
+
readonly issuer: string;
|
|
81
|
+
/** Origin of the server the attestation is for. */
|
|
82
|
+
readonly audience: string;
|
|
83
|
+
/** Attestation type, such as `tempo-id` or `email`. */
|
|
84
|
+
readonly type: string;
|
|
85
|
+
/** Returns a live Tempo ID access token, such as `accounts.session.accessToken`. */
|
|
86
|
+
readonly accessToken: () => MaybePromise<string>;
|
|
87
|
+
/** The DPoP key that binds the token. Absent, the token is sent as a Bearer token. */
|
|
88
|
+
readonly dpop?: Types.DpopKey | undefined;
|
|
89
|
+
/** Seconds to wait for the issuer before the exchange fails. @default 10 */
|
|
90
|
+
readonly timeout?: number | undefined;
|
|
91
|
+
/** Returns the user's ID token, which `email` needs and an issuer's policy may read. */
|
|
92
|
+
readonly idToken?: (() => MaybePromise<string | undefined>) | undefined;
|
|
93
|
+
/** Fetch implementation. @default globalThis.fetch */
|
|
94
|
+
readonly fetch?: typeof globalThis.fetch | undefined;
|
|
95
|
+
};
|
|
96
|
+
}
|
|
34
97
|
//# sourceMappingURL=index.d.ts.map
|