@capxul/sdk 4.20.0-beta.3 → 4.20.0-beta.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,138 @@
1
+ import { a as verifiedConfiguration, d as verifiedPreparation, f as buildCommand, i as prepareConfiguration, l as preparePermission, m as validateReplacementInput, r as authorInvitation, u as preparedIdentity } from "./organizations-wucUfgJr.mjs";
2
+ import { En as CAPXUL_OPERATIONS, Fr as absurd, Ii as Errors, Li as CapxulError, Lt as asCapxulError, X as abandonPrepared } from "./access-B-2qmxF_.mjs";
3
+ //#region src/accounts/permission-approvals.ts
4
+ /** Prepare a Permission write or an invitation's configuration for `signerAddress`, checked. */
5
+ function prepareVerifiedPermissionChange(deps, input, signerAddress, signal) {
6
+ return "permission" in input ? prepareVerifiedPermissionWrite(deps, input, signerAddress, signal) : prepareVerifiedInvitation(deps, input, signerAddress, signal);
7
+ }
8
+ /**
9
+ * Hand the verified identity to the caller before the approval opens. A
10
+ * caller that refuses it has not authorized the command: close the prepared
11
+ * command, so its nonce lane is free again.
12
+ */
13
+ async function delivered(deps, executionId, deliver) {
14
+ let refused;
15
+ try {
16
+ const result = await deliver();
17
+ if (result !== void 0 && !result.ok) refused = result;
18
+ } catch (cause) {
19
+ refused = {
20
+ ok: false,
21
+ error: asCapxulError(cause)
22
+ };
23
+ }
24
+ return refused === void 0 ? {
25
+ ok: true,
26
+ value: { executionId }
27
+ } : abandonPrepared(deps, executionId, "prepared_mismatch", refused);
28
+ }
29
+ /** The command a write sends, decoded exactly as its `org(id).permissions` method decodes it. */
30
+ function command(write) {
31
+ try {
32
+ switch (write.operation) {
33
+ case "create": return {
34
+ ok: true,
35
+ value: buildCommand.create(write)
36
+ };
37
+ case "change": return {
38
+ ok: true,
39
+ value: buildCommand.change(write)
40
+ };
41
+ case "assign": return {
42
+ ok: true,
43
+ value: buildCommand.assign(write)
44
+ };
45
+ case "revoke": return {
46
+ ok: true,
47
+ value: buildCommand.revoke(write)
48
+ };
49
+ case "replace": return {
50
+ ok: true,
51
+ value: {
52
+ operation: "replace",
53
+ requestKey: write.requestKey
54
+ }
55
+ };
56
+ default: return absurd(write);
57
+ }
58
+ } catch (cause) {
59
+ return {
60
+ ok: false,
61
+ error: asCapxulError(cause)
62
+ };
63
+ }
64
+ }
65
+ async function prepareVerifiedPermissionWrite(deps, input, signerAddress, signal) {
66
+ const session = deps.actor?.authSession() ?? null;
67
+ if (session === null || deps.chainId === void 0) return {
68
+ ok: false,
69
+ error: Errors.notAuthenticated()
70
+ };
71
+ const built = command(input.permission);
72
+ if (!built.ok) return built;
73
+ const orgId = input.organizationId;
74
+ const expectedSnapshotHash = input.permission.operation === "replace" ? input.permission.expectedSnapshotHash : void 0;
75
+ const admitted = validateReplacementInput(orgId, built.value, expectedSnapshotHash);
76
+ if (!admitted.ok) return admitted;
77
+ const preparation = {
78
+ orgId,
79
+ command: built.value,
80
+ signerAddress,
81
+ email: session.email,
82
+ chainId: deps.chainId,
83
+ expectedSnapshotHash
84
+ };
85
+ const prepared = await preparePermission(deps, preparation, signal, void 0);
86
+ if (!prepared.ok) return prepared;
87
+ const executionId = prepared.value.executionId;
88
+ const verified = verifiedPreparation(prepared.value, preparation);
89
+ if (!verified.ok) return abandonPrepared(deps, executionId, "prepared_mismatch", verified);
90
+ const { onPrepared } = input;
91
+ return onPrepared === void 0 ? {
92
+ ok: true,
93
+ value: { executionId }
94
+ } : delivered(deps, executionId, () => onPrepared(preparedIdentity(verified.value)));
95
+ }
96
+ async function prepareVerifiedInvitation(deps, input, signerAddress, signal) {
97
+ const session = deps.actor?.authSession() ?? null;
98
+ if (session === null || deps.chainId === void 0) return {
99
+ ok: false,
100
+ error: Errors.notAuthenticated()
101
+ };
102
+ const orgId = input.organizationId;
103
+ const authored = await authorInvitation(deps, orgId, input.invite, signal, void 0);
104
+ if (!authored.ok) return authored;
105
+ const view = authored.value;
106
+ if (view.phase !== "authorizing" || view.commandId === null) return {
107
+ ok: false,
108
+ error: new CapxulError("WRONG_STATE", "The invitation's rights are already set up, or it ended: nothing is left to sign.", { details: {
109
+ method: CAPXUL_OPERATIONS.approvals.prepare,
110
+ currentState: view.phase,
111
+ validStates: ["authorizing"],
112
+ invitationId: view.id
113
+ } })
114
+ };
115
+ const preparation = {
116
+ orgId,
117
+ view,
118
+ commandId: view.commandId,
119
+ signerAddress,
120
+ email: session.email,
121
+ chainId: deps.chainId
122
+ };
123
+ const built = await prepareConfiguration(deps, preparation, signal, void 0);
124
+ if (!built.ok) return built;
125
+ const { executionId, invitationId, commandId } = built.value;
126
+ const verified = verifiedConfiguration(built.value, preparation);
127
+ if (!verified.ok) return abandonPrepared(deps, executionId, "prepared_mismatch", verified);
128
+ const { onPrepared } = input;
129
+ return onPrepared === void 0 ? {
130
+ ok: true,
131
+ value: { executionId }
132
+ } : delivered(deps, executionId, () => onPrepared({
133
+ invitationId,
134
+ commandId
135
+ }));
136
+ }
137
+ //#endregion
138
+ export { prepareVerifiedPermissionChange };