@crediolabs/policy-synth 0.2.0 → 0.3.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/README.md +4 -4
- package/dist/adapters/interpreter/adapter.d.ts +19 -25
- package/dist/adapters/interpreter/adapter.js +54 -487
- package/dist/adapters/interpreter/index.d.ts +1 -1
- package/dist/adapters/interpreter/index.js +1 -1
- package/dist/adapters/oz/adapter.js +1 -15
- package/dist/errors.d.ts +1 -1
- package/dist/index.d.ts +1 -4
- package/dist/index.js +1 -4
- package/dist/install/build-add-context-rule.d.ts +3 -19
- package/dist/install/build-add-context-rule.js +3 -98
- package/dist/install/build-install-policy.d.ts +4 -53
- package/dist/install/build-install-policy.js +44 -123
- package/dist/install/index.d.ts +0 -2
- package/dist/install/index.js +0 -7
- package/dist/ir/index.d.ts +1 -1
- package/dist/ir/types.d.ts +7 -76
- package/dist/ir/types.js +0 -2
- package/dist/predicate/decode.d.ts +1 -1
- package/dist/predicate/decode.js +2 -70
- package/dist/predicate/encode.js +45 -229
- package/dist/predicate/from-json.js +1 -42
- package/dist/record/decode.js +3 -8
- package/dist/review-card/builder.d.ts +4 -3
- package/dist/review-card/builder.js +18 -162
- package/dist/review-card/cross-check.js +2 -105
- package/dist/review-card/render-leaf.d.ts +4 -0
- package/dist/review-card/render-leaf.js +47 -0
- package/dist/run/index.d.ts +36 -139
- package/dist/run/index.js +125 -437
- package/dist/run/schemas.d.ts +168 -1929
- package/dist/run/schemas.js +40 -269
- package/dist/seams/types.d.ts +0 -2
- package/dist/simulate/deny-cases.d.ts +26 -0
- package/dist/simulate/deny-cases.js +282 -0
- package/dist/simulate/evaluate.d.ts +18 -0
- package/dist/simulate/evaluate.js +271 -0
- package/dist/simulate/index.d.ts +4 -0
- package/dist/simulate/index.js +8 -0
- package/dist/synth/compose-from-recording.d.ts +24 -51
- package/dist/synth/compose-from-recording.js +120 -282
- package/dist/synth/deny-cases.d.ts +5 -7
- package/dist/synth/deny-cases.js +23 -373
- package/dist/synth/evaluate.d.ts +0 -21
- package/dist/synth/evaluate.js +56 -336
- package/dist/synth/index.d.ts +0 -5
- package/dist/synth/index.js +0 -5
- package/dist/synth/permit-context.d.ts +2 -9
- package/dist/synth/permit-context.js +1 -82
- package/dist/synth/synthesize-from-recording.d.ts +1 -29
- package/dist/synth/synthesize-from-recording.js +41 -356
- package/dist/types.d.ts +8 -60
- package/dist/types.js +5 -2
- package/dist/verify/simulate.d.ts +1 -14
- package/dist/verify/simulate.js +2 -88
- package/dist/verify/verify.d.ts +1 -7
- package/dist/verify/verify.js +0 -8
- package/dist-cjs/adapters/interpreter/adapter.d.ts +19 -25
- package/dist-cjs/adapters/interpreter/adapter.js +56 -489
- package/dist-cjs/adapters/interpreter/index.d.ts +1 -1
- package/dist-cjs/adapters/interpreter/index.js +1 -2
- package/dist-cjs/adapters/oz/adapter.js +1 -15
- package/dist-cjs/errors.d.ts +1 -1
- package/dist-cjs/index.d.ts +1 -4
- package/dist-cjs/index.js +1 -4
- package/dist-cjs/install/build-add-context-rule.d.ts +3 -19
- package/dist-cjs/install/build-add-context-rule.js +1 -97
- package/dist-cjs/install/build-install-policy.d.ts +4 -53
- package/dist-cjs/install/build-install-policy.js +44 -122
- package/dist-cjs/install/index.d.ts +0 -2
- package/dist-cjs/install/index.js +2 -23
- package/dist-cjs/ir/index.d.ts +1 -1
- package/dist-cjs/ir/types.d.ts +7 -76
- package/dist-cjs/ir/types.js +0 -2
- package/dist-cjs/predicate/decode.d.ts +1 -1
- package/dist-cjs/predicate/decode.js +2 -70
- package/dist-cjs/predicate/encode.js +45 -229
- package/dist-cjs/predicate/from-json.js +1 -42
- package/dist-cjs/record/decode.js +3 -8
- package/dist-cjs/review-card/builder.d.ts +4 -3
- package/dist-cjs/review-card/builder.js +23 -167
- package/dist-cjs/review-card/cross-check.js +7 -110
- package/dist-cjs/review-card/render-leaf.d.ts +4 -0
- package/dist-cjs/review-card/render-leaf.js +52 -0
- package/dist-cjs/run/index.d.ts +36 -139
- package/dist-cjs/run/index.js +125 -444
- package/dist-cjs/run/schemas.d.ts +168 -1929
- package/dist-cjs/run/schemas.js +41 -270
- package/dist-cjs/seams/types.d.ts +0 -2
- package/dist-cjs/simulate/deny-cases.d.ts +26 -0
- package/dist-cjs/simulate/deny-cases.js +286 -0
- package/dist-cjs/simulate/evaluate.d.ts +18 -0
- package/dist-cjs/simulate/evaluate.js +274 -0
- package/dist-cjs/simulate/index.d.ts +4 -0
- package/dist-cjs/simulate/index.js +13 -0
- package/dist-cjs/synth/compose-from-recording.d.ts +24 -51
- package/dist-cjs/synth/compose-from-recording.js +120 -282
- package/dist-cjs/synth/deny-cases.d.ts +5 -7
- package/dist-cjs/synth/deny-cases.js +23 -374
- package/dist-cjs/synth/evaluate.d.ts +0 -21
- package/dist-cjs/synth/evaluate.js +57 -337
- package/dist-cjs/synth/index.d.ts +0 -5
- package/dist-cjs/synth/index.js +1 -11
- package/dist-cjs/synth/permit-context.d.ts +2 -9
- package/dist-cjs/synth/permit-context.js +1 -82
- package/dist-cjs/synth/synthesize-from-recording.d.ts +1 -29
- package/dist-cjs/synth/synthesize-from-recording.js +39 -354
- package/dist-cjs/types.d.ts +8 -60
- package/dist-cjs/types.js +6 -3
- package/dist-cjs/verify/simulate.d.ts +1 -14
- package/dist-cjs/verify/simulate.js +2 -88
- package/dist-cjs/verify/verify.d.ts +1 -7
- package/dist-cjs/verify/verify.js +0 -8
- package/package.json +4 -4
- package/src/adapters/interpreter/adapter.ts +76 -572
- package/src/errors.ts +0 -19
- package/src/index.ts +1 -4
- package/src/install/build-add-context-rule.ts +5 -139
- package/src/install/build-install-policy.ts +93 -214
- package/src/install/index.ts +0 -34
- package/src/predicate/decode.ts +2 -70
- package/src/predicate/encode.ts +49 -261
- package/src/predicate/from-json.ts +1 -43
- package/src/record/decode.ts +3 -8
- package/src/review-card/builder.ts +19 -168
- package/src/review-card/cross-check.ts +3 -105
- package/src/review-card/render-leaf.ts +48 -0
- package/src/run/index.ts +144 -572
- package/src/run/schemas.ts +42 -291
- package/src/simulate/deny-cases.ts +334 -0
- package/src/simulate/evaluate.ts +284 -0
- package/src/simulate/index.ts +11 -0
- package/src/synth/compose-from-recording.ts +148 -347
- package/src/synth/index.ts +0 -13
- package/src/synth/synthesize-from-recording.ts +43 -456
- package/src/types.ts +13 -49
- package/dist/install/authority-overlap.d.ts +0 -134
- package/dist/install/authority-overlap.js +0 -0
- package/dist/install/build-install-predicate.d.ts +0 -96
- package/dist/install/build-install-predicate.js +0 -444
- package/dist/install/build-merge-policy.d.ts +0 -70
- package/dist/install/build-merge-policy.js +0 -130
- package/dist/install/plan-merge-policy.d.ts +0 -49
- package/dist/install/plan-merge-policy.js +0 -86
- package/dist/install/read-account-rules.d.ts +0 -100
- package/dist/install/read-account-rules.js +0 -283
- package/dist-cjs/install/authority-overlap.d.ts +0 -134
- package/dist-cjs/install/authority-overlap.js +0 -0
- package/dist-cjs/install/build-install-predicate.d.ts +0 -96
- package/dist-cjs/install/build-install-predicate.js +0 -479
- package/dist-cjs/install/build-merge-policy.d.ts +0 -70
- package/dist-cjs/install/build-merge-policy.js +0 -134
- package/dist-cjs/install/plan-merge-policy.d.ts +0 -49
- package/dist-cjs/install/plan-merge-policy.js +0 -90
- package/dist-cjs/install/read-account-rules.d.ts +0 -100
- package/dist-cjs/install/read-account-rules.js +0 -296
- package/src/adapters/interpreter/index.ts +0 -8
- package/src/adapters/oz/adapter.ts +0 -376
- package/src/adapters/oz/index.ts +0 -9
- package/src/codegen/compile-gate.ts +0 -167
- package/src/codegen/index.ts +0 -17
- package/src/codegen/template.ts +0 -165
- package/src/install/authority-overlap.ts +0 -0
- package/src/install/build-merge-policy.ts +0 -219
- package/src/install/plan-merge-policy.ts +0 -133
- package/src/install/read-account-rules.ts +0 -376
- package/src/ir/index.ts +0 -13
- package/src/ir/types.ts +0 -132
- package/src/mandate/index.ts +0 -4
- package/src/mandate/to-ir.ts +0 -71
- package/src/mandate/types.ts +0 -21
- package/src/seams/index.ts +0 -11
- package/src/seams/types.ts +0 -81
- package/src/synth/deny-cases.ts +0 -663
- package/src/synth/evaluate.ts +0 -613
- package/src/synth/harness.ts +0 -68
- package/src/synth/minimize.ts +0 -48
- package/src/synth/permit-context.ts +0 -136
- package/src/synth/predicate-literals.ts +0 -27
- package/src/synth/synthesize-from-mandate.ts +0 -82
- package/src/verify/envelope.ts +0 -28
- package/src/verify/index.ts +0 -5
- package/src/verify/simulate.ts +0 -311
- package/src/verify/verify.ts +0 -243
package/src/codegen/template.ts
DELETED
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
// src/codegen/template.ts - deterministic Rust source generator for the OZ
|
|
2
|
-
// `Policy` escape-hatch skeleton.
|
|
3
|
-
//
|
|
4
|
-
// SUPERSEDED. The architecture this belongs to no longer exists. A deployed
|
|
5
|
-
// policy involves exactly two contracts, the OZ smart account and one
|
|
6
|
-
// immutable interpreter, and the synthesiser emits policy DATA rather than
|
|
7
|
-
// code. Nothing on the synthesis path reaches this module, and no install can
|
|
8
|
-
// produce a contract from it. When a constraint falls outside the grammar the
|
|
9
|
-
// answer is to version the synthesiser and the interpreter together, not to
|
|
10
|
-
// put unaudited Rust on chain per policy.
|
|
11
|
-
//
|
|
12
|
-
// Deleting it is a decision nobody has taken yet, not a dependency question.
|
|
13
|
-
// `compile-gate.ts` takes a Rust string and knows nothing about this module,
|
|
14
|
-
// and its tests compile a hardcoded fixture, so the gate keeps working either
|
|
15
|
-
// way. What deleting this would remove is the only in-repo producer of policy
|
|
16
|
-
// Rust for the gate to check. Do not read its presence as the product
|
|
17
|
-
// generating contracts, and do not wire it into the synthesis path.
|
|
18
|
-
//
|
|
19
|
-
// What follows describes the abandoned design.
|
|
20
|
-
//
|
|
21
|
-
// This is the LAST-RESORT tool for constraints the v1 DSL cannot express. It
|
|
22
|
-
// lives OUT of the audited happy-path surface; the synthesiser never emits it
|
|
23
|
-
// automatically. It mirrors Zodiac Roles' `Custom` operator: a hand-written
|
|
24
|
-
// (here: scaffolded) policy the operator assumes responsibility for.
|
|
25
|
-
//
|
|
26
|
-
// The skeleton carries five storage invariants the user MUST preserve when
|
|
27
|
-
// filling in the predicate logic:
|
|
28
|
-
// 1. `smart_account.require_auth()` is invoked INSIDE `enforce`.
|
|
29
|
-
// 2. Storage is keyed by the composite `(smart_account, rule_id)`.
|
|
30
|
-
// 3. Chain reads use `unwrap_or_default()` so a missing key is a permitted
|
|
31
|
-
// default, not a panic.
|
|
32
|
-
// 4. Storage is `persistent()` (NOT `instance()`) so the policy survives
|
|
33
|
-
// contract instance lifecycle changes.
|
|
34
|
-
// 5. `uninstall` removes the stored state.
|
|
35
|
-
//
|
|
36
|
-
// Determinism: same `(spec, name)` -> byte-identical output. No clock, no RNG,
|
|
37
|
-
// no environment reads.
|
|
38
|
-
|
|
39
|
-
/** A small typed description of one uncovered constraint the v1 DSL refused
|
|
40
|
-
* to express. The synthesiser surfaces these from `CompileResult.uncovered`
|
|
41
|
-
* and the codegen escape hatch wraps them into a hand-auditable skeleton. */
|
|
42
|
-
export interface EscapeHatchSpec {
|
|
43
|
-
/** Address of the policy contract the skeleton is generated for. */
|
|
44
|
-
contract: string
|
|
45
|
-
/** The trait method the predicate lives inside (default: 'enforce'). */
|
|
46
|
-
fnName?: string
|
|
47
|
-
/** Human-readable description of each construct the DSL could not cover. */
|
|
48
|
-
uncovered: string[]
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** Format a name for use as a Rust identifier. The input is constrained by
|
|
52
|
-
* the caller (a kebab-case policy name + alphanumeric addresses); this is a
|
|
53
|
-
* belt-and-braces sanitiser that maps anything non-identifier to '_'. */
|
|
54
|
-
function asRustIdent(s: string): string {
|
|
55
|
-
let out = ''
|
|
56
|
-
for (let i = 0; i < s.length; i++) {
|
|
57
|
-
const ch = s.charCodeAt(i)
|
|
58
|
-
const ok =
|
|
59
|
-
(ch >= 48 && ch <= 57) || // 0-9
|
|
60
|
-
(ch >= 65 && ch <= 90) || // A-Z
|
|
61
|
-
(ch >= 97 && ch <= 122) || // a-z
|
|
62
|
-
ch === 95 // _
|
|
63
|
-
out += ok ? s[i] : '_'
|
|
64
|
-
}
|
|
65
|
-
// Identifiers may not start with a digit.
|
|
66
|
-
if (out.length > 0 && out.charCodeAt(0) >= 48 && out.charCodeAt(0) <= 57) {
|
|
67
|
-
out = `_${out}`
|
|
68
|
-
}
|
|
69
|
-
return out || 'policy'
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Render the `uncovered` list as a `//` comment block (one line per item). */
|
|
73
|
-
function renderUncovered(uncovered: string[]): string {
|
|
74
|
-
if (uncovered.length === 0) {
|
|
75
|
-
return ' // (no specific uncovered constructs reported)'
|
|
76
|
-
}
|
|
77
|
-
return uncovered.map((line) => ` // - ${line}`).join('\n')
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Generate a deterministic OZ `Policy` skeleton for an uncovered constraint.
|
|
81
|
-
*
|
|
82
|
-
* The skeleton is NOT a finished policy; the user fills in the predicate
|
|
83
|
-
* logic in the marked TODO(user) holes and audits the result themselves.
|
|
84
|
-
*
|
|
85
|
-
* Same `(spec, name)` always produces byte-identical output. */
|
|
86
|
-
export function generateRust(spec: EscapeHatchSpec, name: string): string {
|
|
87
|
-
const _contract = spec.contract
|
|
88
|
-
const _fnName = spec.fnName ?? 'enforce'
|
|
89
|
-
const structName = asRustIdent(name)
|
|
90
|
-
const _dataKeyName = `${asRustIdent(name)}_data`
|
|
91
|
-
const uncoveredBlock = renderUncovered(spec.uncovered)
|
|
92
|
-
|
|
93
|
-
return `// Auto-generated by the OZ Policy Builder codegen escape hatch.
|
|
94
|
-
// THIS OUTPUT IS THE USER'S TO COMPLETE AND AUDIT.
|
|
95
|
-
// The synthesiser never emits it on the happy path; reach for it only when
|
|
96
|
-
// a constraint cannot be expressed in the v1 DSL grammar (same shape as
|
|
97
|
-
// Zodiac Roles' \`Custom\` operator).
|
|
98
|
-
|
|
99
|
-
#![no_std]
|
|
100
|
-
|
|
101
|
-
use soroban_sdk::{contract, contractimpl, contracttype, Address, Env, Vec};
|
|
102
|
-
|
|
103
|
-
#[contracttype]
|
|
104
|
-
pub enum DataKey {
|
|
105
|
-
/// Composite key \`(smart_account, rule_id)\` so multiple policies on
|
|
106
|
-
/// one smart account coexist without collision.
|
|
107
|
-
State(Address, u32),
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
#[contract]
|
|
111
|
-
pub struct ${structName};
|
|
112
|
-
|
|
113
|
-
#[contractimpl]
|
|
114
|
-
impl ${structName} {
|
|
115
|
-
pub fn install(e: &Env, smart_account: Address, rule_id: u32) {
|
|
116
|
-
// TODO(user): initialise policy state for this (smart_account, rule_id).
|
|
117
|
-
// The skeleton leaves the stored value empty; the predicate body
|
|
118
|
-
// (in \`enforce\`) decides what to read.
|
|
119
|
-
e.storage().persistent().set(
|
|
120
|
-
&DataKey::State(smart_account.clone(), rule_id),
|
|
121
|
-
&(),
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
pub fn enforce(
|
|
126
|
-
e: &Env,
|
|
127
|
-
smart_account: Address,
|
|
128
|
-
rule_id: u32,
|
|
129
|
-
_signers: Vec<Address>,
|
|
130
|
-
) {
|
|
131
|
-
// Invariant 1: authorise the smart account BEFORE any state read or
|
|
132
|
-
// predicate evaluation. Removing this call opens the policy to
|
|
133
|
-
// impersonation by any caller with a valid rule_id.
|
|
134
|
-
smart_account.require_auth();
|
|
135
|
-
|
|
136
|
-
// Invariant 2 + 4: read stored state through the composite key on
|
|
137
|
-
// \`persistent()\` storage. \`unwrap_or_default()\` (invariant 3)
|
|
138
|
-
// makes an empty key a permitted default rather than a panic.
|
|
139
|
-
let _state: () = e
|
|
140
|
-
.storage()
|
|
141
|
-
.persistent()
|
|
142
|
-
.get(&DataKey::State(smart_account.clone(), rule_id))
|
|
143
|
-
.unwrap_or_default();
|
|
144
|
-
|
|
145
|
-
// --- uncovered constraints the v1 DSL refused to express ---
|
|
146
|
-
${uncoveredBlock}
|
|
147
|
-
|
|
148
|
-
// TODO(user): replace the placeholder below with the real predicate
|
|
149
|
-
// body. The recorded flow's \`(contract, fn_name, args)\` is the
|
|
150
|
-
// single authorised call \`Policy::enforce\` receives; match against
|
|
151
|
-
// \`e\` here.
|
|
152
|
-
let _ = (_state, &e);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
pub fn uninstall(e: &Env, smart_account: Address, rule_id: u32) {
|
|
156
|
-
// Invariant 5: \`uninstall\` removes the stored state so a subsequent
|
|
157
|
-
// install starts from a clean slate (and the nonce returns to its
|
|
158
|
-
// post-uninstall value).
|
|
159
|
-
e.storage()
|
|
160
|
-
.persistent()
|
|
161
|
-
.remove(&DataKey::State(smart_account, rule_id));
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
`
|
|
165
|
-
}
|
|
Binary file
|
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
//! Transactions for the merge remedy.
|
|
2
|
-
//!
|
|
3
|
-
//! `plan-merge-policy.ts` decides WHAT should happen; this builds the XDR for
|
|
4
|
-
//! the step the caller asked for. Two calls, in order, because OZ refuses to
|
|
5
|
-
//! attach a policy that is already on the rule:
|
|
6
|
-
//!
|
|
7
|
-
//! step 1 `detach` -> `account.remove_policy(rule_id, policy_id)`
|
|
8
|
-
//! step 2 `reinstall` -> `account.add_policy(rule_id, interpreter, param)`
|
|
9
|
-
//!
|
|
10
|
-
//! The two cannot be emitted together. Simulating step 2 while the old
|
|
11
|
-
//! attachment is still there hits `DuplicatePolicy`, so the second transaction
|
|
12
|
-
//! is only buildable once the first has confirmed.
|
|
13
|
-
//!
|
|
14
|
-
//! Both calls route through the smart account and are authorised the same way
|
|
15
|
-
//! as `revoke_policy`: against the deploy-time admin rule, with the recorded
|
|
16
|
-
//! `rootInvocation` binding the auth payload to this exact rule and policy.
|
|
17
|
-
//! Detaching runs our `uninstall`, which is master-gated, so the signer has to
|
|
18
|
-
//! be a master of the rule either way.
|
|
19
|
-
|
|
20
|
-
import {
|
|
21
|
-
Account,
|
|
22
|
-
Address,
|
|
23
|
-
BASE_FEE,
|
|
24
|
-
Operation,
|
|
25
|
-
rpc,
|
|
26
|
-
TransactionBuilder,
|
|
27
|
-
xdr,
|
|
28
|
-
} from '@stellar/stellar-sdk'
|
|
29
|
-
import { DEFAULT_GRAMMAR_VERSION, encodePolicyInstallParams } from './build-add-context-rule.ts'
|
|
30
|
-
import type { InstallRpcClient } from './build-install-policy.ts'
|
|
31
|
-
import {
|
|
32
|
-
accountEntry,
|
|
33
|
-
authDigest,
|
|
34
|
-
authPayload,
|
|
35
|
-
delegatedSignerEntry,
|
|
36
|
-
signaturePayload,
|
|
37
|
-
} from './oz-auth.ts'
|
|
38
|
-
import type { MergeStep } from './plan-merge-policy.ts'
|
|
39
|
-
|
|
40
|
-
/** Matches the window the install and revoke builders use. */
|
|
41
|
-
const DEFAULT_AUTH_VALID_UNTIL_LEDGERS = 100
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* The context rule the merge authorises AGAINST, which is not the rule being
|
|
45
|
-
* merged.
|
|
46
|
-
*
|
|
47
|
-
* Both calls mutate the smart account itself, so they need the account's own
|
|
48
|
-
* authorisation, and this builder asks rule 0 for it - the same assumption the
|
|
49
|
-
* install and revoke builders make. OpenZeppelin does NOT enforce any
|
|
50
|
-
* admin semantic for rule 0: it is simply the first rule the account's
|
|
51
|
-
* constructor created, and OZ's reference account creates it as `Default`
|
|
52
|
-
* covering every context.
|
|
53
|
-
*
|
|
54
|
-
* The assumption is therefore load-bearing and unverified at build time. If
|
|
55
|
-
* rule 0 does not exist, does not cover `CallContract(<smart account>)`, or
|
|
56
|
-
* does not list the source account as a signer, the transaction is built and
|
|
57
|
-
* then fails when submitted, and the operator pays the fee for a transaction
|
|
58
|
-
* that was never signable. Changing it is a wire-visible change: the id is
|
|
59
|
-
* hashed into the auth digest.
|
|
60
|
-
*/
|
|
61
|
-
export const ADMIN_CONTEXT_RULE_ID = 0
|
|
62
|
-
|
|
63
|
-
export interface BuildMergePolicyArgs {
|
|
64
|
-
smartAccount: string
|
|
65
|
-
sourceAccount: string
|
|
66
|
-
networkPassphrase: string
|
|
67
|
-
ruleId: number
|
|
68
|
-
/** OZ registry id of the interpreter policy on this rule, from the plan. */
|
|
69
|
-
policyId: number
|
|
70
|
-
interpreterAddress: string
|
|
71
|
-
step: MergeStep
|
|
72
|
-
/** Only needed for `reinstall`: the merged predicate, already encoded. */
|
|
73
|
-
encodedPredicate?: string
|
|
74
|
-
predicateHash?: string
|
|
75
|
-
/** Nonce for the reinstall. Read from chain rather than assumed: a detach
|
|
76
|
-
* whose uninstall panicked leaves the old nonce in place. */
|
|
77
|
-
installNonce?: number
|
|
78
|
-
/** Oracle bounds to carry over from the document being replaced. */
|
|
79
|
-
oracleParams?: {
|
|
80
|
-
maxStalenessSeconds?: number
|
|
81
|
-
maxDeviationBps?: number
|
|
82
|
-
maxCrossFeedDeviationBps?: number
|
|
83
|
-
}
|
|
84
|
-
rpc: InstallRpcClient
|
|
85
|
-
baseFee?: number
|
|
86
|
-
authValidUntilLedgers?: number
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export interface BuildMergePolicyResult {
|
|
90
|
-
unsignedXdr: string
|
|
91
|
-
smartAccount: string
|
|
92
|
-
sourceAccount: string
|
|
93
|
-
step: MergeStep
|
|
94
|
-
call: { contract: string; fn: 'remove_policy' | 'add_policy'; ruleId: number }
|
|
95
|
-
authNonce: string
|
|
96
|
-
authValidUntilLedger: number
|
|
97
|
-
rootInvocationXdr: string
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function hostFunctionFor(args: BuildMergePolicyArgs): xdr.HostFunction {
|
|
101
|
-
if (args.step === 'detach') {
|
|
102
|
-
return xdr.HostFunction.hostFunctionTypeInvokeContract(
|
|
103
|
-
new xdr.InvokeContractArgs({
|
|
104
|
-
contractAddress: new Address(args.smartAccount).toScAddress(),
|
|
105
|
-
functionName: 'remove_policy',
|
|
106
|
-
args: [xdr.ScVal.scvU32(args.ruleId), xdr.ScVal.scvU32(args.policyId)],
|
|
107
|
-
})
|
|
108
|
-
)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (!args.encodedPredicate || !args.predicateHash) {
|
|
112
|
-
throw new Error('merge_policy: reinstall needs the merged predicate and its hash')
|
|
113
|
-
}
|
|
114
|
-
return xdr.HostFunction.hostFunctionTypeInvokeContract(
|
|
115
|
-
new xdr.InvokeContractArgs({
|
|
116
|
-
contractAddress: new Address(args.smartAccount).toScAddress(),
|
|
117
|
-
functionName: 'add_policy',
|
|
118
|
-
args: [
|
|
119
|
-
xdr.ScVal.scvU32(args.ruleId),
|
|
120
|
-
new Address(args.interpreterAddress).toScVal(),
|
|
121
|
-
// Same encoder as a fresh install: the field order is ABI-significant
|
|
122
|
-
// and it re-hashes the predicate to confirm the supplied hash.
|
|
123
|
-
encodePolicyInstallParams({
|
|
124
|
-
encodedPredicate: args.encodedPredicate,
|
|
125
|
-
predicateHash: args.predicateHash,
|
|
126
|
-
installNonce: args.installNonce ?? 1,
|
|
127
|
-
grammarVersion: DEFAULT_GRAMMAR_VERSION,
|
|
128
|
-
...(args.oracleParams ? { oracleParams: args.oracleParams } : {}),
|
|
129
|
-
}),
|
|
130
|
-
],
|
|
131
|
-
})
|
|
132
|
-
)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Build the unsigned transaction for one step of the merge.
|
|
137
|
-
*
|
|
138
|
-
* The auth dance mirrors `buildRevokePolicyXdr`: simulate once to record the
|
|
139
|
-
* account's auth entry, rebuild with the payload bound to that recorded
|
|
140
|
-
* `rootInvocation`, then assemble. Binding to the recorded invocation rather
|
|
141
|
-
* than to the input arguments is what makes the signature commit to the exact
|
|
142
|
-
* rule and policy being changed.
|
|
143
|
-
*/
|
|
144
|
-
export async function buildMergePolicyXdr(
|
|
145
|
-
args: BuildMergePolicyArgs
|
|
146
|
-
): Promise<BuildMergePolicyResult> {
|
|
147
|
-
const fn = args.step === 'detach' ? 'remove_policy' : 'add_policy'
|
|
148
|
-
const source = await args.rpc.getAccount(args.sourceAccount)
|
|
149
|
-
const hostFunction = hostFunctionFor(args)
|
|
150
|
-
const makeOperation = (auth: xdr.SorobanAuthorizationEntry[] = []) =>
|
|
151
|
-
Operation.invokeHostFunction({ func: hostFunction, auth })
|
|
152
|
-
const baseFee = args.baseFee !== undefined ? String(args.baseFee) : BASE_FEE
|
|
153
|
-
|
|
154
|
-
const buildTx = (op: xdr.Operation) =>
|
|
155
|
-
new TransactionBuilder(new Account(args.sourceAccount, source.sequenceNumber()), {
|
|
156
|
-
fee: baseFee,
|
|
157
|
-
networkPassphrase: args.networkPassphrase,
|
|
158
|
-
})
|
|
159
|
-
.addOperation(op)
|
|
160
|
-
.setTimeout(0)
|
|
161
|
-
.build()
|
|
162
|
-
|
|
163
|
-
const recorded = await args.rpc.simulateTransaction(buildTx(makeOperation()))
|
|
164
|
-
if (rpc.Api.isSimulationError(recorded)) {
|
|
165
|
-
// Short and stable, like the other builders: the SDK's full error carries
|
|
166
|
-
// host and URL detail that has no business in a user-facing message.
|
|
167
|
-
throw new Error(`merge_policy: ${fn} simulateTransaction failed`)
|
|
168
|
-
}
|
|
169
|
-
const original = (recorded.result?.auth ?? []).find(
|
|
170
|
-
(entry) =>
|
|
171
|
-
entry.credentials().switch().name === 'sorobanCredentialsAddress' &&
|
|
172
|
-
Address.fromScAddress(entry.credentials().address().address()).toString() ===
|
|
173
|
-
args.smartAccount
|
|
174
|
-
)
|
|
175
|
-
if (!original) {
|
|
176
|
-
throw new Error(
|
|
177
|
-
`merge_policy: no Soroban auth entry for smart account ${args.smartAccount}; this call does not route through the smart account`
|
|
178
|
-
)
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const validUntilLedger =
|
|
182
|
-
(await args.rpc.getLatestLedger()).sequence +
|
|
183
|
-
(args.authValidUntilLedgers ?? DEFAULT_AUTH_VALID_UNTIL_LEDGERS)
|
|
184
|
-
const contextRuleIds = [ADMIN_CONTEXT_RULE_ID]
|
|
185
|
-
const digest = authDigest(
|
|
186
|
-
signaturePayload(
|
|
187
|
-
args.networkPassphrase,
|
|
188
|
-
original.credentials().address().nonce(),
|
|
189
|
-
validUntilLedger,
|
|
190
|
-
original.rootInvocation()
|
|
191
|
-
),
|
|
192
|
-
contextRuleIds
|
|
193
|
-
)
|
|
194
|
-
const authEntries = [
|
|
195
|
-
accountEntry(
|
|
196
|
-
original,
|
|
197
|
-
validUntilLedger,
|
|
198
|
-
authPayload([args.sourceAccount], contextRuleIds, () => Buffer.alloc(0))
|
|
199
|
-
),
|
|
200
|
-
...contextRuleIds.map(() => delegatedSignerEntry(args.smartAccount, digest)),
|
|
201
|
-
]
|
|
202
|
-
const txWithAuth = buildTx(makeOperation(authEntries))
|
|
203
|
-
const enforcing = await args.rpc.simulateTransaction(txWithAuth)
|
|
204
|
-
if (rpc.Api.isSimulationError(enforcing)) {
|
|
205
|
-
throw new Error(`merge_policy: ${fn} auth simulateTransaction failed`)
|
|
206
|
-
}
|
|
207
|
-
const finalTx = rpc.assembleTransaction(txWithAuth, enforcing).build()
|
|
208
|
-
|
|
209
|
-
return {
|
|
210
|
-
unsignedXdr: finalTx.toEnvelope().toXDR().toString('base64'),
|
|
211
|
-
smartAccount: args.smartAccount,
|
|
212
|
-
sourceAccount: args.sourceAccount,
|
|
213
|
-
step: args.step,
|
|
214
|
-
call: { contract: args.smartAccount, fn, ruleId: args.ruleId },
|
|
215
|
-
authNonce: original.credentials().address().nonce().toString(),
|
|
216
|
-
authValidUntilLedger: validUntilLedger,
|
|
217
|
-
rootInvocationXdr: original.rootInvocation().toXDR().toString('base64'),
|
|
218
|
-
}
|
|
219
|
-
}
|
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
//! Planning the merge remedy for a cross-rule authority overlap.
|
|
2
|
-
//!
|
|
3
|
-
//! When two rules our interpreter polices can serve the same calls, the
|
|
4
|
-
//! tightening remedy is to replace one rule's predicate with the conjunction
|
|
5
|
-
//! of both (see `authority-overlap.ts`). Carrying that out against an OZ smart
|
|
6
|
-
//! account is not one call, and the reasons are worth stating because they
|
|
7
|
-
//! shape the whole tool:
|
|
8
|
-
//!
|
|
9
|
-
//! 1. A Soroban transaction carries exactly ONE operation - "smart contract
|
|
10
|
-
//! transactions can only have one operation per transaction" - so the
|
|
11
|
-
//! detach and the re-attach cannot be bundled even though the host would
|
|
12
|
-
//! run them in order happily. And `add_policy` panics `DuplicatePolicy`
|
|
13
|
-
//! while the policy is still attached (`smart_account/storage.rs`
|
|
14
|
-
//! `add_policy`), so the second transaction cannot even be simulated
|
|
15
|
-
//! until the first has confirmed.
|
|
16
|
-
//! 2. `remove_policy` calls `try_uninstall` and DISCARDS the result
|
|
17
|
-
//! (`smart_account/storage.rs`: `let _ = ...try_uninstall(...)`), so the
|
|
18
|
-
//! policy is detached whether or not our `uninstall` succeeded. When it
|
|
19
|
-
//! succeeded the document, nonce, signer hash, master set and counters
|
|
20
|
-
//! are gone and the re-install is a fresh install at nonce 1. When it
|
|
21
|
-
//! panicked - our `uninstall` panics `MissingState` if the master set has
|
|
22
|
-
//! been archived - the nonce SURVIVES, and re-installing at 1 would be
|
|
23
|
-
//! refused on chain as a replay, leaving the rule stuck unpoliced. The
|
|
24
|
-
//! nonce is therefore READ at reinstall time rather than assumed.
|
|
25
|
-
//! 3. Because of (2), any rate-limit or spend window on that rule is reset
|
|
26
|
-
//! by the merge. A signer who had consumed most of a window gets a clean
|
|
27
|
-
//! one. That is a real cost of tightening this way and the caller has to
|
|
28
|
-
//! be told, not discover it.
|
|
29
|
-
//! 4. So the caller performs step 1, waits for it to confirm, then asks for
|
|
30
|
-
//! step 2.
|
|
31
|
-
//!
|
|
32
|
-
//! This module is pure. It decides what should happen and why; the XDR is
|
|
33
|
-
//! built by `build-merge-policy.ts`.
|
|
34
|
-
|
|
35
|
-
import type { PredicateNode } from '../types.ts'
|
|
36
|
-
import type { ObservedRule } from './authority-overlap.ts'
|
|
37
|
-
import { type MergeResult, mergeIntoAnd } from './authority-overlap.ts'
|
|
38
|
-
|
|
39
|
-
export type MergeStep = 'detach' | 'reinstall'
|
|
40
|
-
|
|
41
|
-
export interface MergePlanRefused {
|
|
42
|
-
ok: false
|
|
43
|
-
/** Why the merge cannot proceed, in terms the caller can act on. */
|
|
44
|
-
reason: string
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
export interface MergePlanAccepted {
|
|
48
|
-
ok: true
|
|
49
|
-
/** The conjunction to install in step 2. */
|
|
50
|
-
predicate: PredicateNode
|
|
51
|
-
/** OZ registry id of the interpreter policy on this rule, for
|
|
52
|
-
* `remove_policy`. */
|
|
53
|
-
policyId: number
|
|
54
|
-
/** Oracle bounds to re-install with the merged predicate. Carried from the
|
|
55
|
-
* document being replaced: they are tighten-only overrides against the wasm
|
|
56
|
-
* defaults, so omitting them would quietly widen the policy while the
|
|
57
|
-
* operator believed they were tightening it. */
|
|
58
|
-
oracleParams?: {
|
|
59
|
-
maxStalenessSeconds?: number
|
|
60
|
-
maxDeviationBps?: number
|
|
61
|
-
maxCrossFeedDeviationBps?: number
|
|
62
|
-
}
|
|
63
|
-
/** Consequences the caller must see BEFORE signing step 1, because step 1 is
|
|
64
|
-
* the destructive one. */
|
|
65
|
-
warnings: string[]
|
|
66
|
-
/** What the caller should do after the requested step confirms. */
|
|
67
|
-
followUp: string
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export type MergePlan = MergePlanRefused | MergePlanAccepted
|
|
71
|
-
|
|
72
|
-
/** The interpreter's registry id on a rule, or null when it is not attached
|
|
73
|
-
* or the ids were not readable. */
|
|
74
|
-
export function interpreterPolicyId(rule: ObservedRule, interpreterAddress: string): number | null {
|
|
75
|
-
const index = rule.policyAddresses.indexOf(interpreterAddress)
|
|
76
|
-
if (index < 0) return null
|
|
77
|
-
const id = rule.policyIds?.[index]
|
|
78
|
-
return typeof id === 'number' ? id : null
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Decide whether the merge can proceed, and say what it will cost.
|
|
83
|
-
*
|
|
84
|
-
* Refuses rather than guesses whenever the account is not in the shape the
|
|
85
|
-
* remedy assumes: the rule must be policed by our interpreter, its predicate
|
|
86
|
-
* must have been readable, and the ids needed to detach it must be present. A
|
|
87
|
-
* merge built on a predicate we could not read would silently replace a rule
|
|
88
|
-
* with something narrower or wider than its author wrote.
|
|
89
|
-
*/
|
|
90
|
-
export function planMergePolicy(args: {
|
|
91
|
-
rule: ObservedRule
|
|
92
|
-
interpreterAddress: string
|
|
93
|
-
incoming: PredicateNode
|
|
94
|
-
step: MergeStep
|
|
95
|
-
}): MergePlan {
|
|
96
|
-
const policyId = interpreterPolicyId(args.rule, args.interpreterAddress)
|
|
97
|
-
if (policyId === null) {
|
|
98
|
-
return {
|
|
99
|
-
ok: false,
|
|
100
|
-
reason: `rule ${args.rule.id} is not policed by the interpreter at ${args.interpreterAddress}, or its policy ids could not be read, so there is nothing to merge into and no id to detach. The merge hard-pins to that interpreter and has no opt-out: the auth digest it builds binds to the audited deployment. A rule installed against a different interpreter has to be replaced rather than merged.`,
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const existing = args.rule.predicate
|
|
105
|
-
if (!existing) {
|
|
106
|
-
return {
|
|
107
|
-
ok: false,
|
|
108
|
-
reason: `the predicate installed on rule ${args.rule.id} could not be read, so it cannot be conjoined; merging against an unknown predicate would replace the rule with something other than what its author wrote`,
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const merged: MergeResult = mergeIntoAnd(existing, args.incoming)
|
|
113
|
-
if (!merged.ok || !merged.predicate) {
|
|
114
|
-
return { ok: false, reason: merged.reason ?? 'the two predicates cannot be conjoined' }
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const warnings = [
|
|
118
|
-
`detaching the policy uninstalls it, which removes every counter on rule ${args.rule.id}: any rate limit or spend window there restarts from zero after the merge`,
|
|
119
|
-
'the rule is unpoliced between the two transactions, so a signer of it is unconstrained by this policy until step 2 confirms',
|
|
120
|
-
]
|
|
121
|
-
|
|
122
|
-
return {
|
|
123
|
-
ok: true,
|
|
124
|
-
predicate: merged.predicate,
|
|
125
|
-
policyId,
|
|
126
|
-
...(args.rule.oracleBounds ? { oracleParams: args.rule.oracleBounds } : {}),
|
|
127
|
-
warnings,
|
|
128
|
-
followUp:
|
|
129
|
-
args.step === 'detach'
|
|
130
|
-
? "sign and submit this transaction, wait for it to confirm, then call again with step: 'reinstall' to install the merged predicate"
|
|
131
|
-
: 'sign and submit this transaction; the merged predicate is then the only policy governing this rule',
|
|
132
|
-
}
|
|
133
|
-
}
|