payid 0.6.0 → 1.0.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.
- package/dist/{chunk-BLIRABV7.js → chunk-AUW7WDAB.js} +11 -20
- package/dist/{chunk-SSO66YQI.js → chunk-E6VQETBC.js} +13 -0
- package/dist/{chunk-TQJUKEFO.js → chunk-ESTGPUEQ.js} +24 -21
- package/dist/{chunk-2VO4XLTT.js → chunk-EZ3BGZ7G.js} +19 -14
- package/dist/chunk-FZNMDGVK.js +24 -0
- package/dist/chunk-HKHRYRD6.js +752 -0
- package/dist/chunk-X7NYQ47Y.js +27 -0
- package/dist/{chunk-Q4UZCGU4.js → chunk-XMUHMJRD.js} +1 -1
- package/dist/context/index.d.ts +3 -2
- package/dist/context.v1-C1m-tz0o.d.ts +39 -0
- package/dist/context.v2-DIzPotmW.d.ts +37 -0
- package/dist/core/client/index.d.ts +5 -4
- package/dist/core/client/index.js +9 -5
- package/dist/core/server/index.d.ts +4 -3
- package/dist/core/server/index.js +7 -4
- package/dist/{index-2JCvey4-.d.ts → index-CDnE3SGM.d.ts} +18 -3
- package/dist/index-CsynGAGv.d.ts +53 -0
- package/dist/{index-Dj9IEios.d.ts → index-CubM9whW.d.ts} +4 -17
- package/dist/{index-C1DHMQA0.d.ts → index-DSxDlF9J.d.ts} +45 -68
- package/dist/{index-BEvnPzzt.d.ts → index-Dm2VdDEB.d.ts} +2 -1
- package/dist/{index-DSHZvYii.d.ts → index-G_1SiZJo.d.ts} +8 -7
- package/dist/index.d.ts +407 -72
- package/dist/index.js +584 -79
- package/dist/issuer/index.d.ts +3 -2
- package/dist/issuer/index.js +4 -1
- package/dist/rule/index.d.ts +2 -2
- package/dist/rule/index.js +4 -3
- package/dist/rule-a_5ed-93.d.ts +39 -0
- package/dist/sessionPolicy/index.d.ts +3 -3
- package/dist/sessionPolicy/index.js +2 -2
- package/dist/{types-CpXiPRYs.d.ts → types-D2o6XS7a.d.ts} +1 -1
- package/dist/types-i4eTkhWa.d.ts +50 -0
- package/package.json +22 -9
- package/src/rule/engine/rule_engine.wasm +0 -0
- package/dist/chunk-DZ6GVRER.js +0 -309
- package/dist/chunk-QC24X74O.js +0 -41
- package/dist/index-CtdogR8X.d.ts +0 -82
- package/dist/types-B8pJQdMQ.d.ts +0 -26
package/dist/issuer/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
export { a as issueEnvContext, b as issueOracleContext, c as issueRiskContext, d as issueStateContext, s as signAttestation } from '../index-
|
|
2
|
-
import '
|
|
1
|
+
export { a as issueEnvContext, b as issueOracleContext, c as issueRiskContext, d as issueStateContext, e as issueTokenPriceContext, s as signAttestation } from '../index-CDnE3SGM.js';
|
|
2
|
+
import '../context.v2-DIzPotmW.js';
|
|
3
|
+
import '../context.v1-C1m-tz0o.js';
|
|
3
4
|
import 'ethers';
|
package/dist/issuer/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
issueTokenPriceContext
|
|
3
|
+
} from "../chunk-E6VQETBC.js";
|
|
2
4
|
import {
|
|
3
5
|
issueEnvContext,
|
|
4
6
|
issueOracleContext,
|
|
@@ -12,5 +14,6 @@ export {
|
|
|
12
14
|
issueOracleContext,
|
|
13
15
|
issueRiskContext,
|
|
14
16
|
issueStateContext,
|
|
17
|
+
issueTokenPriceContext,
|
|
15
18
|
signAttestation
|
|
16
19
|
};
|
package/dist/rule/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
2
|
-
import '
|
|
1
|
+
export { c as canonicalizeRuleSet, a as combineRules, h as hashRuleSet } from '../index-CubM9whW.js';
|
|
2
|
+
import '../rule-a_5ed-93.js';
|
package/dist/rule/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
hashRuleSet
|
|
3
|
-
} from "../chunk-QC24X74O.js";
|
|
1
|
+
import "../chunk-FZNMDGVK.js";
|
|
4
2
|
import {
|
|
5
3
|
combineRules
|
|
6
4
|
} from "../chunk-GG34PNTF.js";
|
|
7
5
|
import {
|
|
8
6
|
canonicalizeRuleSet
|
|
9
7
|
} from "../chunk-6VPSJFO4.js";
|
|
8
|
+
import {
|
|
9
|
+
hashRuleSet
|
|
10
|
+
} from "../chunk-X7NYQ47Y.js";
|
|
10
11
|
import "../chunk-MLKGABMK.js";
|
|
11
12
|
export {
|
|
12
13
|
canonicalizeRuleSet,
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
interface RuleCondition {
|
|
2
|
+
field: string;
|
|
3
|
+
op: string;
|
|
4
|
+
/** Literal value OR cross-field reference prefixed with "$" (e.g. "$state.dailyLimit") */
|
|
5
|
+
value: any;
|
|
6
|
+
}
|
|
7
|
+
interface SimpleRule {
|
|
8
|
+
id: string;
|
|
9
|
+
if: RuleCondition;
|
|
10
|
+
message?: string;
|
|
11
|
+
}
|
|
12
|
+
interface MultiConditionRule {
|
|
13
|
+
id: string;
|
|
14
|
+
logic: "AND" | "OR";
|
|
15
|
+
conditions: RuleCondition[];
|
|
16
|
+
message?: string;
|
|
17
|
+
}
|
|
18
|
+
interface NestedRule {
|
|
19
|
+
id: string;
|
|
20
|
+
logic: "AND" | "OR";
|
|
21
|
+
rules: AnyRule[];
|
|
22
|
+
message?: string;
|
|
23
|
+
}
|
|
24
|
+
type AnyRule = SimpleRule | MultiConditionRule | NestedRule;
|
|
25
|
+
declare function isSimpleRule(rule: AnyRule): rule is SimpleRule;
|
|
26
|
+
declare function isMultiConditionRule(rule: AnyRule): rule is MultiConditionRule;
|
|
27
|
+
declare function isNestedRule(rule: AnyRule): rule is NestedRule;
|
|
28
|
+
interface RuleConfig {
|
|
29
|
+
version?: string;
|
|
30
|
+
logic: "AND" | "OR";
|
|
31
|
+
rules: AnyRule[];
|
|
32
|
+
/** Optional list of required context namespaces (e.g. ["oracle", "risk"]) */
|
|
33
|
+
requires?: string[];
|
|
34
|
+
message?: string;
|
|
35
|
+
}
|
|
36
|
+
/** @deprecated Use AnyRule instead */
|
|
37
|
+
type Rule = AnyRule;
|
|
38
|
+
|
|
39
|
+
export { type AnyRule as A, type MultiConditionRule as M, type NestedRule as N, type Rule as R, type SimpleRule as S, type RuleCondition as a, type RuleConfig as b, isNestedRule as c, isSimpleRule as d, isMultiConditionRule as i };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { S as SESSION_POLICY_V2_TYPES, b as buildSessionPolicyV2Domain, c as createSessionPolicyPayload, a as createSessionPolicyV2, d as decodeSessionPolicy, e as decodeSessionPolicyV2,
|
|
2
|
-
export { P as PayIDSessionPolicyPayloadV1, S as SessionPolicyV2 } from '../types-
|
|
1
|
+
export { S as SESSION_POLICY_V2_TYPES, b as buildSessionPolicyV2Domain, c as createSessionPolicyPayload, a as createSessionPolicyV2, d as decodeSessionPolicy, e as decodeSessionPolicyV2, f as decodeSessionPolicyV2QR, g as encodeSessionPolicyV2QR } from '../index-G_1SiZJo.js';
|
|
2
|
+
export { P as PayIDSessionPolicyPayloadV1, S as SessionPolicyV2 } from '../types-D2o6XS7a.js';
|
|
3
3
|
import 'ethers';
|
|
4
|
-
import '
|
|
4
|
+
import '../rule-a_5ed-93.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import "../chunk-
|
|
1
|
+
import "../chunk-XMUHMJRD.js";
|
|
2
2
|
import {
|
|
3
3
|
SESSION_POLICY_V2_TYPES,
|
|
4
4
|
buildSessionPolicyV2Domain,
|
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
decodeSessionPolicyV2,
|
|
9
9
|
decodeSessionPolicyV2QR,
|
|
10
10
|
encodeSessionPolicyV2QR
|
|
11
|
-
} from "../chunk-
|
|
11
|
+
} from "../chunk-AUW7WDAB.js";
|
|
12
12
|
import "../chunk-6VPSJFO4.js";
|
|
13
13
|
import "../chunk-KDC67LIN.js";
|
|
14
14
|
import "../chunk-MLKGABMK.js";
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
interface RuleResult {
|
|
2
|
+
decision: "ALLOW" | "REJECT";
|
|
3
|
+
code: string;
|
|
4
|
+
reason?: string;
|
|
5
|
+
}
|
|
6
|
+
interface RuleTraceEntry {
|
|
7
|
+
ruleId: string;
|
|
8
|
+
field: string;
|
|
9
|
+
op: string;
|
|
10
|
+
expected: any;
|
|
11
|
+
actual: any;
|
|
12
|
+
result: "PASS" | "FAIL";
|
|
13
|
+
}
|
|
14
|
+
interface RuleDecisionDebug {
|
|
15
|
+
trace: RuleTraceEntry[];
|
|
16
|
+
}
|
|
17
|
+
interface RuleResultDebug extends RuleResult {
|
|
18
|
+
debug?: RuleDecisionDebug;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface DecisionPayload {
|
|
22
|
+
version: string;
|
|
23
|
+
payId: string;
|
|
24
|
+
payer: string;
|
|
25
|
+
receiver: string;
|
|
26
|
+
asset: string;
|
|
27
|
+
amount: bigint;
|
|
28
|
+
contextHash: string;
|
|
29
|
+
ruleSetHash: string;
|
|
30
|
+
ruleAuthority: string;
|
|
31
|
+
issuedAt: bigint;
|
|
32
|
+
expiresAt: bigint;
|
|
33
|
+
nonce: string;
|
|
34
|
+
requiresAttestation: boolean;
|
|
35
|
+
attestationUIDsHash: string;
|
|
36
|
+
}
|
|
37
|
+
interface DecisionProof {
|
|
38
|
+
payload: DecisionPayload;
|
|
39
|
+
signature: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface RuleSource {
|
|
43
|
+
uri: string;
|
|
44
|
+
hash?: string;
|
|
45
|
+
}
|
|
46
|
+
interface ResolverOptions {
|
|
47
|
+
zgIndexerUrl?: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type { DecisionPayload as D, ResolverOptions as R, DecisionProof as a, RuleDecisionDebug as b, RuleResult as c, RuleResultDebug as d, RuleSource as e, RuleTraceEntry as f };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "payid",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -36,31 +36,44 @@
|
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
38
|
"files": [
|
|
39
|
-
"dist"
|
|
39
|
+
"dist",
|
|
40
|
+
"src/rule/engine/rule_engine.wasm"
|
|
40
41
|
],
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsup",
|
|
44
|
+
"type-check": "tsc --noEmit",
|
|
45
|
+
"test": "echo 'No unit tests — add test files to run with bun test'",
|
|
46
|
+
"prepublishOnly": "bun run type-check && bun run test && bun run build",
|
|
47
|
+
"release": "release-it",
|
|
48
|
+
"release:dry": "release-it --dry-run",
|
|
49
|
+
"release:patch": "release-it patch --no-git.requireCleanWorkingDir",
|
|
50
|
+
"release:minor": "release-it minor --no-git.requireCleanWorkingDir",
|
|
51
|
+
"release:major": "release-it major --no-git.requireCleanWorkingDir"
|
|
52
|
+
},
|
|
41
53
|
"dependencies": {
|
|
54
|
+
"@0gfoundation/0g-storage-ts-sdk": "^1.2.9",
|
|
42
55
|
"ethers": "^6.16.0",
|
|
43
|
-
"
|
|
44
|
-
"payid-types": "^0.2.2"
|
|
56
|
+
"wasi": "^0.0.6"
|
|
45
57
|
},
|
|
46
58
|
"peerDependencies": {
|
|
47
59
|
"typescript": "^5.9.3"
|
|
48
60
|
},
|
|
49
|
-
"scripts": {
|
|
50
|
-
"build": "tsup"
|
|
51
|
-
},
|
|
52
61
|
"devDependencies": {
|
|
53
62
|
"@types/bun": "latest",
|
|
63
|
+
"release-it": "^20.0.1",
|
|
54
64
|
"tsup": "^8.5.1"
|
|
55
65
|
},
|
|
56
|
-
"description": "
|
|
66
|
+
"description": "PAY.ID policy engine — evaluate payment rules and generate EIP-712 Decision Proofs",
|
|
57
67
|
"repository": {
|
|
58
68
|
"type": "git",
|
|
59
69
|
"url": "git+https://github.com/Mad1Duck/payid.git"
|
|
60
70
|
},
|
|
61
71
|
"keywords": [
|
|
62
72
|
"payment",
|
|
63
|
-
"policy"
|
|
73
|
+
"policy",
|
|
74
|
+
"eip-712",
|
|
75
|
+
"web3",
|
|
76
|
+
"evm"
|
|
64
77
|
],
|
|
65
78
|
"author": "rahvanna",
|
|
66
79
|
"license": "ISC",
|
|
Binary file
|
package/dist/chunk-DZ6GVRER.js
DELETED
|
@@ -1,309 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
randomHex
|
|
3
|
-
} from "./chunk-KDC67LIN.js";
|
|
4
|
-
|
|
5
|
-
// src/evaluate.ts
|
|
6
|
-
import { executeRule, preprocessContextV2 } from "payid-rule-engine";
|
|
7
|
-
|
|
8
|
-
// src/normalize.ts
|
|
9
|
-
function normalizeContext(ctx) {
|
|
10
|
-
return {
|
|
11
|
-
...ctx,
|
|
12
|
-
tx: {
|
|
13
|
-
...ctx.tx,
|
|
14
|
-
sender: ctx.tx.sender,
|
|
15
|
-
receiver: ctx.tx.receiver,
|
|
16
|
-
asset: ctx.tx.asset
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// src/core/dicisionTrace.ts
|
|
22
|
-
function toBigIntSafe(v) {
|
|
23
|
-
try {
|
|
24
|
-
if (typeof v === "bigint") return v;
|
|
25
|
-
if (typeof v === "number" && Number.isFinite(v)) return BigInt(Math.trunc(v));
|
|
26
|
-
if (typeof v === "string" && v !== "") return BigInt(v);
|
|
27
|
-
return null;
|
|
28
|
-
} catch {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
function resolveField(obj, fieldExpr) {
|
|
33
|
-
const [path, ...transforms] = fieldExpr.split("|");
|
|
34
|
-
let value = path?.split(".").reduce((o, k) => o?.[k], obj);
|
|
35
|
-
for (const t of transforms) {
|
|
36
|
-
if (value === void 0 || value === null) break;
|
|
37
|
-
if (t.startsWith("div:")) {
|
|
38
|
-
const n = Number(t.slice(4));
|
|
39
|
-
value = Number(value) / n;
|
|
40
|
-
} else if (t.startsWith("mod:")) {
|
|
41
|
-
const n = BigInt(t.slice(4));
|
|
42
|
-
value = BigInt(value) % n;
|
|
43
|
-
} else if (t === "abs") {
|
|
44
|
-
value = Math.abs(Number(value));
|
|
45
|
-
} else if (t === "hour") {
|
|
46
|
-
value = new Date(Number(value) * 1e3).getUTCHours();
|
|
47
|
-
} else if (t === "day") {
|
|
48
|
-
value = new Date(Number(value) * 1e3).getUTCDay();
|
|
49
|
-
} else if (t === "date") {
|
|
50
|
-
value = new Date(Number(value) * 1e3).getUTCDate();
|
|
51
|
-
} else if (t === "month") {
|
|
52
|
-
value = new Date(Number(value) * 1e3).getUTCMonth() + 1;
|
|
53
|
-
} else if (t === "len") {
|
|
54
|
-
value = String(value).length;
|
|
55
|
-
} else if (t === "lower") {
|
|
56
|
-
value = String(value).toLowerCase();
|
|
57
|
-
} else if (t === "upper") {
|
|
58
|
-
value = String(value).toUpperCase();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return value;
|
|
62
|
-
}
|
|
63
|
-
function resolveValue(context, value) {
|
|
64
|
-
if (typeof value === "string" && value.startsWith("$")) {
|
|
65
|
-
return resolveField(context, value.slice(1));
|
|
66
|
-
}
|
|
67
|
-
return value;
|
|
68
|
-
}
|
|
69
|
-
function evaluateCondition(actual, op, expected) {
|
|
70
|
-
switch (op) {
|
|
71
|
-
case ">=":
|
|
72
|
-
case "<=":
|
|
73
|
-
case ">":
|
|
74
|
-
case "<": {
|
|
75
|
-
const a = toBigIntSafe(actual);
|
|
76
|
-
const b = toBigIntSafe(expected);
|
|
77
|
-
if (a === null || b === null) return false;
|
|
78
|
-
if (op === ">=") return a >= b;
|
|
79
|
-
if (op === "<=") return a <= b;
|
|
80
|
-
if (op === ">") return a > b;
|
|
81
|
-
if (op === "<") return a < b;
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
case "==":
|
|
85
|
-
return actual == expected;
|
|
86
|
-
case "!=":
|
|
87
|
-
return actual != expected;
|
|
88
|
-
case "in":
|
|
89
|
-
return Array.isArray(expected) && expected.includes(actual);
|
|
90
|
-
case "not_in":
|
|
91
|
-
return Array.isArray(expected) && !expected.includes(actual);
|
|
92
|
-
case "between":
|
|
93
|
-
return Array.isArray(expected) && actual >= expected[0] && actual <= expected[1];
|
|
94
|
-
case "not_between":
|
|
95
|
-
return Array.isArray(expected) && !(actual >= expected[0] && actual <= expected[1]);
|
|
96
|
-
case "exists":
|
|
97
|
-
return actual !== void 0 && actual !== null;
|
|
98
|
-
case "not_exists":
|
|
99
|
-
return actual === void 0 || actual === null;
|
|
100
|
-
default:
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
function traceCondition(context, ruleId, cond) {
|
|
105
|
-
const actual = resolveField(context, cond.field);
|
|
106
|
-
const expected = resolveValue(context, cond.value);
|
|
107
|
-
const pass = evaluateCondition(actual, cond.op, expected);
|
|
108
|
-
return {
|
|
109
|
-
ruleId,
|
|
110
|
-
field: cond.field,
|
|
111
|
-
op: cond.op,
|
|
112
|
-
expected: cond.value,
|
|
113
|
-
actual,
|
|
114
|
-
result: actual === void 0 ? "FAIL" : pass ? "PASS" : "FAIL"
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
function traceRule(context, rule) {
|
|
118
|
-
if ("if" in rule) {
|
|
119
|
-
return [traceCondition(context, rule.id, rule.if)];
|
|
120
|
-
}
|
|
121
|
-
if ("conditions" in rule) {
|
|
122
|
-
return rule.conditions.map((cond) => traceCondition(context, rule.id, cond));
|
|
123
|
-
}
|
|
124
|
-
if ("rules" in rule) {
|
|
125
|
-
return rule.rules.flatMap((child) => traceRule(context, child));
|
|
126
|
-
}
|
|
127
|
-
return [];
|
|
128
|
-
}
|
|
129
|
-
function buildDecisionTrace(context, ruleConfig) {
|
|
130
|
-
return ruleConfig.rules.flatMap((rule) => traceRule(context, rule));
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// src/evaluate.ts
|
|
134
|
-
async function evaluate(context, ruleConfig, options, wasmBinary) {
|
|
135
|
-
if (!context || typeof context !== "object") {
|
|
136
|
-
throw new Error("evaluate(): context is required");
|
|
137
|
-
}
|
|
138
|
-
if (!context.tx) {
|
|
139
|
-
throw new Error("evaluate(): context.tx is required");
|
|
140
|
-
}
|
|
141
|
-
if (!ruleConfig || typeof ruleConfig !== "object") {
|
|
142
|
-
throw new Error("evaluate(): ruleConfig is required");
|
|
143
|
-
}
|
|
144
|
-
let result;
|
|
145
|
-
try {
|
|
146
|
-
const preparedContext = options?.trustedIssuers ? preprocessContextV2(context, ruleConfig, options.trustedIssuers) : context;
|
|
147
|
-
const normalized = normalizeContext(preparedContext);
|
|
148
|
-
result = await executeRule(normalized, ruleConfig, wasmBinary);
|
|
149
|
-
} catch (err) {
|
|
150
|
-
return {
|
|
151
|
-
decision: "REJECT",
|
|
152
|
-
code: "CONTEXT_OR_ENGINE_ERROR",
|
|
153
|
-
reason: err?.message ?? "rule evaluation failed"
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
if (result.decision !== "ALLOW" && result.decision !== "REJECT") {
|
|
157
|
-
return {
|
|
158
|
-
decision: "REJECT",
|
|
159
|
-
code: "INVALID_ENGINE_OUTPUT",
|
|
160
|
-
reason: "invalid decision value"
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
const baseResult = {
|
|
164
|
-
decision: result.decision,
|
|
165
|
-
code: result.code || "UNKNOWN",
|
|
166
|
-
reason: result.reason
|
|
167
|
-
};
|
|
168
|
-
if (options?.debug) {
|
|
169
|
-
return {
|
|
170
|
-
...baseResult,
|
|
171
|
-
debug: {
|
|
172
|
-
trace: buildDecisionTrace(context, ruleConfig)
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
return baseResult;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// src/utils/subtle.ts
|
|
180
|
-
var subtleCrypto = globalThis.crypto.subtle;
|
|
181
|
-
|
|
182
|
-
// src/utils/fetchJson.ts
|
|
183
|
-
async function fetchJsonWithHashCheck(url, expectedHash) {
|
|
184
|
-
const res = await fetch(url);
|
|
185
|
-
if (!res.ok) {
|
|
186
|
-
throw new Error("RULE_FETCH_FAILED");
|
|
187
|
-
}
|
|
188
|
-
const buffer = await res.arrayBuffer();
|
|
189
|
-
if (expectedHash) {
|
|
190
|
-
const digest = await subtleCrypto.digest(
|
|
191
|
-
"SHA-256",
|
|
192
|
-
buffer
|
|
193
|
-
);
|
|
194
|
-
const actualHash = bufferToHex(digest);
|
|
195
|
-
if (actualHash !== expectedHash) {
|
|
196
|
-
throw new Error("RULE_HASH_MISMATCH");
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
return JSON.parse(new TextDecoder().decode(buffer));
|
|
200
|
-
}
|
|
201
|
-
function bufferToHex(buffer) {
|
|
202
|
-
return [...new Uint8Array(buffer)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// src/resolver/resolver.ts
|
|
206
|
-
async function resolveRule(source) {
|
|
207
|
-
const { uri, hash: hash2 } = source;
|
|
208
|
-
if (uri.startsWith("inline://")) {
|
|
209
|
-
const encoded = uri.replace("inline://", "");
|
|
210
|
-
const json = JSON.parse(atob(encoded));
|
|
211
|
-
return { config: json, source };
|
|
212
|
-
}
|
|
213
|
-
if (uri.startsWith("ipfs://")) {
|
|
214
|
-
const cid = uri.replace("ipfs://", "");
|
|
215
|
-
const url = `https://ipfs.io/ipfs/${cid}`;
|
|
216
|
-
const config = await fetchJsonWithHashCheck(url, hash2);
|
|
217
|
-
return { config, source };
|
|
218
|
-
}
|
|
219
|
-
if (uri.startsWith("http://") || uri.startsWith("https://")) {
|
|
220
|
-
const config = await fetchJsonWithHashCheck(uri, hash2);
|
|
221
|
-
return { config, source };
|
|
222
|
-
}
|
|
223
|
-
throw new Error("UNSUPPORTED_RULE_URI");
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
// src/decision-proof/hash.ts
|
|
227
|
-
import { keccak256 } from "ethers";
|
|
228
|
-
function stableStringify(obj) {
|
|
229
|
-
if (Array.isArray(obj)) {
|
|
230
|
-
return `[${obj.map(stableStringify).join(",")}]`;
|
|
231
|
-
}
|
|
232
|
-
if (obj && typeof obj === "object") {
|
|
233
|
-
return `{${Object.keys(obj).sort().map(
|
|
234
|
-
(k) => `"${k}":${stableStringify(obj[k])}`
|
|
235
|
-
).join(",")}}`;
|
|
236
|
-
}
|
|
237
|
-
return JSON.stringify(obj);
|
|
238
|
-
}
|
|
239
|
-
function toUtf8Bytes(str) {
|
|
240
|
-
return new TextEncoder().encode(str);
|
|
241
|
-
}
|
|
242
|
-
function hashContext(context) {
|
|
243
|
-
return keccak256(toUtf8Bytes(stableStringify(context)));
|
|
244
|
-
}
|
|
245
|
-
function hashRuleSet(ruleConfig) {
|
|
246
|
-
return keccak256(toUtf8Bytes(stableStringify(ruleConfig)));
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
// src/decision-proof/generate.ts
|
|
250
|
-
import { ethers, ZeroAddress } from "ethers";
|
|
251
|
-
var hash = (v) => ethers.keccak256(ethers.toUtf8Bytes(v));
|
|
252
|
-
async function generateDecisionProof(params) {
|
|
253
|
-
const now = params.blockTimestamp ?? Math.floor(Date.now() / 1e3);
|
|
254
|
-
const issuedAt = now - 30;
|
|
255
|
-
const expiresAt = now + (params.ttlSeconds ?? 300);
|
|
256
|
-
const chainId = params.chainId ?? Number((await params.signer.provider.getNetwork()).chainId);
|
|
257
|
-
const requiresAttestation = Array.isArray(params.ruleConfig?.requires) && params.ruleConfig.requires.length > 0;
|
|
258
|
-
const payload = {
|
|
259
|
-
version: hash("2"),
|
|
260
|
-
payId: hash(params.payId),
|
|
261
|
-
payer: params.payer,
|
|
262
|
-
receiver: params.receiver,
|
|
263
|
-
asset: params.asset,
|
|
264
|
-
amount: params.amount,
|
|
265
|
-
contextHash: hashContext(params.context),
|
|
266
|
-
ruleSetHash: params.ruleSetHashOverride ?? hashRuleSet(params.ruleConfig),
|
|
267
|
-
ruleAuthority: params.ruleAuthority ?? ZeroAddress,
|
|
268
|
-
issuedAt: BigInt(issuedAt),
|
|
269
|
-
expiresAt: BigInt(expiresAt),
|
|
270
|
-
nonce: randomHex(32),
|
|
271
|
-
requiresAttestation
|
|
272
|
-
};
|
|
273
|
-
const domain = {
|
|
274
|
-
name: "PAY.ID Decision",
|
|
275
|
-
version: "2",
|
|
276
|
-
chainId,
|
|
277
|
-
verifyingContract: params.verifyingContract
|
|
278
|
-
};
|
|
279
|
-
const types = {
|
|
280
|
-
Decision: [
|
|
281
|
-
{ name: "version", type: "bytes32" },
|
|
282
|
-
{ name: "payId", type: "bytes32" },
|
|
283
|
-
{ name: "payer", type: "address" },
|
|
284
|
-
{ name: "receiver", type: "address" },
|
|
285
|
-
{ name: "asset", type: "address" },
|
|
286
|
-
{ name: "amount", type: "uint256" },
|
|
287
|
-
{ name: "contextHash", type: "bytes32" },
|
|
288
|
-
{ name: "ruleSetHash", type: "bytes32" },
|
|
289
|
-
{ name: "ruleAuthority", type: "address" },
|
|
290
|
-
{ name: "issuedAt", type: "uint64" },
|
|
291
|
-
{ name: "expiresAt", type: "uint64" },
|
|
292
|
-
{ name: "nonce", type: "bytes32" },
|
|
293
|
-
{ name: "requiresAttestation", type: "bool" }
|
|
294
|
-
]
|
|
295
|
-
};
|
|
296
|
-
const signature = await params.signer.signTypedData(domain, types, payload);
|
|
297
|
-
const recovered = ethers.verifyTypedData(domain, types, payload, signature);
|
|
298
|
-
const signerAddress = await params.signer.getAddress();
|
|
299
|
-
if (recovered.toLowerCase() !== signerAddress.toLowerCase()) {
|
|
300
|
-
throw new Error("SIGNATURE_MISMATCH");
|
|
301
|
-
}
|
|
302
|
-
return { payload, signature };
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
export {
|
|
306
|
-
evaluate,
|
|
307
|
-
resolveRule,
|
|
308
|
-
generateDecisionProof
|
|
309
|
-
};
|
package/dist/chunk-QC24X74O.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
combineRules
|
|
3
|
-
} from "./chunk-GG34PNTF.js";
|
|
4
|
-
import {
|
|
5
|
-
canonicalizeRuleSet
|
|
6
|
-
} from "./chunk-6VPSJFO4.js";
|
|
7
|
-
import {
|
|
8
|
-
__export
|
|
9
|
-
} from "./chunk-MLKGABMK.js";
|
|
10
|
-
|
|
11
|
-
// src/rule/index.ts
|
|
12
|
-
var rule_exports = {};
|
|
13
|
-
__export(rule_exports, {
|
|
14
|
-
canonicalizeRuleSet: () => canonicalizeRuleSet,
|
|
15
|
-
combineRules: () => combineRules,
|
|
16
|
-
hashRuleSet: () => hashRuleSet
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
// src/rule/hash.ts
|
|
20
|
-
import { keccak256, toUtf8Bytes } from "ethers";
|
|
21
|
-
function stableStringify(obj) {
|
|
22
|
-
if (Array.isArray(obj)) {
|
|
23
|
-
return `[${obj.map(stableStringify).join(",")}]`;
|
|
24
|
-
}
|
|
25
|
-
if (obj && typeof obj === "object") {
|
|
26
|
-
return `{${Object.keys(obj).sort().map(
|
|
27
|
-
(k) => `"${k}":${stableStringify(obj[k])}`
|
|
28
|
-
).join(",")}}`;
|
|
29
|
-
}
|
|
30
|
-
return JSON.stringify(obj);
|
|
31
|
-
}
|
|
32
|
-
function hashRuleSet(ruleSet) {
|
|
33
|
-
return keccak256(
|
|
34
|
-
toUtf8Bytes(stableStringify(ruleSet))
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export {
|
|
39
|
-
hashRuleSet,
|
|
40
|
-
rule_exports
|
|
41
|
-
};
|
package/dist/index-CtdogR8X.d.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { RuleContext, RuleConfig, RuleResult } from 'payid-types';
|
|
2
|
-
import { ethers } from 'ethers';
|
|
3
|
-
import { R as RuleSource, D as DecisionProof } from './types-B8pJQdMQ.js';
|
|
4
|
-
import { P as PayIDSessionPolicyPayloadV1, S as SessionPolicyV2 } from './types-CpXiPRYs.js';
|
|
5
|
-
|
|
6
|
-
declare class PayIDClient {
|
|
7
|
-
private readonly debugTrace?;
|
|
8
|
-
private readonly wasm?;
|
|
9
|
-
private readonly _ready;
|
|
10
|
-
constructor(debugTrace?: boolean | undefined, wasm?: Uint8Array | undefined);
|
|
11
|
-
ready(): Promise<void>;
|
|
12
|
-
evaluate(context: RuleContext, rule: RuleConfig | RuleSource): Promise<RuleResult>;
|
|
13
|
-
evaluateAndProve(params: {
|
|
14
|
-
context: RuleContext;
|
|
15
|
-
authorityRule: RuleConfig | RuleSource;
|
|
16
|
-
evaluationRule?: RuleConfig;
|
|
17
|
-
sessionPolicy?: PayIDSessionPolicyPayloadV1;
|
|
18
|
-
sessionPolicyV2?: SessionPolicyV2;
|
|
19
|
-
payId: string;
|
|
20
|
-
payer: string;
|
|
21
|
-
receiver: string;
|
|
22
|
-
asset: string;
|
|
23
|
-
amount: bigint;
|
|
24
|
-
signer: ethers.Signer;
|
|
25
|
-
verifyingContract: string;
|
|
26
|
-
ruleAuthority: string;
|
|
27
|
-
ruleSetHashOverride?: string;
|
|
28
|
-
ttlSeconds?: number;
|
|
29
|
-
chainId: number;
|
|
30
|
-
blockTimestamp: number;
|
|
31
|
-
}): Promise<{
|
|
32
|
-
result: RuleResult;
|
|
33
|
-
proof: DecisionProof | null;
|
|
34
|
-
}>;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Create a PayID policy engine instance backed by a WASM rule evaluator.
|
|
39
|
-
*
|
|
40
|
-
* ## Responsibility
|
|
41
|
-
*
|
|
42
|
-
* - Holds the WASM binary used for rule execution
|
|
43
|
-
* - Defines the trust boundary for context attestation verification
|
|
44
|
-
* - Acts as the primary entry point for PayID rule evaluation
|
|
45
|
-
*
|
|
46
|
-
* ## Trust model
|
|
47
|
-
*
|
|
48
|
-
* - If `trustedIssuers` is provided, Context V2 attestation
|
|
49
|
-
* verification is ENFORCED.
|
|
50
|
-
* - If `trustedIssuers` is omitted, the engine runs in
|
|
51
|
-
* legacy (Context V1) mode without cryptographic verification.
|
|
52
|
-
*
|
|
53
|
-
* ## Environment
|
|
54
|
-
*
|
|
55
|
-
* This class is safe to instantiate in:
|
|
56
|
-
* - Browsers
|
|
57
|
-
* - Mobile apps
|
|
58
|
-
* - Edge runtimes
|
|
59
|
-
* - Backend services
|
|
60
|
-
*
|
|
61
|
-
* @param wasm
|
|
62
|
-
* Compiled PayID WASM rule engine binary.
|
|
63
|
-
*
|
|
64
|
-
* @param debugTrace
|
|
65
|
-
* Optional flag to enable decision trace generation for debugging.
|
|
66
|
-
* @example
|
|
67
|
-
* ```ts
|
|
68
|
-
*
|
|
69
|
-
* const payid = new PayID(wasmBinary, debugTrace);
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
declare function createPayID(params: {
|
|
73
|
-
wasm?: Uint8Array;
|
|
74
|
-
debugTrace?: boolean;
|
|
75
|
-
}): PayIDClient;
|
|
76
|
-
|
|
77
|
-
declare const index_createPayID: typeof createPayID;
|
|
78
|
-
declare namespace index {
|
|
79
|
-
export { index_createPayID as createPayID };
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export { createPayID as c, index as i };
|
package/dist/types-B8pJQdMQ.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
interface RuleSource {
|
|
2
|
-
uri: string;
|
|
3
|
-
hash?: string;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
interface DecisionPayload {
|
|
7
|
-
version: string;
|
|
8
|
-
payId: string;
|
|
9
|
-
payer: string;
|
|
10
|
-
receiver: string;
|
|
11
|
-
asset: string;
|
|
12
|
-
amount: bigint;
|
|
13
|
-
contextHash: string;
|
|
14
|
-
ruleSetHash: string;
|
|
15
|
-
ruleAuthority: string;
|
|
16
|
-
issuedAt: bigint;
|
|
17
|
-
expiresAt: bigint;
|
|
18
|
-
nonce: string;
|
|
19
|
-
requiresAttestation: boolean;
|
|
20
|
-
}
|
|
21
|
-
interface DecisionProof {
|
|
22
|
-
payload: DecisionPayload;
|
|
23
|
-
signature: string;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export type { DecisionProof as D, RuleSource as R };
|