@crediolabs/policy-synth 0.1.12 → 0.1.14
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/install/build-install-policy.d.ts +218 -0
- package/dist/install/build-install-policy.js +458 -0
- package/dist/install/get-interpreter-info.d.ts +29 -0
- package/dist/install/get-interpreter-info.js +41 -0
- package/dist/install/oz-auth.d.ts +27 -0
- package/dist/install/oz-auth.js +105 -0
- package/dist/run/index.d.ts +49 -4
- package/dist/run/index.js +324 -4
- package/dist/run/schemas.d.ts +1500 -4
- package/dist/run/schemas.js +288 -0
- package/dist-cjs/install/build-install-policy.d.ts +218 -0
- package/dist-cjs/install/build-install-policy.js +462 -0
- package/dist-cjs/install/get-interpreter-info.d.ts +29 -0
- package/dist-cjs/install/get-interpreter-info.js +44 -0
- package/dist-cjs/install/oz-auth.d.ts +27 -0
- package/dist-cjs/install/oz-auth.js +114 -0
- package/dist-cjs/run/index.d.ts +49 -4
- package/dist-cjs/run/index.js +339 -3
- package/dist-cjs/run/schemas.d.ts +1500 -4
- package/dist-cjs/run/schemas.js +289 -1
- package/package.json +1 -1
- package/src/install/build-install-policy.ts +753 -0
- package/src/install/get-interpreter-info.ts +68 -0
- package/src/install/oz-auth.ts +140 -0
- package/src/run/index.ts +405 -5
- package/src/run/schemas.ts +326 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Network } from '../types.ts';
|
|
2
|
+
export interface InterpreterInfo {
|
|
3
|
+
/** Pinned interpreter contract address. */
|
|
4
|
+
pinnedAddress: string;
|
|
5
|
+
/** Pinned grammar version (matches SELF_VERSION in version.rs). */
|
|
6
|
+
pinnedGrammarVersion: number;
|
|
7
|
+
/** Pinned wasm sha256 (hex). */
|
|
8
|
+
pinnedWasmHash: string;
|
|
9
|
+
/** Network this pin applies to (the address + hash are network-scoped). */
|
|
10
|
+
network: Network;
|
|
11
|
+
/** Present only when the caller supplied a live `deployedGrammarVersion`. */
|
|
12
|
+
deployedGrammarVersion?: number;
|
|
13
|
+
/** True when `deployedGrammarVersion` matches `pinnedGrammarVersion`.
|
|
14
|
+
* Absent when no live verification was performed. */
|
|
15
|
+
liveMatchesPin?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** Build the interpreter-info response. When `deployedGrammarVersion` is
|
|
18
|
+
* supplied (after a live RPC `grammar_version()` call by the run layer),
|
|
19
|
+
* compares it to the pin and sets `liveMatchesPin`. When absent, returns
|
|
20
|
+
* the pin alone. */
|
|
21
|
+
export declare function getInterpreterInfo(args: {
|
|
22
|
+
pinnedAddress: string;
|
|
23
|
+
pinnedGrammarVersion: number;
|
|
24
|
+
pinnedWasmHash: string;
|
|
25
|
+
network: Network;
|
|
26
|
+
/** When supplied, the u32 returned by the live contract's
|
|
27
|
+
* `grammar_version()` RPC call. */
|
|
28
|
+
deployedGrammarVersion?: number;
|
|
29
|
+
}): InterpreterInfo;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// src/install/get-interpreter-info.ts - read-only fingerprint lookup for
|
|
2
|
+
// the interpreter contract.
|
|
3
|
+
//
|
|
4
|
+
// Returns the pinned deployment fingerprint + (optionally) compares a
|
|
5
|
+
// caller-supplied live `grammar_version` against the pin. No fabricated
|
|
6
|
+
// audit field; a real deployed-contract check is worth more than a fake
|
|
7
|
+
// reference.
|
|
8
|
+
//
|
|
9
|
+
// Per design decision 5: phase-04's "audit #44" is aspirational and has no
|
|
10
|
+
// source-of-truth in the repo. Returning a fabricated audit id would be a
|
|
11
|
+
// lie on a security surface. The honest outputs are:
|
|
12
|
+
// - the pinned address (DEPLOYMENTS.md)
|
|
13
|
+
// - the pinned grammar version (SELF_VERSION in version.rs)
|
|
14
|
+
// - the pinned wasm sha256 (DEPLOYMENTS.md)
|
|
15
|
+
// - an OPTIONAL `deployedGrammarVersion` returned by a live `grammar_version()`
|
|
16
|
+
// RPC call, with a `liveMatchesPin` boolean the caller can dispatch on. A
|
|
17
|
+
// mismatch means the deployed wasm is NOT the pinned artifact - the caller
|
|
18
|
+
// should refuse to install until they redeploy.
|
|
19
|
+
//
|
|
20
|
+
// The RPC plumbing lives in the run layer (`run/index.ts`); this module
|
|
21
|
+
// stays pure so it is testable without a network.
|
|
22
|
+
/** Build the interpreter-info response. When `deployedGrammarVersion` is
|
|
23
|
+
* supplied (after a live RPC `grammar_version()` call by the run layer),
|
|
24
|
+
* compares it to the pin and sets `liveMatchesPin`. When absent, returns
|
|
25
|
+
* the pin alone. */
|
|
26
|
+
export function getInterpreterInfo(args) {
|
|
27
|
+
const info = {
|
|
28
|
+
pinnedAddress: args.pinnedAddress,
|
|
29
|
+
pinnedGrammarVersion: args.pinnedGrammarVersion,
|
|
30
|
+
pinnedWasmHash: args.pinnedWasmHash,
|
|
31
|
+
network: args.network,
|
|
32
|
+
};
|
|
33
|
+
if (typeof args.deployedGrammarVersion === 'number') {
|
|
34
|
+
return {
|
|
35
|
+
...info,
|
|
36
|
+
deployedGrammarVersion: args.deployedGrammarVersion,
|
|
37
|
+
liveMatchesPin: args.deployedGrammarVersion === args.pinnedGrammarVersion,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return info;
|
|
41
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { xdr } from '@stellar/stellar-sdk';
|
|
2
|
+
/** `Signer::Delegated(addr)`. */
|
|
3
|
+
export declare const delegatedSigner: (a: string) => xdr.ScVal;
|
|
4
|
+
/** The standard Soroban authorization preimage hash for one entry. */
|
|
5
|
+
export declare function signaturePayload(networkPassphrase: string, nonce: xdr.Int64, signatureExpirationLedger: number, invocation: xdr.SorobanAuthorizedInvocation): Buffer;
|
|
6
|
+
/**
|
|
7
|
+
* `sha256(signature_payload || xdr(context_rule_ids))`.
|
|
8
|
+
*
|
|
9
|
+
* OZ binds the selected rule ids into the digest so a signature for one rule
|
|
10
|
+
* cannot be replayed against another.
|
|
11
|
+
*/
|
|
12
|
+
export declare function authDigest(payload: Buffer, contextRuleIds: number[]): Buffer;
|
|
13
|
+
/** `AuthPayload { signers, context_rule_ids }` - the account's "signature". */
|
|
14
|
+
export declare function authPayload(signerAddresses: string[], contextRuleIds: number[], signatureFor: (addr: string) => Buffer): xdr.ScVal;
|
|
15
|
+
/**
|
|
16
|
+
* The nested entry a `Delegated` signer needs.
|
|
17
|
+
*
|
|
18
|
+
* `require_auth_for_args` authorizes the CURRENT frame, which while
|
|
19
|
+
* `__check_auth` is running is the account contract executing `__check_auth`,
|
|
20
|
+
* with the digest as its single argument.
|
|
21
|
+
*
|
|
22
|
+
* The signer is the transaction source here, so source-account credentials
|
|
23
|
+
* carry it and no separate signature is required.
|
|
24
|
+
*/
|
|
25
|
+
export declare function delegatedSignerEntry(accountId: string, digest: Buffer): xdr.SorobanAuthorizationEntry;
|
|
26
|
+
/** Rebuild the account's entry with the AuthPayload in the signature slot. */
|
|
27
|
+
export declare function accountEntry(original: xdr.SorobanAuthorizationEntry, signatureExpirationLedger: number, payload: xdr.ScVal): xdr.SorobanAuthorizationEntry;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Build the authorization entries OpenZeppelin's smart account requires.
|
|
2
|
+
//
|
|
3
|
+
// This is the piece that makes an end-to-end test of the account -> policy
|
|
4
|
+
// path possible. Without it, every attempt to exercise `__check_auth`
|
|
5
|
+
// produces a false positive: mocked auth skips `__check_auth` entirely,
|
|
6
|
+
// direct invocation is rejected by the host, and recording-mode simulation
|
|
7
|
+
// does not verify auth at all.
|
|
8
|
+
//
|
|
9
|
+
// Two entries are needed per call:
|
|
10
|
+
//
|
|
11
|
+
// 1. The ACCOUNT's entry. Its `signature` slot is not a signature - OZ puts
|
|
12
|
+
// an `AuthPayload { signers, context_rule_ids }` there, which
|
|
13
|
+
// `__check_auth` receives as its `signatures` argument.
|
|
14
|
+
//
|
|
15
|
+
// 2. One entry per DELEGATED signer. `do_check_auth` calls
|
|
16
|
+
// `addr.require_auth_for_args((auth_digest,))` for each, which is a
|
|
17
|
+
// nested authorization requirement the host will not record during
|
|
18
|
+
// simulation (it never runs `__check_auth` in recording mode), so it has
|
|
19
|
+
// to be constructed by hand.
|
|
20
|
+
//
|
|
21
|
+
// The digest OZ binds is NOT the raw auth payload hash:
|
|
22
|
+
//
|
|
23
|
+
// auth_digest = sha256(signature_payload || xdr(context_rule_ids))
|
|
24
|
+
//
|
|
25
|
+
// where `signature_payload` is the standard Soroban authorization preimage
|
|
26
|
+
// hash. See `do_check_auth` in
|
|
27
|
+
// stellar-contracts/packages/accounts/src/smart_account/storage.rs.
|
|
28
|
+
import { Address, hash, xdr } from '@stellar/stellar-sdk';
|
|
29
|
+
const sym = (s) => xdr.ScVal.scvSymbol(s);
|
|
30
|
+
const u32 = (n) => xdr.ScVal.scvU32(n);
|
|
31
|
+
const vec = (i) => xdr.ScVal.scvVec(i);
|
|
32
|
+
const bytes = (b) => xdr.ScVal.scvBytes(b);
|
|
33
|
+
function struct(fields) {
|
|
34
|
+
return xdr.ScVal.scvMap(Object.keys(fields)
|
|
35
|
+
.sort()
|
|
36
|
+
.map((k) => new xdr.ScMapEntry({ key: sym(k), val: fields[k] })));
|
|
37
|
+
}
|
|
38
|
+
/** `Signer::Delegated(addr)`. */
|
|
39
|
+
export const delegatedSigner = (a) => vec([sym('Delegated'), new Address(a).toScVal()]);
|
|
40
|
+
/** The standard Soroban authorization preimage hash for one entry. */
|
|
41
|
+
export function signaturePayload(networkPassphrase, nonce, signatureExpirationLedger, invocation) {
|
|
42
|
+
const preimage = xdr.HashIdPreimage.envelopeTypeSorobanAuthorization(new xdr.HashIdPreimageSorobanAuthorization({
|
|
43
|
+
networkId: hash(Buffer.from(networkPassphrase)),
|
|
44
|
+
nonce,
|
|
45
|
+
signatureExpirationLedger,
|
|
46
|
+
invocation,
|
|
47
|
+
}));
|
|
48
|
+
return hash(preimage.toXDR());
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* `sha256(signature_payload || xdr(context_rule_ids))`.
|
|
52
|
+
*
|
|
53
|
+
* OZ binds the selected rule ids into the digest so a signature for one rule
|
|
54
|
+
* cannot be replayed against another.
|
|
55
|
+
*/
|
|
56
|
+
export function authDigest(payload, contextRuleIds) {
|
|
57
|
+
const idsXdr = vec(contextRuleIds.map(u32)).toXDR();
|
|
58
|
+
return hash(Buffer.concat([payload, idsXdr]));
|
|
59
|
+
}
|
|
60
|
+
/** `AuthPayload { signers, context_rule_ids }` - the account's "signature". */
|
|
61
|
+
export function authPayload(signerAddresses, contextRuleIds, signatureFor) {
|
|
62
|
+
return struct({
|
|
63
|
+
signers: xdr.ScVal.scvMap(signerAddresses
|
|
64
|
+
.map((a) => new xdr.ScMapEntry({ key: delegatedSigner(a), val: bytes(signatureFor(a)) }))
|
|
65
|
+
// Host maps must be sorted by key.
|
|
66
|
+
.sort((x, y) => Buffer.compare(x.key().toXDR(), y.key().toXDR()))),
|
|
67
|
+
context_rule_ids: vec(contextRuleIds.map(u32)),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The nested entry a `Delegated` signer needs.
|
|
72
|
+
*
|
|
73
|
+
* `require_auth_for_args` authorizes the CURRENT frame, which while
|
|
74
|
+
* `__check_auth` is running is the account contract executing `__check_auth`,
|
|
75
|
+
* with the digest as its single argument.
|
|
76
|
+
*
|
|
77
|
+
* The signer is the transaction source here, so source-account credentials
|
|
78
|
+
* carry it and no separate signature is required.
|
|
79
|
+
*/
|
|
80
|
+
export function delegatedSignerEntry(accountId, digest) {
|
|
81
|
+
return new xdr.SorobanAuthorizationEntry({
|
|
82
|
+
credentials: xdr.SorobanCredentials.sorobanCredentialsSourceAccount(),
|
|
83
|
+
rootInvocation: new xdr.SorobanAuthorizedInvocation({
|
|
84
|
+
function: xdr.SorobanAuthorizedFunction.sorobanAuthorizedFunctionTypeContractFn(new xdr.InvokeContractArgs({
|
|
85
|
+
contractAddress: new Address(accountId).toScAddress(),
|
|
86
|
+
functionName: '__check_auth',
|
|
87
|
+
args: [bytes(digest)],
|
|
88
|
+
})),
|
|
89
|
+
subInvocations: [],
|
|
90
|
+
}),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/** Rebuild the account's entry with the AuthPayload in the signature slot. */
|
|
94
|
+
export function accountEntry(original, signatureExpirationLedger, payload) {
|
|
95
|
+
const creds = original.credentials().address();
|
|
96
|
+
return new xdr.SorobanAuthorizationEntry({
|
|
97
|
+
credentials: xdr.SorobanCredentials.sorobanCredentialsAddress(new xdr.SorobanAddressCredentials({
|
|
98
|
+
address: creds.address(),
|
|
99
|
+
nonce: creds.nonce(),
|
|
100
|
+
signatureExpirationLedger,
|
|
101
|
+
signature: payload,
|
|
102
|
+
})),
|
|
103
|
+
rootInvocation: original.rootInvocation(),
|
|
104
|
+
});
|
|
105
|
+
}
|
package/dist/run/index.d.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { type ErrorCode, type PredicateNode, type ProposedPolicy, type RecordedTransaction, type ToolError, type ToolResponse } from '../index.ts';
|
|
2
|
+
import { type BuildInstallPolicyResult, type BuildRevokePolicyResult } from '../install/build-install-policy.ts';
|
|
3
|
+
import { getInterpreterInfo } from '../install/get-interpreter-info.ts';
|
|
2
4
|
import type { SimulationResult } from '../verify/envelope.ts';
|
|
3
|
-
import { type RecordTransactionInput, type SynthesizePolicyInput } from './schemas.ts';
|
|
4
|
-
export type { RecordTransactionInput, SynthesizePolicyInput } from './schemas.ts';
|
|
5
|
-
export { ComposeUserResponsesSchema, InterpreterOptionsSchema, MandateSpecSchema, NetworkSchema, OzAdapterConfigSchema, RecordedTransactionSchema, RecordTransactionInputSchema, SynthesizePolicyInputSchema, ToolErrorSchema, } from './schemas.ts';
|
|
5
|
+
import { type RecordTransactionInput, type SimulatePolicyInput, type SynthesizePolicyInput, type VerifyPolicyInput } from './schemas.ts';
|
|
6
|
+
export type { GetInterpreterInfoInput, InstallPolicyInput, RecordTransactionInput, RevokePolicyInput, SimulatePolicyInput, SynthesizePolicyInput, VerifyPolicyInput, } from './schemas.ts';
|
|
7
|
+
export { ComposeUserResponsesSchema, GetInterpreterInfoInputSchema, InstallPolicyInputSchema, InterpreterOptionsSchema, MandateSpecSchema, NetworkSchema, OraclePriceFixtureSchema, OzAdapterConfigSchema, PINNED_INTERPRETER_GRAMMAR_VERSION, PINNED_INTERPRETER_TESTNET_ADDRESS, PINNED_INTERPRETER_WASM_SHA256, PredicateLeafSchema, PredicateNodeSchema, RecordedTransactionSchema, RecordTransactionInputSchema, RevokePolicyInputSchema, SimulatePolicyInputSchema, SynthesizePolicyInputSchema, ToolErrorSchema, VerifyPolicyInputSchema, } from './schemas.ts';
|
|
6
8
|
export type RunRecordTransactionInput = RecordTransactionInput;
|
|
7
9
|
export type RunSynthesizePolicyInput = SynthesizePolicyInput;
|
|
10
|
+
export type RunSimulatePolicyInput = SimulatePolicyInput;
|
|
11
|
+
export type RunVerifyPolicyInput = VerifyPolicyInput;
|
|
8
12
|
/** `record_transaction` body - wraps `recordTransaction`. The tool input
|
|
9
13
|
* matches the core RecordInput minus the injected `fetcher` (the transport
|
|
10
14
|
* layer does not own the RPC). Returns the core ToolResponse unchanged.
|
|
@@ -35,6 +39,47 @@ export declare function runSynthesizePolicy(raw: unknown): Promise<ToolResponse<
|
|
|
35
39
|
simulation: SimulationResult;
|
|
36
40
|
};
|
|
37
41
|
}>;
|
|
42
|
+
/** `simulate_policy` body - thin wrapper over `simulatePolicy`. The engine
|
|
43
|
+
* already returns fail-closed `{ok:false, error}` for runtime failures
|
|
44
|
+
* (SIMULATION_ERROR), so the try/catch envelope is for raw SDK throws
|
|
45
|
+
* only - same pattern as the other two wrappers. The predicate is
|
|
46
|
+
* passed inline (stateless by design; no `proposed_policy_id` lookup). */
|
|
47
|
+
export declare function runSimulatePolicy(raw: unknown): Promise<ToolResponse<SimulationResult>>;
|
|
48
|
+
/** `verify_policy` body - thin wrapper over `verifyPolicy`. The engine
|
|
49
|
+
* already returns `{ok:false, error}` with code VERIFICATION_FAILED when
|
|
50
|
+
* the minimality check fails; the try/catch envelope is for raw SDK
|
|
51
|
+
* throws only. Mirrors `runSimulatePolicy` exactly. */
|
|
52
|
+
export declare function runVerifyPolicy(raw: unknown): Promise<ToolResponse<true>>;
|
|
53
|
+
/** `install_policy` body - thin wrapper over `buildInstallPolicyXdr`.
|
|
54
|
+
* Returns the unsigned Soroban transaction envelope (base64 XDR) the
|
|
55
|
+
* wallet signs. The wallet signature IS the user-confirmation step - no
|
|
56
|
+
* `action_id` two-call pair (the server is stateless, see server.ts:10-12).
|
|
57
|
+
* Per design decision 4, only CALL 1 (account.add_context_rule) is
|
|
58
|
+
* emitted; CALL 2 (interpreter.install) requires the rule id the account
|
|
59
|
+
* assigns in call 1 and is documented under `followUp` in the response.
|
|
60
|
+
*
|
|
61
|
+
* Default-deny: an interpreter policy address other than the pinned
|
|
62
|
+
* testnet interpreter is REFUSED (the smart account would delegate to
|
|
63
|
+
* an interpreter the caller controls); the same applies to a non-pinned
|
|
64
|
+
* RPC URL (the auth nonce the wallet signs comes from the RPC). Both
|
|
65
|
+
* gates accept an explicit opt-in flag. */
|
|
66
|
+
export declare function runInstallPolicy(raw: unknown): Promise<ToolResponse<BuildInstallPolicyResult>>;
|
|
67
|
+
/** `revoke_policy` body - thin wrapper over `buildRevokePolicyXdr`.
|
|
68
|
+
* Emits an unsigned XDR for `account.remove_context_rule(ruleId)`; the
|
|
69
|
+
* smart account itself handles uninstalling each attached policy. Auth
|
|
70
|
+
* is master-only; the source account MUST be the master signer set.
|
|
71
|
+
*
|
|
72
|
+
* Same RPC pin as install: a non-pinned `rpcUrl` is refused unless
|
|
73
|
+
* `allowUnpinnedRpcUrl: true`. Revoke does not carry an interpreter
|
|
74
|
+
* policy payload, so the interpreter pin is not re-checked here. */
|
|
75
|
+
export declare function runRevokePolicy(raw: unknown): Promise<ToolResponse<BuildRevokePolicyResult>>;
|
|
76
|
+
/** `get_interpreter_info` body - thin wrapper over `getInterpreterInfo`.
|
|
77
|
+
* Returns the pinned deployment fingerprint + an optional live
|
|
78
|
+
* `grammar_version()` comparison. The audit field is deliberately
|
|
79
|
+
* OMITTED (phase-04's "audit #44" has no source of truth in the repo -
|
|
80
|
+
* fabricating it would be a lie on a security surface; the live
|
|
81
|
+
* mismatch check is worth MORE). */
|
|
82
|
+
export declare function runGetInterpreterInfo(raw: unknown): Promise<ToolResponse<ReturnType<typeof getInterpreterInfo>>>;
|
|
38
83
|
/** Build a canonical ToolError for a thrown exception caught by the tool
|
|
39
84
|
* envelope. The MCP SDK stringifies thrown objects as "[object Object]" by
|
|
40
85
|
* default, so we extract a string-friendly message and tag the original
|
|
@@ -46,4 +91,4 @@ export declare function runSynthesizePolicy(raw: unknown): Promise<ToolResponse<
|
|
|
46
91
|
* Exported as `_caughtError` (the leading underscore signals the test-only
|
|
47
92
|
* seam) so the suite in run/index.test.ts can drive the envelope path
|
|
48
93
|
* without standing up a full recordTransaction pipeline. */
|
|
49
|
-
export declare function caughtError(toolName: 'record_transaction' | 'synthesize_policy', code: ErrorCode, e: unknown): ToolError;
|
|
94
|
+
export declare function caughtError(toolName: 'record_transaction' | 'synthesize_policy' | 'simulate_policy' | 'verify_policy' | 'install_policy' | 'revoke_policy' | 'get_interpreter_info', code: ErrorCode, e: unknown): ToolError;
|
package/dist/run/index.js
CHANGED
|
@@ -16,13 +16,18 @@
|
|
|
16
16
|
//
|
|
17
17
|
// No business logic. No retries. No session state. The same call shape can
|
|
18
18
|
// drive the CLI (which calls into the same core directly without MCP).
|
|
19
|
+
import { createHash } from 'node:crypto';
|
|
20
|
+
import { rpc } from '@stellar/stellar-sdk';
|
|
19
21
|
import { placeholderOzConfig, recordTransaction, synthesizeFromMandate, synthesizeFromRecording, } from "../index.js";
|
|
20
|
-
import {
|
|
22
|
+
import { buildInstallPolicyXdr, buildRevokePolicyXdr, rpcClientFromServer, } from "../install/build-install-policy.js";
|
|
23
|
+
import { getInterpreterInfo } from "../install/get-interpreter-info.js";
|
|
24
|
+
import { simulatePolicy, verifyPolicy } from "../verify/index.js";
|
|
25
|
+
import { GetInterpreterInfoInputSchema, InstallPolicyInputSchema, NETWORK_PASSPHRASES, PINNED_INTERPRETER_GRAMMAR_VERSION, PINNED_INTERPRETER_TESTNET_ADDRESS, PINNED_INTERPRETER_WASM_SHA256, RecordTransactionInputSchema, RevokePolicyInputSchema, SimulatePolicyInputSchema, SynthesizePolicyInputSchema, TESTNET_RPC_URL, VerifyPolicyInputSchema, } from "./schemas.js";
|
|
21
26
|
// Re-export the underlying Zod schemas so the MCP package (and any other
|
|
22
27
|
// downstream consumer) can import the canonical input shapes from the same
|
|
23
28
|
// module that owns the tool-body glue. The strict schemas are the source of
|
|
24
29
|
// truth - MCP tool shapes are derived from them.
|
|
25
|
-
export { ComposeUserResponsesSchema, InterpreterOptionsSchema, MandateSpecSchema, NetworkSchema, OzAdapterConfigSchema, RecordedTransactionSchema, RecordTransactionInputSchema, SynthesizePolicyInputSchema, ToolErrorSchema, } from "./schemas.js";
|
|
30
|
+
export { ComposeUserResponsesSchema, GetInterpreterInfoInputSchema, InstallPolicyInputSchema, InterpreterOptionsSchema, MandateSpecSchema, NetworkSchema, OraclePriceFixtureSchema, OzAdapterConfigSchema, PINNED_INTERPRETER_GRAMMAR_VERSION, PINNED_INTERPRETER_TESTNET_ADDRESS, PINNED_INTERPRETER_WASM_SHA256, PredicateLeafSchema, PredicateNodeSchema, RecordedTransactionSchema, RecordTransactionInputSchema, RevokePolicyInputSchema, SimulatePolicyInputSchema, SynthesizePolicyInputSchema, ToolErrorSchema, VerifyPolicyInputSchema, } from "./schemas.js";
|
|
26
31
|
/** `record_transaction` body - wraps `recordTransaction`. The tool input
|
|
27
32
|
* matches the core RecordInput minus the injected `fetcher` (the transport
|
|
28
33
|
* layer does not own the RPC). Returns the core ToolResponse unchanged.
|
|
@@ -117,11 +122,323 @@ function resolveOzConfig(input) {
|
|
|
117
122
|
// placeholder OZ instance addresses are deterministic.
|
|
118
123
|
return placeholderOzConfig('mainnet');
|
|
119
124
|
}
|
|
125
|
+
/** `simulate_policy` body - thin wrapper over `simulatePolicy`. The engine
|
|
126
|
+
* already returns fail-closed `{ok:false, error}` for runtime failures
|
|
127
|
+
* (SIMULATION_ERROR), so the try/catch envelope is for raw SDK throws
|
|
128
|
+
* only - same pattern as the other two wrappers. The predicate is
|
|
129
|
+
* passed inline (stateless by design; no `proposed_policy_id` lookup). */
|
|
130
|
+
export async function runSimulatePolicy(raw) {
|
|
131
|
+
const parsed = SimulatePolicyInputSchema.safeParse(raw);
|
|
132
|
+
if (!parsed.success) {
|
|
133
|
+
return {
|
|
134
|
+
ok: false,
|
|
135
|
+
error: validationError('simulate_policy', parsed.error.issues),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
const input = parsed.data;
|
|
139
|
+
try {
|
|
140
|
+
// The recursive PredicateNodeSchema + ContractInvocationSchema are
|
|
141
|
+
// typed `z.ZodType<unknown>` to survive TS's circular inference; the
|
|
142
|
+
// engine wants typed `PredicateNode | null` + `RecordedTransaction`.
|
|
143
|
+
// The schema already validated the shape, so assert through the
|
|
144
|
+
// unknown back to the core types. Same pattern as the recordedTx
|
|
145
|
+
// cast in `runSynthesizePolicy`.
|
|
146
|
+
return simulatePolicy(input.predicate, input.permitTx, {
|
|
147
|
+
...(input.validUntilLedger !== undefined
|
|
148
|
+
? { validUntilLedger: input.validUntilLedger }
|
|
149
|
+
: {}),
|
|
150
|
+
...(input.oraclePricesByAsset !== undefined
|
|
151
|
+
? { oraclePricesByAsset: input.oraclePricesByAsset }
|
|
152
|
+
: {}),
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
catch (e) {
|
|
156
|
+
return {
|
|
157
|
+
ok: false,
|
|
158
|
+
error: caughtError('simulate_policy', 'SIMULATION_ERROR', e),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/** `verify_policy` body - thin wrapper over `verifyPolicy`. The engine
|
|
163
|
+
* already returns `{ok:false, error}` with code VERIFICATION_FAILED when
|
|
164
|
+
* the minimality check fails; the try/catch envelope is for raw SDK
|
|
165
|
+
* throws only. Mirrors `runSimulatePolicy` exactly. */
|
|
166
|
+
export async function runVerifyPolicy(raw) {
|
|
167
|
+
const parsed = VerifyPolicyInputSchema.safeParse(raw);
|
|
168
|
+
if (!parsed.success) {
|
|
169
|
+
return {
|
|
170
|
+
ok: false,
|
|
171
|
+
error: validationError('verify_policy', parsed.error.issues),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
const input = parsed.data;
|
|
175
|
+
try {
|
|
176
|
+
// Same cast as runSimulatePolicy: the recursive schemas are typed
|
|
177
|
+
// `unknown`; the engine wants typed `PredicateNode` +
|
|
178
|
+
// `RecordedTransaction`. The schema already validated the shape.
|
|
179
|
+
return verifyPolicy(input.predicate, input.permitTx, {
|
|
180
|
+
...(input.validUntilLedger !== undefined ? { validUntilLedger: input.validUntilLedger } : {}),
|
|
181
|
+
...(input.oraclePricesByAsset !== undefined
|
|
182
|
+
? { oraclePricesByAsset: input.oraclePricesByAsset }
|
|
183
|
+
: {}),
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
catch (e) {
|
|
187
|
+
return {
|
|
188
|
+
ok: false,
|
|
189
|
+
error: caughtError('verify_policy', 'VERIFICATION_FAILED', e),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/** `install_policy` body - thin wrapper over `buildInstallPolicyXdr`.
|
|
194
|
+
* Returns the unsigned Soroban transaction envelope (base64 XDR) the
|
|
195
|
+
* wallet signs. The wallet signature IS the user-confirmation step - no
|
|
196
|
+
* `action_id` two-call pair (the server is stateless, see server.ts:10-12).
|
|
197
|
+
* Per design decision 4, only CALL 1 (account.add_context_rule) is
|
|
198
|
+
* emitted; CALL 2 (interpreter.install) requires the rule id the account
|
|
199
|
+
* assigns in call 1 and is documented under `followUp` in the response.
|
|
200
|
+
*
|
|
201
|
+
* Default-deny: an interpreter policy address other than the pinned
|
|
202
|
+
* testnet interpreter is REFUSED (the smart account would delegate to
|
|
203
|
+
* an interpreter the caller controls); the same applies to a non-pinned
|
|
204
|
+
* RPC URL (the auth nonce the wallet signs comes from the RPC). Both
|
|
205
|
+
* gates accept an explicit opt-in flag. */
|
|
206
|
+
export async function runInstallPolicy(raw) {
|
|
207
|
+
const parsed = InstallPolicyInputSchema.safeParse(raw);
|
|
208
|
+
if (!parsed.success) {
|
|
209
|
+
return {
|
|
210
|
+
ok: false,
|
|
211
|
+
error: validationError('install_policy', parsed.error.issues),
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
const input = parsed.data;
|
|
215
|
+
// ---- Pinning gates (default-deny) ----
|
|
216
|
+
const pinningError = enforceInterpreterPin(input.rule.policies, input.allowUnpinnedInterpreter);
|
|
217
|
+
if (pinningError) {
|
|
218
|
+
return { ok: false, error: pinningError };
|
|
219
|
+
}
|
|
220
|
+
if (input.rpcUrl && input.rpcUrl !== TESTNET_RPC_URL && input.allowUnpinnedRpcUrl !== true) {
|
|
221
|
+
return {
|
|
222
|
+
ok: false,
|
|
223
|
+
error: {
|
|
224
|
+
code: 'INSTALL_BUILD_FAILED',
|
|
225
|
+
message: `install_policy: rpcUrl must equal the pinned TESTNET_RPC_URL; set allowUnpinnedRpcUrl: true to opt in to a custom endpoint`,
|
|
226
|
+
severity: 'error',
|
|
227
|
+
retryable: false,
|
|
228
|
+
remediation: { toolCall: { name: 'install_policy', args: {} } },
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
let rpcClient;
|
|
233
|
+
try {
|
|
234
|
+
rpcClient = buildRpcClientFromInput(input.rpcUrl);
|
|
235
|
+
}
|
|
236
|
+
catch (e) {
|
|
237
|
+
return {
|
|
238
|
+
ok: false,
|
|
239
|
+
error: caughtError('install_policy', 'INSTALL_BUILD_FAILED', e),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
try {
|
|
243
|
+
const interpreterPolicy = input.rule.policies.find((p) => p.kind === 'interpreter');
|
|
244
|
+
const encodedPredicate = interpreterPolicy?.predicateBlobBase64 ?? '';
|
|
245
|
+
const predicateHash = createHash('sha256')
|
|
246
|
+
.update(Buffer.from(encodedPredicate, 'base64'))
|
|
247
|
+
.digest('hex');
|
|
248
|
+
const result = await buildInstallPolicyXdr({
|
|
249
|
+
smartAccount: input.smartAccount,
|
|
250
|
+
sourceAccount: input.sourceAccount,
|
|
251
|
+
networkPassphrase: NETWORK_PASSPHRASES.testnet,
|
|
252
|
+
rule: input.rule,
|
|
253
|
+
installNonce: input.installNonce,
|
|
254
|
+
encodedPredicate,
|
|
255
|
+
predicateHash,
|
|
256
|
+
rpc: rpcClient,
|
|
257
|
+
...(input.baseFee !== undefined ? { baseFee: input.baseFee } : {}),
|
|
258
|
+
});
|
|
259
|
+
return { ok: true, data: result };
|
|
260
|
+
}
|
|
261
|
+
catch (e) {
|
|
262
|
+
return {
|
|
263
|
+
ok: false,
|
|
264
|
+
error: caughtError('install_policy', 'INSTALL_BUILD_FAILED', e),
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
/** `revoke_policy` body - thin wrapper over `buildRevokePolicyXdr`.
|
|
269
|
+
* Emits an unsigned XDR for `account.remove_context_rule(ruleId)`; the
|
|
270
|
+
* smart account itself handles uninstalling each attached policy. Auth
|
|
271
|
+
* is master-only; the source account MUST be the master signer set.
|
|
272
|
+
*
|
|
273
|
+
* Same RPC pin as install: a non-pinned `rpcUrl` is refused unless
|
|
274
|
+
* `allowUnpinnedRpcUrl: true`. Revoke does not carry an interpreter
|
|
275
|
+
* policy payload, so the interpreter pin is not re-checked here. */
|
|
276
|
+
export async function runRevokePolicy(raw) {
|
|
277
|
+
const parsed = RevokePolicyInputSchema.safeParse(raw);
|
|
278
|
+
if (!parsed.success) {
|
|
279
|
+
return {
|
|
280
|
+
ok: false,
|
|
281
|
+
error: validationError('revoke_policy', parsed.error.issues),
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
const input = parsed.data;
|
|
285
|
+
if (input.rpcUrl && input.rpcUrl !== TESTNET_RPC_URL && input.allowUnpinnedRpcUrl !== true) {
|
|
286
|
+
return {
|
|
287
|
+
ok: false,
|
|
288
|
+
error: {
|
|
289
|
+
code: 'REVOKE_BUILD_FAILED',
|
|
290
|
+
message: `revoke_policy: rpcUrl must equal the pinned TESTNET_RPC_URL; set allowUnpinnedRpcUrl: true to opt in to a custom endpoint`,
|
|
291
|
+
severity: 'error',
|
|
292
|
+
retryable: false,
|
|
293
|
+
remediation: { toolCall: { name: 'revoke_policy', args: {} } },
|
|
294
|
+
},
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
let rpcClient;
|
|
298
|
+
try {
|
|
299
|
+
rpcClient = buildRpcClientFromInput(input.rpcUrl);
|
|
300
|
+
}
|
|
301
|
+
catch (e) {
|
|
302
|
+
return {
|
|
303
|
+
ok: false,
|
|
304
|
+
error: caughtError('revoke_policy', 'REVOKE_BUILD_FAILED', e),
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
try {
|
|
308
|
+
const result = await buildRevokePolicyXdr({
|
|
309
|
+
smartAccount: input.smartAccount,
|
|
310
|
+
sourceAccount: input.sourceAccount,
|
|
311
|
+
ruleId: input.ruleId,
|
|
312
|
+
networkPassphrase: NETWORK_PASSPHRASES.testnet,
|
|
313
|
+
rpc: rpcClient,
|
|
314
|
+
...(input.baseFee !== undefined ? { baseFee: input.baseFee } : {}),
|
|
315
|
+
});
|
|
316
|
+
return { ok: true, data: result };
|
|
317
|
+
}
|
|
318
|
+
catch (e) {
|
|
319
|
+
return {
|
|
320
|
+
ok: false,
|
|
321
|
+
error: caughtError('revoke_policy', 'REVOKE_BUILD_FAILED', e),
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/** `get_interpreter_info` body - thin wrapper over `getInterpreterInfo`.
|
|
326
|
+
* Returns the pinned deployment fingerprint + an optional live
|
|
327
|
+
* `grammar_version()` comparison. The audit field is deliberately
|
|
328
|
+
* OMITTED (phase-04's "audit #44" has no source of truth in the repo -
|
|
329
|
+
* fabricating it would be a lie on a security surface; the live
|
|
330
|
+
* mismatch check is worth MORE). */
|
|
331
|
+
export async function runGetInterpreterInfo(raw) {
|
|
332
|
+
const parsed = GetInterpreterInfoInputSchema.safeParse(raw);
|
|
333
|
+
if (!parsed.success) {
|
|
334
|
+
return {
|
|
335
|
+
ok: false,
|
|
336
|
+
error: validationError('get_interpreter_info', parsed.error.issues),
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
const input = parsed.data;
|
|
340
|
+
const network = input.network ?? 'testnet';
|
|
341
|
+
// Asking about mainnet used to return the TESTNET pin with `network:
|
|
342
|
+
// 'mainnet'` stamped on it. DEPLOYMENTS.md:3 says plainly "Testnet only.
|
|
343
|
+
// Nothing is deployed to mainnet yet", so that answer described a contract
|
|
344
|
+
// that does not exist at an address that is not on that network. This tool
|
|
345
|
+
// exists to tell a caller what they are installing against; inventing a
|
|
346
|
+
// mainnet deployment is the same failure as inventing an audit reference.
|
|
347
|
+
if (network === 'mainnet') {
|
|
348
|
+
return {
|
|
349
|
+
ok: false,
|
|
350
|
+
error: {
|
|
351
|
+
code: 'RECORDING_FAILED',
|
|
352
|
+
message: 'get_interpreter_info: no interpreter is deployed to mainnet. The pinned address and wasm hash are testnet-only; mainnet deployment is gated on the interpreter audit.',
|
|
353
|
+
severity: 'error',
|
|
354
|
+
retryable: false,
|
|
355
|
+
remediation: {
|
|
356
|
+
toolCall: { name: 'get_interpreter_info', args: { network: 'testnet' } },
|
|
357
|
+
},
|
|
358
|
+
},
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
try {
|
|
362
|
+
let deployedGrammarVersion;
|
|
363
|
+
if (input.verifyLive === true) {
|
|
364
|
+
const rpcClient = buildRpcClientFromInput(input.rpcUrl);
|
|
365
|
+
deployedGrammarVersion = await rpcClient.getContractVersion(PINNED_INTERPRETER_TESTNET_ADDRESS);
|
|
366
|
+
}
|
|
367
|
+
const info = getInterpreterInfo({
|
|
368
|
+
pinnedAddress: PINNED_INTERPRETER_TESTNET_ADDRESS,
|
|
369
|
+
pinnedGrammarVersion: PINNED_INTERPRETER_GRAMMAR_VERSION,
|
|
370
|
+
pinnedWasmHash: PINNED_INTERPRETER_WASM_SHA256,
|
|
371
|
+
network,
|
|
372
|
+
...(deployedGrammarVersion !== undefined ? { deployedGrammarVersion } : {}),
|
|
373
|
+
});
|
|
374
|
+
return { ok: true, data: info };
|
|
375
|
+
}
|
|
376
|
+
catch (e) {
|
|
377
|
+
return {
|
|
378
|
+
ok: false,
|
|
379
|
+
error: caughtError('get_interpreter_info', 'RECORDING_FAILED', e),
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
/** Build an InstallRpcClient from an optional URL override, falling back
|
|
384
|
+
* to the public testnet RPC (matching the design-decision rule that we
|
|
385
|
+
* reuse `record/rpc.ts` and never hard-code a different RPC client). */
|
|
386
|
+
function buildRpcClientFromInput(urlOverride) {
|
|
387
|
+
if (urlOverride) {
|
|
388
|
+
return rpcClientFromServer(new rpc.Server(urlOverride, { allowHttp: false }), NETWORK_PASSPHRASES.testnet);
|
|
389
|
+
}
|
|
390
|
+
// Default: public testnet RPC. Matches the brief; the mainnet pin would
|
|
391
|
+
// need a separate deploy and is out of scope for this tool.
|
|
392
|
+
//
|
|
393
|
+
// NOT `createRpcServer` - that returns an RpcFetcher, a bare
|
|
394
|
+
// `(hash) => Promise<SorobanTxResponse|null>` for the RECORDER. Passing it
|
|
395
|
+
// here produced a client whose `getAccount` was undefined, so every live
|
|
396
|
+
// call died with "server.getAccount is not a function". The install path
|
|
397
|
+
// needs the full Server surface.
|
|
398
|
+
return rpcClientFromServer(new rpc.Server(TESTNET_RPC_URL, { allowHttp: false }), NETWORK_PASSPHRASES.testnet);
|
|
399
|
+
}
|
|
400
|
+
/** Default-deny: refuse any interpreter policy whose address differs from
|
|
401
|
+
* the pinned testnet interpreter. An interpreter the caller controls
|
|
402
|
+
* can permit anything, so the smart account's authorization must bind
|
|
403
|
+
* to the pinned contract unless the caller explicitly opts in via
|
|
404
|
+
* `allowUnpinnedInterpreter`. OZ built-in policies are not interpreters
|
|
405
|
+
* and pass through unchanged. Returns a ToolError to surface through
|
|
406
|
+
* the run-layer envelope, or null when the policies are all pinned. */
|
|
407
|
+
function enforceInterpreterPin(policies, allowUnpinned) {
|
|
408
|
+
for (const p of policies) {
|
|
409
|
+
if (p.kind !== 'interpreter')
|
|
410
|
+
continue;
|
|
411
|
+
if (p.interpreterAddress === PINNED_INTERPRETER_TESTNET_ADDRESS)
|
|
412
|
+
continue;
|
|
413
|
+
if (allowUnpinned === true)
|
|
414
|
+
continue;
|
|
415
|
+
return {
|
|
416
|
+
code: 'INSTALL_BUILD_FAILED',
|
|
417
|
+
message: `install_policy: interpreter policy address ${p.interpreterAddress} != pinned ${PINNED_INTERPRETER_TESTNET_ADDRESS}; set allowUnpinnedInterpreter: true to opt in to a non-pinned interpreter`,
|
|
418
|
+
severity: 'error',
|
|
419
|
+
retryable: false,
|
|
420
|
+
remediation: { toolCall: { name: 'install_policy', args: {} } },
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
120
425
|
/** Build a canonical ToolError for a Zod validation failure. The remediation
|
|
121
426
|
* hint points the agent back at the right tool with an empty arg bag - the
|
|
122
427
|
* tool name IS the machine-readable hint. */
|
|
123
428
|
function validationError(toolName, issues) {
|
|
124
|
-
const code = toolName === 'record_transaction'
|
|
429
|
+
const code = toolName === 'record_transaction'
|
|
430
|
+
? 'RECORDING_FAILED'
|
|
431
|
+
: toolName === 'synthesize_policy'
|
|
432
|
+
? 'SYNTHESIS_ERROR'
|
|
433
|
+
: toolName === 'simulate_policy'
|
|
434
|
+
? 'SIMULATION_ERROR'
|
|
435
|
+
: toolName === 'verify_policy'
|
|
436
|
+
? 'VERIFICATION_FAILED'
|
|
437
|
+
: toolName === 'install_policy'
|
|
438
|
+
? 'INSTALL_BUILD_FAILED'
|
|
439
|
+
: toolName === 'revoke_policy'
|
|
440
|
+
? 'REVOKE_BUILD_FAILED'
|
|
441
|
+
: 'RECORDING_FAILED';
|
|
125
442
|
return {
|
|
126
443
|
code,
|
|
127
444
|
message: `${toolName}: invalid input: ${issues
|
|
@@ -202,8 +519,11 @@ function safeStringify(v) {
|
|
|
202
519
|
return value.toString();
|
|
203
520
|
if (typeof value === 'function')
|
|
204
521
|
return `[function ${value.name || 'anonymous'}]`;
|
|
522
|
+
// `stack` is a server-controlled diagnostic. Stack traces from a host
|
|
523
|
+
// we do not own are reconnaissance, not a signal the caller can act on.
|
|
524
|
+
// Surface `name` + `message` only.
|
|
205
525
|
if (value instanceof Error) {
|
|
206
|
-
return { name: value.name, message: value.message
|
|
526
|
+
return { name: value.name, message: value.message };
|
|
207
527
|
}
|
|
208
528
|
if (value !== null && typeof value === 'object') {
|
|
209
529
|
if (seen.has(value))
|