@phala/cloud 0.2.5 → 0.2.9

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.
@@ -99,7 +99,7 @@ export declare const DstackAppFullResponseV20260121Schema: z.ZodObject<{
99
99
  os_image_hash?: string | null | undefined;
100
100
  }>>>;
101
101
  kms_type: z.ZodOptional<z.ZodNullable<z.ZodEnum<["phala", "ethereum", "base", "legacy"]>>>;
102
- kms_info: z.ZodOptional<z.ZodNullable<z.ZodObject<{
102
+ kms_info: z.ZodOptional<z.ZodNullable<z.ZodEffects<z.ZodObject<{
103
103
  chain_id: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
104
104
  dstack_kms_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
105
105
  dstack_app_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
@@ -120,6 +120,20 @@ export declare const DstackAppFullResponseV20260121Schema: z.ZodObject<{
120
120
  dstack_kms_address?: string | null | undefined;
121
121
  dstack_app_address?: string | null | undefined;
122
122
  rpc_endpoint?: string | null | undefined;
123
+ }>, {
124
+ chain_id?: number | null | undefined;
125
+ encrypted_env_pubkey?: string | null | undefined;
126
+ deployer_address?: string | null | undefined;
127
+ dstack_kms_address?: string | null | undefined;
128
+ dstack_app_address?: string | null | undefined;
129
+ rpc_endpoint?: string | null | undefined;
130
+ }, {
131
+ chain_id?: number | null | undefined;
132
+ encrypted_env_pubkey?: string | null | undefined;
133
+ deployer_address?: string | null | undefined;
134
+ dstack_kms_address?: string | null | undefined;
135
+ dstack_app_address?: string | null | undefined;
136
+ rpc_endpoint?: string | null | undefined;
123
137
  }>>>;
124
138
  status: z.ZodString;
125
139
  progress: z.ZodOptional<z.ZodNullable<z.ZodObject<{
@@ -428,7 +442,7 @@ export declare const DstackAppFullResponseV20260121Schema: z.ZodObject<{
428
442
  os_image_hash?: string | null | undefined;
429
443
  }>>>;
430
444
  kms_type: z.ZodOptional<z.ZodNullable<z.ZodEnum<["phala", "ethereum", "base", "legacy"]>>>;
431
- kms_info: z.ZodOptional<z.ZodNullable<z.ZodObject<{
445
+ kms_info: z.ZodOptional<z.ZodNullable<z.ZodEffects<z.ZodObject<{
432
446
  chain_id: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
433
447
  dstack_kms_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
434
448
  dstack_app_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
@@ -449,6 +463,20 @@ export declare const DstackAppFullResponseV20260121Schema: z.ZodObject<{
449
463
  dstack_kms_address?: string | null | undefined;
450
464
  dstack_app_address?: string | null | undefined;
451
465
  rpc_endpoint?: string | null | undefined;
466
+ }>, {
467
+ chain_id?: number | null | undefined;
468
+ encrypted_env_pubkey?: string | null | undefined;
469
+ deployer_address?: string | null | undefined;
470
+ dstack_kms_address?: string | null | undefined;
471
+ dstack_app_address?: string | null | undefined;
472
+ rpc_endpoint?: string | null | undefined;
473
+ }, {
474
+ chain_id?: number | null | undefined;
475
+ encrypted_env_pubkey?: string | null | undefined;
476
+ deployer_address?: string | null | undefined;
477
+ dstack_kms_address?: string | null | undefined;
478
+ dstack_app_address?: string | null | undefined;
479
+ rpc_endpoint?: string | null | undefined;
452
480
  }>>>;
453
481
  status: z.ZodString;
454
482
  progress: z.ZodOptional<z.ZodNullable<z.ZodObject<{
@@ -1157,7 +1185,7 @@ export declare const DstackAppWithCvmResponseV20260121Schema: z.ZodUnion<[z.ZodO
1157
1185
  os_image_hash?: string | null | undefined;
1158
1186
  }>>>;
1159
1187
  kms_type: z.ZodOptional<z.ZodNullable<z.ZodEnum<["phala", "ethereum", "base", "legacy"]>>>;
1160
- kms_info: z.ZodOptional<z.ZodNullable<z.ZodObject<{
1188
+ kms_info: z.ZodOptional<z.ZodNullable<z.ZodEffects<z.ZodObject<{
1161
1189
  chain_id: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
1162
1190
  dstack_kms_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1163
1191
  dstack_app_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
@@ -1178,6 +1206,20 @@ export declare const DstackAppWithCvmResponseV20260121Schema: z.ZodUnion<[z.ZodO
1178
1206
  dstack_kms_address?: string | null | undefined;
1179
1207
  dstack_app_address?: string | null | undefined;
1180
1208
  rpc_endpoint?: string | null | undefined;
1209
+ }>, {
1210
+ chain_id?: number | null | undefined;
1211
+ encrypted_env_pubkey?: string | null | undefined;
1212
+ deployer_address?: string | null | undefined;
1213
+ dstack_kms_address?: string | null | undefined;
1214
+ dstack_app_address?: string | null | undefined;
1215
+ rpc_endpoint?: string | null | undefined;
1216
+ }, {
1217
+ chain_id?: number | null | undefined;
1218
+ encrypted_env_pubkey?: string | null | undefined;
1219
+ deployer_address?: string | null | undefined;
1220
+ dstack_kms_address?: string | null | undefined;
1221
+ dstack_app_address?: string | null | undefined;
1222
+ rpc_endpoint?: string | null | undefined;
1181
1223
  }>>>;
1182
1224
  status: z.ZodString;
1183
1225
  progress: z.ZodOptional<z.ZodNullable<z.ZodObject<{
@@ -1486,7 +1528,7 @@ export declare const DstackAppWithCvmResponseV20260121Schema: z.ZodUnion<[z.ZodO
1486
1528
  os_image_hash?: string | null | undefined;
1487
1529
  }>>>;
1488
1530
  kms_type: z.ZodOptional<z.ZodNullable<z.ZodEnum<["phala", "ethereum", "base", "legacy"]>>>;
1489
- kms_info: z.ZodOptional<z.ZodNullable<z.ZodObject<{
1531
+ kms_info: z.ZodOptional<z.ZodNullable<z.ZodEffects<z.ZodObject<{
1490
1532
  chain_id: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
1491
1533
  dstack_kms_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
1492
1534
  dstack_app_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
@@ -1507,6 +1549,20 @@ export declare const DstackAppWithCvmResponseV20260121Schema: z.ZodUnion<[z.ZodO
1507
1549
  dstack_kms_address?: string | null | undefined;
1508
1550
  dstack_app_address?: string | null | undefined;
1509
1551
  rpc_endpoint?: string | null | undefined;
1552
+ }>, {
1553
+ chain_id?: number | null | undefined;
1554
+ encrypted_env_pubkey?: string | null | undefined;
1555
+ deployer_address?: string | null | undefined;
1556
+ dstack_kms_address?: string | null | undefined;
1557
+ dstack_app_address?: string | null | undefined;
1558
+ rpc_endpoint?: string | null | undefined;
1559
+ }, {
1560
+ chain_id?: number | null | undefined;
1561
+ encrypted_env_pubkey?: string | null | undefined;
1562
+ deployer_address?: string | null | undefined;
1563
+ dstack_kms_address?: string | null | undefined;
1564
+ dstack_app_address?: string | null | undefined;
1565
+ rpc_endpoint?: string | null | undefined;
1510
1566
  }>>>;
1511
1567
  status: z.ZodString;
1512
1568
  progress: z.ZodOptional<z.ZodNullable<z.ZodObject<{
@@ -2214,7 +2270,7 @@ export declare const DstackAppListResponseV20260121Schema: z.ZodObject<{
2214
2270
  os_image_hash?: string | null | undefined;
2215
2271
  }>>>;
2216
2272
  kms_type: z.ZodOptional<z.ZodNullable<z.ZodEnum<["phala", "ethereum", "base", "legacy"]>>>;
2217
- kms_info: z.ZodOptional<z.ZodNullable<z.ZodObject<{
2273
+ kms_info: z.ZodOptional<z.ZodNullable<z.ZodEffects<z.ZodObject<{
2218
2274
  chain_id: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
2219
2275
  dstack_kms_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
2220
2276
  dstack_app_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
@@ -2235,6 +2291,20 @@ export declare const DstackAppListResponseV20260121Schema: z.ZodObject<{
2235
2291
  dstack_kms_address?: string | null | undefined;
2236
2292
  dstack_app_address?: string | null | undefined;
2237
2293
  rpc_endpoint?: string | null | undefined;
2294
+ }>, {
2295
+ chain_id?: number | null | undefined;
2296
+ encrypted_env_pubkey?: string | null | undefined;
2297
+ deployer_address?: string | null | undefined;
2298
+ dstack_kms_address?: string | null | undefined;
2299
+ dstack_app_address?: string | null | undefined;
2300
+ rpc_endpoint?: string | null | undefined;
2301
+ }, {
2302
+ chain_id?: number | null | undefined;
2303
+ encrypted_env_pubkey?: string | null | undefined;
2304
+ deployer_address?: string | null | undefined;
2305
+ dstack_kms_address?: string | null | undefined;
2306
+ dstack_app_address?: string | null | undefined;
2307
+ rpc_endpoint?: string | null | undefined;
2238
2308
  }>>>;
2239
2309
  status: z.ZodString;
2240
2310
  progress: z.ZodOptional<z.ZodNullable<z.ZodObject<{
@@ -2543,7 +2613,7 @@ export declare const DstackAppListResponseV20260121Schema: z.ZodObject<{
2543
2613
  os_image_hash?: string | null | undefined;
2544
2614
  }>>>;
2545
2615
  kms_type: z.ZodOptional<z.ZodNullable<z.ZodEnum<["phala", "ethereum", "base", "legacy"]>>>;
2546
- kms_info: z.ZodOptional<z.ZodNullable<z.ZodObject<{
2616
+ kms_info: z.ZodOptional<z.ZodNullable<z.ZodEffects<z.ZodObject<{
2547
2617
  chain_id: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
2548
2618
  dstack_kms_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
2549
2619
  dstack_app_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
@@ -2564,6 +2634,20 @@ export declare const DstackAppListResponseV20260121Schema: z.ZodObject<{
2564
2634
  dstack_kms_address?: string | null | undefined;
2565
2635
  dstack_app_address?: string | null | undefined;
2566
2636
  rpc_endpoint?: string | null | undefined;
2637
+ }>, {
2638
+ chain_id?: number | null | undefined;
2639
+ encrypted_env_pubkey?: string | null | undefined;
2640
+ deployer_address?: string | null | undefined;
2641
+ dstack_kms_address?: string | null | undefined;
2642
+ dstack_app_address?: string | null | undefined;
2643
+ rpc_endpoint?: string | null | undefined;
2644
+ }, {
2645
+ chain_id?: number | null | undefined;
2646
+ encrypted_env_pubkey?: string | null | undefined;
2647
+ deployer_address?: string | null | undefined;
2648
+ dstack_kms_address?: string | null | undefined;
2649
+ dstack_app_address?: string | null | undefined;
2650
+ rpc_endpoint?: string | null | undefined;
2567
2651
  }>>>;
2568
2652
  status: z.ZodString;
2569
2653
  progress: z.ZodOptional<z.ZodNullable<z.ZodObject<{
@@ -83,7 +83,7 @@ export declare const CvmOsInfoV20260121Schema: z.ZodObject<{
83
83
  os_image_hash?: string | null | undefined;
84
84
  }>;
85
85
  export type CvmOsInfoV20260121 = z.infer<typeof CvmOsInfoV20260121Schema>;
86
- export declare const CvmKmsInfoV20260121Schema: z.ZodObject<{
86
+ export declare const CvmKmsInfoV20260121Schema: z.ZodEffects<z.ZodObject<{
87
87
  chain_id: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
88
88
  dstack_kms_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
89
89
  dstack_app_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
@@ -104,6 +104,20 @@ export declare const CvmKmsInfoV20260121Schema: z.ZodObject<{
104
104
  dstack_kms_address?: string | null | undefined;
105
105
  dstack_app_address?: string | null | undefined;
106
106
  rpc_endpoint?: string | null | undefined;
107
+ }>, {
108
+ chain_id?: number | null | undefined;
109
+ encrypted_env_pubkey?: string | null | undefined;
110
+ deployer_address?: string | null | undefined;
111
+ dstack_kms_address?: string | null | undefined;
112
+ dstack_app_address?: string | null | undefined;
113
+ rpc_endpoint?: string | null | undefined;
114
+ }, {
115
+ chain_id?: number | null | undefined;
116
+ encrypted_env_pubkey?: string | null | undefined;
117
+ deployer_address?: string | null | undefined;
118
+ dstack_kms_address?: string | null | undefined;
119
+ dstack_app_address?: string | null | undefined;
120
+ rpc_endpoint?: string | null | undefined;
107
121
  }>;
108
122
  export type CvmKmsInfoV20260121 = z.infer<typeof CvmKmsInfoV20260121Schema>;
109
123
  export declare const CvmProgressInfoV20260121Schema: z.ZodObject<{
@@ -265,7 +279,7 @@ export declare const CvmInfoV20260121Schema: z.ZodObject<{
265
279
  os_image_hash?: string | null | undefined;
266
280
  }>>>;
267
281
  kms_type: z.ZodOptional<z.ZodNullable<z.ZodEnum<["phala", "ethereum", "base", "legacy"]>>>;
268
- kms_info: z.ZodOptional<z.ZodNullable<z.ZodObject<{
282
+ kms_info: z.ZodOptional<z.ZodNullable<z.ZodEffects<z.ZodObject<{
269
283
  chain_id: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
270
284
  dstack_kms_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
271
285
  dstack_app_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
@@ -286,6 +300,20 @@ export declare const CvmInfoV20260121Schema: z.ZodObject<{
286
300
  dstack_kms_address?: string | null | undefined;
287
301
  dstack_app_address?: string | null | undefined;
288
302
  rpc_endpoint?: string | null | undefined;
303
+ }>, {
304
+ chain_id?: number | null | undefined;
305
+ encrypted_env_pubkey?: string | null | undefined;
306
+ deployer_address?: string | null | undefined;
307
+ dstack_kms_address?: string | null | undefined;
308
+ dstack_app_address?: string | null | undefined;
309
+ rpc_endpoint?: string | null | undefined;
310
+ }, {
311
+ chain_id?: number | null | undefined;
312
+ encrypted_env_pubkey?: string | null | undefined;
313
+ deployer_address?: string | null | undefined;
314
+ dstack_kms_address?: string | null | undefined;
315
+ dstack_app_address?: string | null | undefined;
316
+ rpc_endpoint?: string | null | undefined;
289
317
  }>>>;
290
318
  status: z.ZodString;
291
319
  progress: z.ZodOptional<z.ZodNullable<z.ZodObject<{
@@ -595,7 +623,7 @@ export declare const CvmInfoDetailV20260121Schema: z.ZodObject<{
595
623
  os_image_hash?: string | null | undefined;
596
624
  }>>>;
597
625
  kms_type: z.ZodOptional<z.ZodNullable<z.ZodEnum<["phala", "ethereum", "base", "legacy"]>>>;
598
- kms_info: z.ZodOptional<z.ZodNullable<z.ZodObject<{
626
+ kms_info: z.ZodOptional<z.ZodNullable<z.ZodEffects<z.ZodObject<{
599
627
  chain_id: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
600
628
  dstack_kms_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
601
629
  dstack_app_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
@@ -616,6 +644,20 @@ export declare const CvmInfoDetailV20260121Schema: z.ZodObject<{
616
644
  dstack_kms_address?: string | null | undefined;
617
645
  dstack_app_address?: string | null | undefined;
618
646
  rpc_endpoint?: string | null | undefined;
647
+ }>, {
648
+ chain_id?: number | null | undefined;
649
+ encrypted_env_pubkey?: string | null | undefined;
650
+ deployer_address?: string | null | undefined;
651
+ dstack_kms_address?: string | null | undefined;
652
+ dstack_app_address?: string | null | undefined;
653
+ rpc_endpoint?: string | null | undefined;
654
+ }, {
655
+ chain_id?: number | null | undefined;
656
+ encrypted_env_pubkey?: string | null | undefined;
657
+ deployer_address?: string | null | undefined;
658
+ dstack_kms_address?: string | null | undefined;
659
+ dstack_app_address?: string | null | undefined;
660
+ rpc_endpoint?: string | null | undefined;
619
661
  }>>>;
620
662
  status: z.ZodString;
621
663
  progress: z.ZodOptional<z.ZodNullable<z.ZodObject<{
@@ -930,7 +972,7 @@ export declare const PaginatedCvmInfosV20260121Schema: z.ZodObject<{
930
972
  os_image_hash?: string | null | undefined;
931
973
  }>>>;
932
974
  kms_type: z.ZodOptional<z.ZodNullable<z.ZodEnum<["phala", "ethereum", "base", "legacy"]>>>;
933
- kms_info: z.ZodOptional<z.ZodNullable<z.ZodObject<{
975
+ kms_info: z.ZodOptional<z.ZodNullable<z.ZodEffects<z.ZodObject<{
934
976
  chain_id: z.ZodOptional<z.ZodNullable<z.ZodNumber>>;
935
977
  dstack_kms_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
936
978
  dstack_app_address: z.ZodOptional<z.ZodNullable<z.ZodString>>;
@@ -951,6 +993,20 @@ export declare const PaginatedCvmInfosV20260121Schema: z.ZodObject<{
951
993
  dstack_kms_address?: string | null | undefined;
952
994
  dstack_app_address?: string | null | undefined;
953
995
  rpc_endpoint?: string | null | undefined;
996
+ }>, {
997
+ chain_id?: number | null | undefined;
998
+ encrypted_env_pubkey?: string | null | undefined;
999
+ deployer_address?: string | null | undefined;
1000
+ dstack_kms_address?: string | null | undefined;
1001
+ dstack_app_address?: string | null | undefined;
1002
+ rpc_endpoint?: string | null | undefined;
1003
+ }, {
1004
+ chain_id?: number | null | undefined;
1005
+ encrypted_env_pubkey?: string | null | undefined;
1006
+ deployer_address?: string | null | undefined;
1007
+ dstack_kms_address?: string | null | undefined;
1008
+ dstack_app_address?: string | null | undefined;
1009
+ rpc_endpoint?: string | null | undefined;
954
1010
  }>>>;
955
1011
  status: z.ZodString;
956
1012
  progress: z.ZodOptional<z.ZodNullable<z.ZodObject<{
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Phala Cloud API structured error codes.
3
+ *
4
+ * Use with ResourceError.errorCode to match specific errors:
5
+ * ```typescript
6
+ * if (error instanceof ResourceError && error.errorCode === ErrorCodes.CVM_APP_ID_CONFLICT) {
7
+ * // handle idempotency conflict
8
+ * }
9
+ * ```
10
+ */
11
+ export declare const ErrorCodes: {
12
+ readonly NODE_NOT_FOUND: "ERR-01-001";
13
+ readonly COMPOSE_FILE_REQUIRED: "ERR-01-002";
14
+ readonly INVALID_COMPOSE_FILE: "ERR-01-003";
15
+ readonly DUPLICATE_CVM_NAME: "ERR-01-004";
16
+ readonly HASH_REGISTRATION_REQUIRED: "ERR-01-005";
17
+ readonly HASH_INVALID_OR_EXPIRED: "ERR-01-006";
18
+ readonly TX_VERIFICATION_FAILED: "ERR-01-007";
19
+ readonly HASH_NOT_ALLOWED: "ERR-01-008";
20
+ readonly INSTANCE_TYPE_NOT_FOUND: "ERR-02-001";
21
+ readonly RESOURCE_NOT_AVAILABLE: "ERR-02-002";
22
+ readonly INSUFFICIENT_VCPU: "ERR-02-003";
23
+ readonly INSUFFICIENT_MEMORY: "ERR-02-004";
24
+ readonly INSUFFICIENT_SLOTS: "ERR-02-005";
25
+ readonly GPU_ALLOCATION_ERROR: "ERR-02-006";
26
+ readonly INSUFFICIENT_GPU: "ERR-02-007";
27
+ readonly INVALID_REQUEST: "ERR-02-008";
28
+ readonly INCOMPATIBLE_CONFIG: "ERR-02-009";
29
+ readonly IMAGE_NOT_FOUND: "ERR-02-010";
30
+ readonly KMS_NOT_FOUND: "ERR-02-011";
31
+ readonly TEEPOD_NOT_ACCESSIBLE: "ERR-02-012";
32
+ readonly OS_IMAGE_NOT_COMPATIBLE: "ERR-02-013";
33
+ readonly NODE_CAPACITY_NOT_CONFIGURED: "ERR-02-014";
34
+ readonly QUOTA_EXCEEDED: "ERR-02-015";
35
+ readonly CVM_NOT_FOUND: "ERR-03-001";
36
+ readonly MULTIPLE_CVMS_SAME_NAME: "ERR-03-002";
37
+ readonly CVM_NOT_IN_WORKSPACE: "ERR-03-003";
38
+ readonly CVM_ACCESS_DENIED: "ERR-03-005";
39
+ readonly REPLICA_IMAGE_NOT_AVAILABLE: "ERR-03-006";
40
+ readonly CVM_APP_ID_CONFLICT: "ERR-03-007";
41
+ readonly INSUFFICIENT_BALANCE: "ERR-04-001";
42
+ readonly MAX_CVM_LIMIT: "ERR-04-002";
43
+ readonly RESOURCE_LIMIT_EXCEEDED: "ERR-04-003";
44
+ readonly TOKEN_LIMIT_EXCEEDED: "ERR-05-001";
45
+ readonly TOKEN_RATE_LIMIT: "ERR-05-002";
46
+ readonly OAUTH_EMAIL_INVALID: "ERR-06-001";
47
+ };
48
+ export type ErrorCode = (typeof ErrorCodes)[keyof typeof ErrorCodes];
@@ -376,3 +376,12 @@ export declare function formatStructuredError(error: ResourceError, options?: {
376
376
  /** Show links (default: true) */
377
377
  showLinks?: boolean;
378
378
  }): string;
379
+ /**
380
+ * Check if an error is a conflict error (409).
381
+ */
382
+ export declare function isConflictError(error: PhalaCloudError): boolean;
383
+ /**
384
+ * Check if an error is an app_id conflict from idempotent CVM creation.
385
+ * This means the app_id already has an active CVM with a different compose_hash.
386
+ */
387
+ export declare function isAppIdConflictError(error: PhalaCloudError): error is ResourceError;
@@ -3,7 +3,8 @@ export { getComposeHash, dumpAppCompose, preprocessAppCompose, sortObject, withC
3
3
  export { asHex } from "./as-hex";
4
4
  export { validateActionParameters, safeValidateActionParameters } from "./validate-parameters";
5
5
  export { isValidHostname, convertToHostname, generateFallbackHostname, generateDefaultCvmName, } from "./hostname";
6
- export { parseApiError, PhalaCloudError, RequestError, ValidationError, AuthError, BusinessError, ServerError, UnknownError, ResourceError, formatValidationErrors, formatErrorMessage, formatStructuredError, getErrorMessage, getValidationFields, type ValidationErrorItem, type StructuredErrorDetail, type ErrorLink, type StructuredErrorResponse, ApiErrorSchema, type ApiError, } from "./errors";
6
+ export { parseApiError, PhalaCloudError, RequestError, ValidationError, AuthError, BusinessError, ServerError, UnknownError, ResourceError, formatValidationErrors, formatErrorMessage, formatStructuredError, getErrorMessage, isConflictError, isAppIdConflictError, getValidationFields, type ValidationErrorItem, type StructuredErrorDetail, type ErrorLink, type StructuredErrorResponse, ApiErrorSchema, type ApiError, } from "./errors";
7
+ export { ErrorCodes, type ErrorCode } from "./error_codes";
7
8
  export { createNetworkClients, extractNetworkClients, checkNetworkStatus, checkBalance, validateNetworkPrerequisites, waitForTransactionReceipt, executeTransaction, NetworkError, WalletError, TransactionError, type NetworkConfig, type WalletConnection, type NetworkClients, type BalanceCheckResult, type TransactionOptions, type TransactionResult, } from "./network";
8
9
  export { createTransactionTracker, executeBatchTransactions, executeTransactionWithRetry, estimateTransactionGas, type TransactionState, type TransactionStatus, type TransactionTracker, type BatchTransactionOptions, type BatchTransactionResult, type RetryOptions, type GasEstimationOptions, } from "./transaction";
9
10
  export { createClientsFromPrivateKey, createClientsFromBrowser, autoCreateClients, switchToNetwork, addNetwork, } from "./client-factories";
@@ -0,0 +1,35 @@
1
+ /** Webhook event envelope */
2
+ export interface WebhookEvent<T = unknown> {
3
+ id: string;
4
+ event: string;
5
+ version: string;
6
+ created_at: string;
7
+ workspace: {
8
+ id: string;
9
+ name: string;
10
+ };
11
+ data: T;
12
+ }
13
+ /**
14
+ * Verify an HMAC-SHA256 webhook signature.
15
+ *
16
+ * @returns true if signature is valid and timestamp is within tolerance
17
+ */
18
+ export declare function verifyWebhookSignature(params: {
19
+ secret: string;
20
+ timestamp: string;
21
+ body: string;
22
+ signature: string;
23
+ toleranceSeconds?: number;
24
+ }): boolean;
25
+ /**
26
+ * Parse and verify a webhook event from HTTP request headers and body.
27
+ *
28
+ * @throws Error if signature is invalid, timestamp is expired, or headers are missing
29
+ */
30
+ export declare function parseWebhookEvent<T = unknown>(params: {
31
+ headers: Record<string, string | string[] | undefined>;
32
+ body: string;
33
+ secret: string;
34
+ toleranceSeconds?: number;
35
+ }): WebhookEvent<T>;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Webhook signature verification utilities.
3
+ *
4
+ * Exported separately from the main entry point because these
5
+ * depend on `node:crypto` and must not be included in browser bundles.
6
+ *
7
+ * Usage:
8
+ * import { verifyWebhookSignature, parseWebhookEvent } from "@phala/cloud/webhook";
9
+ */
10
+ export { verifyWebhookSignature, parseWebhookEvent } from "./utils/verify-webhook";
11
+ export type { WebhookEvent } from "./utils/verify-webhook";
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/webhook.ts
21
+ var webhook_exports = {};
22
+ __export(webhook_exports, {
23
+ parseWebhookEvent: () => parseWebhookEvent,
24
+ verifyWebhookSignature: () => verifyWebhookSignature
25
+ });
26
+ module.exports = __toCommonJS(webhook_exports);
27
+
28
+ // src/utils/verify-webhook.ts
29
+ var import_node_crypto = require("crypto");
30
+ function verifyWebhookSignature(params) {
31
+ const { secret, timestamp, body, signature, toleranceSeconds = 300 } = params;
32
+ const ts = Number.parseInt(timestamp, 10);
33
+ if (Number.isNaN(ts)) return false;
34
+ const now = Math.floor(Date.now() / 1e3);
35
+ if (Math.abs(now - ts) > toleranceSeconds) return false;
36
+ const expected = `sha256=${(0, import_node_crypto.createHmac)("sha256", secret).update(`${timestamp}.${body}`).digest("hex")}`;
37
+ const expectedBuf = Buffer.from(expected, "utf-8");
38
+ const actualBuf = Buffer.from(signature, "utf-8");
39
+ if (expectedBuf.length !== actualBuf.length) return false;
40
+ return (0, import_node_crypto.timingSafeEqual)(expectedBuf, actualBuf);
41
+ }
42
+ function parseWebhookEvent(params) {
43
+ const { headers, body, secret, toleranceSeconds } = params;
44
+ const normalised = {};
45
+ for (const [key, value] of Object.entries(headers)) {
46
+ const resolved = Array.isArray(value) ? value[0] : value;
47
+ if (resolved != null) {
48
+ normalised[key.toLowerCase()] = resolved;
49
+ }
50
+ }
51
+ const timestamp = normalised["x-webhook-timestamp"];
52
+ const signature = normalised["x-webhook-signature"];
53
+ if (!timestamp || !signature) {
54
+ throw new Error(
55
+ "Missing required webhook headers: X-Webhook-Timestamp and X-Webhook-Signature"
56
+ );
57
+ }
58
+ const valid = verifyWebhookSignature({ secret, timestamp, body, signature, toleranceSeconds });
59
+ if (!valid) {
60
+ throw new Error("Invalid webhook signature or expired timestamp");
61
+ }
62
+ return JSON.parse(body);
63
+ }
64
+ // Annotate the CommonJS export names for ESM import in node:
65
+ 0 && (module.exports = {
66
+ parseWebhookEvent,
67
+ verifyWebhookSignature
68
+ });
@@ -0,0 +1,40 @@
1
+ // src/utils/verify-webhook.ts
2
+ import { createHmac, timingSafeEqual } from "crypto";
3
+ function verifyWebhookSignature(params) {
4
+ const { secret, timestamp, body, signature, toleranceSeconds = 300 } = params;
5
+ const ts = Number.parseInt(timestamp, 10);
6
+ if (Number.isNaN(ts)) return false;
7
+ const now = Math.floor(Date.now() / 1e3);
8
+ if (Math.abs(now - ts) > toleranceSeconds) return false;
9
+ const expected = `sha256=${createHmac("sha256", secret).update(`${timestamp}.${body}`).digest("hex")}`;
10
+ const expectedBuf = Buffer.from(expected, "utf-8");
11
+ const actualBuf = Buffer.from(signature, "utf-8");
12
+ if (expectedBuf.length !== actualBuf.length) return false;
13
+ return timingSafeEqual(expectedBuf, actualBuf);
14
+ }
15
+ function parseWebhookEvent(params) {
16
+ const { headers, body, secret, toleranceSeconds } = params;
17
+ const normalised = {};
18
+ for (const [key, value] of Object.entries(headers)) {
19
+ const resolved = Array.isArray(value) ? value[0] : value;
20
+ if (resolved != null) {
21
+ normalised[key.toLowerCase()] = resolved;
22
+ }
23
+ }
24
+ const timestamp = normalised["x-webhook-timestamp"];
25
+ const signature = normalised["x-webhook-signature"];
26
+ if (!timestamp || !signature) {
27
+ throw new Error(
28
+ "Missing required webhook headers: X-Webhook-Timestamp and X-Webhook-Signature"
29
+ );
30
+ }
31
+ const valid = verifyWebhookSignature({ secret, timestamp, body, signature, toleranceSeconds });
32
+ if (!valid) {
33
+ throw new Error("Invalid webhook signature or expired timestamp");
34
+ }
35
+ return JSON.parse(body);
36
+ }
37
+ export {
38
+ parseWebhookEvent,
39
+ verifyWebhookSignature
40
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@phala/cloud",
3
- "version": "0.2.5",
3
+ "version": "0.2.9",
4
4
  "description": "TypeScript SDK for Phala Cloud API",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -11,11 +11,16 @@
11
11
  "import": "./dist/index.mjs",
12
12
  "require": "./dist/index.js"
13
13
  },
14
+ "./webhook": {
15
+ "types": "./dist/webhook.d.ts",
16
+ "import": "./dist/webhook.mjs",
17
+ "require": "./dist/webhook.js"
18
+ },
14
19
  "./package.json": "./package.json"
15
20
  },
16
21
  "files": ["dist/**/*", "!dist/**/*.map", "README.md", "LICENSE"],
17
22
  "scripts": {
18
- "build": "tsup src/index.ts --format cjs,esm && tsc --emitDeclarationOnly --declaration --outDir dist",
23
+ "build": "tsup src/index.ts src/webhook.ts --format cjs,esm && tsc --emitDeclarationOnly --declaration --outDir dist",
19
24
  "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
20
25
  "fmt": "biome format --write",
21
26
  "test": "vitest --run --exclude '**/*.e2e.test.ts'",