@agenticprimitives/payments 0.0.0-alpha.10

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 (84) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +44 -0
  3. package/dist/entitlement/index.d.ts +125 -0
  4. package/dist/entitlement/index.d.ts.map +1 -0
  5. package/dist/entitlement/index.js +128 -0
  6. package/dist/entitlement/index.js.map +1 -0
  7. package/dist/entitlement/voucher.d.ts +85 -0
  8. package/dist/entitlement/voucher.d.ts.map +1 -0
  9. package/dist/entitlement/voucher.js +109 -0
  10. package/dist/entitlement/voucher.js.map +1 -0
  11. package/dist/index.d.ts +135 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +60 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/mandate-sign.d.ts +187 -0
  16. package/dist/mandate-sign.d.ts.map +1 -0
  17. package/dist/mandate-sign.js +177 -0
  18. package/dist/mandate-sign.js.map +1 -0
  19. package/dist/mandate.d.ts +33 -0
  20. package/dist/mandate.d.ts.map +1 -0
  21. package/dist/mandate.js +40 -0
  22. package/dist/mandate.js.map +1 -0
  23. package/dist/ops.d.ts +55 -0
  24. package/dist/ops.d.ts.map +1 -0
  25. package/dist/ops.js +66 -0
  26. package/dist/ops.js.map +1 -0
  27. package/dist/rails/escrow.d.ts +156 -0
  28. package/dist/rails/escrow.d.ts.map +1 -0
  29. package/dist/rails/escrow.js +88 -0
  30. package/dist/rails/escrow.js.map +1 -0
  31. package/dist/rails/invoice.d.ts +65 -0
  32. package/dist/rails/invoice.d.ts.map +1 -0
  33. package/dist/rails/invoice.js +57 -0
  34. package/dist/rails/invoice.js.map +1 -0
  35. package/dist/rails/recurring.d.ts +56 -0
  36. package/dist/rails/recurring.d.ts.map +1 -0
  37. package/dist/rails/recurring.js +58 -0
  38. package/dist/rails/recurring.js.map +1 -0
  39. package/dist/rails/wallet.d.ts +16 -0
  40. package/dist/rails/wallet.d.ts.map +1 -0
  41. package/dist/rails/wallet.js +20 -0
  42. package/dist/rails/wallet.js.map +1 -0
  43. package/dist/rails/x402/executor.d.ts +118 -0
  44. package/dist/rails/x402/executor.d.ts.map +1 -0
  45. package/dist/rails/x402/executor.js +243 -0
  46. package/dist/rails/x402/executor.js.map +1 -0
  47. package/dist/rails/x402/index.d.ts +11 -0
  48. package/dist/rails/x402/index.d.ts.map +1 -0
  49. package/dist/rails/x402/index.js +8 -0
  50. package/dist/rails/x402/index.js.map +1 -0
  51. package/dist/rails/x402/nonce-store.d.ts +30 -0
  52. package/dist/rails/x402/nonce-store.d.ts.map +1 -0
  53. package/dist/rails/x402/nonce-store.js +35 -0
  54. package/dist/rails/x402/nonce-store.js.map +1 -0
  55. package/dist/rails/x402/quote.d.ts +41 -0
  56. package/dist/rails/x402/quote.d.ts.map +1 -0
  57. package/dist/rails/x402/quote.js +84 -0
  58. package/dist/rails/x402/quote.js.map +1 -0
  59. package/dist/rails/x402/resource.d.ts +42 -0
  60. package/dist/rails/x402/resource.d.ts.map +1 -0
  61. package/dist/rails/x402/resource.js +71 -0
  62. package/dist/rails/x402/resource.js.map +1 -0
  63. package/dist/rails/x402/wire.d.ts +81 -0
  64. package/dist/rails/x402/wire.d.ts.map +1 -0
  65. package/dist/rails/x402/wire.js +125 -0
  66. package/dist/rails/x402/wire.js.map +1 -0
  67. package/dist/receipt.d.ts +36 -0
  68. package/dist/receipt.d.ts.map +1 -0
  69. package/dist/receipt.js +50 -0
  70. package/dist/receipt.js.map +1 -0
  71. package/dist/refund.d.ts +29 -0
  72. package/dist/refund.d.ts.map +1 -0
  73. package/dist/refund.js +19 -0
  74. package/dist/refund.js.map +1 -0
  75. package/dist/split.d.ts +27 -0
  76. package/dist/split.d.ts.map +1 -0
  77. package/dist/split.js +33 -0
  78. package/dist/split.js.map +1 -0
  79. package/dist/transfer.d.ts +51 -0
  80. package/dist/transfer.d.ts.map +1 -0
  81. package/dist/transfer.js +54 -0
  82. package/dist/transfer.js.map +1 -0
  83. package/package.json +61 -0
  84. package/spec.md +5 -0
@@ -0,0 +1,135 @@
1
+ /**
2
+ * @agenticprimitives/payments — PaymentMandate + rail abstraction.
3
+ * Spine Layer 9b.
4
+ *
5
+ * Authoritative spec: specs/243-payments.md
6
+ */
7
+ import type { Address, Hex } from '@agenticprimitives/types';
8
+ export declare const PACKAGE_NAME = "@agenticprimitives/payments";
9
+ export declare const PACKAGE_STATUS: "w1-foundational";
10
+ export declare const SPEC_REF = "specs/243-payments.md";
11
+ export type Hex32 = `0x${string}`;
12
+ export type PaymentRail = 'x402' | 'wallet' | 'sponsored-userop' | 'escrow' | 'invoice' | 'confidential-aztec' | 'confidential-zcash' | 'confidential-zk-paymaster';
13
+ export interface AssetRef {
14
+ id: string;
15
+ symbol?: string;
16
+ decimals?: number;
17
+ }
18
+ export type AmountPolicy = {
19
+ kind: 'exact';
20
+ amount: bigint;
21
+ asset: AssetRef;
22
+ chain: number;
23
+ } | {
24
+ kind: 'range';
25
+ minAmount: bigint;
26
+ maxAmount: bigint;
27
+ asset: AssetRef;
28
+ chain: number;
29
+ } | {
30
+ kind: 'formula';
31
+ formulaId: Hex32;
32
+ maxAmount: bigint;
33
+ asset: AssetRef;
34
+ chain: number;
35
+ };
36
+ export interface MandateConstraints {
37
+ maxAggregateAmount?: bigint;
38
+ frequency?: {
39
+ maxRedemptionsPerWindow: number;
40
+ windowSeconds: number;
41
+ };
42
+ categories?: string[];
43
+ excludedCategories?: string[];
44
+ geoFence?: string[];
45
+ }
46
+ export interface ContextBinding {
47
+ intentId?: string;
48
+ agreementCommitment?: Hex32;
49
+ taskId?: Hex32;
50
+ artifactHash?: Hex32;
51
+ resource?: {
52
+ method: string;
53
+ url: string;
54
+ requestBodyHash: Hex32;
55
+ };
56
+ /** EXC-R2 — the ExchangeOrder this payment leg belongs to (273/274). x402 sets `orderHash = quoteId`. */
57
+ orderHash?: Hex32;
58
+ /** EXC-R2 — distinguishes legs within one order (charge / refund / split-out / escrow-release). */
59
+ legId?: Hex32;
60
+ chain: number;
61
+ asset: AssetRef;
62
+ nonce: bigint;
63
+ validFrom: number;
64
+ expiresAt: number;
65
+ }
66
+ export type PaymentMandateMode = 'open' | 'closed';
67
+ export interface PaymentMandate {
68
+ mandateId: Hex32;
69
+ payer: Address;
70
+ payee: Address;
71
+ granter: Address;
72
+ rail: PaymentRail;
73
+ railConfig?: Record<string, unknown>;
74
+ amountPolicy: AmountPolicy;
75
+ mandateConstraints?: MandateConstraints;
76
+ nonce: bigint;
77
+ maxRedemptions: number;
78
+ validFrom: number;
79
+ expiresAt: number;
80
+ contextBinding: ContextBinding;
81
+ delegationRef?: Hex32;
82
+ mode: PaymentMandateMode;
83
+ requiresClosedMandateForFinalCharge?: boolean;
84
+ reasonHash: Hex32;
85
+ signature: Hex;
86
+ }
87
+ /** PMT-3.1: at least one context-binding handle MUST be populated. */
88
+ export declare function assertContextBindingValid(cb: ContextBinding): void;
89
+ /** PMT-INV-14 / PMT-10.1: closed mandates are always one-shot. */
90
+ export declare function assertClosedMandateInvariants(mandate: PaymentMandate): void;
91
+ export declare function computeMandateId(args: {
92
+ payer: Address;
93
+ nonce: bigint;
94
+ rail: PaymentRail;
95
+ chain: number;
96
+ }): Hex32;
97
+ export interface PaymentRailExecutor {
98
+ rail: PaymentRail;
99
+ verifyMandate(mandate: PaymentMandate): Promise<{
100
+ valid: boolean;
101
+ reason?: string;
102
+ }>;
103
+ prepareRedemption(mandate: PaymentMandate): Promise<{
104
+ planId: Hex32;
105
+ details: Record<string, unknown>;
106
+ }>;
107
+ executeRedemption(plan: {
108
+ planId: Hex32;
109
+ }): Promise<{
110
+ receiptHash: Hex32;
111
+ settlementHash: Hex32;
112
+ }>;
113
+ }
114
+ export declare function registerRail(executor: PaymentRailExecutor): void;
115
+ export declare function getRail(rail: PaymentRail): PaymentRailExecutor | undefined;
116
+ export { PAYMENT_MANDATE_DOMAIN_NAME, PAYMENT_MANDATE_DOMAIN_VERSION, PAYMENT_MANDATE_EIP712_TYPES, ERC1271_MAGIC, mandateAmount, hashContextBinding, hashMandateBindings, paymentMandateDomain, buildPaymentMandateTypedData, paymentMandateDigest, signPaymentMandate, verifyPaymentMandateSignature, } from './mandate-sign.js';
117
+ export type { MandateDomainOpts, MandateSigner, Erc1271Reader } from './mandate-sign.js';
118
+ export { PAYMENT_RECEIPT_TYPE, settlementEpochBucket, buildPaymentReceiptCredential } from './receipt.js';
119
+ export type { PaymentReceiptInput } from './receipt.js';
120
+ export * as x402 from './rails/x402/index.js';
121
+ export * as entitlement from './entitlement/index.js';
122
+ export { ERC20_TRANSFER_ABI, ERC20_APPROVE_ABI, buildErc20Transfer, buildErc20Approve, buildNativeTransfer } from './transfer.js';
123
+ export type { TransferPlan } from './transfer.js';
124
+ export { buildClosedMandate } from './mandate.js';
125
+ export type { ClosedMandateInput } from './mandate.js';
126
+ export * as wallet from './rails/wallet.js';
127
+ export * as invoice from './rails/invoice.js';
128
+ export * as escrow from './rails/escrow.js';
129
+ export * as recurring from './rails/recurring.js';
130
+ export { buildRefund } from './refund.js';
131
+ export type { RefundInput, RefundPlan } from './refund.js';
132
+ export { buildSplitPayout, BPS_DENOMINATOR } from './split.js';
133
+ export type { SplitRecipient, SplitLeg } from './split.js';
134
+ export * as ops from './ops.js';
135
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,OAAO,KAAK,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAE7D,eAAO,MAAM,YAAY,gCAAgC,CAAC;AAC1D,eAAO,MAAM,cAAc,EAAG,iBAA0B,CAAC;AACzD,eAAO,MAAM,QAAQ,0BAA0B,CAAC;AAEhD,MAAM,MAAM,KAAK,GAAG,KAAK,MAAM,EAAE,CAAC;AAElC,MAAM,MAAM,WAAW,GACnB,MAAM,GACN,QAAQ,GACR,kBAAkB,GAClB,QAAQ,GAC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,uBAAuB,CAAC;AAG9C,OAAO,KAAK,WAAW,MAAM,wBAAwB,CAAC;AAGtD,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAClI,YAAY,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,YAAY,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAGvD,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC;AAC5C,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAC;AAC9C,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC;AAC5C,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC/D,YAAY,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG3D,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @agenticprimitives/payments — PaymentMandate + rail abstraction.
3
+ * Spine Layer 9b.
4
+ *
5
+ * Authoritative spec: specs/243-payments.md
6
+ */
7
+ import { keccak_256 } from '@noble/hashes/sha3.js';
8
+ import { utf8ToBytes } from '@noble/hashes/utils.js';
9
+ export const PACKAGE_NAME = '@agenticprimitives/payments';
10
+ export const PACKAGE_STATUS = 'w1-foundational';
11
+ export const SPEC_REF = 'specs/243-payments.md';
12
+ /** PMT-3.1: at least one context-binding handle MUST be populated. */
13
+ export function assertContextBindingValid(cb) {
14
+ const any = cb.intentId || cb.agreementCommitment || cb.taskId || cb.artifactHash || cb.resource;
15
+ if (!any) {
16
+ throw new Error('[payments/PMT-3.1] ContextBinding MUST populate at least one of intentId / agreementCommitment / taskId / artifactHash / resource');
17
+ }
18
+ }
19
+ /** PMT-INV-14 / PMT-10.1: closed mandates are always one-shot. */
20
+ export function assertClosedMandateInvariants(mandate) {
21
+ if (mandate.mode === 'closed' && mandate.maxRedemptions !== 1) {
22
+ throw new Error(`[payments/PMT-INV-14] closed mandate MUST have maxRedemptions = 1`);
23
+ }
24
+ }
25
+ export function computeMandateId(args) {
26
+ const blob = `${args.payer.toLowerCase()}:${args.nonce}:${args.rail}:${args.chain}`;
27
+ const digest = keccak_256(utf8ToBytes(blob));
28
+ let hex = '0x';
29
+ for (const v of digest)
30
+ hex += v.toString(16).padStart(2, '0');
31
+ return hex;
32
+ }
33
+ const _rails = new Map();
34
+ export function registerRail(executor) {
35
+ _rails.set(executor.rail, executor);
36
+ }
37
+ export function getRail(rail) {
38
+ return _rails.get(rail);
39
+ }
40
+ // Spec 243 §4.2 / PMT-INV-02/12 — EIP-712 mandate signing + ERC-1271 verification.
41
+ export { PAYMENT_MANDATE_DOMAIN_NAME, PAYMENT_MANDATE_DOMAIN_VERSION, PAYMENT_MANDATE_EIP712_TYPES, ERC1271_MAGIC, mandateAmount, hashContextBinding, hashMandateBindings, paymentMandateDomain, buildPaymentMandateTypedData, paymentMandateDigest, signPaymentMandate, verifyPaymentMandateSignature, } from './mandate-sign.js';
42
+ // Spec 243 §7 / X402-D9.3 — PaymentReceipt verifiable credential.
43
+ export { PAYMENT_RECEIPT_TYPE, settlementEpochBucket, buildPaymentReceiptCredential } from './receipt.js';
44
+ // Spec 272 — the x402 rail (staged executor + v2 wire + nonce store + resource binding).
45
+ export * as x402 from './rails/x402/index.js';
46
+ // Spec 272 §10 — entitlements (pay-once-then-access; credits = maxUses:N).
47
+ export * as entitlement from './entitlement/index.js';
48
+ // Spec 243 §5.5 — shared transfer plumbing + closed-mandate builder.
49
+ export { ERC20_TRANSFER_ABI, ERC20_APPROVE_ABI, buildErc20Transfer, buildErc20Approve, buildNativeTransfer } from './transfer.js';
50
+ export { buildClosedMandate } from './mandate.js';
51
+ // Spec 243 §5.5 — general-purpose rails + profiles + payout legs.
52
+ export * as wallet from './rails/wallet.js';
53
+ export * as invoice from './rails/invoice.js';
54
+ export * as escrow from './rails/escrow.js';
55
+ export * as recurring from './rails/recurring.js';
56
+ export { buildRefund } from './refund.js';
57
+ export { buildSplitPayout, BPS_DENOMINATOR } from './split.js';
58
+ // Spec 243 §5.5 — ops core (idempotent event log + reconciliation + export).
59
+ export * as ops from './ops.js';
60
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,uBAAuB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAC;AAGrD,MAAM,CAAC,MAAM,YAAY,GAAG,6BAA6B,CAAC;AAC1D,MAAM,CAAC,MAAM,cAAc,GAAG,iBAA0B,CAAC;AACzD,MAAM,CAAC,MAAM,QAAQ,GAAG,uBAAuB,CAAC;AAyEhD,sEAAsE;AACtE,MAAM,UAAU,yBAAyB,CAAC,EAAkB;IAC1D,MAAM,GAAG,GAAG,EAAE,CAAC,QAAQ,IAAI,EAAE,CAAC,mBAAmB,IAAI,EAAE,CAAC,MAAM,IAAI,EAAE,CAAC,YAAY,IAAI,EAAE,CAAC,QAAQ,CAAC;IACjG,IAAI,CAAC,GAAG,EAAE,CAAC;QACT,MAAM,IAAI,KAAK,CACb,mIAAmI,CACpI,CAAC;IACJ,CAAC;AACH,CAAC;AAED,kEAAkE;AAClE,MAAM,UAAU,6BAA6B,CAAC,OAAuB;IACnE,IAAI,OAAO,CAAC,IAAI,KAAK,QAAQ,IAAI,OAAO,CAAC,cAAc,KAAK,CAAC,EAAE,CAAC;QAC9D,MAAM,IAAI,KAAK,CAAC,mEAAmE,CAA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AqE;AACrE,OAAO,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAElI,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAGlD,kEAAkE;AAClE,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC;AAC5C,OAAO,KAAK,OAAO,MAAM,oBAAoB,CAAC;AAC9C,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAC;AAC5C,OAAO,KAAK,SAAS,MAAM,sBAAsB,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAG/D,6EAA6E;AAC7E,OAAO,KAAK,GAAG,MAAM,UAAU,CAAC"}
@@ -0,0 +1,187 @@
1
+ /**
2
+ * PMT-INV-02 / PMT-INV-12 — EIP-712 mandate signing + ERC-1271 verification.
3
+ *
4
+ * The payer SA signs an EIP-712 typed-data digest of the mandate. The signed
5
+ * struct flattens the load-bearing fields and folds the entire `ContextBinding`
6
+ * into one `contextBindingHash` — so no field can be stripped or substituted
7
+ * after signing (PMT-INV-02 / INV-07 / INV-08), and verification is one
8
+ * ERC-1271 call against the payer SA (PMT-INV-12 — no raw EOA signatures).
9
+ *
10
+ * Pure crypto only (viem) — the actual ERC-1271 read is injected so the package
11
+ * stays transport-agnostic (no hard publicClient dependency).
12
+ */
13
+ import { type Address, type Hex } from 'viem';
14
+ import type { PaymentMandate, ContextBinding, Hex32 } from './index.js';
15
+ export declare const PAYMENT_MANDATE_DOMAIN_NAME = "AgenticPaymentMandate";
16
+ export declare const PAYMENT_MANDATE_DOMAIN_VERSION = "1";
17
+ /** ERC-1271 magic value for a valid signature. */
18
+ export declare const ERC1271_MAGIC = "0x1626ba7e";
19
+ export declare const PAYMENT_MANDATE_EIP712_TYPES: {
20
+ readonly PaymentMandate: readonly [{
21
+ readonly name: "mandateId";
22
+ readonly type: "bytes32";
23
+ }, {
24
+ readonly name: "payer";
25
+ readonly type: "address";
26
+ }, {
27
+ readonly name: "payee";
28
+ readonly type: "address";
29
+ }, {
30
+ readonly name: "granter";
31
+ readonly type: "address";
32
+ }, {
33
+ readonly name: "asset";
34
+ readonly type: "address";
35
+ }, {
36
+ readonly name: "amount";
37
+ readonly type: "uint256";
38
+ }, {
39
+ readonly name: "nonce";
40
+ readonly type: "uint256";
41
+ }, {
42
+ readonly name: "maxRedemptions";
43
+ readonly type: "uint256";
44
+ }, {
45
+ readonly name: "validFrom";
46
+ readonly type: "uint256";
47
+ }, {
48
+ readonly name: "expiresAt";
49
+ readonly type: "uint256";
50
+ }, {
51
+ readonly name: "mode";
52
+ readonly type: "string";
53
+ }, {
54
+ readonly name: "rail";
55
+ readonly type: "string";
56
+ }, {
57
+ readonly name: "reasonHash";
58
+ readonly type: "bytes32";
59
+ }, {
60
+ readonly name: "contextBindingHash";
61
+ readonly type: "bytes32";
62
+ }, {
63
+ readonly name: "bindingHash";
64
+ readonly type: "bytes32";
65
+ }];
66
+ };
67
+ /**
68
+ * EXT-PMT-2 — fold the mandate's binding fields the core typed-data omitted into one committed hash:
69
+ * railConfig (the x402 quote binding: quoteId + resourceHash), mandateConstraints (aggregate scope),
70
+ * delegationRef, and the requiresClosedMandateForFinalCharge flag. Absent fields hash to a stable
71
+ * sentinel, so adding/removing one changes the digest.
72
+ */
73
+ export declare function hashMandateBindings(mandate: PaymentMandate): Hex32;
74
+ /** The charge amount carried by the mandate (exact → amount; range/formula → maxAmount). */
75
+ export declare function mandateAmount(mandate: PaymentMandate): bigint;
76
+ /**
77
+ * Canonical hash of the ENTIRE context binding (PMT-INV-02). Every field is
78
+ * folded in; absent fields hash to zero, so adding/removing one changes the hash.
79
+ */
80
+ export declare function hashContextBinding(cb: ContextBinding): Hex32;
81
+ export interface MandateDomainOpts {
82
+ chainId: number;
83
+ /** Optional binding to a specific contract (e.g. the PaymentEnforcer). Omit for a chain-scoped mandate. */
84
+ verifyingContract?: Address;
85
+ }
86
+ export declare function paymentMandateDomain(opts: MandateDomainOpts): {
87
+ readonly verifyingContract?: `0x${string}` | undefined;
88
+ readonly name: "AgenticPaymentMandate";
89
+ readonly version: "1";
90
+ readonly chainId: number;
91
+ };
92
+ /** The full EIP-712 typed-data object (domain + types + message) for a mandate. */
93
+ export declare function buildPaymentMandateTypedData(mandate: PaymentMandate, opts: MandateDomainOpts): {
94
+ domain: {
95
+ readonly verifyingContract?: `0x${string}` | undefined;
96
+ readonly name: "AgenticPaymentMandate";
97
+ readonly version: "1";
98
+ readonly chainId: number;
99
+ };
100
+ types: {
101
+ readonly PaymentMandate: readonly [{
102
+ readonly name: "mandateId";
103
+ readonly type: "bytes32";
104
+ }, {
105
+ readonly name: "payer";
106
+ readonly type: "address";
107
+ }, {
108
+ readonly name: "payee";
109
+ readonly type: "address";
110
+ }, {
111
+ readonly name: "granter";
112
+ readonly type: "address";
113
+ }, {
114
+ readonly name: "asset";
115
+ readonly type: "address";
116
+ }, {
117
+ readonly name: "amount";
118
+ readonly type: "uint256";
119
+ }, {
120
+ readonly name: "nonce";
121
+ readonly type: "uint256";
122
+ }, {
123
+ readonly name: "maxRedemptions";
124
+ readonly type: "uint256";
125
+ }, {
126
+ readonly name: "validFrom";
127
+ readonly type: "uint256";
128
+ }, {
129
+ readonly name: "expiresAt";
130
+ readonly type: "uint256";
131
+ }, {
132
+ readonly name: "mode";
133
+ readonly type: "string";
134
+ }, {
135
+ readonly name: "rail";
136
+ readonly type: "string";
137
+ }, {
138
+ readonly name: "reasonHash";
139
+ readonly type: "bytes32";
140
+ }, {
141
+ readonly name: "contextBindingHash";
142
+ readonly type: "bytes32";
143
+ }, {
144
+ readonly name: "bindingHash";
145
+ readonly type: "bytes32";
146
+ }];
147
+ };
148
+ primaryType: "PaymentMandate";
149
+ message: {
150
+ readonly mandateId: `0x${string}`;
151
+ readonly payer: `0x${string}`;
152
+ readonly payee: `0x${string}`;
153
+ readonly granter: `0x${string}`;
154
+ readonly asset: Address;
155
+ readonly amount: bigint;
156
+ readonly nonce: bigint;
157
+ readonly maxRedemptions: bigint;
158
+ readonly validFrom: bigint;
159
+ readonly expiresAt: bigint;
160
+ readonly mode: import("./index.js").PaymentMandateMode;
161
+ readonly rail: import("./index.js").PaymentRail;
162
+ readonly reasonHash: `0x${string}`;
163
+ readonly contextBindingHash: `0x${string}`;
164
+ readonly bindingHash: `0x${string}`;
165
+ };
166
+ };
167
+ /** The 32-byte EIP-712 digest the payer SA's ERC-1271 validates over. */
168
+ export declare function paymentMandateDigest(mandate: PaymentMandate, opts: MandateDomainOpts): Hex32;
169
+ /** Signer surface — an SA signer that produces an ERC-1271-validatable signature over typed data. */
170
+ export interface MandateSigner {
171
+ signTypedData(args: {
172
+ domain: Record<string, unknown>;
173
+ types: typeof PAYMENT_MANDATE_EIP712_TYPES;
174
+ primaryType: 'PaymentMandate';
175
+ message: Record<string, unknown>;
176
+ }): Promise<Hex>;
177
+ }
178
+ /** Sign an unsigned mandate (`signature: '0x'`) → a fully-populated mandate. */
179
+ export declare function signPaymentMandate(mandate: PaymentMandate, signer: MandateSigner, opts: MandateDomainOpts): Promise<PaymentMandate>;
180
+ /** Injected ERC-1271 read: returns the 4-byte magic from `payer.isValidSignature(digest, signature)`. */
181
+ export type Erc1271Reader = (account: Address, digest: Hex32, signature: Hex) => Promise<Hex>;
182
+ /**
183
+ * PMT-INV-12 — verify the mandate's signature via the payer SA's ERC-1271. Fail-closed: any read
184
+ * error or non-magic result is `false` (never throws on a bad signature).
185
+ */
186
+ export declare function verifyPaymentMandateSignature(mandate: PaymentMandate, opts: MandateDomainOpts, read1271: Erc1271Reader): Promise<boolean>;
187
+ //# sourceMappingURL=mandate-sign.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mandate-sign.d.ts","sourceRoot":"","sources":["../src/mandate-sign.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAML,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AA4B,CAAC;QAC3C,WAAW,EAAE,gBAAgB,CAAC;QAC9B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;KAClC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;CAClB;AAED,gFAAgF;AAChF,wBAAsB,kBAAkB,CACtC,OAAO,EAAE,cAAc,EACvB,MAAM,EAAE,aAAa,EACrB,IAAI,EAAE,iBAAiB,GACtB,OAAO,CAAC,cAAc,CAAC,CASzB;AAED,yGAAyG;AACzG,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;AAE9F;;;GAGG;AACH,wBAAsB,6BAA6B,CACjD,OAAO,EAAE,cAAc,EACvB,IAAI,EAAE,iBAAiB,EACvB,QAAQ,EAAE,aAAa,GACtB,OAAO,CAAC,OAAO,CAAC,CASlB"}
@@ -0,0 +1,177 @@
1
+ /**
2
+ * PMT-INV-02 / PMT-INV-12 — EIP-712 mandate signing + ERC-1271 verification.
3
+ *
4
+ * The payer SA signs an EIP-712 typed-data digest of the mandate. The signed
5
+ * struct flattens the load-bearing fields and folds the entire `ContextBinding`
6
+ * into one `contextBindingHash` — so no field can be stripped or substituted
7
+ * after signing (PMT-INV-02 / INV-07 / INV-08), and verification is one
8
+ * ERC-1271 call against the payer SA (PMT-INV-12 — no raw EOA signatures).
9
+ *
10
+ * Pure crypto only (viem) — the actual ERC-1271 read is injected so the package
11
+ * stays transport-agnostic (no hard publicClient dependency).
12
+ */
13
+ import { hashTypedData, keccak256, encodeAbiParameters, toHex, toBytes, } from 'viem';
14
+ export const PAYMENT_MANDATE_DOMAIN_NAME = 'AgenticPaymentMandate';
15
+ export const PAYMENT_MANDATE_DOMAIN_VERSION = '1';
16
+ /** ERC-1271 magic value for a valid signature. */
17
+ export const ERC1271_MAGIC = '0x1626ba7e';
18
+ export const PAYMENT_MANDATE_EIP712_TYPES = {
19
+ PaymentMandate: [
20
+ { name: 'mandateId', type: 'bytes32' },
21
+ { name: 'payer', type: 'address' },
22
+ { name: 'payee', type: 'address' },
23
+ { name: 'granter', type: 'address' },
24
+ { name: 'asset', type: 'address' },
25
+ { name: 'amount', type: 'uint256' },
26
+ { name: 'nonce', type: 'uint256' },
27
+ { name: 'maxRedemptions', type: 'uint256' },
28
+ { name: 'validFrom', type: 'uint256' },
29
+ { name: 'expiresAt', type: 'uint256' },
30
+ { name: 'mode', type: 'string' },
31
+ { name: 'rail', type: 'string' },
32
+ { name: 'reasonHash', type: 'bytes32' },
33
+ { name: 'contextBindingHash', type: 'bytes32' },
34
+ // EXT-PMT-2: commit the previously-unsigned binding fields — railConfig (the quote binding,
35
+ // quoteId/resourceHash), delegationRef, mandateConstraints, and requiresClosedMandateForFinalCharge —
36
+ // so a signature can't be replayed against a different quote / delegation / constraint set.
37
+ { name: 'bindingHash', type: 'bytes32' },
38
+ ],
39
+ };
40
+ const ZERO32 = ('0x' + '00'.repeat(32));
41
+ const hashStr = (s) => (s ? keccak256(toBytes(s)) : ZERO32);
42
+ /** Deterministic serialization (sorted keys, bigint→decimal, undefined dropped) for the loose
43
+ * railConfig / mandateConstraints objects so their hash is stable across key order. */
44
+ function canonicalJson(v) {
45
+ if (v === null || v === undefined)
46
+ return 'null';
47
+ if (typeof v === 'bigint')
48
+ return v.toString();
49
+ if (typeof v !== 'object')
50
+ return JSON.stringify(v);
51
+ if (Array.isArray(v))
52
+ return `[${v.map(canonicalJson).join(',')}]`;
53
+ const o = v;
54
+ return `{${Object.keys(o).sort().filter((k) => o[k] !== undefined).map((k) => `${JSON.stringify(k)}:${canonicalJson(o[k])}`).join(',')}}`;
55
+ }
56
+ /**
57
+ * EXT-PMT-2 — fold the mandate's binding fields the core typed-data omitted into one committed hash:
58
+ * railConfig (the x402 quote binding: quoteId + resourceHash), mandateConstraints (aggregate scope),
59
+ * delegationRef, and the requiresClosedMandateForFinalCharge flag. Absent fields hash to a stable
60
+ * sentinel, so adding/removing one changes the digest.
61
+ */
62
+ export function hashMandateBindings(mandate) {
63
+ const railConfigHash = hashStr(canonicalJson(mandate.railConfig ?? null));
64
+ const constraintsHash = hashStr(canonicalJson(mandate.mandateConstraints ?? null));
65
+ return keccak256(encodeAbiParameters([{ type: 'bytes32' }, { type: 'bytes32' }, { type: 'bytes32' }, { type: 'bool' }], [railConfigHash, constraintsHash, mandate.delegationRef ?? ZERO32, !!mandate.requiresClosedMandateForFinalCharge]));
66
+ }
67
+ /** The charge amount carried by the mandate (exact → amount; range/formula → maxAmount). */
68
+ export function mandateAmount(mandate) {
69
+ const ap = mandate.amountPolicy;
70
+ return ap.kind === 'exact' ? ap.amount : ap.maxAmount;
71
+ }
72
+ /**
73
+ * Canonical hash of the ENTIRE context binding (PMT-INV-02). Every field is
74
+ * folded in; absent fields hash to zero, so adding/removing one changes the hash.
75
+ */
76
+ export function hashContextBinding(cb) {
77
+ const resourceHash = cb.resource
78
+ ? keccak256(encodeAbiParameters([{ type: 'string' }, { type: 'string' }, { type: 'bytes32' }], [cb.resource.method, cb.resource.url, cb.resource.requestBodyHash]))
79
+ : ZERO32;
80
+ return keccak256(encodeAbiParameters([
81
+ { type: 'bytes32' }, // intentIdHash
82
+ { type: 'bytes32' }, // agreementCommitment
83
+ { type: 'bytes32' }, // taskId
84
+ { type: 'bytes32' }, // artifactHash
85
+ { type: 'bytes32' }, // resourceHash
86
+ { type: 'bytes32' }, // orderHash
87
+ { type: 'bytes32' }, // legId
88
+ { type: 'uint256' }, // chain
89
+ { type: 'address' }, // asset
90
+ { type: 'uint256' }, // nonce
91
+ { type: 'uint256' }, // validFrom
92
+ { type: 'uint256' }, // expiresAt
93
+ ], [
94
+ hashStr(cb.intentId),
95
+ cb.agreementCommitment ?? ZERO32,
96
+ cb.taskId ?? ZERO32,
97
+ cb.artifactHash ?? ZERO32,
98
+ resourceHash,
99
+ cb.orderHash ?? ZERO32,
100
+ cb.legId ?? ZERO32,
101
+ BigInt(cb.chain),
102
+ cb.asset.id,
103
+ cb.nonce,
104
+ BigInt(cb.validFrom),
105
+ BigInt(cb.expiresAt),
106
+ ]));
107
+ }
108
+ export function paymentMandateDomain(opts) {
109
+ return {
110
+ name: PAYMENT_MANDATE_DOMAIN_NAME,
111
+ version: PAYMENT_MANDATE_DOMAIN_VERSION,
112
+ chainId: opts.chainId,
113
+ ...(opts.verifyingContract ? { verifyingContract: opts.verifyingContract } : {}),
114
+ };
115
+ }
116
+ function mandateMessage(mandate) {
117
+ return {
118
+ mandateId: mandate.mandateId,
119
+ payer: mandate.payer,
120
+ payee: mandate.payee,
121
+ granter: mandate.granter,
122
+ asset: mandate.amountPolicy.asset.id,
123
+ amount: mandateAmount(mandate),
124
+ nonce: mandate.nonce,
125
+ maxRedemptions: BigInt(mandate.maxRedemptions),
126
+ validFrom: BigInt(mandate.validFrom),
127
+ expiresAt: BigInt(mandate.expiresAt),
128
+ mode: mandate.mode,
129
+ rail: mandate.rail,
130
+ reasonHash: mandate.reasonHash,
131
+ contextBindingHash: hashContextBinding(mandate.contextBinding),
132
+ bindingHash: hashMandateBindings(mandate),
133
+ };
134
+ }
135
+ /** The full EIP-712 typed-data object (domain + types + message) for a mandate. */
136
+ export function buildPaymentMandateTypedData(mandate, opts) {
137
+ return {
138
+ domain: paymentMandateDomain(opts),
139
+ types: PAYMENT_MANDATE_EIP712_TYPES,
140
+ primaryType: 'PaymentMandate',
141
+ message: mandateMessage(mandate),
142
+ };
143
+ }
144
+ /** The 32-byte EIP-712 digest the payer SA's ERC-1271 validates over. */
145
+ export function paymentMandateDigest(mandate, opts) {
146
+ const td = buildPaymentMandateTypedData(mandate, opts);
147
+ return hashTypedData(td);
148
+ }
149
+ /** Sign an unsigned mandate (`signature: '0x'`) → a fully-populated mandate. */
150
+ export async function signPaymentMandate(mandate, signer, opts) {
151
+ const td = buildPaymentMandateTypedData(mandate, opts);
152
+ const signature = await signer.signTypedData({
153
+ domain: td.domain,
154
+ types: td.types,
155
+ primaryType: td.primaryType,
156
+ message: td.message,
157
+ });
158
+ return { ...mandate, signature };
159
+ }
160
+ /**
161
+ * PMT-INV-12 — verify the mandate's signature via the payer SA's ERC-1271. Fail-closed: any read
162
+ * error or non-magic result is `false` (never throws on a bad signature).
163
+ */
164
+ export async function verifyPaymentMandateSignature(mandate, opts, read1271) {
165
+ if (!mandate.signature || mandate.signature === '0x')
166
+ return false;
167
+ const digest = paymentMandateDigest(mandate, opts);
168
+ try {
169
+ const res = await read1271(mandate.payer, digest, mandate.signature);
170
+ return typeof res === 'string' && res.slice(0, 10).toLowerCase() === ERC1271_MAGIC;
171
+ }
172
+ catch {
173
+ return false;
174
+ }
175
+ }
176
+ void toHex; // reserved for future debug helpers
177
+ //# sourceMappingURL=mandate-sign.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mandate-sign.js","sourceRoot":"","sources":["../src/mandate-sign.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EACL,aAAa,EACb,SAAS,EACT,mBAAmB,EACnB,KAAK,EACL,OAAO,GAGR,MAAM,MAAM,CAAC;AAGd,MAAM,CAAC,MAAM,2BAA2B,GAAG,uBAAuB,CAAC;AACnE,MAAM,CAAC,MAAM,8BAA8B,GAAG,GAAG,CAAC;AAElD,kDAAkD;AAClD,MAAM,CAAC,MAAM,aAAa,GAAG,YAAY,CAAC;AAE1C,MAAM,CAAC,MAAM,4BAA4B,GAAG;IAC1C,cAAc,EAAE;QACd,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,SAAS,EAAE;QACtC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;QAClC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;QAClC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;QACpC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;QAClC,EAAE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,OAAO,KAAK,CAAC;IACnE,MAAM,MAAM,GAAG,oBAAoB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACnD,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,MAAM,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC;QACrE,OAAO,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,WAAW,EAAE,KAAK,aAAa,CAAC;IACrF,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED,KAAK,KAAK,CAAC,CAAC,oCAAoC"}
@@ -0,0 +1,33 @@
1
+ /**
2
+ * `buildClosedMandate` — construct an unsigned closed (one-shot) PaymentMandate.
3
+ *
4
+ * The wallet + invoice rails (and the demo) build a closed mandate, sign it via
5
+ * `signPaymentMandate`, then settle it with a single transfer. Closed + one-shot per
6
+ * PMT-10 / PMT-INV-14. `orderHash`/`legId` (EXC-R2) bind the leg to an ExchangeOrder.
7
+ */
8
+ import type { Address } from '@agenticprimitives/types';
9
+ import { type PaymentMandate, type PaymentRail, type AssetRef, type Hex32 } from './index.js';
10
+ export interface ClosedMandateInput {
11
+ payer: Address;
12
+ payee: Address;
13
+ asset: AssetRef;
14
+ amount: bigint;
15
+ chain: number;
16
+ rail: PaymentRail;
17
+ nonce: bigint;
18
+ expiresAt: number;
19
+ validFrom?: number;
20
+ granter?: Address;
21
+ orderHash?: Hex32;
22
+ legId?: Hex32;
23
+ reasonHash?: Hex32;
24
+ resource?: {
25
+ method: string;
26
+ url: string;
27
+ requestBodyHash: Hex32;
28
+ };
29
+ delegationRef?: Hex32;
30
+ }
31
+ /** Build the unsigned closed mandate (signature `'0x'`); sign with `signPaymentMandate`. */
32
+ export declare function buildClosedMandate(input: ClosedMandateInput): PaymentMandate;
33
+ //# sourceMappingURL=mandate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mandate.d.ts","sourceRoot":"","sources":["../src/mandate.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAoB,KAAK,cAAc,EAAE,KAAK,WAAW,EAAE,KAAK,QAAQ,EAAE,KAAK,KAAK,EAAE,MAAM,YAAY,CAAC;AAIhH,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,OAAO,CAAC;IACf,KAAK,EAAE,OAAO,CAAC;IACf,KAAK,EAAE,QAAQ,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,WAAW,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,SAAS,CAAC,EAAE,KAAK,CAAC;IAClB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,UAAU,CAAC,EAAE,KAAK,CAAC;IACnB,QAAQ,CAAC,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,eAAe,EAAE,KAAK,CAAA;KAAE,CAAC;IACnE,aAAa,CAAC,EAAE,KAAK,CAAC;CACvB;AAED,4FAA4F;AAC5F,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,kBAAkB,GAAG,cAAc,CA4B5E"}