@zing-protocol/zing-sdk 0.0.1 → 0.0.2

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.
Files changed (227) hide show
  1. package/dist/_generated/index.js +6 -0
  2. package/dist/_generated/utils/index.d.ts +12 -0
  3. package/dist/_generated/utils/index.js +135 -0
  4. package/dist/_generated/zing_framework/coin_utils.d.ts +15 -0
  5. package/dist/_generated/zing_framework/coin_utils.js +19 -0
  6. package/dist/_generated/zing_framework/deps/sui/object.d.ts +8 -0
  7. package/{src/_generated/zing_governance/deps/sui/object.ts → dist/_generated/zing_framework/deps/sui/object.js} +4 -7
  8. package/dist/_generated/zing_framework/derived_object_bag.d.ts +166 -0
  9. package/dist/_generated/zing_framework/derived_object_bag.js +221 -0
  10. package/dist/_generated/zing_framework/derived_table.d.ts +153 -0
  11. package/dist/_generated/zing_framework/derived_table.js +200 -0
  12. package/dist/_generated/zing_governance/admin.d.ts +20 -0
  13. package/dist/_generated/zing_governance/admin.js +26 -0
  14. package/dist/_generated/zing_governance/deps/sui/object.d.ts +8 -0
  15. package/{src/_generated/zing_identity/deps/sui/object.ts → dist/_generated/zing_governance/deps/sui/object.js} +4 -7
  16. package/dist/_generated/zing_governance/treasury.d.ts +56 -0
  17. package/dist/_generated/zing_governance/treasury.js +78 -0
  18. package/dist/_generated/zing_identity/config.d.ts +87 -0
  19. package/dist/_generated/zing_identity/config.js +98 -0
  20. package/{src/_generated/zing_studio/deps/std/type_name.ts → dist/_generated/zing_identity/deps/std/type_name.d.ts} +3 -11
  21. package/dist/_generated/zing_identity/deps/std/type_name.js +22 -0
  22. package/dist/_generated/zing_identity/deps/sui/object.d.ts +8 -0
  23. package/{src/_generated/zing_studio/deps/sui/object.ts → dist/_generated/zing_identity/deps/sui/object.js} +4 -7
  24. package/dist/_generated/zing_identity/deps/sui/object_table.d.ts +12 -0
  25. package/{src/_generated/zing_identity/deps/sui/object_table.ts → dist/_generated/zing_identity/deps/sui/object_table.js} +7 -10
  26. package/dist/_generated/zing_identity/deps/sui/table.d.ts +12 -0
  27. package/{src/_generated/zing_studio/deps/sui/table.ts → dist/_generated/zing_identity/deps/sui/table.js} +7 -10
  28. package/{src/_generated/zing_identity/deps/sui/vec_set.ts → dist/_generated/zing_identity/deps/sui/vec_set.d.ts} +3 -14
  29. package/dist/_generated/zing_identity/deps/sui/vec_set.js +22 -0
  30. package/dist/_generated/zing_identity/ecdsa.d.ts +81 -0
  31. package/dist/_generated/zing_identity/ecdsa.js +87 -0
  32. package/dist/_generated/zing_identity/identity.d.ts +262 -0
  33. package/dist/_generated/zing_identity/identity.js +294 -0
  34. package/dist/_generated/zing_identity/reclaim.d.ts +356 -0
  35. package/dist/_generated/zing_identity/reclaim.js +414 -0
  36. package/dist/_generated/zing_studio/app.d.ts +351 -0
  37. package/dist/_generated/zing_studio/app.js +451 -0
  38. package/dist/_generated/zing_studio/article.d.ts +331 -0
  39. package/dist/_generated/zing_studio/article.js +331 -0
  40. package/dist/_generated/zing_studio/config.d.ts +203 -0
  41. package/dist/_generated/zing_studio/config.js +233 -0
  42. package/{src/_generated/zing_identity/deps/std/type_name.ts → dist/_generated/zing_studio/deps/std/type_name.d.ts} +3 -11
  43. package/dist/_generated/zing_studio/deps/std/type_name.js +22 -0
  44. package/dist/_generated/zing_studio/deps/sui/bag.d.ts +12 -0
  45. package/{src/_generated/zing_studio/deps/sui/bag.ts → dist/_generated/zing_studio/deps/sui/bag.js} +7 -10
  46. package/dist/_generated/zing_studio/deps/sui/balance.d.ts +12 -0
  47. package/{src/_generated/zing_studio/deps/sui/balance.ts → dist/_generated/zing_studio/deps/sui/balance.js} +4 -7
  48. package/dist/_generated/zing_studio/deps/sui/coin.d.ts +12 -0
  49. package/{src/_generated/zing_studio/deps/sui/coin.ts → dist/_generated/zing_studio/deps/sui/coin.js} +5 -8
  50. package/dist/_generated/zing_studio/deps/sui/dynamic_field.d.ts +126 -0
  51. package/dist/_generated/zing_studio/deps/sui/dynamic_field.js +140 -0
  52. package/dist/_generated/zing_studio/deps/sui/object.d.ts +8 -0
  53. package/{src/_generated/zing_framework/deps/sui/object.ts → dist/_generated/zing_studio/deps/sui/object.js} +4 -7
  54. package/dist/_generated/zing_studio/deps/sui/table.d.ts +12 -0
  55. package/{src/_generated/zing_identity/deps/sui/table.ts → dist/_generated/zing_studio/deps/sui/table.js} +7 -10
  56. package/dist/_generated/zing_studio/deps/sui/vec_map.d.ts +30 -0
  57. package/{src/_generated/zing_studio/deps/sui/vec_map.ts → dist/_generated/zing_studio/deps/sui/vec_map.js} +15 -21
  58. package/{src/_generated/zing_studio/deps/sui/vec_set.ts → dist/_generated/zing_studio/deps/sui/vec_set.d.ts} +3 -14
  59. package/dist/_generated/zing_studio/deps/sui/vec_set.js +22 -0
  60. package/dist/_generated/zing_studio/deps/walrus/blob.d.ts +20 -0
  61. package/{src/_generated/zing_studio/deps/walrus/blob.ts → dist/_generated/zing_studio/deps/walrus/blob.js} +11 -12
  62. package/dist/_generated/zing_studio/deps/walrus/events.d.ts +83 -0
  63. package/dist/_generated/zing_studio/deps/walrus/events.js +122 -0
  64. package/dist/_generated/zing_studio/deps/walrus/metadata.d.ts +71 -0
  65. package/dist/_generated/zing_studio/deps/walrus/metadata.js +73 -0
  66. package/dist/_generated/zing_studio/deps/walrus/storage_resource.d.ts +12 -0
  67. package/{src/_generated/zing_studio/deps/walrus/storage_resource.ts → dist/_generated/zing_studio/deps/walrus/storage_resource.js} +7 -8
  68. package/dist/_generated/zing_studio/deps/zing_framework/derived_object_bag.d.ts +12 -0
  69. package/{src/_generated/zing_studio/deps/zing_framework/derived_object_bag.ts → dist/_generated/zing_studio/deps/zing_framework/derived_object_bag.js} +7 -8
  70. package/dist/_generated/zing_studio/deps/zing_framework/derived_table.d.ts +12 -0
  71. package/{src/_generated/zing_studio/deps/zing_framework/derived_table.ts → dist/_generated/zing_studio/deps/zing_framework/derived_table.js} +7 -8
  72. package/dist/_generated/zing_studio/donation.d.ts +24 -0
  73. package/{src/_generated/zing_studio/donation.ts → dist/_generated/zing_studio/donation.js} +7 -8
  74. package/dist/_generated/zing_studio/enclave.d.ts +187 -0
  75. package/dist/_generated/zing_studio/enclave.js +231 -0
  76. package/dist/_generated/zing_studio/nitro_attestation.d.ts +148 -0
  77. package/dist/_generated/zing_studio/nitro_attestation.js +183 -0
  78. package/dist/_generated/zing_studio/referral.d.ts +166 -0
  79. package/dist/_generated/zing_studio/referral.js +224 -0
  80. package/dist/_generated/zing_studio/storage.d.ts +327 -0
  81. package/dist/_generated/zing_studio/storage.js +365 -0
  82. package/dist/_generated/zing_studio/studio.d.ts +282 -0
  83. package/dist/_generated/zing_studio/studio.js +360 -0
  84. package/dist/bcs.d.ts +61 -0
  85. package/dist/bcs.js +109 -0
  86. package/dist/client/index.d.ts +228 -0
  87. package/dist/client/index.js +296 -0
  88. package/dist/client/types.d.ts +23 -0
  89. package/dist/client/types.js +1 -0
  90. package/dist/components/index.js +3 -0
  91. package/dist/components/wallet-provider.d.ts +7 -0
  92. package/dist/components/wallet-provider.js +33 -0
  93. package/dist/components/walrus-provider.d.ts +31 -0
  94. package/dist/components/walrus-provider.js +71 -0
  95. package/dist/components/zing-provider.d.ts +15 -0
  96. package/dist/components/zing-provider.js +22 -0
  97. package/dist/config/common.d.ts +23 -0
  98. package/dist/config/common.js +61 -0
  99. package/dist/config/index.d.ts +8 -0
  100. package/dist/config/index.js +28 -0
  101. package/dist/config/mainnet.d.ts +6 -0
  102. package/dist/config/mainnet.js +80 -0
  103. package/dist/config/testnet.d.ts +6 -0
  104. package/dist/config/testnet.js +81 -0
  105. package/dist/config/types.d.ts +49 -0
  106. package/dist/config/types.js +1 -0
  107. package/dist/const.d.ts +4 -0
  108. package/dist/const.js +25 -0
  109. package/dist/getters.d.ts +228 -0
  110. package/dist/getters.js +317 -0
  111. package/{src/hooks/index.ts → dist/hooks/index.d.ts} +2 -3
  112. package/dist/hooks/index.js +9 -0
  113. package/dist/hooks/useGetStudio.d.ts +26 -0
  114. package/dist/hooks/useGetStudio.js +17 -0
  115. package/dist/hooks/useGetTierPlan.d.ts +7 -0
  116. package/dist/hooks/useGetTierPlan.js +85 -0
  117. package/dist/hooks/useReclaim.d.ts +12 -0
  118. package/dist/hooks/useReclaim.js +87 -0
  119. package/dist/hooks/useSignAndExecuteTransaction.d.ts +6 -0
  120. package/dist/hooks/useSignAndExecuteTransaction.js +40 -0
  121. package/dist/hooks/useSignPersonalMessage.d.ts +8 -0
  122. package/dist/hooks/useSignPersonalMessage.js +21 -0
  123. package/dist/hooks/useZingInfiniteQueries.d.ts +75 -0
  124. package/dist/hooks/useZingInfiniteQueries.js +28 -0
  125. package/dist/hooks/useZingMutation.d.ts +35 -0
  126. package/dist/hooks/useZingMutation.js +37 -0
  127. package/dist/hooks/useZingQueries.d.ts +127 -0
  128. package/dist/hooks/useZingQueries.js +37 -0
  129. package/dist/hooks/useZingQuery.d.ts +147 -0
  130. package/dist/hooks/useZingQuery.js +34 -0
  131. package/{src/index.ts → dist/index.d.ts} +2 -3
  132. package/dist/index.js +9 -0
  133. package/dist/lib/utils/colorful-logger.d.ts +2 -0
  134. package/dist/lib/utils/colorful-logger.js +23 -0
  135. package/dist/lib/utils/const.d.ts +25 -0
  136. package/dist/lib/utils/const.js +25 -0
  137. package/dist/lib/utils/helpers.d.ts +10 -0
  138. package/dist/lib/utils/helpers.js +49 -0
  139. package/dist/lib/utils/index.js +4 -0
  140. package/dist/lib/utils/init-app-with-shadow.d.ts +6 -0
  141. package/dist/lib/utils/init-app-with-shadow.js +28 -0
  142. package/dist/lib/utils/quilt.d.ts +61 -0
  143. package/dist/lib/utils/quilt.js +194 -0
  144. package/dist/lib/utils/reclaim.d.ts +29 -0
  145. package/dist/lib/utils/reclaim.js +88 -0
  146. package/dist/lib/utils/types.d.ts +5 -0
  147. package/dist/lib/utils/types.js +1 -0
  148. package/dist/mutations/index.js +3 -0
  149. package/dist/mutations/seal.d.ts +12 -0
  150. package/dist/mutations/seal.js +41 -0
  151. package/dist/mutations/signer.d.ts +10 -0
  152. package/dist/mutations/signer.js +176 -0
  153. package/dist/mutations/walrus.d.ts +98 -0
  154. package/dist/mutations/walrus.js +488 -0
  155. package/dist/stores/index.js +3 -0
  156. package/dist/stores/walletStore.d.ts +48 -0
  157. package/dist/stores/walletStore.js +232 -0
  158. package/dist/stores/walrusStore.d.ts +28 -0
  159. package/dist/stores/walrusStore.js +61 -0
  160. package/dist/stores/zkloginStore.d.ts +28 -0
  161. package/dist/stores/zkloginStore.js +27 -0
  162. package/dist/types.d.ts +99 -0
  163. package/dist/types.js +1 -0
  164. package/package.json +14 -6
  165. package/src/_generated/utils/index.ts +0 -181
  166. package/src/_generated/zing_framework/coin_utils.ts +0 -41
  167. package/src/_generated/zing_framework/derived_object_bag.ts +0 -400
  168. package/src/_generated/zing_framework/derived_table.ts +0 -366
  169. package/src/_generated/zing_governance/admin.ts +0 -47
  170. package/src/_generated/zing_governance/treasury.ts +0 -134
  171. package/src/_generated/zing_identity/config.ts +0 -191
  172. package/src/_generated/zing_identity/ecdsa.ts +0 -166
  173. package/src/_generated/zing_identity/identity.ts +0 -661
  174. package/src/_generated/zing_identity/reclaim.ts +0 -803
  175. package/src/_generated/zing_studio/app.ts +0 -855
  176. package/src/_generated/zing_studio/article.ts +0 -598
  177. package/src/_generated/zing_studio/config.ts +0 -475
  178. package/src/_generated/zing_studio/deps/sui/dynamic_field.ts +0 -272
  179. package/src/_generated/zing_studio/deps/walrus/events.ts +0 -124
  180. package/src/_generated/zing_studio/deps/walrus/metadata.ts +0 -137
  181. package/src/_generated/zing_studio/enclave.ts +0 -466
  182. package/src/_generated/zing_studio/nitro_attestation.ts +0 -308
  183. package/src/_generated/zing_studio/referral.ts +0 -447
  184. package/src/_generated/zing_studio/storage.ts +0 -664
  185. package/src/_generated/zing_studio/studio.ts +0 -682
  186. package/src/bcs.ts +0 -139
  187. package/src/client/index.ts +0 -557
  188. package/src/client/types.ts +0 -28
  189. package/src/components/wallet-provider.tsx +0 -53
  190. package/src/components/walrus-provider.tsx +0 -212
  191. package/src/components/zing-provider.tsx +0 -54
  192. package/src/config/common.ts +0 -82
  193. package/src/config/index.ts +0 -45
  194. package/src/config/mainnet.ts +0 -109
  195. package/src/config/testnet.ts +0 -111
  196. package/src/config/types.ts +0 -69
  197. package/src/const.ts +0 -32
  198. package/src/getters.ts +0 -523
  199. package/src/hooks/useGetStudio.ts +0 -22
  200. package/src/hooks/useGetTierPlan.ts +0 -115
  201. package/src/hooks/useReclaim.ts +0 -165
  202. package/src/hooks/useSignAndExecuteTransaction.ts +0 -57
  203. package/src/hooks/useSignPersonalMessage.ts +0 -28
  204. package/src/hooks/useZingInfiniteQueries.ts +0 -92
  205. package/src/hooks/useZingMutation.ts +0 -66
  206. package/src/hooks/useZingQueries.ts +0 -107
  207. package/src/hooks/useZingQuery.ts +0 -88
  208. package/src/lib/utils/colorful-logger.ts +0 -27
  209. package/src/lib/utils/const.ts +0 -25
  210. package/src/lib/utils/helpers.ts +0 -78
  211. package/src/lib/utils/init-app-with-shadow.ts +0 -42
  212. package/src/lib/utils/quilt.ts +0 -317
  213. package/src/lib/utils/reclaim.ts +0 -148
  214. package/src/lib/utils/types.ts +0 -14
  215. package/src/mutations/seal.ts +0 -78
  216. package/src/mutations/signer.ts +0 -241
  217. package/src/mutations/walrus.ts +0 -862
  218. package/src/stores/walletStore.ts +0 -360
  219. package/src/stores/walrusStore.ts +0 -118
  220. package/src/stores/zkloginStore.ts +0 -53
  221. package/src/types.ts +0 -120
  222. package/tsconfig.json +0 -12
  223. /package/{src/_generated/index.js → dist/_generated/index.d.ts} +0 -0
  224. /package/{src/components/index.ts → dist/components/index.d.ts} +0 -0
  225. /package/{src/lib/utils/index.ts → dist/lib/utils/index.d.ts} +0 -0
  226. /package/{src/mutations/index.ts → dist/mutations/index.d.ts} +0 -0
  227. /package/{src/stores/index.ts → dist/stores/index.d.ts} +0 -0
@@ -1,88 +0,0 @@
1
- import { useZingClient } from "../components/zing-provider.js";
2
- import { useQuery } from "@tanstack/react-query";
3
- import type { ZingClient } from "../client/index.js";
4
- import type { SuiNetwork } from "../types.js";
5
- import type { UseQueryOptions } from "@tanstack/react-query";
6
-
7
- // ---------------------------------------------
8
- // Method map
9
- // ---------------------------------------------
10
- const zingQueryMethodMap = (zingClient: ZingClient) => ({
11
- getIdentifierByAddress: zingClient.getIdentifierByAddress.bind(zingClient),
12
- getIdentifierInfoByIdentifier:
13
- zingClient.getIdentifierInfoByIdentifier.bind(zingClient),
14
- getStudio: zingClient.getStudio.bind(zingClient),
15
- getStorageSpace: zingClient.getStorageSpace.bind(zingClient),
16
- getStorageTreasury: zingClient.getStorageTreasury.bind(zingClient),
17
- getMember: zingClient.getMember.bind(zingClient),
18
- getBalance: zingClient.getBalance.bind(zingClient),
19
- getWork: zingClient.getWork.bind(zingClient),
20
- });
21
-
22
- type ZingQueryMethods = ReturnType<typeof zingQueryMethodMap>;
23
-
24
- type ExtractParams<T> = T extends (...args: infer P) => any ? P : never;
25
-
26
- type MethodParamsMap = {
27
- [K in keyof ZingQueryMethods]: ExtractParams<ZingQueryMethods[K]>;
28
- };
29
-
30
- type ExtractReturnType<T> = T extends (...args: any[]) => infer R ? R : never;
31
-
32
- type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
33
-
34
- type MethodReturnMap = {
35
- [K in keyof ZingQueryMethods]: UnwrapPromise<
36
- ExtractReturnType<ZingQueryMethods[K]>
37
- >;
38
- };
39
-
40
- type UseZingQueryParams<
41
- K extends keyof ZingQueryMethods = keyof ZingQueryMethods,
42
- > = {
43
- method: K;
44
- params: MethodParamsMap[K];
45
- };
46
-
47
- // ---------------------------------------------
48
- // Hook
49
- // ---------------------------------------------
50
-
51
- type ExtraQueryOptions<TData> = Omit<
52
- UseQueryOptions<TData, Error, TData, readonly unknown[]>,
53
- "queryKey" | "queryFn"
54
- >;
55
-
56
- export function getZingQueryKey<K extends keyof ZingQueryMethods>(
57
- network: SuiNetwork,
58
- method: K,
59
- memo: string[],
60
- ): readonly [string, K, string[]] {
61
- return [network, method, memo] as const;
62
- }
63
-
64
- export function useZingQuery<K extends keyof ZingQueryMethods>(
65
- input: UseZingQueryParams<K>,
66
- options: ExtraQueryOptions<MethodReturnMap[K]> = {},
67
- memo: string[] = [],
68
- ) {
69
- const zingClient = useZingClient();
70
- const methodMap = zingQueryMethodMap(zingClient);
71
-
72
- const { method, params } = input;
73
-
74
- return useQuery<MethodReturnMap[K]>({
75
- queryKey: [zingClient.network, method, memo],
76
- gcTime: options.gcTime,
77
- staleTime: options.staleTime,
78
- enabled: options.enabled,
79
- retry: options.retry,
80
-
81
- queryFn: async () => {
82
- const fn = methodMap[method];
83
- return (fn as (...args: MethodParamsMap[K]) => MethodReturnMap[K])(
84
- ...params,
85
- );
86
- },
87
- });
88
- }
@@ -1,27 +0,0 @@
1
- import { COLORS } from "./const.js";
2
- import type { ColorType, ValueOf } from "./types.js";
3
-
4
- export const colorfulLog = (message: string, type: ColorType) => {
5
- let color: ValueOf<typeof COLORS>;
6
-
7
- switch (type) {
8
- case "success":
9
- color = COLORS.FgGreen;
10
- break;
11
- case "info":
12
- color = COLORS.FgBlue;
13
- break;
14
- case "error":
15
- color = COLORS.FgRed;
16
- break;
17
- case "warning":
18
- color = COLORS.FgYellow;
19
- break;
20
- default:
21
- color = COLORS[type];
22
- break;
23
- }
24
-
25
- console.info(color, message);
26
- console.info(COLORS["Reset"]);
27
- };
@@ -1,25 +0,0 @@
1
- export const COLORS = {
2
- Reset: "\x1b[0m",
3
- Bright: "\x1b[1m",
4
- Dim: "\x1b[2m",
5
- Underscore: "\x1b[4m",
6
- Blink: "\x1b[5m",
7
- Reverse: "\x1b[7m",
8
- Hidden: "\x1b[8m",
9
- FgBlack: "\x1b[30m",
10
- FgRed: "\x1b[31m",
11
- FgGreen: "\x1b[32m",
12
- FgYellow: "\x1b[33m",
13
- FgBlue: "\x1b[34m",
14
- FgMagenta: "\x1b[35m",
15
- FgCyan: "\x1b[36m",
16
- FgWhite: "\x1b[37m",
17
- BgBlack: "\x1b[40m",
18
- BgRed: "\x1b[41m",
19
- BgGreen: "\x1b[42m",
20
- BgYellow: "\x1b[43m",
21
- BgBlue: "\x1b[44m",
22
- BgMagenta: "\x1b[45m",
23
- BgCyan: "\x1b[46m",
24
- BgWhite: "\x1b[47m",
25
- } as const;
@@ -1,78 +0,0 @@
1
- import type { ExcludeValuesFromBaseArrayType } from "./types.js";
2
-
3
- export const excludeValuesFromBaseArray = <
4
- B extends string[],
5
- E extends (string | number)[],
6
- >(
7
- baseArray: B,
8
- excludeArray: E,
9
- ) =>
10
- baseArray.filter(
11
- (value) => !excludeArray.includes(value),
12
- ) as ExcludeValuesFromBaseArrayType<B, E>;
13
-
14
- export const sleep = async (time: number) =>
15
- new Promise((r) => setTimeout(r, time));
16
-
17
- export const formatDate = (date: Date) =>
18
- date.toLocaleDateString("en-US", {
19
- year: "numeric",
20
- month: "short",
21
- day: "numeric",
22
- });
23
-
24
- export const formatBalance = (
25
- balance: string | number | bigint,
26
- decimal: number,
27
- digits: number = 4,
28
- ): string => {
29
- const normalizedBalance = Number(balance) / Math.pow(10, decimal);
30
-
31
- const fixed = normalizedBalance.toFixed(digits);
32
-
33
- return fixed.replace(/\.?0+$/, "");
34
- };
35
-
36
- export const formatTimeAgo = (date: Date) => {
37
- const now = new Date();
38
- const diffInHours = Math.floor(
39
- (now.getTime() - date.getTime()) / (1000 * 60 * 60),
40
- );
41
-
42
- if (diffInHours < 1) return "Just now";
43
- if (diffInHours < 24) return `${diffInHours}h ago`;
44
- if (diffInHours < 168) return `${Math.floor(diffInHours / 24)}d ago`;
45
- return formatDate(date);
46
- };
47
-
48
- export function formatDaysAuto(days: number): string {
49
- if (days < 30) return `${days} day${days === 1 ? "" : "s"}`;
50
-
51
- const months = days / 30;
52
- if (months < 12) {
53
- const rounded = Math.floor(months);
54
- return `${rounded} month${rounded === 1 ? "" : "s"}`;
55
- }
56
-
57
- const years = Math.floor(days / 365);
58
- return `${years} year${years === 1 ? "" : "s"}`;
59
- }
60
-
61
- export const formatBytes = (bytes: number): string => {
62
- if (bytes === 0) return "0 B";
63
-
64
- const k = 1024;
65
- const sizes = ["B", "KB", "MB", "GB", "TB"];
66
- const i = Math.floor(Math.log(bytes) / Math.log(k));
67
-
68
- return `${(bytes / Math.pow(k, i)).toFixed(2)} ${sizes[i]}`;
69
- };
70
-
71
- export const getDaysRemaining = (comp: number) => {
72
- const now = new Date();
73
- const diffTime = comp - now.getTime();
74
- const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
75
- return diffDays;
76
- };
77
-
78
- export const bytesToGB = (bytes: number) => bytes / (1024 * 1024 * 1024);
@@ -1,42 +0,0 @@
1
- import { createRoot } from "react-dom/client";
2
- import type { ReactElement } from "react";
3
-
4
- export const initAppWithShadow = ({
5
- id,
6
- app,
7
- inlineCss,
8
- }: {
9
- id: string;
10
- inlineCss: string;
11
- app: ReactElement;
12
- }) => {
13
- const root = document.createElement("div");
14
- root.id = id;
15
-
16
- document.body.append(root);
17
-
18
- const rootIntoShadow = document.createElement("div");
19
- rootIntoShadow.id = `shadow-root-${id}`;
20
-
21
- const shadowRoot = root.attachShadow({ mode: "open" });
22
-
23
- if (navigator.userAgent.includes("Firefox")) {
24
- /**
25
- * In the firefox environment, adoptedStyleSheets cannot be used due to the bug
26
- * @url https://bugzilla.mozilla.org/show_bug.cgi?id=1770592
27
- *
28
- * Injecting styles into the document, this may cause style conflicts with the host page
29
- */
30
- const styleElement = document.createElement("style");
31
- styleElement.innerHTML = inlineCss;
32
- shadowRoot.appendChild(styleElement);
33
- } else {
34
- /** Inject styles into shadow dom */
35
- const globalStyleSheet = new CSSStyleSheet();
36
- globalStyleSheet.replaceSync(inlineCss);
37
- shadowRoot.adoptedStyleSheets = [globalStyleSheet];
38
- }
39
-
40
- shadowRoot.appendChild(rootIntoShadow);
41
- createRoot(rootIntoShadow).render(app);
42
- };
@@ -1,317 +0,0 @@
1
- import { QuiltIndexV1, QuiltPatchTags } from "../../bcs.js";
2
- import { getSourceSymbols } from "../../getters.js";
3
- import { bcs } from "@mysten/sui/bcs";
4
- import type { EncodingType } from "@mysten/walrus";
5
-
6
- const MAX_NUM_SLIVERS_FOR_QUILT_INDEX = 10;
7
- const QUILT_INDEX_SIZE_BYTES_LENGTH = 4;
8
- const QUILT_VERSION_BYTES_LENGTH = 1;
9
- const QUILT_INDEX_PREFIX_SIZE =
10
- QUILT_VERSION_BYTES_LENGTH + QUILT_INDEX_SIZE_BYTES_LENGTH;
11
- const QUILT_PATCH_BLOB_HEADER_SIZE = 1 + 4 + 1; // bcs length of QuiltPatchBlobHeader
12
-
13
- const BLOB_IDENTIFIER_SIZE_BYTES_LENGTH = 2;
14
- const TAGS_SIZE_BYTES_LENGTH = 2;
15
- const MAX_BLOB_IDENTIFIER_BYTES_LENGTH =
16
- (1 << (8 * BLOB_IDENTIFIER_SIZE_BYTES_LENGTH)) - 1;
17
- const REQUIRED_ALIGNMENT_BY_ENCODING_TYPE = {
18
- RS2: 2,
19
- RedStuff: 2,
20
- };
21
-
22
- const MAX_SYMBOL_SIZE_BY_ENCODING_TYPE = {
23
- RS2: 2 ** 16 - 1,
24
- RedStuff: 2 ** 16 - 1,
25
- };
26
-
27
- function canBlobsFitIntoMatrix(
28
- blobsSizes: number[],
29
- nColumns: number,
30
- columnSize: number,
31
- ): boolean {
32
- return (
33
- blobsSizes.reduce((acc, size) => acc + Math.ceil(size / columnSize), 0) <=
34
- nColumns
35
- );
36
- }
37
-
38
- // Add these constants at the top of your quilt-size.ts file
39
- const DIGEST_LEN = 32;
40
- const BLOB_ID_LEN = 32;
41
-
42
- /**
43
- * Calculate the encoded blob length including all overhead.
44
- * This matches the Walrus SDK's encodedBlobLength function.
45
- */
46
- function encodedBlobLength(
47
- unencodedLength: number,
48
- nShards: number,
49
- encodingType: EncodingType = "RS2",
50
- ): number {
51
- const sliverSize = encodedSliverSize(unencodedLength, nShards, encodingType);
52
- const metadata = nShards * DIGEST_LEN * 2 + BLOB_ID_LEN;
53
- return nShards * metadata + sliverSize;
54
- }
55
-
56
- /**
57
- * Calculate the encoded sliver size.
58
- * This matches the Walrus SDK's encodedSliverSize function.
59
- */
60
- function encodedSliverSize(
61
- unencodedLength: number,
62
- nShards: number,
63
- encodingType: EncodingType = "RS2",
64
- ): number {
65
- const { primarySymbols, secondarySymbols } = getSourceSymbols(
66
- nShards,
67
- encodingType,
68
- );
69
-
70
- let symbolSize =
71
- Math.floor(
72
- (Math.max(unencodedLength, 1) - 1) / (primarySymbols * secondarySymbols),
73
- ) + 1;
74
-
75
- if (encodingType === "RS2" && symbolSize % 2 === 1) {
76
- symbolSize = symbolSize + 1;
77
- }
78
-
79
- const singleShardSize = (primarySymbols + secondarySymbols) * symbolSize;
80
-
81
- return singleShardSize * nShards;
82
- }
83
-
84
- export interface QuiltSizeInput {
85
- blobs: {
86
- size: number; // Size of the blob content in bytes
87
- identifier: string;
88
- tags?: Record<string, string>;
89
- }[];
90
- numShards: number;
91
- encodingType?: EncodingType;
92
- }
93
-
94
- export interface QuiltSizeResult {
95
- quiltSize: number; // Total size of the quilt in bytes
96
- symbolSize: number; // Size of each symbol
97
- numRows: number;
98
- numColumns: number;
99
- indexSize: number;
100
- blobSizes: number[]; // Size of each blob including metadata
101
- }
102
-
103
- /**
104
- * Finds the minimum symbol size needed to store blobs in a fixed number of columns.
105
- * Each blob must be stored in consecutive columns exclusively.
106
- *
107
- * A binary search is used to find the minimum symbol size:
108
- * 1. Compute the upper and lower bounds for the symbol size.
109
- * 2. Check if the all the blobs can be fit into the quilt with the current symbol size.
110
- * 3. Adjust the bounds based on the result and repeat until the symbol size is found.
111
- *
112
- * @param blobsSizes - Slice of blob lengths, including the index size as the first element.
113
- * Note that the len of the blob_size should be between 1 and n_columns.
114
- * @param nColumns - Number of columns available.
115
- * @param nRows - Number of rows available.
116
- * @param maxNumColumnsForQuiltIndex - The maximum number of columns that can be used to
117
- * store the quilt index.
118
- * @param encodingType - The encoding type to use.
119
- *
120
- * @returns The minimum symbol size needed.
121
- **/
122
- export function computeSymbolSize(
123
- blobsSizes: number[],
124
- nColumns: number,
125
- nRows: number,
126
- maxNumColumnsForQuiltIndex: number,
127
- encodingType: EncodingType = "RS2",
128
- ): number {
129
- if (blobsSizes.length > nColumns) {
130
- throw new Error(
131
- "Too many blobs, the number of blobs must be less than the number of columns",
132
- );
133
- }
134
-
135
- if (blobsSizes.length === 0) {
136
- throw new Error("No blobs provided");
137
- }
138
-
139
- let minVal = Math.max(
140
- blobsSizes.reduce((acc, size) => acc + size, 0) / (nColumns * nRows),
141
- blobsSizes[0] / (nRows * maxNumColumnsForQuiltIndex),
142
- Math.ceil(QUILT_INDEX_PREFIX_SIZE / nRows),
143
- );
144
-
145
- let maxVal = Math.ceil(
146
- (Math.max(...blobsSizes) / (nColumns / blobsSizes.length)) * nRows,
147
- );
148
-
149
- while (minVal < maxVal) {
150
- const mid = (minVal + maxVal) / 2;
151
- if (canBlobsFitIntoMatrix(blobsSizes, nColumns, mid * nRows)) {
152
- maxVal = mid;
153
- } else {
154
- minVal = mid + 1;
155
- }
156
- }
157
-
158
- const symbolSize =
159
- Math.ceil(minVal / REQUIRED_ALIGNMENT_BY_ENCODING_TYPE[encodingType]) *
160
- REQUIRED_ALIGNMENT_BY_ENCODING_TYPE[encodingType];
161
-
162
- if (!canBlobsFitIntoMatrix(blobsSizes, nColumns, symbolSize * nRows)) {
163
- throw new Error("Quilt oversize");
164
- }
165
-
166
- if (symbolSize > MAX_SYMBOL_SIZE_BY_ENCODING_TYPE[encodingType]) {
167
- throw new Error(
168
- `Quilt oversize: the resulting symbol size ${symbolSize} is larger than the maximum symbol size ${MAX_SYMBOL_SIZE_BY_ENCODING_TYPE[encodingType]}; remove some blobs`,
169
- );
170
- }
171
-
172
- return symbolSize;
173
- }
174
-
175
- /**
176
- * Calculate the quilt size without actually encoding the data.
177
- * This is useful for estimating costs before uploading.
178
- *
179
- * @param input - Configuration with blob sizes, identifiers, tags, and encoding params
180
- * @returns Information about the resulting quilt size and structure
181
- */
182
- export function calculateQuiltSize(input: QuiltSizeInput): QuiltSizeResult {
183
- const { blobs, numShards, encodingType = "RS2" } = input;
184
-
185
- const { primarySymbols: nRows, secondarySymbols: nCols } = getSourceSymbols(
186
- numShards,
187
- encodingType,
188
- );
189
-
190
- // Sort blobs by identifier (same as encoding logic)
191
- const sortedBlobs = [...blobs].sort((a, b) =>
192
- a.identifier < b.identifier ? -1 : 1,
193
- );
194
-
195
- // Validate identifiers
196
- const identifiers = new Set<string>();
197
- for (const blob of sortedBlobs) {
198
- if (identifiers.has(blob.identifier)) {
199
- throw new Error(`Duplicate blob identifier: ${blob.identifier}`);
200
- }
201
- if (blob.identifier.length > MAX_BLOB_IDENTIFIER_BYTES_LENGTH) {
202
- throw new Error(`Blob identifier too long: ${blob.identifier}`);
203
- }
204
- identifiers.add(blob.identifier);
205
- }
206
-
207
- // Build index structure (same as encoding)
208
- const index = {
209
- patches: sortedBlobs.map((blob) => ({
210
- startIndex: 0,
211
- endIndex: 0,
212
- identifier: blob.identifier,
213
- tags: blob.tags ?? {},
214
- })),
215
- };
216
-
217
- // Calculate index size
218
- const indexSize =
219
- QUILT_INDEX_PREFIX_SIZE + QuiltIndexV1.serialize(index).toBytes().length;
220
-
221
- // Calculate metadata sizes
222
- const blobMetadataSizes = sortedBlobs.map((blob) => {
223
- const identifierBytes = bcs.string().serialize(blob.identifier).toBytes();
224
- let metadataSize =
225
- QUILT_PATCH_BLOB_HEADER_SIZE +
226
- BLOB_IDENTIFIER_SIZE_BYTES_LENGTH +
227
- identifierBytes.length;
228
-
229
- if (blob.tags && Object.keys(blob.tags).length > 0) {
230
- const tagsBytes = QuiltPatchTags.serialize(blob.tags).toBytes();
231
- metadataSize += TAGS_SIZE_BYTES_LENGTH + tagsBytes.length;
232
- }
233
-
234
- return metadataSize;
235
- });
236
-
237
- // Calculate total blob sizes (metadata + content)
238
- const blobSizes = [
239
- indexSize,
240
- ...sortedBlobs.map((blob, i) => blobMetadataSizes[i] + blob.size),
241
- ];
242
-
243
- // Compute symbol size
244
- const symbolSize = computeSymbolSize(
245
- blobSizes,
246
- nCols,
247
- nRows,
248
- MAX_NUM_SLIVERS_FOR_QUILT_INDEX,
249
- encodingType,
250
- );
251
-
252
- const columnSize = symbolSize * nRows;
253
-
254
- // Calculate how many columns are actually used
255
- const indexColumnsNeeded = Math.ceil(indexSize / columnSize);
256
- let currentColumn = indexColumnsNeeded;
257
-
258
- for (const blobSize of blobSizes.slice(1)) {
259
- // Skip index (it's first in blobSizes)
260
- const columnsNeeded = Math.ceil(blobSize / columnSize);
261
- currentColumn += columnsNeeded;
262
- }
263
-
264
- // The actual unencoded size is based on COLUMNS USED, not all columns
265
- // Each column is columnSize bytes tall
266
- const actualUnencodedQuiltSize = currentColumn * columnSize;
267
-
268
- // Now use the ACTUAL size for encoding calculation
269
- const encodedQuiltSize = encodedBlobLength(
270
- actualUnencodedQuiltSize,
271
- numShards,
272
- encodingType,
273
- );
274
-
275
- return {
276
- quiltSize: encodedQuiltSize,
277
- symbolSize,
278
- numRows: nRows,
279
- numColumns: nCols,
280
- indexSize,
281
- blobSizes,
282
- };
283
- }
284
-
285
- /**
286
- * Calculate quilt size accounting for encryption overhead.
287
- * AES-GCM adds 16 bytes of authentication tag.
288
- *
289
- * @param files - Array of files with original sizes
290
- * @param numShards - Number of shards
291
- * @param ivSize - Size of IV (typically 12 bytes for GCM)
292
- * @param encodingType - Encoding type
293
- * @returns Quilt size information
294
- */
295
- export function calculateEncryptedQuiltSize(
296
- files: Array<{
297
- size: number;
298
- identifier: string;
299
- tags?: Record<string, string>;
300
- }>,
301
- numShards: number,
302
- ivSize: number = 12,
303
- encodingType?: EncodingType,
304
- ): QuiltSizeResult {
305
- const GCM_TAG_SIZE = 16; // AES-GCM authentication tag
306
-
307
- return calculateQuiltSize({
308
- blobs: files.map((file) => ({
309
- // Account for IV + encrypted data + GCM tag
310
- size: ivSize + file.size + GCM_TAG_SIZE,
311
- identifier: file.identifier,
312
- tags: file.tags,
313
- })),
314
- numShards,
315
- encodingType,
316
- });
317
- }