@shelby-protocol/react 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 (46) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +104 -0
  3. package/dist/index.cjs +2 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.ts +3 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +2 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/mutations/index.d.ts +5 -0
  10. package/dist/mutations/index.d.ts.map +1 -0
  11. package/dist/mutations/useCommitBlobs.d.ts +50 -0
  12. package/dist/mutations/useCommitBlobs.d.ts.map +1 -0
  13. package/dist/mutations/useEncodeBlobs.d.ts +78 -0
  14. package/dist/mutations/useEncodeBlobs.d.ts.map +1 -0
  15. package/dist/mutations/useRegisterCommitments.d.ts +69 -0
  16. package/dist/mutations/useRegisterCommitments.d.ts.map +1 -0
  17. package/dist/mutations/useUploadBlobs.d.ts +79 -0
  18. package/dist/mutations/useUploadBlobs.d.ts.map +1 -0
  19. package/dist/queries/index.d.ts +3 -0
  20. package/dist/queries/index.d.ts.map +1 -0
  21. package/dist/queries/useAccountBlobs.d.ts +30 -0
  22. package/dist/queries/useAccountBlobs.d.ts.map +1 -0
  23. package/dist/queries/useBlobMetadata.d.ts +29 -0
  24. package/dist/queries/useBlobMetadata.d.ts.map +1 -0
  25. package/dist/types/mutations.d.ts +6 -0
  26. package/dist/types/mutations.d.ts.map +1 -0
  27. package/dist/types/queries.d.ts +6 -0
  28. package/dist/types/queries.d.ts.map +1 -0
  29. package/dist/types/signers.d.ts +9 -0
  30. package/dist/types/signers.d.ts.map +1 -0
  31. package/dist/types/walletAdapter.d.ts +3 -0
  32. package/dist/types/walletAdapter.d.ts.map +1 -0
  33. package/package.json +80 -0
  34. package/src/index.ts +2 -0
  35. package/src/mutations/index.ts +4 -0
  36. package/src/mutations/useCommitBlobs.tsx +83 -0
  37. package/src/mutations/useEncodeBlobs.tsx +122 -0
  38. package/src/mutations/useRegisterCommitments.tsx +129 -0
  39. package/src/mutations/useUploadBlobs.tsx +194 -0
  40. package/src/queries/index.ts +2 -0
  41. package/src/queries/useAccountBlobs.tsx +72 -0
  42. package/src/queries/useBlobMetadata.tsx +56 -0
  43. package/src/types/mutations.ts +16 -0
  44. package/src/types/queries.ts +16 -0
  45. package/src/types/signers.ts +11 -0
  46. package/src/types/walletAdapter.ts +4 -0
@@ -0,0 +1,29 @@
1
+ import type { Network } from "@aptos-labs/ts-sdk";
2
+ import type { BlobMetadata, ShelbyBlobClient } from "@shelby-protocol/sdk/browser";
3
+ import type { UseQueryOptionsWithClient } from "../types/queries";
4
+ export declare const getUseBlobMetadataQueryKey: (params: Parameters<ShelbyBlobClient["getBlobMetadata"]>[0] & {
5
+ network: Network;
6
+ }) => string[];
7
+ export type UseBlobMetadataOptions = UseQueryOptionsWithClient<BlobMetadata | null> & Parameters<ShelbyBlobClient["getBlobMetadata"]>[0];
8
+ /**
9
+ * Queries the metadata for a specific blob.
10
+ *
11
+ * This query fetches the metadata for a single blob identified by account and blob name.
12
+ * Returns `null` if the blob is not found.
13
+ *
14
+ * @example
15
+ * ```tsx
16
+ * import { ShelbyClient } from "@shelby-protocol/sdk/browser";
17
+ * import { Network } from "@aptos-labs/ts-sdk";
18
+ * import { useBlobMetadata } from "@shelby-protocol/react";
19
+ *
20
+ * const shelbyClient = new ShelbyClient({ network: Network.SHELBYNET });
21
+ * const { data: metadata } = useBlobMetadata({
22
+ * client: shelbyClient,
23
+ * account: '0x123...',
24
+ * name: 'file1.txt',
25
+ * });
26
+ * ```
27
+ */
28
+ export declare function useBlobMetadata({ account, name, client: shelbyClient, ...options }: UseBlobMetadataOptions): import("@tanstack/react-query").UseQueryResult<BlobMetadata | null, Error>;
29
+ //# sourceMappingURL=useBlobMetadata.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useBlobMetadata.d.ts","sourceRoot":"","sources":["../../src/queries/useBlobMetadata.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,KAAK,EACV,YAAY,EACZ,gBAAgB,EACjB,MAAM,8BAA8B,CAAC;AAEtC,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,kBAAkB,CAAC;AAElE,eAAO,MAAM,0BAA0B,GACrC,QAAQ,UAAU,CAAC,gBAAgB,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG;IAC3D,OAAO,EAAE,OAAO,CAAC;CAClB,aAC2E,CAAC;AAE/E,MAAM,MAAM,sBAAsB,GAChC,yBAAyB,CAAC,YAAY,GAAG,IAAI,CAAC,GAC5C,UAAU,CAAC,gBAAgB,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,eAAe,CAAC,EAC9B,OAAO,EACP,IAAI,EACJ,MAAM,EAAE,YAAY,EACpB,GAAG,OAAO,EACX,EAAE,sBAAsB,8EAYxB"}
@@ -0,0 +1,6 @@
1
+ import type { ShelbyClient } from "@shelby-protocol/sdk/browser";
2
+ import type { DefaultError, MutationKey, UseMutationOptions } from "@tanstack/react-query";
3
+ export type UseMutationOptionsWithClient<TMutationFnData = unknown, TError = DefaultError, TData = TMutationFnData, TMutationKey extends MutationKey = MutationKey> = Omit<UseMutationOptions<TMutationFnData, TError, TData, TMutationKey>, "mutationFn"> & {
4
+ client: ShelbyClient;
5
+ };
6
+ //# sourceMappingURL=mutations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mutations.d.ts","sourceRoot":"","sources":["../../src/types/mutations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,KAAK,EACV,YAAY,EACZ,WAAW,EACX,kBAAkB,EACnB,MAAM,uBAAuB,CAAC;AAE/B,MAAM,MAAM,4BAA4B,CACtC,eAAe,GAAG,OAAO,EACzB,MAAM,GAAG,YAAY,EACrB,KAAK,GAAG,eAAe,EACvB,YAAY,SAAS,WAAW,GAAG,WAAW,IAC5C,IAAI,CACN,kBAAkB,CAAC,eAAe,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,CAAC,EAChE,YAAY,CACb,GAAG;IAAE,MAAM,EAAE,YAAY,CAAA;CAAE,CAAC"}
@@ -0,0 +1,6 @@
1
+ import type { ShelbyClient } from "@shelby-protocol/sdk/browser";
2
+ import type { DefaultError, QueryKey, UseQueryOptions } from "@tanstack/react-query";
3
+ export type UseQueryOptionsWithClient<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> = Omit<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, "queryFn" | "queryKey"> & {
4
+ client: ShelbyClient;
5
+ };
6
+ //# sourceMappingURL=queries.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"queries.d.ts","sourceRoot":"","sources":["../../src/types/queries.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,KAAK,EACV,YAAY,EACZ,QAAQ,EACR,eAAe,EAChB,MAAM,uBAAuB,CAAC;AAE/B,MAAM,MAAM,yBAAyB,CACnC,YAAY,GAAG,OAAO,EACtB,MAAM,GAAG,YAAY,EACrB,KAAK,GAAG,YAAY,EACpB,SAAS,SAAS,QAAQ,GAAG,QAAQ,IACnC,IAAI,CACN,eAAe,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,CAAC,EACvD,SAAS,GAAG,UAAU,CACvB,GAAG;IAAE,MAAM,EAAE,YAAY,CAAA;CAAE,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { Account, AccountAddressInput } from "@aptos-labs/ts-sdk";
2
+ import type { SignAndSubmitTransactionFn } from "./walletAdapter";
3
+ export type AccountSigner = Account;
4
+ export type WalletAdapterSigner = {
5
+ account: AccountAddressInput;
6
+ signAndSubmitTransaction: SignAndSubmitTransactionFn;
7
+ };
8
+ export type Signer = AccountSigner | WalletAdapterSigner;
9
+ //# sourceMappingURL=signers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signers.d.ts","sourceRoot":"","sources":["../../src/types/signers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AACvE,OAAO,KAAK,EAAE,0BAA0B,EAAE,MAAM,iBAAiB,CAAC;AAElE,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC;AAEpC,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,EAAE,mBAAmB,CAAC;IAC7B,wBAAwB,EAAE,0BAA0B,CAAC;CACtD,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG,aAAa,GAAG,mBAAmB,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { WalletContextState } from "@aptos-labs/wallet-adapter-react";
2
+ export type SignAndSubmitTransactionFn = WalletContextState["signAndSubmitTransaction"];
3
+ //# sourceMappingURL=walletAdapter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"walletAdapter.d.ts","sourceRoot":"","sources":["../../src/types/walletAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kCAAkC,CAAC;AAE3E,MAAM,MAAM,0BAA0B,GACpC,kBAAkB,CAAC,0BAA0B,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,80 @@
1
+ {
2
+ "name": "@shelby-protocol/react",
3
+ "version": "0.0.2",
4
+ "description": "React library for the Shelby Protocol",
5
+ "private": false,
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.mjs",
9
+ "types": "./dist/index.d.ts",
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js",
17
+ "require": "./dist/index.cjs"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist",
22
+ "src",
23
+ "!tests",
24
+ "!src/**/*.test.ts",
25
+ "!src/**/*.test.tsx",
26
+ "README.md",
27
+ "CHANGELOG.md"
28
+ ],
29
+ "dependencies": {
30
+ "p-limit": "^7.2.0"
31
+ },
32
+ "peerDependencies": {
33
+ "@aptos-labs/ts-sdk": ">=5.0.0",
34
+ "@aptos-labs/wallet-adapter-react": "^6.0.0 || ^7.0.0",
35
+ "@tanstack/react-query": ">=5.0.0",
36
+ "react": "^18.0.0 || ^19.0.0",
37
+ "react-dom": "^18.0.0 || ^19.0.0",
38
+ "typescript": ">=5.0.4",
39
+ "@shelby-protocol/sdk": "0.0.7"
40
+ },
41
+ "peerDependenciesMeta": {
42
+ "@aptos-labs/wallet-adapter-react": {
43
+ "optional": true
44
+ }
45
+ },
46
+ "devDependencies": {
47
+ "@aptos-labs/ts-sdk": "^5.1.1",
48
+ "@aptos-labs/wallet-adapter-react": "^6.0.0 || ^7.0.0",
49
+ "@testing-library/dom": "^10.4.0",
50
+ "@testing-library/react": "^16.3.0",
51
+ "@types/react": "^19.2.0",
52
+ "@types/react-dom": "^19.2.0",
53
+ "@vitest/browser": "^3.1.2",
54
+ "@vitest/coverage-v8": "^3.2.4",
55
+ "dotenv": "^16.5.0",
56
+ "happy-dom": "^20.0.10",
57
+ "tsup": "^8.4.0",
58
+ "vitest": "^3.1.2",
59
+ "@shelby-protocol/sdk": "0.0.7"
60
+ },
61
+ "keywords": [
62
+ "aptos",
63
+ "apt",
64
+ "web3",
65
+ "blockchain",
66
+ "dapps",
67
+ "wallet",
68
+ "shelby"
69
+ ],
70
+ "scripts": {
71
+ "build": "tsup",
72
+ "build:types": "tsc --project tsconfig.build.json",
73
+ "check:types": "tsc --noEmit",
74
+ "dev": "tsup --watch",
75
+ "lint": "biome check .",
76
+ "fmt": "biome check . --write",
77
+ "test": "vitest dev --coverage",
78
+ "test:once": "vitest run --coverage"
79
+ }
80
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./mutations/index";
2
+ export * from "./queries/index";
@@ -0,0 +1,4 @@
1
+ export * from "./useCommitBlobs";
2
+ export * from "./useEncodeBlobs";
3
+ export * from "./useRegisterCommitments";
4
+ export * from "./useUploadBlobs";
@@ -0,0 +1,83 @@
1
+ import type { AccountAddressInput } from "@aptos-labs/ts-sdk";
2
+ import { useMutation } from "@tanstack/react-query";
3
+ import pLimit from "p-limit";
4
+ import type { UseMutationOptionsWithClient } from "../types/mutations";
5
+
6
+ export type UseCommitBlobsVariables = {
7
+ /**
8
+ * The account to commit the blobs to.
9
+ */
10
+ account: AccountAddressInput;
11
+ /**
12
+ * The blobs to commit.
13
+ */
14
+ blobs: {
15
+ blobName: string;
16
+ blobData: Uint8Array;
17
+ }[];
18
+ /**
19
+ * The maximum number of concurrent uploads.
20
+ * @default 3
21
+ */
22
+ maxConcurrentUploads?: number;
23
+ };
24
+
25
+ export type UseCommitBlobsOptions = UseMutationOptionsWithClient<
26
+ void,
27
+ Error,
28
+ UseCommitBlobsVariables
29
+ >;
30
+
31
+ /**
32
+ * Uploads blobs data to the Shelby RPCs.
33
+ *
34
+ * This mutation uploads multiple blobs data to the RPC endpoint with configurable
35
+ * concurrency control.
36
+ *
37
+ * @example
38
+ * ```tsx
39
+ * import { ShelbyClient } from "@shelby-protocol/sdk/browser";
40
+ * import { Network } from "@aptos-labs/ts-sdk";
41
+ * import { useCommitBlobs } from "@shelby-protocol/react";
42
+ *
43
+ * const shelbyClient = new ShelbyClient({ network: Network.SHELBYNET });
44
+ * const commitBlobs = useCommitBlobs({
45
+ * client: shelbyClient,
46
+ * onSuccess: () => console.log('Blobs committed successfully'),
47
+ * });
48
+ *
49
+ * commitBlobs.mutate({
50
+ * account: '0x123...',
51
+ * blobs: [
52
+ * { blobName: 'file1.txt', blobData: new Uint8Array([...]) },
53
+ * { blobName: 'file2.txt', blobData: new Uint8Array([...]) },
54
+ * ],
55
+ * });
56
+ * ```
57
+ */
58
+ export function useCommitBlobs({
59
+ client: shelbyClient,
60
+ ...options
61
+ }: UseCommitBlobsOptions) {
62
+ return useMutation({
63
+ mutationFn: async ({
64
+ account,
65
+ blobs,
66
+ maxConcurrentUploads = 3,
67
+ }: UseCommitBlobsVariables) => {
68
+ const limit = pLimit(maxConcurrentUploads);
69
+ const uploadPromises = blobs.map((blob) =>
70
+ limit(() =>
71
+ shelbyClient.rpc.putBlob({
72
+ account,
73
+ blobName: blob.blobName,
74
+ blobData: blob.blobData,
75
+ }),
76
+ ),
77
+ );
78
+
79
+ await Promise.all(uploadPromises);
80
+ },
81
+ ...options,
82
+ });
83
+ }
@@ -0,0 +1,122 @@
1
+ import {
2
+ type BlobCommitments,
3
+ createDefaultErasureCodingProvider,
4
+ type ErasureCodingProvider,
5
+ generateCommitments,
6
+ } from "@shelby-protocol/sdk/browser";
7
+ import { useMutation } from "@tanstack/react-query";
8
+ import type { UseMutationOptionsWithClient } from "../types/mutations";
9
+
10
+ export type UseEncodeBlobsOnChunkEvent = {
11
+ /**
12
+ * The index of the blob being encoded.
13
+ */
14
+ blobIndex: number;
15
+ /**
16
+ * The index of the chunkset being encoded.
17
+ */
18
+ chunksetIndex: number;
19
+ /**
20
+ * The index of the chunk being encoded.
21
+ */
22
+ chunkIndex: number;
23
+ /**
24
+ * The data of the chunk being encoded.
25
+ */
26
+ chunkData: Uint8Array;
27
+ /**
28
+ * The progress of the encoding.
29
+ */
30
+ progress: number;
31
+ };
32
+
33
+ export type UseEncodeBlobsVariables = {
34
+ /**
35
+ * The blobs to encode.
36
+ */
37
+ blobs: { blobData: Uint8Array }[];
38
+ /**
39
+ * The erasure coding provider to use.
40
+ */
41
+ provider?: ErasureCodingProvider;
42
+ /**
43
+ * The callback to call when a chunk is encoded.
44
+ */
45
+ onChunk?: (event: UseEncodeBlobsOnChunkEvent) => void;
46
+ };
47
+
48
+ export type UseEncodeBlobsOptions = UseMutationOptionsWithClient<
49
+ BlobCommitments[],
50
+ Error,
51
+ UseEncodeBlobsVariables
52
+ >;
53
+
54
+ /**
55
+ * Encodes blobs using erasure coding.
56
+ *
57
+ * This mutation generates blob commitments (merkle roots and erasure coding chunks)
58
+ * from raw blob data. It supports custom erasure coding providers and progress callbacks
59
+ * for tracking encoding progress.
60
+ *
61
+ * @example
62
+ * ```tsx
63
+ * import { ShelbyClient } from "@shelby-protocol/sdk/browser";
64
+ * import { Network } from "@aptos-labs/ts-sdk";
65
+ * import { useEncodeBlobs } from "@shelby-protocol/react";
66
+ *
67
+ * const shelbyClient = new ShelbyClient({ network: Network.SHELBYNET });
68
+ * const encodeBlobs = useEncodeBlobs({
69
+ * client: shelbyClient,
70
+ * onSuccess: (commitments) => console.log('Encoded', commitments.length, 'blobs'),
71
+ * });
72
+ *
73
+ * encodeBlobs.mutate({
74
+ * blobs: [{ blobData: new Uint8Array([...]) }],
75
+ * onChunk: ({ blobIndex, progress }) => {
76
+ * console.log(`Blob ${blobIndex}: ${(progress * 100).toFixed(1)}%`);
77
+ * },
78
+ * });
79
+ * ```
80
+ */
81
+ export function useEncodeBlobs({
82
+ client: shelbyClient,
83
+ ...options
84
+ }: UseEncodeBlobsOptions) {
85
+ return useMutation({
86
+ mutationFn: async ({ blobs, provider, onChunk }) => {
87
+ const activeProvider =
88
+ provider ?? (await createDefaultErasureCodingProvider());
89
+
90
+ const results: BlobCommitments[] = new Array(blobs.length);
91
+ await Promise.all(
92
+ blobs.map(async ({ blobData }, blobIndex) => {
93
+ const chunksetCount = Math.ceil(
94
+ blobData.length / activeProvider.config.chunkSizeBytes,
95
+ );
96
+
97
+ const blobCommitments = await generateCommitments(
98
+ activeProvider,
99
+ blobData,
100
+ (
101
+ chunksetIndex: number,
102
+ chunkIndex: number,
103
+ chunkData: Uint8Array,
104
+ ) =>
105
+ onChunk?.({
106
+ blobIndex,
107
+ chunksetIndex,
108
+ chunkIndex,
109
+ chunkData,
110
+ progress: chunksetIndex / chunksetCount,
111
+ }),
112
+ );
113
+
114
+ results[blobIndex] = blobCommitments;
115
+ }),
116
+ );
117
+
118
+ return results;
119
+ },
120
+ ...options,
121
+ });
122
+ }
@@ -0,0 +1,129 @@
1
+ import { AccountAddress } from "@aptos-labs/ts-sdk";
2
+ import {
3
+ type BlobCommitments,
4
+ DEFAULT_CHUNKSET_SIZE_BYTES,
5
+ expectedTotalChunksets,
6
+ ShelbyBlobClient,
7
+ type UploadOptions,
8
+ } from "@shelby-protocol/sdk/browser";
9
+ import { useMutation } from "@tanstack/react-query";
10
+ import type { UseMutationOptionsWithClient } from "../types/mutations";
11
+ import type { AccountSigner, Signer } from "../types/signers";
12
+
13
+ export type UseRegisterCommitmentsVariables = {
14
+ /**
15
+ * The signer to use for the transaction.
16
+ * @see {@link Signer}
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * const signer = new Account.generate();
21
+ * registerCommitments.mutate({
22
+ * signer,
23
+ * commitments: [
24
+ * { blobName: 'file1.txt', commitment: blobCommitments[0] },
25
+ * ],
26
+ * });
27
+ * ```
28
+ */
29
+ signer: Signer;
30
+ /**
31
+ * The commitments to register.
32
+ */
33
+ commitments: { blobName: string; commitment: BlobCommitments }[];
34
+ /**
35
+ * The expiration time of the commitments in microseconds.
36
+ */
37
+ expirationMicros: number;
38
+ /**
39
+ * Optional transaction building options.
40
+ */
41
+ options?: UploadOptions;
42
+ };
43
+
44
+ export type UseRegisterCommitmentsOptions = UseMutationOptionsWithClient<
45
+ { hash: string },
46
+ Error,
47
+ UseRegisterCommitmentsVariables
48
+ >;
49
+
50
+ /**
51
+ * Registers blob commitments on-chain.
52
+ *
53
+ * This mutation registers blob commitments (merkle roots) on the Aptos blockchain
54
+ * as part of the blob upload process. It supports both account signers and wallet adapter
55
+ * signers, and handles batch registration of multiple blobs.
56
+ *
57
+ * @example
58
+ * ```tsx
59
+ * import { ShelbyClient } from "@shelby-protocol/sdk/browser";
60
+ * import { Network } from "@aptos-labs/ts-sdk";
61
+ * import { useRegisterCommitments } from "@shelby-protocol/react";
62
+ *
63
+ * const shelbyClient = new ShelbyClient({ network: Network.SHELBYNET });
64
+ * const registerCommitments = useRegisterCommitments({
65
+ * client: shelbyClient,
66
+ * onSuccess: ({ hash }) => console.log('Transaction hash:', hash),
67
+ * });
68
+ *
69
+ * registerCommitments.mutate({
70
+ * signer: accountSigner,
71
+ * commitments: [
72
+ * { blobName: 'file1.txt', commitment: blobCommitments[0] },
73
+ * ],
74
+ * expirationMicros: Date.now() * 1000 + 86400000000, // 1 day
75
+ * });
76
+ * ```
77
+ */
78
+ export function useRegisterCommitments({
79
+ client: shelbyClient,
80
+ ...options
81
+ }: UseRegisterCommitmentsOptions) {
82
+ return useMutation({
83
+ mutationFn: async ({
84
+ commitments,
85
+ expirationMicros,
86
+ options,
87
+ signer: signerOrFn,
88
+ }) => {
89
+ if (!("account" in signerOrFn)) {
90
+ const accountSigner: AccountSigner = signerOrFn;
91
+
92
+ const { transaction } =
93
+ await shelbyClient.coordination.batchRegisterBlobs({
94
+ account: accountSigner,
95
+ expirationMicros,
96
+ options,
97
+ blobs: commitments.map((commitment) => ({
98
+ blobName: commitment.blobName,
99
+ blobSize: commitment.commitment.raw_data_size,
100
+ blobMerkleRoot: commitment.commitment.blob_merkle_root,
101
+ })),
102
+ });
103
+
104
+ return transaction;
105
+ }
106
+
107
+ const { account, signAndSubmitTransaction } = signerOrFn;
108
+ const transaction = await signAndSubmitTransaction({
109
+ data: ShelbyBlobClient.createBatchRegisterBlobsPayload({
110
+ account: AccountAddress.from(account),
111
+ expirationMicros,
112
+ blobs: commitments.map((commitment) => ({
113
+ blobName: commitment.blobName,
114
+ blobSize: commitment.commitment.raw_data_size,
115
+ blobMerkleRoot: commitment.commitment.blob_merkle_root,
116
+ numChunksets: expectedTotalChunksets(
117
+ commitment.commitment.raw_data_size,
118
+ options?.chunksetSizeBytes ?? DEFAULT_CHUNKSET_SIZE_BYTES,
119
+ ),
120
+ })),
121
+ }),
122
+ options: options?.build?.options,
123
+ });
124
+
125
+ return transaction;
126
+ },
127
+ ...options,
128
+ });
129
+ }