payid 0.4.1 → 0.4.2

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.
@@ -0,0 +1,154 @@
1
+ import { ethers } from 'ethers';
2
+ import { RuleContext, RuleConfig, RuleResult } from 'payid-types';
3
+ import { R as RuleSource, D as DecisionProof } from './types-B8pJQdMQ.js';
4
+
5
+ interface UserOperation {
6
+ sender: string;
7
+ nonce: string;
8
+ initCode: string;
9
+ callData: string;
10
+ callGasLimit: string;
11
+ verificationGasLimit: string;
12
+ preVerificationGas: string;
13
+ maxFeePerGas: string;
14
+ maxPriorityFeePerGas: string;
15
+ paymasterAndData: string;
16
+ signature: string;
17
+ }
18
+
19
+ /**
20
+ * @class PayIDServer
21
+ * @description Server-side PayID engine.
22
+ *
23
+ * Digunakan ketika butuh:
24
+ * - Context V2 (env, state, oracle, risk) dengan trusted issuers
25
+ * - Build ERC-4337 UserOperation untuk bundler
26
+ *
27
+ * Signer di-inject saat construct — jangan pakai ini di browser.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * // Server/bundler side
32
+ * const server = new PayIDServer(wasmBinary, serverSigner, trustedIssuers)
33
+ *
34
+ * const { result, proof } = await server.evaluateAndProve({
35
+ * context: contextV2, // ← Context V2 dengan attestations
36
+ * authorityRule,
37
+ * payId: "pay.id/merchant",
38
+ * payer: "0xPAYER",
39
+ * receiver: "0xRECEIVER",
40
+ * asset: USDT_ADDRESS,
41
+ * amount: parseUnits("100", 6),
42
+ * verifyingContract: PAYID_VERIFIER_ADDRESS,
43
+ * ruleAuthority: RULE_AUTHORITY_ADDRESS,
44
+ * })
45
+ * ```
46
+ */
47
+ declare class PayIDServer {
48
+ private readonly signer;
49
+ private readonly trustedIssuers?;
50
+ private readonly debugTrace?;
51
+ private readonly wasm?;
52
+ constructor(signer: ethers.Signer, trustedIssuers?: Set<string> | undefined, debugTrace?: boolean | undefined, wasm?: Uint8Array | undefined);
53
+ evaluateAndProve(params: {
54
+ context: RuleContext;
55
+ authorityRule: RuleConfig | RuleSource;
56
+ evaluationRule?: RuleConfig;
57
+ payId: string;
58
+ payer: string;
59
+ receiver: string;
60
+ asset: string;
61
+ amount: bigint;
62
+ verifyingContract: string;
63
+ ruleAuthority: string;
64
+ ttlSeconds?: number;
65
+ chainId: number;
66
+ blockTimestamp: number;
67
+ }): Promise<{
68
+ result: RuleResult;
69
+ proof: DecisionProof | null;
70
+ }>;
71
+ buildUserOperation(params: {
72
+ proof: DecisionProof;
73
+ smartAccount: string;
74
+ nonce: string;
75
+ gas: any;
76
+ targetContract: string;
77
+ paymasterAndData?: string;
78
+ attestationUIDs?: string[];
79
+ }): UserOperation;
80
+ }
81
+
82
+ /**
83
+ * Create a PayID policy engine instance backed by a WASM rule evaluator.
84
+ *
85
+ * ## Responsibility
86
+ *
87
+ * - Holds the WASM binary used for rule execution
88
+ * - Defines the trust boundary for context attestation verification
89
+ * - Acts as the primary entry point for PayID rule evaluation
90
+ *
91
+ * ## Trust model
92
+ *
93
+ * - If `trustedIssuers` is provided, Context V2 attestation
94
+ * verification is ENFORCED.
95
+ * - If `trustedIssuers` is omitted, the engine runs in
96
+ * legacy (Context V1) mode without cryptographic verification.
97
+ *
98
+ * ## Environment
99
+ *
100
+ * This class is safe to instantiate in:
101
+ * - Browsers
102
+ * - Mobile apps
103
+ * - Edge runtimes
104
+ * - Backend services
105
+ *
106
+ * @param wasm
107
+ * Compiled PayID WASM rule engine binary.
108
+ *
109
+ * @param signer
110
+ * Signer account
111
+ *
112
+ * @param debugTrace
113
+ * Optional flag to enable decision trace generation for debugging.
114
+ *
115
+ * @param trustedIssuers
116
+ * Optional set of trusted attestation issuer addresses.
117
+ *
118
+ * When provided, Context V2 attestation verification is ENFORCED:
119
+ * - Only attestations issued by addresses in this set are accepted.
120
+ * - Missing, expired, or invalid attestations cause evaluation to fail.
121
+ *
122
+ * When omitted, the engine runs in legacy (Context V1) mode
123
+ * without cryptographic verification.
124
+ *
125
+ * ⚠️ Important:
126
+ * - Do NOT pass an empty Set.
127
+ * An empty set means "no issuer is trusted" and will
128
+ * cause all attestations to be rejected.
129
+ *
130
+ * @example
131
+ * ```ts
132
+ * const trustedIssuers = new Set([
133
+ * TIME_ISSUER,
134
+ * STATE_ISSUER,
135
+ * ORACLE_ISSUER,
136
+ * RISK_ISSUER
137
+ * ]);
138
+ *
139
+ * const payid = new PayID(wasmBinary, ethers.Signer, debugTrace, trustedIssuers);
140
+ * ```
141
+ */
142
+ declare function createPayID(params: {
143
+ wasm?: Uint8Array;
144
+ signer: ethers.Signer;
145
+ debugTrace?: boolean;
146
+ trustedIssuers?: Set<string>;
147
+ }): PayIDServer;
148
+
149
+ declare const index_createPayID: typeof createPayID;
150
+ declare namespace index {
151
+ export { index_createPayID as createPayID };
152
+ }
153
+
154
+ export { type UserOperation as U, createPayID as c, index as i };
@@ -0,0 +1,110 @@
1
+ import * as payid_types from 'payid-types';
2
+ import { RuleConfig, AnyRule } from 'payid-types';
3
+
4
+ /**
5
+ * Combine an authoritative rule set with additional ephemeral rules
6
+ * for off-chain evaluation.
7
+ *
8
+ * This helper merges:
9
+ * - A **default (authoritative) rule set** owned by the receiver
10
+ * - One or more **ephemeral rule constraints** (e.g. session / QR rules)
11
+ *
12
+ * The resulting rule set is intended **ONLY for off-chain evaluation**
13
+ * and MUST NOT be used as an authoritative rule on-chain.
14
+ *
15
+ * ## Security model
16
+ *
17
+ * - The default rule set defines sovereignty and ownership.
18
+ * - Ephemeral rules can only further restrict behavior
19
+ * (logical AND composition).
20
+ * - Ephemeral rules MUST NOT bypass or weaken default rules.
21
+ *
22
+ * ## Canonicalization
23
+ *
24
+ * - The combined rule set is canonicalized to ensure deterministic
25
+ * hashing and evaluation behavior.
26
+ *
27
+ * ## Invariants
28
+ *
29
+ * - The resulting rule set always uses logical AND semantics.
30
+ * - Rule order is normalized via canonicalization.
31
+ *
32
+ * @param defaultRuleSet
33
+ * The authoritative rule configuration (on-chain registered).
34
+ *
35
+ * @param sessionRule
36
+ * A list of additional rule conditions derived from an ephemeral
37
+ * policy (session, QR, intent, etc.).
38
+ *
39
+ * @returns
40
+ * A canonicalized rule configuration suitable for off-chain
41
+ * evaluation.
42
+ */
43
+ declare function combineRules(defaultRuleSet: RuleConfig, sessionRule: any[]): {
44
+ version: string;
45
+ logic: "AND" | "OR";
46
+ rules: payid_types.AnyRule[];
47
+ };
48
+
49
+ /**
50
+ * Canonicalize a rule set into a deterministic, order-independent form.
51
+ *
52
+ * Supports all three v4 rule formats:
53
+ * - Format A: { id, if, message? }
54
+ * - Format B: { id, logic, conditions[], message? }
55
+ * - Format C: { id, logic, rules[], message? }
56
+ */
57
+ declare function canonicalizeRuleSet(ruleSet: {
58
+ version?: string;
59
+ logic: "AND" | "OR";
60
+ rules: any[];
61
+ }): {
62
+ version: string;
63
+ logic: "AND" | "OR";
64
+ rules: AnyRule[];
65
+ };
66
+
67
+ /**
68
+ * Compute a deterministic hash of a canonicalized rule set.
69
+ *
70
+ * This function produces the `ruleSetHash` used to:
71
+ * - Reference authoritative rules in on-chain registries
72
+ * - Bind decision proofs to a specific rule configuration
73
+ * - Ensure integrity between off-chain evaluation and on-chain verification
74
+ *
75
+ * ## Canonicalization requirement
76
+ *
77
+ * - The input rule set MUST already be canonicalized using
78
+ * `canonicalizeRuleSet`.
79
+ * - Hashing a non-canonical rule set may result in inconsistent
80
+ * hashes for semantically identical rules.
81
+ *
82
+ * ## Security model
83
+ *
84
+ * - The hash represents the exact structure of the rule set at the
85
+ * time of hashing.
86
+ * - Any mutation (key order, rule order, value change) will produce
87
+ * a different hash.
88
+ *
89
+ * ## Invariants
90
+ *
91
+ * - This function does NOT perform canonicalization.
92
+ * - This function is pure and deterministic.
93
+ * - The same canonical rule set will always yield the same hash.
94
+ *
95
+ * @param ruleSet
96
+ * A canonicalized rule configuration object.
97
+ *
98
+ * @returns
99
+ * A `bytes32` hex string (keccak256) uniquely identifying the rule set.
100
+ */
101
+ declare function hashRuleSet(ruleSet: any): string;
102
+
103
+ declare const index_canonicalizeRuleSet: typeof canonicalizeRuleSet;
104
+ declare const index_combineRules: typeof combineRules;
105
+ declare const index_hashRuleSet: typeof hashRuleSet;
106
+ declare namespace index {
107
+ export { index_canonicalizeRuleSet as canonicalizeRuleSet, index_combineRules as combineRules, index_hashRuleSet as hashRuleSet };
108
+ }
109
+
110
+ export { canonicalizeRuleSet as a, combineRules as c, hashRuleSet as h, index as i };