payid 0.5.9 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,13 +2,14 @@ import {
2
2
  combineRules
3
3
  } from "./chunk-GG34PNTF.js";
4
4
  import {
5
- decodeSessionPolicy
6
- } from "./chunk-MXKZJKXE.js";
5
+ decodeSessionPolicy,
6
+ decodeSessionPolicyV2
7
+ } from "./chunk-BLIRABV7.js";
7
8
  import {
8
9
  evaluate,
9
10
  generateDecisionProof,
10
11
  resolveRule
11
- } from "./chunk-PAJYP7JI.js";
12
+ } from "./chunk-DZ6GVRER.js";
12
13
  import {
13
14
  __export
14
15
  } from "./chunk-MLKGABMK.js";
@@ -43,7 +44,10 @@ var PayIDClient = class {
43
44
  }
44
45
  async evaluateAndProve(params) {
45
46
  const authorityConfig = isRuleSource(params.authorityRule) ? (await resolveRule(params.authorityRule)).config : params.authorityRule;
46
- const evalConfig = params.evaluationRule ?? (params.sessionPolicy ? combineRules(
47
+ const evalConfig = params.evaluationRule ?? (params.sessionPolicyV2 ? (() => {
48
+ decodeSessionPolicyV2(params.sessionPolicyV2, params.blockTimestamp);
49
+ return authorityConfig;
50
+ })() : params.sessionPolicy ? combineRules(
47
51
  authorityConfig,
48
52
  decodeSessionPolicy(
49
53
  params.sessionPolicy,
@@ -0,0 +1,207 @@
1
+ import {
2
+ canonicalizeRuleSet
3
+ } from "./chunk-6VPSJFO4.js";
4
+ import {
5
+ randomHex
6
+ } from "./chunk-KDC67LIN.js";
7
+
8
+ // src/sessionPolicy/create.ts
9
+ import { ethers } from "ethers";
10
+ async function createSessionPolicyPayload(params) {
11
+ const issuedAt = Math.floor(Date.now() / 1e3);
12
+ const nonce = randomHex(16);
13
+ const payload = {
14
+ version: "payid.session.policy.v1",
15
+ receiver: params.receiver,
16
+ rule: canonicalizeRuleSet(params.rule),
17
+ issuedAt,
18
+ expiresAt: params.expiresAt,
19
+ nonce
20
+ };
21
+ const message = ethers.keccak256(
22
+ ethers.toUtf8Bytes(JSON.stringify(payload))
23
+ );
24
+ const signature = await params.signer.signMessage(message);
25
+ return { ...payload, signature };
26
+ }
27
+ var SESSION_POLICY_V2_DOMAIN_NAME = "PAY.ID SessionPolicy";
28
+ var SESSION_POLICY_V2_DOMAIN_VERSION = "1";
29
+ var SESSION_POLICY_V2_TYPES = {
30
+ SessionPolicy: [
31
+ { name: "receiver", type: "address" },
32
+ { name: "ruleSetHash", type: "bytes32" },
33
+ { name: "ruleAuthority", type: "address" },
34
+ { name: "allowedAsset", type: "address" },
35
+ { name: "maxAmount", type: "uint256" },
36
+ { name: "expiresAt", type: "uint64" },
37
+ { name: "policyNonce", type: "bytes32" },
38
+ { name: "payId", type: "bytes32" }
39
+ ]
40
+ };
41
+ function buildSessionPolicyV2Domain(chainId, verifyingContract) {
42
+ return {
43
+ name: SESSION_POLICY_V2_DOMAIN_NAME,
44
+ version: SESSION_POLICY_V2_DOMAIN_VERSION,
45
+ chainId,
46
+ verifyingContract
47
+ };
48
+ }
49
+ async function createSessionPolicyV2(params) {
50
+ const {
51
+ receiver,
52
+ ruleSetHash,
53
+ ruleAuthority,
54
+ allowedAsset,
55
+ maxAmount,
56
+ expiresAt,
57
+ payId,
58
+ chainId,
59
+ verifyingContract,
60
+ signer
61
+ } = params;
62
+ if (!ethers.isAddress(receiver)) {
63
+ throw new Error(`SESSION_POLICY_V2: receiver address tidak valid: ${receiver}`);
64
+ }
65
+ if (maxAmount <= 0n) {
66
+ throw new Error("SESSION_POLICY_V2: maxAmount harus > 0");
67
+ }
68
+ if (expiresAt <= Math.floor(Date.now() / 1e3)) {
69
+ throw new Error("SESSION_POLICY_V2: expiresAt sudah lewat");
70
+ }
71
+ const policyNonce = randomHex(32);
72
+ const payIdBytes32 = ethers.keccak256(ethers.toUtf8Bytes(payId));
73
+ const domain = buildSessionPolicyV2Domain(chainId, verifyingContract);
74
+ const value = {
75
+ receiver,
76
+ ruleSetHash,
77
+ ruleAuthority,
78
+ allowedAsset,
79
+ maxAmount,
80
+ expiresAt,
81
+ policyNonce,
82
+ payId: payIdBytes32
83
+ };
84
+ const signature = await signer.signTypedData(
85
+ domain,
86
+ SESSION_POLICY_V2_TYPES,
87
+ value
88
+ );
89
+ const recovered = ethers.verifyTypedData(domain, SESSION_POLICY_V2_TYPES, value, signature);
90
+ if (recovered.toLowerCase() !== receiver.toLowerCase()) {
91
+ throw new Error("SESSION_POLICY_V2: self-verification gagal");
92
+ }
93
+ return {
94
+ version: "payid.session.policy.v2",
95
+ receiver,
96
+ ruleSetHash,
97
+ ruleAuthority,
98
+ allowedAsset,
99
+ maxAmount: maxAmount.toString(),
100
+ expiresAt,
101
+ policyNonce,
102
+ payId,
103
+ chainId,
104
+ verifyingContract,
105
+ signature
106
+ };
107
+ }
108
+
109
+ // src/sessionPolicy/decode.ts
110
+ import { ethers as ethers2 } from "ethers";
111
+ function decodeSessionPolicy(sessionPolicy, now) {
112
+ if (sessionPolicy.version !== "payid.session.policy.v1") {
113
+ throw new Error("INVALID_SESSION_POLICY_VERSION");
114
+ }
115
+ if (now > sessionPolicy.expiresAt) {
116
+ throw new Error("SESSION_POLICY_EXPIRED");
117
+ }
118
+ const payload = {
119
+ version: sessionPolicy.version,
120
+ receiver: sessionPolicy.receiver,
121
+ rule: sessionPolicy.rule,
122
+ issuedAt: sessionPolicy.issuedAt,
123
+ expiresAt: sessionPolicy.expiresAt,
124
+ nonce: sessionPolicy.nonce
125
+ };
126
+ const message = ethers2.keccak256(ethers2.toUtf8Bytes(JSON.stringify(payload)));
127
+ const recovered = ethers2.verifyMessage(message, sessionPolicy.signature);
128
+ if (recovered.toLowerCase() !== sessionPolicy.receiver.toLowerCase()) {
129
+ throw new Error("INVALID_SESSION_POLICY_SIGNATURE");
130
+ }
131
+ return sessionPolicy.rule;
132
+ }
133
+ function decodeSessionPolicyV2(policy, blockTimestamp) {
134
+ if (policy.version !== "payid.session.policy.v2") {
135
+ throw new Error("INVALID_SESSION_POLICY_V2_VERSION");
136
+ }
137
+ if (blockTimestamp >= policy.expiresAt) {
138
+ throw new Error("SESSION_POLICY_V2_EXPIRED");
139
+ }
140
+ const payIdBytes32 = ethers2.keccak256(ethers2.toUtf8Bytes(policy.payId));
141
+ const domain = buildSessionPolicyV2Domain(policy.chainId, policy.verifyingContract);
142
+ const value = {
143
+ receiver: policy.receiver,
144
+ ruleSetHash: policy.ruleSetHash,
145
+ ruleAuthority: policy.ruleAuthority,
146
+ allowedAsset: policy.allowedAsset,
147
+ maxAmount: BigInt(policy.maxAmount),
148
+ expiresAt: policy.expiresAt,
149
+ policyNonce: policy.policyNonce,
150
+ payId: payIdBytes32
151
+ };
152
+ const recovered = ethers2.verifyTypedData(domain, SESSION_POLICY_V2_TYPES, value, policy.signature);
153
+ if (recovered.toLowerCase() !== policy.receiver.toLowerCase()) {
154
+ throw new Error("INVALID_SESSION_POLICY_V2_SIGNATURE");
155
+ }
156
+ return policy;
157
+ }
158
+ function encodeSessionPolicyV2QR(policy) {
159
+ const encoded = Buffer.from(JSON.stringify(policy), "utf-8").toString("base64url");
160
+ return `payid-v2:${encoded}`;
161
+ }
162
+ function decodeSessionPolicyV2QR(qrString) {
163
+ const PREFIX = "payid-v2:";
164
+ if (!qrString.startsWith(PREFIX)) {
165
+ throw new Error(
166
+ `QR_FORMAT_UNKNOWN: harus diawali "${PREFIX}". Got: ${qrString.slice(0, 20)}...`
167
+ );
168
+ }
169
+ let policy;
170
+ try {
171
+ const json = Buffer.from(qrString.slice(PREFIX.length), "base64url").toString("utf-8");
172
+ policy = JSON.parse(json);
173
+ } catch {
174
+ throw new Error("QR_CORRUPT: tidak bisa di-decode");
175
+ }
176
+ const required = [
177
+ "version",
178
+ "receiver",
179
+ "ruleSetHash",
180
+ "ruleAuthority",
181
+ "allowedAsset",
182
+ "maxAmount",
183
+ "expiresAt",
184
+ "policyNonce",
185
+ "payId",
186
+ "chainId",
187
+ "verifyingContract",
188
+ "signature"
189
+ ];
190
+ for (const f of required) {
191
+ if (policy[f] == null) {
192
+ throw new Error(`SESSION_POLICY_V2_INVALID: field "${f}" tidak ada`);
193
+ }
194
+ }
195
+ return policy;
196
+ }
197
+
198
+ export {
199
+ createSessionPolicyPayload,
200
+ SESSION_POLICY_V2_TYPES,
201
+ buildSessionPolicyV2Domain,
202
+ createSessionPolicyV2,
203
+ decodeSessionPolicy,
204
+ decodeSessionPolicyV2,
205
+ encodeSessionPolicyV2QR,
206
+ decodeSessionPolicyV2QR
207
+ };
@@ -11,9 +11,9 @@ function normalizeContext(ctx) {
11
11
  ...ctx,
12
12
  tx: {
13
13
  ...ctx.tx,
14
- sender: ctx.tx.sender?.toLowerCase(),
15
- receiver: ctx.tx.receiver?.toLowerCase(),
16
- asset: ctx.tx.asset.toUpperCase()
14
+ sender: ctx.tx.sender,
15
+ receiver: ctx.tx.receiver,
16
+ asset: ctx.tx.asset
17
17
  }
18
18
  };
19
19
  }
@@ -295,7 +295,8 @@ async function generateDecisionProof(params) {
295
295
  };
296
296
  const signature = await params.signer.signTypedData(domain, types, payload);
297
297
  const recovered = ethers.verifyTypedData(domain, types, payload, signature);
298
- if (recovered.toLowerCase() !== params.payer.toLowerCase()) {
298
+ const signerAddress = await params.signer.getAddress();
299
+ if (recovered.toLowerCase() !== signerAddress.toLowerCase()) {
299
300
  throw new Error("SIGNATURE_MISMATCH");
300
301
  }
301
302
  return { payload, signature };
@@ -0,0 +1,30 @@
1
+ import {
2
+ SESSION_POLICY_V2_TYPES,
3
+ buildSessionPolicyV2Domain,
4
+ createSessionPolicyPayload,
5
+ createSessionPolicyV2,
6
+ decodeSessionPolicy,
7
+ decodeSessionPolicyV2,
8
+ decodeSessionPolicyV2QR,
9
+ encodeSessionPolicyV2QR
10
+ } from "./chunk-BLIRABV7.js";
11
+ import {
12
+ __export
13
+ } from "./chunk-MLKGABMK.js";
14
+
15
+ // src/sessionPolicy/index.ts
16
+ var sessionPolicy_exports = {};
17
+ __export(sessionPolicy_exports, {
18
+ SESSION_POLICY_V2_TYPES: () => SESSION_POLICY_V2_TYPES,
19
+ buildSessionPolicyV2Domain: () => buildSessionPolicyV2Domain,
20
+ createSessionPolicyPayload: () => createSessionPolicyPayload,
21
+ createSessionPolicyV2: () => createSessionPolicyV2,
22
+ decodeSessionPolicy: () => decodeSessionPolicy,
23
+ decodeSessionPolicyV2: () => decodeSessionPolicyV2,
24
+ decodeSessionPolicyV2QR: () => decodeSessionPolicyV2QR,
25
+ encodeSessionPolicyV2QR: () => encodeSessionPolicyV2QR
26
+ });
27
+
28
+ export {
29
+ sessionPolicy_exports
30
+ };
@@ -2,7 +2,7 @@ import {
2
2
  evaluate,
3
3
  generateDecisionProof,
4
4
  resolveRule
5
- } from "./chunk-PAJYP7JI.js";
5
+ } from "./chunk-DZ6GVRER.js";
6
6
  import {
7
7
  __export
8
8
  } from "./chunk-MLKGABMK.js";
@@ -1,5 +1,5 @@
1
- export { c as createPayID } from '../../index-BPJ_oOfy.js';
1
+ export { c as createPayID } from '../../index-CtdogR8X.js';
2
2
  import 'payid-types';
3
3
  import 'ethers';
4
4
  import '../../types-B8pJQdMQ.js';
5
- import '../../types-BmMf7udp.js';
5
+ import '../../types-CpXiPRYs.js';
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  createPayID
3
- } from "../../chunk-ROBSNIIZ.js";
3
+ } from "../../chunk-2VO4XLTT.js";
4
4
  import "../../chunk-GG34PNTF.js";
5
- import "../../chunk-MXKZJKXE.js";
5
+ import "../../chunk-BLIRABV7.js";
6
6
  import "../../chunk-6VPSJFO4.js";
7
- import "../../chunk-PAJYP7JI.js";
7
+ import "../../chunk-DZ6GVRER.js";
8
8
  import "../../chunk-KDC67LIN.js";
9
9
  import "../../chunk-MLKGABMK.js";
10
10
  export {
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  createPayID
3
- } from "../../chunk-AYJYFAXJ.js";
4
- import "../../chunk-PAJYP7JI.js";
3
+ } from "../../chunk-TQJUKEFO.js";
4
+ import "../../chunk-DZ6GVRER.js";
5
5
  import "../../chunk-KDC67LIN.js";
6
6
  import "../../chunk-MLKGABMK.js";
7
7
  export {
@@ -1,7 +1,7 @@
1
1
  import { RuleContext, RuleConfig, RuleResult } from 'payid-types';
2
2
  import { ethers } from 'ethers';
3
3
  import { R as RuleSource, D as DecisionProof } from './types-B8pJQdMQ.js';
4
- import { P as PayIDSessionPolicyPayloadV1 } from './types-BmMf7udp.js';
4
+ import { P as PayIDSessionPolicyPayloadV1, S as SessionPolicyV2 } from './types-CpXiPRYs.js';
5
5
 
6
6
  declare class PayIDClient {
7
7
  private readonly debugTrace?;
@@ -15,6 +15,7 @@ declare class PayIDClient {
15
15
  authorityRule: RuleConfig | RuleSource;
16
16
  evaluationRule?: RuleConfig;
17
17
  sessionPolicy?: PayIDSessionPolicyPayloadV1;
18
+ sessionPolicyV2?: SessionPolicyV2;
18
19
  payId: string;
19
20
  payer: string;
20
21
  receiver: string;
@@ -0,0 +1,103 @@
1
+ import { ethers, TypedDataField } from 'ethers';
2
+ import { RuleConfig } from 'payid-types';
3
+ import { P as PayIDSessionPolicyPayloadV1, S as SessionPolicyV2 } from './types-CpXiPRYs.js';
4
+
5
+ /**
6
+ * Create and sign an ephemeral PayID session policy payload (V1).
7
+ * @deprecated Use createSessionPolicyV2 for new implementations.
8
+ */
9
+ declare function createSessionPolicyPayload(params: {
10
+ receiver: string;
11
+ rule: RuleConfig;
12
+ expiresAt: number;
13
+ signer: ethers.Signer;
14
+ }): Promise<PayIDSessionPolicyPayloadV1>;
15
+
16
+ declare const SESSION_POLICY_V2_TYPES: Record<string, TypedDataField[]>;
17
+ /**
18
+ * Build EIP-712 domain untuk SessionPolicyV2.
19
+ * Bind ke chainId + verifyingContract — policy tidak bisa di-replay di chain lain.
20
+ */
21
+ declare function buildSessionPolicyV2Domain(chainId: number, verifyingContract: string): {
22
+ name: string;
23
+ version: string;
24
+ chainId: number;
25
+ verifyingContract: string;
26
+ };
27
+ /**
28
+ * Create and sign a Channel A Session Policy (V2).
29
+ *
30
+ * Receiver sign CONSTRAINTS — bukan transaksi spesifik.
31
+ * Receiver tidak perlu tahu siapa payer-nya saat membuat ini.
32
+ *
33
+ * Security model:
34
+ * - Receiver sign constraints (rule, maxAmount, asset, expiry)
35
+ * - Payer sign context transaksinya sendiri
36
+ * - Chain enforce keduanya — tidak ada self-approval
37
+ *
38
+ * Perbedaan dari V1:
39
+ * - Bind ke ruleSetHash on-chain → payer tidak bisa swap rule
40
+ * - Bind ke allowedAsset+maxAmount → payer tidak bisa exceed
41
+ * - Bind ke chainId+verifyingContract (EIP-712) → anti cross-chain replay
42
+ * - Signed via EIP-712 → human-readable di wallet UI
43
+ *
44
+ * @throws Jika maxAmount <= 0, expiresAt sudah lewat, atau address invalid
45
+ */
46
+ declare function createSessionPolicyV2(params: {
47
+ receiver: string;
48
+ ruleSetHash: string;
49
+ ruleAuthority: string;
50
+ allowedAsset: string;
51
+ maxAmount: bigint;
52
+ expiresAt: number;
53
+ payId: string;
54
+ chainId: number;
55
+ verifyingContract: string;
56
+ signer: ethers.Signer;
57
+ }): Promise<SessionPolicyV2>;
58
+
59
+ /**
60
+ * Decode and verify a V1 session policy.
61
+ * @deprecated Use decodeSessionPolicyV2 for new implementations.
62
+ */
63
+ declare function decodeSessionPolicy(sessionPolicy: PayIDSessionPolicyPayloadV1, now: number): RuleConfig;
64
+ /**
65
+ * Verify and decode a Channel A SessionPolicyV2.
66
+ *
67
+ * Checks (in order):
68
+ * 1. version === "payid.session.policy.v2"
69
+ * 2. blockTimestamp < expiresAt (belum expired)
70
+ * 3. EIP-712 signature valid dan recovered === receiver
71
+ *
72
+ * @param policy - SessionPolicyV2 dari QR / URL
73
+ * @param blockTimestamp - Timestamp dari block, bukan Date.now()
74
+ * @returns policy yang terverifikasi (same object)
75
+ * @throws INVALID_SESSION_POLICY_V2_VERSION | SESSION_POLICY_V2_EXPIRED | INVALID_SESSION_POLICY_V2_SIGNATURE
76
+ */
77
+ declare function decodeSessionPolicyV2(policy: SessionPolicyV2, blockTimestamp: number): SessionPolicyV2;
78
+ /**
79
+ * Encode SessionPolicyV2 ke QR / URL string.
80
+ * Format: "payid-v2:<base64url(JSON)>"
81
+ */
82
+ declare function encodeSessionPolicyV2QR(policy: SessionPolicyV2): string;
83
+ /**
84
+ * Decode QR / URL string ke SessionPolicyV2.
85
+ * @throws QR_FORMAT_UNKNOWN | QR_CORRUPT | SESSION_POLICY_V2_INVALID
86
+ */
87
+ declare function decodeSessionPolicyV2QR(qrString: string): SessionPolicyV2;
88
+
89
+ declare const index_PayIDSessionPolicyPayloadV1: typeof PayIDSessionPolicyPayloadV1;
90
+ declare const index_SESSION_POLICY_V2_TYPES: typeof SESSION_POLICY_V2_TYPES;
91
+ declare const index_SessionPolicyV2: typeof SessionPolicyV2;
92
+ declare const index_buildSessionPolicyV2Domain: typeof buildSessionPolicyV2Domain;
93
+ declare const index_createSessionPolicyPayload: typeof createSessionPolicyPayload;
94
+ declare const index_createSessionPolicyV2: typeof createSessionPolicyV2;
95
+ declare const index_decodeSessionPolicy: typeof decodeSessionPolicy;
96
+ declare const index_decodeSessionPolicyV2: typeof decodeSessionPolicyV2;
97
+ declare const index_decodeSessionPolicyV2QR: typeof decodeSessionPolicyV2QR;
98
+ declare const index_encodeSessionPolicyV2QR: typeof encodeSessionPolicyV2QR;
99
+ declare namespace index {
100
+ export { index_PayIDSessionPolicyPayloadV1 as PayIDSessionPolicyPayloadV1, index_SESSION_POLICY_V2_TYPES as SESSION_POLICY_V2_TYPES, index_SessionPolicyV2 as SessionPolicyV2, index_buildSessionPolicyV2Domain as buildSessionPolicyV2Domain, index_createSessionPolicyPayload as createSessionPolicyPayload, index_createSessionPolicyV2 as createSessionPolicyV2, index_decodeSessionPolicy as decodeSessionPolicy, index_decodeSessionPolicyV2 as decodeSessionPolicyV2, index_decodeSessionPolicyV2QR as decodeSessionPolicyV2QR, index_encodeSessionPolicyV2QR as encodeSessionPolicyV2QR };
101
+ }
102
+
103
+ export { SESSION_POLICY_V2_TYPES as S, createSessionPolicyV2 as a, buildSessionPolicyV2Domain as b, createSessionPolicyPayload as c, decodeSessionPolicy as d, decodeSessionPolicyV2 as e, encodeSessionPolicyV2QR as f, decodeSessionPolicyV2QR as g, index as i };
package/dist/index.d.ts CHANGED
@@ -3,12 +3,12 @@ import { R as RuleSource } from './types-B8pJQdMQ.js';
3
3
  import { U as UserOperation } from './index-C1DHMQA0.js';
4
4
  export { i as server } from './index-C1DHMQA0.js';
5
5
  import { ethers } from 'ethers';
6
- export { i as sessionPolicy } from './index-BQQnMG2H.js';
6
+ export { i as sessionPolicy } from './index-DSHZvYii.js';
7
7
  export { i as rule } from './index-Dj9IEios.js';
8
8
  export { i as issuer } from './index-2JCvey4-.js';
9
9
  export { i as context } from './index-BEvnPzzt.js';
10
- export { i as client } from './index-BPJ_oOfy.js';
11
- import './types-BmMf7udp.js';
10
+ export { i as client } from './index-CtdogR8X.js';
11
+ import './types-CpXiPRYs.js';
12
12
 
13
13
  interface PayIDClient {
14
14
  /**
package/dist/index.js CHANGED
@@ -10,24 +10,24 @@ import {
10
10
  } from "./chunk-QC24X74O.js";
11
11
  import {
12
12
  sessionPolicy_exports
13
- } from "./chunk-IQNCMOIE.js";
14
- import {
15
- client_exports
16
- } from "./chunk-ROBSNIIZ.js";
17
- import "./chunk-GG34PNTF.js";
18
- import "./chunk-MXKZJKXE.js";
19
- import "./chunk-6VPSJFO4.js";
13
+ } from "./chunk-Q4UZCGU4.js";
20
14
  import {
21
15
  buildPayERC20CallData,
22
16
  buildPayETHCallData,
23
17
  buildUserOperation,
24
18
  server_exports
25
- } from "./chunk-AYJYFAXJ.js";
19
+ } from "./chunk-TQJUKEFO.js";
20
+ import {
21
+ client_exports
22
+ } from "./chunk-2VO4XLTT.js";
23
+ import "./chunk-GG34PNTF.js";
24
+ import "./chunk-BLIRABV7.js";
25
+ import "./chunk-6VPSJFO4.js";
26
26
  import {
27
27
  evaluate,
28
28
  generateDecisionProof,
29
29
  resolveRule
30
- } from "./chunk-PAJYP7JI.js";
30
+ } from "./chunk-DZ6GVRER.js";
31
31
  import "./chunk-KDC67LIN.js";
32
32
  import {
33
33
  __export
@@ -1,4 +1,4 @@
1
- export { c as createSessionPolicyPayload, d as decodeSessionPolicy } from '../index-BQQnMG2H.js';
2
- export { P as PayIDSessionPolicyPayloadV1 } from '../types-BmMf7udp.js';
1
+ export { S as SESSION_POLICY_V2_TYPES, b as buildSessionPolicyV2Domain, c as createSessionPolicyPayload, a as createSessionPolicyV2, d as decodeSessionPolicy, e as decodeSessionPolicyV2, g as decodeSessionPolicyV2QR, f as encodeSessionPolicyV2QR } from '../index-DSHZvYii.js';
2
+ export { P as PayIDSessionPolicyPayloadV1, S as SessionPolicyV2 } from '../types-CpXiPRYs.js';
3
3
  import 'ethers';
4
4
  import 'payid-types';
@@ -1,13 +1,24 @@
1
+ import "../chunk-Q4UZCGU4.js";
1
2
  import {
2
- createSessionPolicyPayload
3
- } from "../chunk-IQNCMOIE.js";
4
- import {
5
- decodeSessionPolicy
6
- } from "../chunk-MXKZJKXE.js";
3
+ SESSION_POLICY_V2_TYPES,
4
+ buildSessionPolicyV2Domain,
5
+ createSessionPolicyPayload,
6
+ createSessionPolicyV2,
7
+ decodeSessionPolicy,
8
+ decodeSessionPolicyV2,
9
+ decodeSessionPolicyV2QR,
10
+ encodeSessionPolicyV2QR
11
+ } from "../chunk-BLIRABV7.js";
7
12
  import "../chunk-6VPSJFO4.js";
8
13
  import "../chunk-KDC67LIN.js";
9
14
  import "../chunk-MLKGABMK.js";
10
15
  export {
16
+ SESSION_POLICY_V2_TYPES,
17
+ buildSessionPolicyV2Domain,
11
18
  createSessionPolicyPayload,
12
- decodeSessionPolicy
19
+ createSessionPolicyV2,
20
+ decodeSessionPolicy,
21
+ decodeSessionPolicyV2,
22
+ decodeSessionPolicyV2QR,
23
+ encodeSessionPolicyV2QR
13
24
  };
@@ -0,0 +1,66 @@
1
+ import { RuleConfig } from 'payid-types';
2
+
3
+ /**
4
+ * SessionPolicy V1 — off-chain only, rule injected inline.
5
+ *
6
+ * @deprecated Use SessionPolicyV2 for new implementations.
7
+ * V1 tidak ada chain binding (chainId, verifyingContract) sehingga
8
+ * policy bisa di-replay di chain lain, dan tidak ada constraint
9
+ * ruleSetHash / maxAmount.
10
+ */
11
+ interface PayIDSessionPolicyPayloadV1 {
12
+ version: "payid.session.policy.v1" | string;
13
+ receiver: string;
14
+ rule: RuleConfig;
15
+ expiresAt: number;
16
+ nonce: string;
17
+ issuedAt: number;
18
+ signature: string;
19
+ }
20
+ /**
21
+ * SessionPolicyV2 — Channel A, on-chain rule binding.
22
+ *
23
+ * Perbedaan dari V1:
24
+ *
25
+ * 1. ruleSetHash + ruleAuthority
26
+ * Receiver commit ke rule set yang terdaftar on-chain.
27
+ * Payer tidak bisa swap rule dengan yang lebih longgar.
28
+ *
29
+ * 2. allowedAsset + maxAmount
30
+ * Receiver set constraint token dan batas amount.
31
+ * Payer tidak bisa melebihi maxAmount atau pakai token lain.
32
+ *
33
+ * 3. chainId + verifyingContract (EIP-712 domain)
34
+ * Policy di-bind ke chain dan contract tertentu.
35
+ * Policy tidak bisa di-replay di chain lain.
36
+ *
37
+ * 4. Signed via EIP-712 (bukan raw keccak256)
38
+ * Type-safe, human-readable di wallet UI, standard.
39
+ */
40
+ interface SessionPolicyV2 {
41
+ version: "payid.session.policy.v2";
42
+ /** Receiver address — pemilik policy */
43
+ receiver: string;
44
+ /** ruleSetHash dari CombinedRuleStorage — binding ke rule on-chain */
45
+ ruleSetHash: string;
46
+ /** CombinedRuleStorage address — authority rule */
47
+ ruleAuthority: string;
48
+ /** Token address yang diizinkan. ZeroAddress = ETH. */
49
+ allowedAsset: string;
50
+ /** Max amount per transaksi (string karena bigint tidak JSON-safe) */
51
+ maxAmount: string;
52
+ /** Unix timestamp expiry */
53
+ expiresAt: number;
54
+ /** Nonce unik per policy — anti-replay */
55
+ policyNonce: string;
56
+ /** PAY.ID identifier string (e.g. "pay.id/toko-budi") */
57
+ payId: string;
58
+ /** Chain ID — bagian dari EIP-712 domain */
59
+ chainId: number;
60
+ /** PayIDVerifier contract address — bagian dari EIP-712 domain */
61
+ verifyingContract: string;
62
+ /** EIP-712 signature dari receiver */
63
+ signature: string;
64
+ }
65
+
66
+ export type { PayIDSessionPolicyPayloadV1 as P, SessionPolicyV2 as S };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "payid",
3
- "version": "0.5.9",
3
+ "version": "0.6.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,47 +0,0 @@
1
- import {
2
- decodeSessionPolicy
3
- } from "./chunk-MXKZJKXE.js";
4
- import {
5
- canonicalizeRuleSet
6
- } from "./chunk-6VPSJFO4.js";
7
- import {
8
- randomHex
9
- } from "./chunk-KDC67LIN.js";
10
- import {
11
- __export
12
- } from "./chunk-MLKGABMK.js";
13
-
14
- // src/sessionPolicy/index.ts
15
- var sessionPolicy_exports = {};
16
- __export(sessionPolicy_exports, {
17
- createSessionPolicyPayload: () => createSessionPolicyPayload,
18
- decodeSessionPolicy: () => decodeSessionPolicy
19
- });
20
-
21
- // src/sessionPolicy/create.ts
22
- import { ethers } from "ethers";
23
- async function createSessionPolicyPayload(params) {
24
- const issuedAt = Math.floor(Date.now() / 1e3);
25
- const nonce = randomHex(16);
26
- const payload = {
27
- version: "payid.session.policy.v1",
28
- receiver: params.receiver,
29
- rule: canonicalizeRuleSet(params.rule),
30
- issuedAt,
31
- expiresAt: params.expiresAt,
32
- nonce
33
- };
34
- const message = ethers.keccak256(
35
- ethers.toUtf8Bytes(JSON.stringify(payload))
36
- );
37
- const signature = await params.signer.signMessage(message);
38
- return {
39
- ...payload,
40
- signature
41
- };
42
- }
43
-
44
- export {
45
- createSessionPolicyPayload,
46
- sessionPolicy_exports
47
- };
@@ -1,33 +0,0 @@
1
- // src/sessionPolicy/decode.ts
2
- import { ethers } from "ethers";
3
- function decodeSessionPolicy(sessionPolicy, now) {
4
- if (sessionPolicy.version !== "payid.session.policy.v1") {
5
- throw new Error("INVALID_SESSION_POLICY_VERSION");
6
- }
7
- if (now > sessionPolicy.expiresAt) {
8
- throw new Error("SESSION_POLICY_EXPIRED");
9
- }
10
- const payload = {
11
- version: sessionPolicy.version,
12
- receiver: sessionPolicy.receiver,
13
- rule: sessionPolicy.rule,
14
- issuedAt: sessionPolicy.issuedAt,
15
- expiresAt: sessionPolicy.expiresAt,
16
- nonce: sessionPolicy.nonce
17
- };
18
- const message = ethers.keccak256(
19
- ethers.toUtf8Bytes(JSON.stringify(payload))
20
- );
21
- const recovered = ethers.verifyMessage(
22
- message,
23
- sessionPolicy.signature
24
- );
25
- if (recovered.toLowerCase() !== sessionPolicy.receiver.toLowerCase()) {
26
- throw new Error("INVALID_SESSION_POLICY_SIGNATURE");
27
- }
28
- return sessionPolicy.rule;
29
- }
30
-
31
- export {
32
- decodeSessionPolicy
33
- };
@@ -1,114 +0,0 @@
1
- import { ethers } from 'ethers';
2
- import { RuleConfig } from 'payid-types';
3
- import { P as PayIDSessionPolicyPayloadV1 } from './types-BmMf7udp.js';
4
-
5
- /**
6
- * Create and sign an ephemeral PayID session policy payload.
7
- *
8
- * A session policy represents a **temporary, off-chain consent**
9
- * granted by the receiver to apply additional rule constraints
10
- * during rule evaluation (e.g. session limits, QR payments,
11
- * intent-scoped conditions).
12
- *
13
- * ## Security model
14
- *
15
- * - The session policy is signed by the receiver.
16
- * - The signature proves **explicit consent** for the included rule.
17
- * - This policy does NOT establish on-chain authority and MUST NOT
18
- * be registered or referenced in any on-chain rule registry.
19
- *
20
- * ## Canonicalization
21
- *
22
- * - The rule set is canonicalized BEFORE signing to ensure
23
- * deterministic hashing and signature verification.
24
- * - The exact payload signed here MUST be used verbatim during
25
- * policy verification.
26
- *
27
- * ## Lifecycle
28
- *
29
- * - Session policies are valid only until `expiresAt`.
30
- * - Expired policies MUST be rejected by the verifier.
31
- *
32
- * @param params
33
- * @param params.receiver
34
- * Address of the receiver granting the session policy.
35
- *
36
- * @param params.rule
37
- * Rule configuration to be applied as an **off-chain evaluation
38
- * override** during the session.
39
- *
40
- * @param params.expiresAt
41
- * UNIX timestamp (seconds) indicating when the session policy
42
- * becomes invalid.
43
- *
44
- * @param params.signer
45
- * Signer controlling the receiver address, used to sign the
46
- * session policy payload.
47
- *
48
- * @returns
49
- * A signed `PayIDSessionPolicyPayloadV1` that may be transmitted
50
- * to clients and verified using `decodeSessionPolicy`.
51
- *
52
- * @throws
53
- * May throw if signing fails or the signer is misconfigured.
54
- */
55
- declare function createSessionPolicyPayload(params: {
56
- receiver: string;
57
- rule: RuleConfig;
58
- expiresAt: number;
59
- signer: ethers.Signer;
60
- }): Promise<PayIDSessionPolicyPayloadV1>;
61
-
62
- /**
63
- * Decode and verify an ephemeral PayID session policy.
64
- *
65
- * This function validates that a session policy:
66
- * - Uses a supported policy version
67
- * - Has not expired
68
- * - Was cryptographically signed by the declared receiver
69
- *
70
- * If all checks pass, the embedded rule configuration is returned
71
- * and may be used as an **off-chain evaluation override**
72
- * (e.g. combined with an authoritative on-chain rule).
73
- *
74
- * ## Security model
75
- *
76
- * - The session policy signature represents **explicit consent**
77
- * from the receiver for temporary rule constraints.
78
- * - This policy does NOT establish on-chain authority and MUST NOT
79
- * be used to derive `ruleSetHash` or interact with rule registries.
80
- *
81
- * ## Invariants
82
- *
83
- * - The payload verified here MUST match exactly the payload that was signed.
84
- * - No canonicalization or mutation is performed during verification.
85
- * - Expired or invalidly signed policies are rejected immediately.
86
- *
87
- * @export
88
- *
89
- * @param sessionPolicy
90
- * A signed session policy payload created by
91
- * `createSessionPolicyPayload`.
92
- *
93
- * @param now
94
- * Current UNIX timestamp (seconds) used to validate policy expiry.
95
- *
96
- * @returns
97
- * A `RuleConfig` representing the session's evaluation rule.
98
- *
99
- * @throws
100
- * Throws if:
101
- * - The policy version is unsupported
102
- * - The policy has expired
103
- * - The signature does not match the receiver
104
- */
105
- declare function decodeSessionPolicy(sessionPolicy: PayIDSessionPolicyPayloadV1, now: number): RuleConfig;
106
-
107
- declare const index_PayIDSessionPolicyPayloadV1: typeof PayIDSessionPolicyPayloadV1;
108
- declare const index_createSessionPolicyPayload: typeof createSessionPolicyPayload;
109
- declare const index_decodeSessionPolicy: typeof decodeSessionPolicy;
110
- declare namespace index {
111
- export { index_PayIDSessionPolicyPayloadV1 as PayIDSessionPolicyPayloadV1, index_createSessionPolicyPayload as createSessionPolicyPayload, index_decodeSessionPolicy as decodeSessionPolicy };
112
- }
113
-
114
- export { createSessionPolicyPayload as c, decodeSessionPolicy as d, index as i };
@@ -1,13 +0,0 @@
1
- import { RuleConfig } from 'payid-types';
2
-
3
- interface PayIDSessionPolicyPayloadV1 {
4
- version: "payid.session.policy.v1" | string;
5
- receiver: string;
6
- rule: RuleConfig;
7
- expiresAt: number;
8
- nonce: string;
9
- issuedAt: number;
10
- signature: string;
11
- }
12
-
13
- export type { PayIDSessionPolicyPayloadV1 as P };