-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcommit_cvm_update.ts
More file actions
79 lines (70 loc) · 2.43 KB
/
commit_cvm_update.ts
File metadata and controls
79 lines (70 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { z } from "zod";
import { CvmIdObjectSchema, CvmIdSchema, refineCvmId } from "../../types/cvm_id";
import { defineAction } from "../../utils/define-action";
/**
* Commit CVM update (token-based, no auth required)
*
* Completes a two-phase CVM update using a one-time commit token generated
* during a prepare-only PATCH request. This enables multisig workflows where
* the on-chain signer is different from the original API caller.
*
* @example
* ```typescript
* import { createClient, patchCvm, commitCvmUpdate } from '@phala/cloud'
*
* const client = createClient()
*
* // Phase 1: prepare-only
* const result = await patchCvm(client, {
* id: 'my-cvm',
* docker_compose_file: newComposeYaml,
* prepareOnly: true,
* })
*
* if (result.requiresOnChainHash && result.commitToken) {
* // ... multisig approval happens externally ...
*
* // Phase 2: commit with token
* const committed = await commitCvmUpdate(client, {
* id: 'my-cvm',
* token: result.commitToken,
* composeHash: result.composeHash,
* transactionHash: '0x...',
* })
* console.log(`Update started: ${committed.correlationId}`)
* }
* ```
*/
export const CommitCvmUpdateRequestSchema = refineCvmId(
CvmIdObjectSchema.extend({
token: z.string().describe("One-time commit token from prepare-only flow"),
composeHash: z.string().describe("Compose hash from Phase 1 response"),
transactionHash: z.string().describe("Transaction hash proving on-chain registration"),
}),
);
export type CommitCvmUpdateRequest = z.input<typeof CommitCvmUpdateRequestSchema>;
const CommitCvmUpdateResultSchema = z.object({
correlationId: z.string(),
status: z.string(),
});
export type CommitCvmUpdateResult = z.infer<typeof CommitCvmUpdateResultSchema>;
const { action: commitCvmUpdate, safeAction: safeCommitCvmUpdate } = defineAction<
CommitCvmUpdateRequest,
typeof CommitCvmUpdateResultSchema
>(CommitCvmUpdateResultSchema, async (client, request) => {
const parsed = CommitCvmUpdateRequestSchema.parse(request);
const { cvmId } = CvmIdSchema.parse(parsed);
const response = await client.post<{
correlation_id: string;
status: string;
}>(`/cvms/${cvmId}/commit-update`, {
token: parsed.token,
compose_hash: parsed.composeHash,
transaction_hash: parsed.transactionHash,
});
return {
correlationId: response.correlation_id,
status: response.status,
};
});
export { commitCvmUpdate, safeCommitCvmUpdate };