open-agentpay 0.1.0 → 0.2.5
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/LICENSE +201 -0
- package/README.md +125 -4
- package/dist/client/client.d.ts +37 -0
- package/dist/client/client.d.ts.map +1 -0
- package/dist/client/client.js +90 -0
- package/dist/client/client.js.map +1 -0
- package/dist/client/errors.d.ts +23 -0
- package/dist/client/errors.d.ts.map +1 -0
- package/dist/client/errors.js +38 -0
- package/dist/client/errors.js.map +1 -0
- package/dist/client/index.d.ts +6 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +6 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/interfaces.d.ts +34 -0
- package/dist/client/interfaces.d.ts.map +1 -0
- package/dist/client/interfaces.js +5 -0
- package/dist/client/interfaces.js.map +1 -0
- package/dist/client/signer.d.ts +136 -0
- package/dist/client/signer.d.ts.map +1 -0
- package/dist/client/signer.js +300 -0
- package/dist/client/signer.js.map +1 -0
- package/dist/client/transport.d.ts +44 -0
- package/dist/client/transport.d.ts.map +1 -0
- package/dist/client/transport.js +389 -0
- package/dist/client/transport.js.map +1 -0
- package/dist/core/codecs.d.ts +35 -0
- package/dist/core/codecs.d.ts.map +1 -0
- package/dist/core/codecs.js +177 -0
- package/dist/core/codecs.js.map +1 -0
- package/dist/core/errors.d.ts +33 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +48 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/index.d.ts +6 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +6 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/interop.d.ts +10 -0
- package/dist/core/interop.d.ts.map +1 -0
- package/dist/core/interop.js +28 -0
- package/dist/core/interop.js.map +1 -0
- package/dist/core/models.d.ts +155 -0
- package/dist/core/models.d.ts.map +1 -0
- package/dist/core/models.js +463 -0
- package/dist/core/models.js.map +1 -0
- package/dist/core/state.d.ts +82 -0
- package/dist/core/state.d.ts.map +1 -0
- package/dist/core/state.js +87 -0
- package/dist/core/state.js.map +1 -0
- package/dist/evm/constants.d.ts +52 -0
- package/dist/evm/constants.d.ts.map +1 -0
- package/dist/evm/constants.js +72 -0
- package/dist/evm/constants.js.map +1 -0
- package/dist/evm/eip3009.d.ts +43 -0
- package/dist/evm/eip3009.d.ts.map +1 -0
- package/dist/evm/eip3009.js +145 -0
- package/dist/evm/eip3009.js.map +1 -0
- package/dist/evm/index.d.ts +3 -0
- package/dist/evm/index.d.ts.map +1 -0
- package/dist/evm/index.js +3 -0
- package/dist/evm/index.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/integrations/index.d.ts +4 -0
- package/dist/integrations/index.d.ts.map +1 -0
- package/dist/integrations/index.js +4 -0
- package/dist/integrations/index.js.map +1 -0
- package/dist/integrations/langchain.d.ts +40 -0
- package/dist/integrations/langchain.d.ts.map +1 -0
- package/dist/integrations/langchain.js +68 -0
- package/dist/integrations/langchain.js.map +1 -0
- package/dist/integrations/mcp.d.ts +29 -0
- package/dist/integrations/mcp.d.ts.map +1 -0
- package/dist/integrations/mcp.js +73 -0
- package/dist/integrations/mcp.js.map +1 -0
- package/dist/integrations/vercel-ai.d.ts +31 -0
- package/dist/integrations/vercel-ai.d.ts.map +1 -0
- package/dist/integrations/vercel-ai.js +50 -0
- package/dist/integrations/vercel-ai.js.map +1 -0
- package/dist/policy/db.d.ts +14 -0
- package/dist/policy/db.d.ts.map +1 -0
- package/dist/policy/db.js +86 -0
- package/dist/policy/db.js.map +1 -0
- package/dist/policy/errors.d.ts +31 -0
- package/dist/policy/errors.d.ts.map +1 -0
- package/dist/policy/errors.js +48 -0
- package/dist/policy/errors.js.map +1 -0
- package/dist/policy/evaluator.d.ts +22 -0
- package/dist/policy/evaluator.d.ts.map +1 -0
- package/dist/policy/evaluator.js +152 -0
- package/dist/policy/evaluator.js.map +1 -0
- package/dist/policy/index.d.ts +7 -0
- package/dist/policy/index.d.ts.map +1 -0
- package/dist/policy/index.js +7 -0
- package/dist/policy/index.js.map +1 -0
- package/dist/policy/interfaces.d.ts +59 -0
- package/dist/policy/interfaces.d.ts.map +1 -0
- package/dist/policy/interfaces.js +7 -0
- package/dist/policy/interfaces.js.map +1 -0
- package/dist/policy/models.d.ts +161 -0
- package/dist/policy/models.d.ts.map +1 -0
- package/dist/policy/models.js +449 -0
- package/dist/policy/models.js.map +1 -0
- package/dist/policy/sqlite-engine.d.ts +28 -0
- package/dist/policy/sqlite-engine.d.ts.map +1 -0
- package/dist/policy/sqlite-engine.js +501 -0
- package/dist/policy/sqlite-engine.js.map +1 -0
- package/dist/state/buyer-coordinator.d.ts +33 -0
- package/dist/state/buyer-coordinator.d.ts.map +1 -0
- package/dist/state/buyer-coordinator.js +363 -0
- package/dist/state/buyer-coordinator.js.map +1 -0
- package/dist/state/errors.d.ts +45 -0
- package/dist/state/errors.d.ts.map +1 -0
- package/dist/state/errors.js +88 -0
- package/dist/state/errors.js.map +1 -0
- package/dist/state/guards.d.ts +24 -0
- package/dist/state/guards.d.ts.map +1 -0
- package/dist/state/guards.js +211 -0
- package/dist/state/guards.js.map +1 -0
- package/dist/state/index.d.ts +5 -0
- package/dist/state/index.d.ts.map +1 -0
- package/dist/state/index.js +5 -0
- package/dist/state/index.js.map +1 -0
- package/dist/state/reconciliation.d.ts +105 -0
- package/dist/state/reconciliation.d.ts.map +1 -0
- package/dist/state/reconciliation.js +297 -0
- package/dist/state/reconciliation.js.map +1 -0
- package/package.json +98 -12
- package/index.d.ts +0 -2
- package/index.js +0 -8
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EIP-712 & EIP-3009 LocalSigner implementation for AgentPay.
|
|
3
|
+
*
|
|
4
|
+
* Uses viem for all cryptographic operations.
|
|
5
|
+
* Enforces firewall reservation binding and secret redaction.
|
|
6
|
+
*/
|
|
7
|
+
import { PaymentAuthorization, type PaymentChallenge } from '../core/models.js';
|
|
8
|
+
import type { ReservationToken } from '../policy/models.js';
|
|
9
|
+
import type { ISigner, SignAuthorizationOptions } from './interfaces.js';
|
|
10
|
+
/** Standard EIP-3009 message type definition for TransferWithAuthorization */
|
|
11
|
+
export declare const EIP3009_MESSAGE_TYPES: {
|
|
12
|
+
readonly TransferWithAuthorization: readonly [{
|
|
13
|
+
readonly name: "from";
|
|
14
|
+
readonly type: "address";
|
|
15
|
+
}, {
|
|
16
|
+
readonly name: "to";
|
|
17
|
+
readonly type: "address";
|
|
18
|
+
}, {
|
|
19
|
+
readonly name: "value";
|
|
20
|
+
readonly type: "uint256";
|
|
21
|
+
}, {
|
|
22
|
+
readonly name: "validAfter";
|
|
23
|
+
readonly type: "uint256";
|
|
24
|
+
}, {
|
|
25
|
+
readonly name: "validBefore";
|
|
26
|
+
readonly type: "uint256";
|
|
27
|
+
}, {
|
|
28
|
+
readonly name: "nonce";
|
|
29
|
+
readonly type: "bytes32";
|
|
30
|
+
}];
|
|
31
|
+
};
|
|
32
|
+
/** Standard token names for major stablecoins */
|
|
33
|
+
export declare const DEFAULT_TOKEN_NAMES: Readonly<Record<string, string>>;
|
|
34
|
+
/**
|
|
35
|
+
* Generate a cryptographically secure 32-byte hex nonce with 0x prefix.
|
|
36
|
+
*/
|
|
37
|
+
export declare function generateNonce(): `0x${string}`;
|
|
38
|
+
/**
|
|
39
|
+
* Extract the integer chain ID from a CAIP-2 network string.
|
|
40
|
+
*
|
|
41
|
+
* @param network CAIP-2 network identifier (e.g. 'eip155:84532').
|
|
42
|
+
* @returns Integer chain ID.
|
|
43
|
+
* @throws UnsupportedNetworkError If the network is not an eip155 EVM chain.
|
|
44
|
+
*/
|
|
45
|
+
export declare function parseCaip2ChainId(network: string): number;
|
|
46
|
+
/**
|
|
47
|
+
* Local private key signer for AgentPay clients.
|
|
48
|
+
*
|
|
49
|
+
* - Adheres to EIP-712 and EIP-3009 specifications.
|
|
50
|
+
* - Uses viem strictly; implements zero custom cryptography.
|
|
51
|
+
* - Enforces mandatory policy reservation verification before signing.
|
|
52
|
+
* - Redacts private key in toString, inspect, and toJSON for financial safety.
|
|
53
|
+
*/
|
|
54
|
+
export declare class LocalSigner implements ISigner {
|
|
55
|
+
#private;
|
|
56
|
+
readonly address: `0x${string}`;
|
|
57
|
+
constructor(privateKey: string | Uint8Array, options?: {
|
|
58
|
+
tokenNameMap?: Record<string, string>;
|
|
59
|
+
});
|
|
60
|
+
toString(): string;
|
|
61
|
+
toJSON(): Record<string, unknown>;
|
|
62
|
+
/**
|
|
63
|
+
* Generate a new random LocalSigner using cryptographically secure randomness.
|
|
64
|
+
*/
|
|
65
|
+
static generate(): LocalSigner;
|
|
66
|
+
/**
|
|
67
|
+
* Resolve the EIP-712 domain token name for a payment challenge.
|
|
68
|
+
*/
|
|
69
|
+
resolveTokenName(challenge: PaymentChallenge, explicitName?: string): string;
|
|
70
|
+
/**
|
|
71
|
+
* Build EIP-712 domain, types, and message dictionaries conforming to EIP-3009.
|
|
72
|
+
*/
|
|
73
|
+
buildEip712Data(challenge: PaymentChallenge, nonce: string, options?: {
|
|
74
|
+
tokenName?: string;
|
|
75
|
+
tokenVersion?: string;
|
|
76
|
+
}): {
|
|
77
|
+
domain: {
|
|
78
|
+
name: string;
|
|
79
|
+
version: string;
|
|
80
|
+
chainId: number;
|
|
81
|
+
verifyingContract: `0x${string}`;
|
|
82
|
+
};
|
|
83
|
+
types: {
|
|
84
|
+
readonly TransferWithAuthorization: readonly [{
|
|
85
|
+
readonly name: "from";
|
|
86
|
+
readonly type: "address";
|
|
87
|
+
}, {
|
|
88
|
+
readonly name: "to";
|
|
89
|
+
readonly type: "address";
|
|
90
|
+
}, {
|
|
91
|
+
readonly name: "value";
|
|
92
|
+
readonly type: "uint256";
|
|
93
|
+
}, {
|
|
94
|
+
readonly name: "validAfter";
|
|
95
|
+
readonly type: "uint256";
|
|
96
|
+
}, {
|
|
97
|
+
readonly name: "validBefore";
|
|
98
|
+
readonly type: "uint256";
|
|
99
|
+
}, {
|
|
100
|
+
readonly name: "nonce";
|
|
101
|
+
readonly type: "bytes32";
|
|
102
|
+
}];
|
|
103
|
+
};
|
|
104
|
+
message: {
|
|
105
|
+
from: `0x${string}`;
|
|
106
|
+
to: `0x${string}`;
|
|
107
|
+
value: bigint;
|
|
108
|
+
validAfter: bigint;
|
|
109
|
+
validBefore: bigint;
|
|
110
|
+
nonce: `0x${string}`;
|
|
111
|
+
};
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* Sign an EIP-712/3009 transfer authorization after verifying the policy reservation.
|
|
115
|
+
*
|
|
116
|
+
* Safety Guardrails:
|
|
117
|
+
* 1. Verifies reservation.intentId === challenge.challengeId
|
|
118
|
+
* 2. Verifies reservation.amount === challenge.amountBaseUnits
|
|
119
|
+
* 3. Verifies reservation.expiresAt >= challenge.validBefore
|
|
120
|
+
*
|
|
121
|
+
* @returns PaymentAuthorization with cryptographic signature.
|
|
122
|
+
*/
|
|
123
|
+
signPaymentAuthorization(challenge: PaymentChallenge, reservation: ReservationToken, options?: SignAuthorizationOptions): Promise<PaymentAuthorization>;
|
|
124
|
+
/**
|
|
125
|
+
* Verify that a PaymentAuthorization cryptographically matches the PaymentChallenge
|
|
126
|
+
* and was signed by the expected payer.
|
|
127
|
+
*
|
|
128
|
+
* @returns True if signature is valid and signer matches expected payer; False otherwise.
|
|
129
|
+
*/
|
|
130
|
+
static verifyAuthorization(authorization: PaymentAuthorization, challenge: PaymentChallenge, options?: {
|
|
131
|
+
expectedPayer?: string;
|
|
132
|
+
tokenName?: string;
|
|
133
|
+
tokenVersion?: string;
|
|
134
|
+
}): Promise<boolean>;
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=signer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../../src/client/signer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,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;;;;;;;;gBAQ5B,KAAK,MAAM,EAAE;;;;;;;IAUxC;;;;;;;;;OASG;IACG,wBAAwB,CAC5B,SAAS,EAAE,gBAAgB,EAC3B,WAAW,EAAE,gBAAgB,EAC7B,OAAO,CAAC,EAAE,wBAAwB,GACjC,OAAO,CAAC,oBAAoB,CAAC;IA4EhC;;;;;OAKG;WACU,mBAAmB,CAC9B,aAAa,EAAE,oBAAoB,EACnC,SAAS,EAAE,gBAAgB,EAC3B,OAAO,CAAC,EAAE;QAAE,aAAa,CAAC,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,MAAM,CAAC;QAAC,YAAY,CAAC,EAAE,MAAM,CAAA;KAAE,GAC9E,OAAO,CAAC,OAAO,CAAC;CA+DpB"}
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EIP-712 & EIP-3009 LocalSigner implementation for AgentPay.
|
|
3
|
+
*
|
|
4
|
+
* Uses viem for all cryptographic operations.
|
|
5
|
+
* Enforces firewall reservation binding and secret redaction.
|
|
6
|
+
*/
|
|
7
|
+
import { parseSignature, recoverTypedDataAddress } from 'viem';
|
|
8
|
+
import { privateKeyToAccount } from 'viem/accounts';
|
|
9
|
+
import { PaymentAuthorization, SignatureData, } from '../core/models.js';
|
|
10
|
+
import { InvalidPrivateKeyError, ReservationMismatchError, SignerError, UnsupportedNetworkError, } from './errors.js';
|
|
11
|
+
/** CAIP-2 parsing regex for eip155 namespace: eip155:<chain_id> */
|
|
12
|
+
const CAIP2_EIP155_RE = /^eip155:([0-9]+)$/;
|
|
13
|
+
/** Standard EIP-3009 message type definition for TransferWithAuthorization */
|
|
14
|
+
export const EIP3009_MESSAGE_TYPES = {
|
|
15
|
+
TransferWithAuthorization: [
|
|
16
|
+
{ name: 'from', type: 'address' },
|
|
17
|
+
{ name: 'to', type: 'address' },
|
|
18
|
+
{ name: 'value', type: 'uint256' },
|
|
19
|
+
{ name: 'validAfter', type: 'uint256' },
|
|
20
|
+
{ name: 'validBefore', type: 'uint256' },
|
|
21
|
+
{ name: 'nonce', type: 'bytes32' },
|
|
22
|
+
],
|
|
23
|
+
};
|
|
24
|
+
/** Standard token names for major stablecoins */
|
|
25
|
+
export const DEFAULT_TOKEN_NAMES = Object.freeze({
|
|
26
|
+
usdc: 'USD Coin',
|
|
27
|
+
eurc: 'Euro Coin',
|
|
28
|
+
});
|
|
29
|
+
/**
|
|
30
|
+
* Generate a cryptographically secure 32-byte hex nonce with 0x prefix.
|
|
31
|
+
*/
|
|
32
|
+
export function generateNonce() {
|
|
33
|
+
const bytes = new Uint8Array(32);
|
|
34
|
+
crypto.getRandomValues(bytes);
|
|
35
|
+
return `0x${Buffer.from(bytes).toString('hex')}`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Extract the integer chain ID from a CAIP-2 network string.
|
|
39
|
+
*
|
|
40
|
+
* @param network CAIP-2 network identifier (e.g. 'eip155:84532').
|
|
41
|
+
* @returns Integer chain ID.
|
|
42
|
+
* @throws UnsupportedNetworkError If the network is not an eip155 EVM chain.
|
|
43
|
+
*/
|
|
44
|
+
export function parseCaip2ChainId(network) {
|
|
45
|
+
const match = CAIP2_EIP155_RE.exec(network.trim());
|
|
46
|
+
if (!match || !match[1]) {
|
|
47
|
+
throw new UnsupportedNetworkError(`Unsupported network identifier: ${JSON.stringify(network)}. ` +
|
|
48
|
+
"LocalSigner only supports EVM chains conforming to 'eip155:<chainId>'.");
|
|
49
|
+
}
|
|
50
|
+
return Number.parseInt(match[1], 10);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Local private key signer for AgentPay clients.
|
|
54
|
+
*
|
|
55
|
+
* - Adheres to EIP-712 and EIP-3009 specifications.
|
|
56
|
+
* - Uses viem strictly; implements zero custom cryptography.
|
|
57
|
+
* - Enforces mandatory policy reservation verification before signing.
|
|
58
|
+
* - Redacts private key in toString, inspect, and toJSON for financial safety.
|
|
59
|
+
*/
|
|
60
|
+
export class LocalSigner {
|
|
61
|
+
#account;
|
|
62
|
+
#tokenNameMap;
|
|
63
|
+
address;
|
|
64
|
+
constructor(privateKey, options) {
|
|
65
|
+
try {
|
|
66
|
+
let cleanKey;
|
|
67
|
+
if (typeof privateKey === 'string') {
|
|
68
|
+
const trimmed = privateKey.trim();
|
|
69
|
+
cleanKey = (trimmed.startsWith('0x') ? trimmed : `0x${trimmed}`);
|
|
70
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(cleanKey)) {
|
|
71
|
+
throw new Error('Private key must be a 32-byte hex string');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
else if (privateKey instanceof Uint8Array) {
|
|
75
|
+
if (privateKey.byteLength !== 32) {
|
|
76
|
+
throw new Error(`Private key bytes must be 32 bytes, got ${privateKey.byteLength}`);
|
|
77
|
+
}
|
|
78
|
+
cleanKey = `0x${Buffer.from(privateKey).toString('hex')}`;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
throw new TypeError(`privateKey must be string or Uint8Array, got ${typeof privateKey}`);
|
|
82
|
+
}
|
|
83
|
+
this.#account = privateKeyToAccount(cleanKey);
|
|
84
|
+
}
|
|
85
|
+
catch (exc) {
|
|
86
|
+
throw new InvalidPrivateKeyError(`Failed to load EVM private key: ${exc instanceof Error ? exc.message : String(exc)}`);
|
|
87
|
+
}
|
|
88
|
+
this.address = this.#account.address.toLowerCase();
|
|
89
|
+
this.#tokenNameMap = new Map();
|
|
90
|
+
if (options?.tokenNameMap) {
|
|
91
|
+
for (const [k, v] of Object.entries(options.tokenNameMap)) {
|
|
92
|
+
this.#tokenNameMap.set(k.toLowerCase(), v);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
toString() {
|
|
97
|
+
return `LocalSigner(address=${this.address})`;
|
|
98
|
+
}
|
|
99
|
+
[Symbol.for('nodejs.util.inspect.custom')]() {
|
|
100
|
+
return `<LocalSigner address=${this.address}>`;
|
|
101
|
+
}
|
|
102
|
+
toJSON() {
|
|
103
|
+
return { address: this.address };
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Generate a new random LocalSigner using cryptographically secure randomness.
|
|
107
|
+
*/
|
|
108
|
+
static generate() {
|
|
109
|
+
const bytes = new Uint8Array(32);
|
|
110
|
+
crypto.getRandomValues(bytes);
|
|
111
|
+
return new LocalSigner(bytes);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Resolve the EIP-712 domain token name for a payment challenge.
|
|
115
|
+
*/
|
|
116
|
+
resolveTokenName(challenge, explicitName) {
|
|
117
|
+
if (explicitName) {
|
|
118
|
+
return explicitName;
|
|
119
|
+
}
|
|
120
|
+
const assetLower = challenge.asset?.toLowerCase();
|
|
121
|
+
if (assetLower && this.#tokenNameMap.has(assetLower)) {
|
|
122
|
+
return this.#tokenNameMap.get(assetLower);
|
|
123
|
+
}
|
|
124
|
+
const symbolLower = challenge.assetSymbol?.toLowerCase();
|
|
125
|
+
if (symbolLower && this.#tokenNameMap.has(symbolLower)) {
|
|
126
|
+
return this.#tokenNameMap.get(symbolLower);
|
|
127
|
+
}
|
|
128
|
+
if (symbolLower && DEFAULT_TOKEN_NAMES[symbolLower]) {
|
|
129
|
+
return DEFAULT_TOKEN_NAMES[symbolLower];
|
|
130
|
+
}
|
|
131
|
+
return challenge.assetSymbol || 'USD Coin';
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Build EIP-712 domain, types, and message dictionaries conforming to EIP-3009.
|
|
135
|
+
*/
|
|
136
|
+
buildEip712Data(challenge, nonce, options) {
|
|
137
|
+
if (!challenge.network) {
|
|
138
|
+
throw new UnsupportedNetworkError('PaymentChallenge network is required for EIP-3009 signing');
|
|
139
|
+
}
|
|
140
|
+
if (!challenge.asset) {
|
|
141
|
+
throw new SignerError('PaymentChallenge asset is required for EIP-3009 signing');
|
|
142
|
+
}
|
|
143
|
+
if (!challenge.payee) {
|
|
144
|
+
throw new SignerError('PaymentChallenge payee is required for EIP-3009 signing');
|
|
145
|
+
}
|
|
146
|
+
const chainId = parseCaip2ChainId(challenge.network);
|
|
147
|
+
const domainName = this.resolveTokenName(challenge, options?.tokenName);
|
|
148
|
+
const tokenVersion = options?.tokenVersion ?? '2';
|
|
149
|
+
const domain = {
|
|
150
|
+
name: domainName,
|
|
151
|
+
version: tokenVersion,
|
|
152
|
+
chainId,
|
|
153
|
+
verifyingContract: challenge.asset,
|
|
154
|
+
};
|
|
155
|
+
const types = EIP3009_MESSAGE_TYPES;
|
|
156
|
+
const normNonce = (nonce.startsWith('0x') ? nonce.toLowerCase() : `0x${nonce.toLowerCase()}`);
|
|
157
|
+
const message = {
|
|
158
|
+
from: this.address,
|
|
159
|
+
to: challenge.payee,
|
|
160
|
+
value: challenge.amountBaseUnits,
|
|
161
|
+
validAfter: BigInt(challenge.validAfter),
|
|
162
|
+
validBefore: BigInt(challenge.validBefore),
|
|
163
|
+
nonce: normNonce,
|
|
164
|
+
};
|
|
165
|
+
return { domain, types, message };
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Sign an EIP-712/3009 transfer authorization after verifying the policy reservation.
|
|
169
|
+
*
|
|
170
|
+
* Safety Guardrails:
|
|
171
|
+
* 1. Verifies reservation.intentId === challenge.challengeId
|
|
172
|
+
* 2. Verifies reservation.amount === challenge.amountBaseUnits
|
|
173
|
+
* 3. Verifies reservation.expiresAt >= challenge.validBefore
|
|
174
|
+
*
|
|
175
|
+
* @returns PaymentAuthorization with cryptographic signature.
|
|
176
|
+
*/
|
|
177
|
+
async signPaymentAuthorization(challenge, reservation, options) {
|
|
178
|
+
// Guardrail 1: Policy intent binding
|
|
179
|
+
if (reservation.intentId !== challenge.challengeId) {
|
|
180
|
+
throw new ReservationMismatchError(`Reservation intent_id (${JSON.stringify(reservation.intentId)}) does not match ` +
|
|
181
|
+
`challenge_id (${JSON.stringify(challenge.challengeId)}). Refusing to sign.`);
|
|
182
|
+
}
|
|
183
|
+
// Guardrail 2: Amount exact match
|
|
184
|
+
if (reservation.amount !== challenge.amountBaseUnits) {
|
|
185
|
+
throw new ReservationMismatchError(`Reservation amount (${reservation.amount.toString()}) does not match ` +
|
|
186
|
+
`challenge amount (${challenge.amountBaseUnits.toString()}). Refusing to sign.`);
|
|
187
|
+
}
|
|
188
|
+
// Guardrail 3: Expiration safety window
|
|
189
|
+
if (reservation.expiresAt < challenge.validBefore) {
|
|
190
|
+
throw new ReservationMismatchError(`Reservation expires_at (${reservation.expiresAt}) does not cover ` +
|
|
191
|
+
`challenge valid_before (${challenge.validBefore}). Refusing to sign.`);
|
|
192
|
+
}
|
|
193
|
+
const signNonce = options?.nonce ? (options.nonce.startsWith('0x') ? options.nonce : `0x${options.nonce}`) : generateNonce();
|
|
194
|
+
const { domain, types, message } = this.buildEip712Data(challenge, signNonce, {
|
|
195
|
+
tokenName: options?.tokenName,
|
|
196
|
+
tokenVersion: options?.tokenVersion,
|
|
197
|
+
});
|
|
198
|
+
let rawSig;
|
|
199
|
+
try {
|
|
200
|
+
rawSig = await this.#account.signTypedData({
|
|
201
|
+
domain,
|
|
202
|
+
types,
|
|
203
|
+
primaryType: 'TransferWithAuthorization',
|
|
204
|
+
message,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
catch (exc) {
|
|
208
|
+
throw new SignerError(`Cryptographic signing failed: ${exc instanceof Error ? exc.message : String(exc)}`);
|
|
209
|
+
}
|
|
210
|
+
const parsed = parseSignature(rawSig);
|
|
211
|
+
const rHex = parsed.r;
|
|
212
|
+
const sHex = parsed.s;
|
|
213
|
+
const vVal = Number(parsed.v);
|
|
214
|
+
const rClean = rHex.slice(2).padStart(64, '0');
|
|
215
|
+
const sClean = sHex.slice(2).padStart(64, '0');
|
|
216
|
+
const vHex = vVal.toString(16).padStart(2, '0');
|
|
217
|
+
const serializedHex = `${rClean}${sClean}${vHex}`;
|
|
218
|
+
const sigData = new SignatureData({
|
|
219
|
+
v: vVal,
|
|
220
|
+
r: rHex,
|
|
221
|
+
s: sHex,
|
|
222
|
+
serialized: serializedHex,
|
|
223
|
+
compact: `0x${serializedHex}`,
|
|
224
|
+
});
|
|
225
|
+
return new PaymentAuthorization({
|
|
226
|
+
version: '2.0',
|
|
227
|
+
challengeId: challenge.challengeId,
|
|
228
|
+
payer: this.address,
|
|
229
|
+
scheme: challenge.scheme,
|
|
230
|
+
nonce: signNonce,
|
|
231
|
+
validAfter: challenge.validAfter,
|
|
232
|
+
validBefore: challenge.validBefore,
|
|
233
|
+
signature: sigData,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Verify that a PaymentAuthorization cryptographically matches the PaymentChallenge
|
|
238
|
+
* and was signed by the expected payer.
|
|
239
|
+
*
|
|
240
|
+
* @returns True if signature is valid and signer matches expected payer; False otherwise.
|
|
241
|
+
*/
|
|
242
|
+
static async verifyAuthorization(authorization, challenge, options) {
|
|
243
|
+
try {
|
|
244
|
+
if (!challenge.network || !challenge.asset || !challenge.payee) {
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
const targetPayer = (options?.expectedPayer ?? authorization.payer).toLowerCase();
|
|
248
|
+
const chainId = parseCaip2ChainId(challenge.network);
|
|
249
|
+
const symbolLower = challenge.assetSymbol?.toLowerCase();
|
|
250
|
+
const domainName = options?.tokenName ??
|
|
251
|
+
(symbolLower && DEFAULT_TOKEN_NAMES[symbolLower]) ??
|
|
252
|
+
challenge.assetSymbol ??
|
|
253
|
+
'USD Coin';
|
|
254
|
+
const tokenVersion = options?.tokenVersion ?? '2';
|
|
255
|
+
const domain = {
|
|
256
|
+
name: domainName,
|
|
257
|
+
version: tokenVersion,
|
|
258
|
+
chainId,
|
|
259
|
+
verifyingContract: challenge.asset,
|
|
260
|
+
};
|
|
261
|
+
const types = EIP3009_MESSAGE_TYPES;
|
|
262
|
+
const normNonce = (authorization.nonce.startsWith('0x')
|
|
263
|
+
? authorization.nonce.toLowerCase()
|
|
264
|
+
: `0x${authorization.nonce.toLowerCase()}`);
|
|
265
|
+
const message = {
|
|
266
|
+
from: authorization.payer,
|
|
267
|
+
to: challenge.payee,
|
|
268
|
+
value: challenge.amountBaseUnits,
|
|
269
|
+
validAfter: BigInt(authorization.validAfter),
|
|
270
|
+
validBefore: BigInt(authorization.validBefore),
|
|
271
|
+
nonce: normNonce,
|
|
272
|
+
};
|
|
273
|
+
let sigHex;
|
|
274
|
+
if (authorization.signature instanceof SignatureData) {
|
|
275
|
+
sigHex = authorization.signature.compact;
|
|
276
|
+
}
|
|
277
|
+
else if (typeof authorization.signature === 'string') {
|
|
278
|
+
const clean = authorization.signature.startsWith('0x')
|
|
279
|
+
? authorization.signature
|
|
280
|
+
: `0x${authorization.signature}`;
|
|
281
|
+
sigHex = clean.toLowerCase();
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
const recovered = await recoverTypedDataAddress({
|
|
287
|
+
domain,
|
|
288
|
+
types,
|
|
289
|
+
primaryType: 'TransferWithAuthorization',
|
|
290
|
+
message,
|
|
291
|
+
signature: sigHex,
|
|
292
|
+
});
|
|
293
|
+
return recovered.toLowerCase() === targetPayer;
|
|
294
|
+
}
|
|
295
|
+
catch {
|
|
296
|
+
return false;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
//# sourceMappingURL=signer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signer.js","sourceRoot":"","sources":["../../src/client/signer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,MAAM,CAAC;AAC/D,OAAO,EAA0B,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAC5E,OAAO,EACL,oBAAoB,EAEpB,aAAa,GACd,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EACL,sBAAsB,EACtB,wBAAwB,EACxB,WAAW,EACX,uBAAuB,GACxB,MAAM,aAAa,CAAC;AAGrB,mEAAmE;AACnE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AAM,IAAI,OAAO,aAAa,CAAC,SAAS,KAAK,QAAQ,EAAE,CAAC;gBACvD,MAAM,KAAK,GAAG,aAAa,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC;oBACpD,CAAC,CAAC,aAAa,CAAC,SAAS;oBACzB,CAAC,CAAC,KAAK,aAAa,CAAC,SAAS,EAAE,CAAC;gBACnC,MAAM,GAAG,KAAK,CAAC,WAAW,EAAmB,CAAC;YAChD,CAAC;iBAAM,CAAC;gBACN,OAAO,KAAK,CAAC;YACf,CAAC;YAED,MAAM,SAAS,GAAG,MAAM,uBAAuB,CAAC;gBAC9C,MAAM;gBACN,KAAK;gBACL,WAAW,EAAE,2BAA2B;gBACxC,OAAO;gBACP,SAAS,EAAE,MAAM;aAClB,CAAC,CAAC;YAEH,OAAO,SAAS,CAAC,WAAW,EAAE,KAAK,WAAW,CAAC;QACjD,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP Fetch Interceptor and Transport for AgentPay.
|
|
3
|
+
*
|
|
4
|
+
* Implements transparent 402 Payment Required interception, policy evaluation,
|
|
5
|
+
* EIP-712 / EIP-3009 signing, agentpay-wallet authorization, and post-dispatch safety.
|
|
6
|
+
*/
|
|
7
|
+
import type { Clock, IPolicyEngine } from '../policy/interfaces.js';
|
|
8
|
+
import type { ISigner } from './interfaces.js';
|
|
9
|
+
export type CredentialMode = 'block' | 'meter' | 'allow';
|
|
10
|
+
export declare const ALLOW_UNMETERED_HEADER = "x-agentpay-allow-unmetered";
|
|
11
|
+
export declare const DEFAULT_CREDENTIAL_HEADERS: readonly string[];
|
|
12
|
+
export declare const DEFAULT_CREDENTIAL_VALUE_PREFIXES: readonly string[];
|
|
13
|
+
export interface AgentPayRequestInit extends RequestInit {
|
|
14
|
+
params?: Record<string, string | number | boolean | undefined | null> | URLSearchParams;
|
|
15
|
+
}
|
|
16
|
+
export interface AgentPayClientOptions {
|
|
17
|
+
signer: ISigner;
|
|
18
|
+
policy: IPolicyEngine;
|
|
19
|
+
baseFetch?: typeof globalThis.fetch;
|
|
20
|
+
baseUrl?: string;
|
|
21
|
+
defaultHeaders?: Record<string, string>;
|
|
22
|
+
autoRetryOn402?: boolean;
|
|
23
|
+
raiseOnPolicyRejection?: boolean;
|
|
24
|
+
clock?: Clock;
|
|
25
|
+
unmeteredCredentialPolicy?: CredentialMode;
|
|
26
|
+
credentialHeaders?: readonly string[];
|
|
27
|
+
credentialValuePrefixes?: readonly string[];
|
|
28
|
+
estimateCost?: (req: Request) => bigint | number;
|
|
29
|
+
actualCost?: (res: Response) => bigint | number | null;
|
|
30
|
+
credentialTtlSeconds?: number;
|
|
31
|
+
walletChallengeMode?: boolean;
|
|
32
|
+
}
|
|
33
|
+
export declare function computeRequestBinding(method: string, urlStr: string, bodyBytes: Uint8Array | null): string;
|
|
34
|
+
export declare class AgentPayTransport {
|
|
35
|
+
readonly options: Readonly<AgentPayClientOptions>;
|
|
36
|
+
private readonly baseFetch;
|
|
37
|
+
private readonly clock;
|
|
38
|
+
private readonly credentialHeaders;
|
|
39
|
+
private readonly credentialValuePrefixes;
|
|
40
|
+
constructor(options: AgentPayClientOptions);
|
|
41
|
+
fetch(input: string | URL | Request, init?: RequestInit): Promise<Response>;
|
|
42
|
+
}
|
|
43
|
+
export declare function createAgentPayFetch(options: AgentPayClientOptions): typeof globalThis.fetch;
|
|
44
|
+
//# sourceMappingURL=transport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transport.d.ts","sourceRoot":"","sources":["../../src/client/transport.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAWH,OAAO,KAAK,EAAE,KAAK,EAAwB,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAC1F,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAE/C,MAAM,MAAM,cA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