@mysten/walrus 0.3.1 → 0.4.0

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 (80) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/README.md +184 -23
  3. package/dist/cjs/client.d.ts +90 -2
  4. package/dist/cjs/client.js +300 -2
  5. package/dist/cjs/client.js.map +3 -3
  6. package/dist/cjs/files/blob.d.ts +21 -0
  7. package/dist/cjs/files/blob.js +94 -0
  8. package/dist/cjs/files/blob.js.map +7 -0
  9. package/dist/cjs/files/file.d.ts +21 -0
  10. package/dist/cjs/files/file.js +62 -0
  11. package/dist/cjs/files/file.js.map +7 -0
  12. package/dist/cjs/files/readers/blob.d.ts +70 -0
  13. package/dist/cjs/files/readers/blob.js +132 -0
  14. package/dist/cjs/files/readers/blob.js.map +7 -0
  15. package/dist/cjs/files/readers/local.d.ts +12 -0
  16. package/dist/cjs/files/readers/local.js +61 -0
  17. package/dist/cjs/files/readers/local.js.map +7 -0
  18. package/dist/cjs/files/readers/quilt-file.d.ts +21 -0
  19. package/dist/cjs/files/readers/quilt-file.js +75 -0
  20. package/dist/cjs/files/readers/quilt-file.js.map +7 -0
  21. package/dist/cjs/files/readers/quilt.d.ts +29 -0
  22. package/dist/cjs/files/readers/quilt.js +219 -0
  23. package/dist/cjs/files/readers/quilt.js.map +7 -0
  24. package/dist/cjs/index.d.ts +3 -0
  25. package/dist/cjs/index.js +7 -1
  26. package/dist/cjs/index.js.map +2 -2
  27. package/dist/cjs/types.d.ts +28 -1
  28. package/dist/cjs/types.js.map +1 -1
  29. package/dist/cjs/utils/index.d.ts +15 -0
  30. package/dist/cjs/utils/index.js +42 -6
  31. package/dist/cjs/utils/index.js.map +2 -2
  32. package/dist/cjs/utils/quilts.d.ts +34 -0
  33. package/dist/cjs/utils/quilts.js +166 -14
  34. package/dist/cjs/utils/quilts.js.map +3 -3
  35. package/dist/esm/client.d.ts +90 -2
  36. package/dist/esm/client.js +300 -2
  37. package/dist/esm/client.js.map +3 -3
  38. package/dist/esm/files/blob.d.ts +21 -0
  39. package/dist/esm/files/blob.js +74 -0
  40. package/dist/esm/files/blob.js.map +7 -0
  41. package/dist/esm/files/file.d.ts +21 -0
  42. package/dist/esm/files/file.js +42 -0
  43. package/dist/esm/files/file.js.map +7 -0
  44. package/dist/esm/files/readers/blob.d.ts +70 -0
  45. package/dist/esm/files/readers/blob.js +112 -0
  46. package/dist/esm/files/readers/blob.js.map +7 -0
  47. package/dist/esm/files/readers/local.d.ts +12 -0
  48. package/dist/esm/files/readers/local.js +41 -0
  49. package/dist/esm/files/readers/local.js.map +7 -0
  50. package/dist/esm/files/readers/quilt-file.d.ts +21 -0
  51. package/dist/esm/files/readers/quilt-file.js +55 -0
  52. package/dist/esm/files/readers/quilt-file.js.map +7 -0
  53. package/dist/esm/files/readers/quilt.d.ts +29 -0
  54. package/dist/esm/files/readers/quilt.js +203 -0
  55. package/dist/esm/files/readers/quilt.js.map +7 -0
  56. package/dist/esm/index.d.ts +3 -0
  57. package/dist/esm/index.js +7 -1
  58. package/dist/esm/index.js.map +2 -2
  59. package/dist/esm/types.d.ts +28 -1
  60. package/dist/esm/utils/index.d.ts +15 -0
  61. package/dist/esm/utils/index.js +42 -6
  62. package/dist/esm/utils/index.js.map +2 -2
  63. package/dist/esm/utils/quilts.d.ts +34 -0
  64. package/dist/esm/utils/quilts.js +169 -11
  65. package/dist/esm/utils/quilts.js.map +2 -2
  66. package/dist/tsconfig.esm.tsbuildinfo +1 -1
  67. package/dist/tsconfig.tsbuildinfo +1 -1
  68. package/package.json +13 -9
  69. package/dist/cjs/quilt/reader.d.ts +0 -27
  70. package/dist/cjs/quilt/reader.js +0 -267
  71. package/dist/cjs/quilt/reader.js.map +0 -7
  72. package/dist/cjs/quilt/write.d.ts +0 -21
  73. package/dist/cjs/quilt/write.js +0 -167
  74. package/dist/cjs/quilt/write.js.map +0 -7
  75. package/dist/esm/quilt/reader.d.ts +0 -27
  76. package/dist/esm/quilt/reader.js +0 -247
  77. package/dist/esm/quilt/reader.js.map +0 -7
  78. package/dist/esm/quilt/write.d.ts +0 -21
  79. package/dist/esm/quilt/write.js +0 -156
  80. package/dist/esm/quilt/write.js.map +0 -7
package/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @mysten/walrus
2
2
 
3
+ ## 0.4.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 2473630: Add writeFilesFlow method to client
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [44354ab]
12
+ - @mysten/sui@1.36.2
13
+
3
14
  ## 0.3.1
4
15
 
5
16
  ### Patch Changes
package/README.md CHANGED
@@ -69,46 +69,177 @@ The `WalrusClient` exposes high level methods for reading and writing blobs, as
69
69
  methods for the individual steps in the process that can be used to implement more complex flows
70
70
  when you want more control to implement more optimized implementations.
71
71
 
72
- ### Reading a blob
72
+ ## WalrusFiles
73
73
 
74
- The `readBlob` method will read a blob given the `blobId` and return `Uint8Array` containing the
75
- blobs content:
74
+ The `WalrusFile` API provides a higher level abstraction so that applications don't need to worry
75
+ about how data is stored in walrus. Today it handles data stored directly in blobs, and data stored
76
+ in Quilts, but may be expanded to cover other storage patterns in the future.
77
+
78
+ ### Reading files
79
+
80
+ To read files, you can use the `getFiles` method. This method accepts both Blob IDs and Quilt IDs,
81
+ and will return a `WalrusFile`.
82
+
83
+ It is encouraged to always read files in batches when possible, which will allow the client to be
84
+ more efficient when loading multiple files from the same quilt.
76
85
 
77
86
  ```ts
78
- const blob = await walrusClient.readBlob({ blobId });
87
+ const [file1, file2] = await walrusClient.getFiles({ ids: [anyBlobId, orQuiltId] });
79
88
  ```
80
89
 
81
- ### Writing Blobs
90
+ A `WalrusFile` works like a `Response` object from the `fetch` API:
82
91
 
83
- Thw `writeBlob` method can be used to write a blob (as a `Uint8Array`) to walrus. You will need to
84
- specify how long the blob should be stored for, and if the blob should be deletable.
92
+ ```ts
93
+ // get contents as a Uint8Array
94
+ const bytes = await file1.bytes();
95
+ // Parse the contents as a `utf-8` string
96
+ const text = await file1.text();
97
+ // Parse the contents as JSON
98
+ const json = await file2.json();
99
+ ```
85
100
 
86
- You will also need to provide a `signer` instance that signs and pays for the transaction/storage
87
- fees. The signer's address will need to have sufficient `SUI` to cover the transactions that
88
- register the blob, and certify its availability after it's been uploaded. It will also need to own
89
- sufficient `WAL` to pay to store the blob for the specified number of epochs, as well as the write
90
- fee for writing the blob.
101
+ A `WalrusFile` may also have and `identifier` and `tags` properties if the file was stored in a
102
+ quilt.
91
103
 
92
- The exact costs will depend on the size of the blobs, as well as the current gas and storage prices.
104
+ ```ts
105
+ const identifier: string | null = await file1.getIdentifier();
106
+ const tags: Record<string, string> = await file1.getTags();
107
+ ```
108
+
109
+ ### WalrusBlobs
110
+
111
+ You can also get a `WalrusBlob` instead of a `WalrusFile` if you have the blobId:
93
112
 
94
113
  ```ts
95
- const file = new TextEncoder().encode('Hello from the TS SDK!!!\n');
114
+ const blob = await walrusClient.getBlob({ blobId });
115
+ ```
96
116
 
97
- const { blobId } = await walrusClient.writeBlob({
98
- blob: file,
99
- deletable: false,
117
+ If the blob is a quilt, you can read the files in the quilt:
118
+
119
+ ```ts
120
+ // Get all files:
121
+ const files = await blob.files();
122
+ // Get files by identifier
123
+ const [readme] = await blob.files({ identifiers: ['README.md'] });
124
+ // Get files by tag
125
+ const textFiles: WalrusFile[] = await blob.files({ tags: [{ 'content-type': 'text/plain' }] });
126
+ // Get files by quilt id
127
+ const filesById = await blob.files({ ids: [quiltID] });
128
+ ```
129
+
130
+ ### Writing files
131
+
132
+ You can also construct a `WalrusFile` from a `Uint8Array`, `Blob`, or a `string` which can then be
133
+ stored on walrus:
134
+
135
+ ```ts
136
+ const file1 = WalrusFile.from(new Uint8Array([1, 2, 3]));
137
+ const file2 = WalrusFile.from(new Blob([new Uint8Array([1, 2, 3])]));
138
+ const file3 = WalrusFile.from('Hello from the TS SDK!!!\n', {
139
+ identifier: 'README.md',
140
+ tags: {
141
+ 'content-type': 'text/plain',
142
+ },
143
+ });
144
+ ```
145
+
146
+ Once you have your files you can use the `writeFiles` method to write them to walrus.
147
+
148
+ Along with the files, you will also need to provide a `Signer` instance that signs and pays for the
149
+ transaction/storage fees. The signer's address will need to have sufficient `SUI` to cover the
150
+ transactions that register the blob, and certify its availability after it's been uploaded. The
151
+ Signer must own sufficient `WAL` to pay to store the blob for the specified number of epochs, as
152
+ well as the write fee for writing the blob.
153
+
154
+ The exact costs will depend on the size of the blobs, as well as the current gas and storage prices.
155
+
156
+ ```ts
157
+ const results: {
158
+ id: string;
159
+ blobId: string;
160
+ blobObject: Blob.$inferType;
161
+ }[] = walrusClient.writeFiles({
162
+ files: [file1, file2, file3],
100
163
  epochs: 3,
164
+ deletable: true,
101
165
  signer: keypair,
102
166
  });
103
167
  ```
104
168
 
105
- ### Writing blobs with an upload relay
169
+ Currently the provided files will all be written into a single quilt. Future versions of the SDK may
170
+ optimize how files are stored to be more efficient by splitting files into multiple quilts.
171
+
172
+ The current quilt encoding is less efficient for single files, so writing multiple files together is
173
+ recommended when possible. Writing raw blobs directly is also possible using the `writeBlob` API
174
+ (described below)
175
+
176
+ ### Writing files in browser environments
177
+
178
+ When the transactions to upload a blob are signed by a wallet in a browser, some wallets will use
179
+ popups to prompt the user for a signature. If the popups are not opened in direct response to a user
180
+ interaction, they may be blocked by the browser.
181
+
182
+ To avoid this, we need to ensure that we execute the transactions that register and certify the blob
183
+ in separate events handlers by creating separate buttons for the user to click for each step.
184
+
185
+ The `client.writeFilesFlow` method returns an object with a set of methods that break the write flow
186
+ into several smaller steps:
187
+
188
+ 1. `encode` - Encodes the files and generates a blobId
189
+ 2. `register` - returns a transaction that will register the blob on-chain
190
+ 3. `upload` - Uploads the data to storage nodes
191
+ 4. `certify` - returns a transaction that will certify the blob on-chain
192
+ 5. `listFiles` - returns a list of the created files
193
+
194
+ Here's a simplified example showing the core API usage with separate user interactions:
195
+
196
+ ```tsx
197
+ // Step 1: Create and encode the flow (can be done immediately when file is selected)
198
+ const flow = walrusClient.writeFilesFlow({
199
+ files: [
200
+ WalrusFile.from({
201
+ contents: new Uint8Array(fileData),
202
+ identifier: 'my-file.txt',
203
+ }),
204
+ ],
205
+ epochs: 3,
206
+ owner: currentAccount.address,
207
+ deletable: true,
208
+ });
209
+
210
+ await flow.encode();
211
+
212
+ // Step 2: Register the blob (triggered by user clicking a register button after the encode step)
213
+ async function handleRegister() {
214
+ const registerTx = flow.register();
215
+ await signAndExecuteTransaction({ transaction: registerTx });
216
+ // Step 3: Upload the data to storage nodes
217
+ // This can be done immediately after the register step, or as a separate step the user initiates
218
+ await flow.upload();
219
+ }
220
+
221
+ // Step 4: Certify the blob (triggered by user clicking a certify button after the blob is uploaded)
222
+ async function handleCertify() {
223
+ const certifyTx = flow.certify();
224
+
225
+ await signAndExecuteTransaction({ transaction: certifyTx });
226
+
227
+ // Step 5: Get the new files
228
+ const files = await flow.listFiles();
229
+ console.log('Uploaded files', files);
230
+ }
231
+ ```
232
+
233
+ This approach ensures that each transaction signing step is separated into different user
234
+ interactions, allowing wallet popups to work properly without being blocked by the browser.
235
+
236
+ ## Writing blobs with an upload relay
106
237
 
107
238
  Writing blobs directly from a client requires a lot of requests to write data to all of the storage
108
239
  nodes. An upload relay can be used to offload the work of these writes to a server, reducing
109
240
  complexity for the client.
110
241
 
111
- To use an upload relay, you can add the `uploadRelay` option when creating your `WalrusClient`:
242
+ To use an upload relay, you can add the `uploadRelay` option when creating your `WalrusClient`.
112
243
 
113
244
  ```ts
114
245
  const client = new SuiClient({
@@ -185,12 +316,42 @@ const client = new SuiClient({
185
316
  );
186
317
  ```
187
318
 
188
- ### Full API
319
+ ## Interacting with blobs directly
320
+
321
+ In case you do not want to use the `WalrusFile` abstractions, you can use the `readBlob` and
322
+ `writeBlob` APIs directly
323
+
324
+ ### Reading blobs
325
+
326
+ The `readBlob` method will read a blob given the blobId and return Uint8Array containing the blobs
327
+ content:
328
+
329
+ ```ts
330
+ const blob = await walrusClient.readBlob({ blobId });
331
+ ```
332
+
333
+ ### Writing Blobs
334
+
335
+ Thw writeBlob method can be used to write a blob (as a `Uint8Array`) to walrus. You will need to
336
+ specify how long the blob should be stored for, and if the blob should be deletable.
337
+
338
+ ```ts
339
+ const file = new TextEncoder().encode('Hello from the TS SDK!!!\n');
340
+
341
+ const { blobId } = await walrusClient.writeBlob({
342
+ blob: file,
343
+ deletable: false,
344
+ epochs: 3,
345
+ signer: keypair,
346
+ });
347
+ ```
348
+
349
+ ## Full API
189
350
 
190
351
  For a complete overview of the available methods on the `WalrusClient` you can reference type
191
352
  [TypeDocs](http://sdk.mystenlabs.com/typedoc/classes/_mysten_walrus.WalrusClient.html)
192
353
 
193
- ### Examples
354
+ ## Examples
194
355
 
195
356
  There are a number of simple
196
357
  [examples you can reference](https://github.com/MystenLabs/ts-sdks/tree/main/packages/walrus/examples)
@@ -281,7 +442,7 @@ const walrusClient = new WalrusClient({
281
442
  });
282
443
  ```
283
444
 
284
- ### Loading the wasm module in vite or client side apps
445
+ ## Loading the wasm module in vite or client side apps
285
446
 
286
447
  The walrus SDK requires wasm bindings to encode and decode blobs. When running in node or bun and
287
448
  some bundlers this will work without any additional configuration.
@@ -322,7 +483,7 @@ const nextConfig: NextConfig = {
322
483
  };
323
484
  ```
324
485
 
325
- ### Known fetch limitations you might run into
486
+ ## Known fetch limitations you might run into
326
487
 
327
488
  - Some nodes can be slow to respond. When running in node, the default connectTimeout is 10 seconds
328
489
  and can cause request timeouts
@@ -5,7 +5,9 @@ import type { TransactionResult } from '@mysten/sui/transactions';
5
5
  import { Transaction } from '@mysten/sui/transactions';
6
6
  import { Blob } from './contracts/walrus/blob.js';
7
7
  import type { BlobStatus } from './storage-node/types.js';
8
- import type { CertifyBlobOptions, ComputeBlobMetadataOptions, DeleteBlobOptions, ExtendBlobOptions, GetBlobMetadataOptions, GetSecondarySliverOptions, GetSliversOptions, GetStorageConfirmationOptions, GetVerifiedBlobStatusOptions, ProtocolMessageCertificate, ReadBlobOptions, RegisterBlobOptions, SliversForNode, StorageWithSizeOptions, WalrusClientConfig, WalrusClientExtensionOptions, WriteBlobAttributesOptions, WriteBlobOptions, WriteBlobToUploadRelayOptions, WriteEncodedBlobOptions, WriteEncodedBlobToNodesOptions, WriteMetadataOptions, WriteSliverOptions, WriteSliversToNodeOptions } from './types.js';
8
+ import type { CertifyBlobOptions, ComputeBlobMetadataOptions, DeleteBlobOptions, ExtendBlobOptions, GetBlobMetadataOptions, GetSecondarySliverOptions, GetSliversOptions, GetStorageConfirmationOptions, GetVerifiedBlobStatusOptions, ProtocolMessageCertificate, ReadBlobOptions, RegisterBlobOptions, SliversForNode, StorageWithSizeOptions, WalrusClientConfig, WalrusClientExtensionOptions, WriteBlobAttributesOptions, WriteBlobOptions, WriteBlobToUploadRelayOptions, WriteEncodedBlobOptions, WriteEncodedBlobToNodesOptions, WriteMetadataOptions, WriteQuiltOptions, WriteSliverOptions, WriteSliversToNodeOptions, WriteFilesOptions, WriteFilesFlowOptions, WriteFilesFlow } from './types.js';
9
+ import { WalrusBlob } from './files/blob.js';
10
+ import { WalrusFile } from './files/file.js';
9
11
  export declare class WalrusClient {
10
12
  #private;
11
13
  constructor(config: WalrusClientConfig);
@@ -187,7 +189,14 @@ export declare class WalrusClient {
187
189
  $kind: "V1";
188
190
  };
189
191
  }>;
190
- getSecondarySliver({ blobId, index, signal }: GetSecondarySliverOptions): Promise<{
192
+ getSecondarySliver: ({ blobId, index, signal }: GetSecondarySliverOptions) => Promise<{
193
+ symbols: {
194
+ data: number[];
195
+ symbol_size: number;
196
+ };
197
+ index: number;
198
+ }>;
199
+ internalGetSecondarySliver({ blobId, index, signal }: GetSecondarySliverOptions): Promise<{
191
200
  symbols: {
192
201
  data: number[];
193
202
  symbol_size: number;
@@ -587,6 +596,55 @@ export declare class WalrusClient {
587
596
  deletable: boolean;
588
597
  };
589
598
  }>;
599
+ writeQuilt({ blobs, ...options }: WriteQuiltOptions): Promise<{
600
+ index: {
601
+ patches: {
602
+ patchId: string;
603
+ endIndex: number;
604
+ identifier: string;
605
+ tags: Map<string, string> | Record<string, string>;
606
+ startIndex: number;
607
+ }[];
608
+ };
609
+ blobId: string;
610
+ blobObject: {
611
+ id: {
612
+ id: string;
613
+ };
614
+ registered_epoch: number;
615
+ blob_id: string;
616
+ size: string;
617
+ encoding_type: number;
618
+ certified_epoch: number | null;
619
+ storage: {
620
+ id: {
621
+ id: string;
622
+ };
623
+ start_epoch: number;
624
+ end_epoch: number;
625
+ storage_size: string;
626
+ };
627
+ deletable: boolean;
628
+ };
629
+ }>;
630
+ encodeQuilt({ blobs, }: {
631
+ blobs: {
632
+ contents: Uint8Array;
633
+ identifier: string;
634
+ tags?: Record<string, string>;
635
+ }[];
636
+ }): Promise<{
637
+ quilt: Uint8Array<ArrayBuffer>;
638
+ index: {
639
+ patches: ({
640
+ endIndex: number;
641
+ identifier: string;
642
+ tags: Map<string, string> | Record<string, string>;
643
+ } & {
644
+ startIndex: number;
645
+ })[];
646
+ };
647
+ }>;
590
648
  /**
591
649
  * Reset cached data in the client
592
650
  *
@@ -596,4 +654,34 @@ export declare class WalrusClient {
596
654
  * ```
597
655
  */
598
656
  reset(): void;
657
+ getBlob({ blobId }: {
658
+ blobId: string;
659
+ }): Promise<WalrusBlob>;
660
+ getFiles({ ids }: {
661
+ ids: string[];
662
+ }): Promise<WalrusFile[]>;
663
+ writeFiles({ files, ...options }: WriteFilesOptions): Promise<{
664
+ id: string;
665
+ blobId: string;
666
+ blobObject: {
667
+ id: {
668
+ id: string;
669
+ };
670
+ registered_epoch: number;
671
+ blob_id: string;
672
+ size: string;
673
+ encoding_type: number;
674
+ certified_epoch: number | null;
675
+ storage: {
676
+ id: {
677
+ id: string;
678
+ };
679
+ start_epoch: number;
680
+ end_epoch: number;
681
+ storage_size: string;
682
+ };
683
+ deletable: boolean;
684
+ };
685
+ }[]>;
686
+ writeFilesFlow({ files, epochs, deletable, owner, attributes, signal, }: WriteFilesFlowOptions): WriteFilesFlow;
599
687
  }