@peridotvault/peridotbase-sdk 0.0.0-stage → 0.2.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/auth.d.ts +33 -0
- package/dist/auth.js +72 -0
- package/dist/client.d.ts +1149 -0
- package/dist/client.js +6030 -0
- package/dist/ddl.d.ts +31 -0
- package/dist/ddl.js +28 -0
- package/dist/errors.d.ts +32 -0
- package/dist/errors.js +187 -0
- package/dist/expr.d.ts +127 -0
- package/dist/expr.js +1177 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +28 -0
- package/dist/org-multisig.d.ts +29 -0
- package/dist/org-multisig.js +90 -0
- package/dist/pda.d.ts +95 -0
- package/dist/pda.js +327 -0
- package/dist/sql.d.ts +244 -0
- package/dist/sql.js +1523 -0
- package/dist/tooling.d.ts +68 -0
- package/dist/tooling.js +432 -0
- package/dist/v3.d.ts +357 -0
- package/dist/v3.js +1649 -0
- package/dist/values.d.ts +229 -0
- package/dist/values.js +1362 -0
- package/dist/verify.d.ts +37 -0
- package/dist/verify.js +210 -0
- package/idl/peridotbase_core.json +17404 -0
- package/package.json +43 -4
- package/README.md +0 -3
package/dist/verify.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Standalone, read-only policy verifier.
|
|
3
|
+
*
|
|
4
|
+
* Decodes the on-chain policy list of a table and re-checks it with the same
|
|
5
|
+
* rules the program applies (`validate_policies` + `validate_expr`). Nothing
|
|
6
|
+
* here trusts a GUI or an indexer: the catalog account is the source of truth.
|
|
7
|
+
*
|
|
8
|
+
* const report = await verifyTablePolicies(client, tableId);
|
|
9
|
+
* const exact = await verifyTablePolicies(client, tableId, { "1": exprNode });
|
|
10
|
+
*/
|
|
11
|
+
import type { ExprNode } from "./expr";
|
|
12
|
+
import type { ColumnSpec } from "./values";
|
|
13
|
+
import type { PolicyModel } from "./client";
|
|
14
|
+
export declare const POLICY_OP_BITS: {
|
|
15
|
+
readonly insert: 1;
|
|
16
|
+
readonly update: 2;
|
|
17
|
+
readonly delete: 4;
|
|
18
|
+
readonly select: 8;
|
|
19
|
+
};
|
|
20
|
+
export interface PolicyVerification {
|
|
21
|
+
policyId: bigint;
|
|
22
|
+
role: string;
|
|
23
|
+
ops: number;
|
|
24
|
+
operations: string[];
|
|
25
|
+
bytes: number;
|
|
26
|
+
problems: string[];
|
|
27
|
+
/** Present when an expected predicate was supplied: the compiled bytes. */
|
|
28
|
+
expectedBytes?: string;
|
|
29
|
+
}
|
|
30
|
+
/** Mirrors `expr::validate_expr`: returns the list of structural problems. */
|
|
31
|
+
export declare function verifyPredicateBytecode(code: Uint8Array, columnCount: number): string[];
|
|
32
|
+
/**
|
|
33
|
+
* Verifies a decoded policy list. `expected` maps a policy id to the predicate
|
|
34
|
+
* the application claims to have installed; the source must compile to exactly
|
|
35
|
+
* the stored bytes (use `null` for "no predicate").
|
|
36
|
+
*/
|
|
37
|
+
export declare function verifyPolicies(policies: PolicyModel[], columns: ColumnSpec[], expected?: Record<string, ExprNode | null>): PolicyVerification[];
|
package/dist/verify.js
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.POLICY_OP_BITS = void 0;
|
|
4
|
+
exports.verifyPredicateBytecode = verifyPredicateBytecode;
|
|
5
|
+
exports.verifyPolicies = verifyPolicies;
|
|
6
|
+
const expr_1 = require("./expr");
|
|
7
|
+
exports.POLICY_OP_BITS = {
|
|
8
|
+
insert: 0x01,
|
|
9
|
+
update: 0x02,
|
|
10
|
+
delete: 0x04,
|
|
11
|
+
select: 0x08,
|
|
12
|
+
};
|
|
13
|
+
const OP_ALL_BITS = 0x0f;
|
|
14
|
+
const MAX_EXPR_BYTES = 256;
|
|
15
|
+
const MAX_EXPR_STEPS = 512;
|
|
16
|
+
const MAX_EXPR_DEPTH = 64;
|
|
17
|
+
/** Mirrors `expr::validate_expr`: returns the list of structural problems. */
|
|
18
|
+
function verifyPredicateBytecode(code, columnCount) {
|
|
19
|
+
const problems = [];
|
|
20
|
+
if (code.length === 0)
|
|
21
|
+
return problems;
|
|
22
|
+
if (code.length > MAX_EXPR_BYTES) {
|
|
23
|
+
problems.push(`predicate is ${code.length} bytes, limit ${MAX_EXPR_BYTES}`);
|
|
24
|
+
}
|
|
25
|
+
let pc = 0;
|
|
26
|
+
let depth = 0;
|
|
27
|
+
let steps = 0;
|
|
28
|
+
const u8 = () => {
|
|
29
|
+
if (pc >= code.length)
|
|
30
|
+
throw new Error("truncated operand");
|
|
31
|
+
return code[pc++];
|
|
32
|
+
};
|
|
33
|
+
const u16 = () => {
|
|
34
|
+
if (pc + 2 > code.length)
|
|
35
|
+
throw new Error("truncated u16");
|
|
36
|
+
const value = code[pc] | (code[pc + 1] << 8);
|
|
37
|
+
pc += 2;
|
|
38
|
+
return value;
|
|
39
|
+
};
|
|
40
|
+
try {
|
|
41
|
+
while (pc < code.length) {
|
|
42
|
+
steps += 1;
|
|
43
|
+
if (steps > MAX_EXPR_STEPS) {
|
|
44
|
+
problems.push(`predicate exceeds ${MAX_EXPR_STEPS} steps`);
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
const op = u8();
|
|
48
|
+
switch (op) {
|
|
49
|
+
case 0x01: // PUSH_NULL
|
|
50
|
+
case 0x0c: // LOAD_SIGNER
|
|
51
|
+
depth += 1;
|
|
52
|
+
break;
|
|
53
|
+
case 0x02: // PUSH_BOOL
|
|
54
|
+
if (u8() > 1)
|
|
55
|
+
problems.push("PUSH_BOOL operand is not 0/1");
|
|
56
|
+
depth += 1;
|
|
57
|
+
break;
|
|
58
|
+
case 0x03: // PUSH_I64
|
|
59
|
+
if (pc + 8 > code.length)
|
|
60
|
+
throw new Error("truncated i64");
|
|
61
|
+
pc += 8;
|
|
62
|
+
depth += 1;
|
|
63
|
+
break;
|
|
64
|
+
case 0x04: // PUSH_DECIMAL
|
|
65
|
+
if (pc + 17 > code.length)
|
|
66
|
+
throw new Error("truncated decimal");
|
|
67
|
+
pc += 17;
|
|
68
|
+
depth += 1;
|
|
69
|
+
break;
|
|
70
|
+
case 0x05: {
|
|
71
|
+
const length = u16();
|
|
72
|
+
if (pc + length > code.length)
|
|
73
|
+
throw new Error("truncated text");
|
|
74
|
+
pc += length;
|
|
75
|
+
depth += 1;
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
case 0x06: // PUSH_UUID
|
|
79
|
+
if (pc + 16 > code.length)
|
|
80
|
+
throw new Error("truncated uuid");
|
|
81
|
+
pc += 16;
|
|
82
|
+
depth += 1;
|
|
83
|
+
break;
|
|
84
|
+
case 0x07: // PUSH_RANGE
|
|
85
|
+
if (pc + 17 > code.length)
|
|
86
|
+
throw new Error("truncated range");
|
|
87
|
+
pc += 17;
|
|
88
|
+
depth += 1;
|
|
89
|
+
break;
|
|
90
|
+
case 0x08: // PUSH_INTERVAL
|
|
91
|
+
if (pc + 16 > code.length)
|
|
92
|
+
throw new Error("truncated interval");
|
|
93
|
+
pc += 16;
|
|
94
|
+
depth += 1;
|
|
95
|
+
break;
|
|
96
|
+
case 0x0a: // LOAD_NEW
|
|
97
|
+
case 0x0b: { // LOAD_OLD
|
|
98
|
+
const column = u8();
|
|
99
|
+
if (column >= columnCount) {
|
|
100
|
+
problems.push(`LOAD_${op === 0x0a ? "NEW" : "OLD"} column ${column} is out of range`);
|
|
101
|
+
}
|
|
102
|
+
depth += 1;
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
case 0x14:
|
|
106
|
+
case 0x15:
|
|
107
|
+
case 0x16:
|
|
108
|
+
case 0x17:
|
|
109
|
+
case 0x18:
|
|
110
|
+
case 0x19:
|
|
111
|
+
case 0x1a:
|
|
112
|
+
case 0x1b:
|
|
113
|
+
case 0x1c:
|
|
114
|
+
case 0x1d:
|
|
115
|
+
case 0x23:
|
|
116
|
+
case 0x24:
|
|
117
|
+
case 0x25:
|
|
118
|
+
case 0x26:
|
|
119
|
+
case 0x27:
|
|
120
|
+
case 0x2c:
|
|
121
|
+
case 0x2d:
|
|
122
|
+
case 0x2e:
|
|
123
|
+
case 0x2f:
|
|
124
|
+
case 0x30:
|
|
125
|
+
case 0x1e:
|
|
126
|
+
case 0x1f:
|
|
127
|
+
case 0x28:
|
|
128
|
+
case 0x29:
|
|
129
|
+
case 0x2a:
|
|
130
|
+
case 0x2b:
|
|
131
|
+
if (depth < 2)
|
|
132
|
+
problems.push(`opcode 0x${op.toString(16)} underflows the stack`);
|
|
133
|
+
depth -= 1;
|
|
134
|
+
break;
|
|
135
|
+
case 0x20:
|
|
136
|
+
case 0x21:
|
|
137
|
+
case 0x22:
|
|
138
|
+
if (depth < 1)
|
|
139
|
+
problems.push(`opcode 0x${op.toString(16)} underflows the stack`);
|
|
140
|
+
break;
|
|
141
|
+
case 0x32: { // JUMP_IF_FALSE
|
|
142
|
+
const target = u16();
|
|
143
|
+
if (target > code.length)
|
|
144
|
+
problems.push(`jump target ${target} is past the bytecode`);
|
|
145
|
+
if (depth < 1)
|
|
146
|
+
problems.push("JUMP_IF_FALSE underflows the stack");
|
|
147
|
+
depth -= 1;
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case 0x33: { // JUMP
|
|
151
|
+
const target = u16();
|
|
152
|
+
if (target > code.length)
|
|
153
|
+
problems.push(`jump target ${target} is past the bytecode`);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
default:
|
|
157
|
+
throw new Error(`unknown opcode 0x${op.toString(16)} at ${pc - 1}`);
|
|
158
|
+
}
|
|
159
|
+
if (depth > MAX_EXPR_DEPTH)
|
|
160
|
+
problems.push(`stack depth exceeds ${MAX_EXPR_DEPTH}`);
|
|
161
|
+
if (depth < 0)
|
|
162
|
+
problems.push("stack depth is negative");
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
problems.push(error.message || String(error));
|
|
167
|
+
}
|
|
168
|
+
if (depth !== 1 && depth !== 0)
|
|
169
|
+
problems.push(`predicate leaves ${depth} values on the stack`);
|
|
170
|
+
return problems;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Verifies a decoded policy list. `expected` maps a policy id to the predicate
|
|
174
|
+
* the application claims to have installed; the source must compile to exactly
|
|
175
|
+
* the stored bytes (use `null` for "no predicate").
|
|
176
|
+
*/
|
|
177
|
+
function verifyPolicies(policies, columns, expected = {}) {
|
|
178
|
+
const seen = new Set();
|
|
179
|
+
return policies.map((policy) => {
|
|
180
|
+
const problems = verifyPredicateBytecode(policy.predicate, columns.length);
|
|
181
|
+
const id = policy.policyId.toString();
|
|
182
|
+
if (seen.has(id))
|
|
183
|
+
problems.push(`duplicate policy id ${id}`);
|
|
184
|
+
seen.add(id);
|
|
185
|
+
if (policy.ops === 0 || (policy.ops & ~OP_ALL_BITS) !== 0) {
|
|
186
|
+
problems.push(`ops mask 0x${policy.ops.toString(16)} is not a data-operation mask`);
|
|
187
|
+
}
|
|
188
|
+
if (!policy.role)
|
|
189
|
+
problems.push("policy has no role");
|
|
190
|
+
const report = {
|
|
191
|
+
policyId: policy.policyId,
|
|
192
|
+
role: policy.role,
|
|
193
|
+
ops: policy.ops,
|
|
194
|
+
operations: Object.entries(exports.POLICY_OP_BITS)
|
|
195
|
+
.filter(([, bit]) => (policy.ops & bit) !== 0)
|
|
196
|
+
.map(([name]) => name),
|
|
197
|
+
bytes: policy.predicate.length,
|
|
198
|
+
problems,
|
|
199
|
+
};
|
|
200
|
+
if (id in expected) {
|
|
201
|
+
const wanted = expected[id];
|
|
202
|
+
const compiled = wanted ? (0, expr_1.compileExpression)(wanted, columns) : Buffer.alloc(0);
|
|
203
|
+
if (!Buffer.from(policy.predicate).equals(compiled)) {
|
|
204
|
+
problems.push(`stored predicate does not match the expected source`);
|
|
205
|
+
}
|
|
206
|
+
report.expectedBytes = Buffer.from(compiled).toString("hex");
|
|
207
|
+
}
|
|
208
|
+
return report;
|
|
209
|
+
});
|
|
210
|
+
}
|