@mysten/walrus 0.3.1 → 0.5.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 +17 -0
  2. package/README.md +193 -27
  3. package/dist/cjs/client.d.ts +90 -2
  4. package/dist/cjs/client.js +292 -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 +33 -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 +292 -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 +33 -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 +15 -11
  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,22 @@
1
1
  # @mysten/walrus
2
2
 
3
+ ## 0.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 3fd7e41: Move delete, epochs and owner options from writeFilesFlow to flow.register
8
+
9
+ ## 0.4.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 2473630: Add writeFilesFlow method to client
14
+
15
+ ### Patch Changes
16
+
17
+ - Updated dependencies [44354ab]
18
+ - @mysten/sui@1.36.2
19
+
3
20
  ## 0.3.1
4
21
 
5
22
  ### Patch Changes
package/README.md CHANGED
@@ -60,55 +60,191 @@ any other desired behavior.
60
60
 
61
61
  ## SDK Overview
62
62
 
63
- The walrus SDK is designed primarily to be used to build aggregators and publishers, and may not be
64
- optimal when used directly in client side apps. Reading and writing directly from storage nodes
65
- requires a lot of requests (~2200 to write a blob, ~335 to read a blob). For most apps, using
66
- aggregators and publishers with optimized reads/writes will provide a better user experience.
63
+ The Walrus TS SDK is designed to work directly with walrus storage nodes, or with the walrus upload
64
+ relay. When using the walrus SDK without an upload relay, it is important to understand that reading
65
+ and writing walrus blobs requires a lot of requests (~2200 to write a blob, ~335 to read a blob).
66
+ The upload relay will reduce the number of requests needed to write a blob, but reads through the
67
+ walrus SDK will still require a lot of requests. For many applications, using publishers and
68
+ aggregators is recommended, but the TS SDK can be useful when building applications where the
69
+ application needs to directly interact with walrus or users need to pay for their own storage
70
+ directly.
67
71
 
68
72
  The `WalrusClient` exposes high level methods for reading and writing blobs, as well as lower level
69
73
  methods for the individual steps in the process that can be used to implement more complex flows
70
74
  when you want more control to implement more optimized implementations.
71
75
 
72
- ### Reading a blob
76
+ ## WalrusFiles
73
77
 
74
- The `readBlob` method will read a blob given the `blobId` and return `Uint8Array` containing the
75
- blobs content:
78
+ The `WalrusFile` API provides a higher level abstraction so that applications don't need to worry
79
+ about how data is stored in walrus. Today it handles data stored directly in blobs, and data stored
80
+ in Quilts, but may be expanded to cover other storage patterns in the future.
81
+
82
+ ### Reading files
83
+
84
+ To read files, you can use the `getFiles` method. This method accepts both Blob IDs and Quilt IDs,
85
+ and will return a `WalrusFile`.
86
+
87
+ It is encouraged to always read files in batches when possible, which will allow the client to be
88
+ more efficient when loading multiple files from the same quilt.
76
89
 
77
90
  ```ts
78
- const blob = await walrusClient.readBlob({ blobId });
91
+ const [file1, file2] = await walrusClient.getFiles({ ids: [anyBlobId, orQuiltId] });
79
92
  ```
80
93
 
81
- ### Writing Blobs
94
+ A `WalrusFile` works like a `Response` object from the `fetch` API:
82
95
 
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.
96
+ ```ts
97
+ // get contents as a Uint8Array
98
+ const bytes = await file1.bytes();
99
+ // Parse the contents as a `utf-8` string
100
+ const text = await file1.text();
101
+ // Parse the contents as JSON
102
+ const json = await file2.json();
103
+ ```
85
104
 
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.
105
+ A `WalrusFile` may also have and `identifier` and `tags` properties if the file was stored in a
106
+ quilt.
91
107
 
92
- The exact costs will depend on the size of the blobs, as well as the current gas and storage prices.
108
+ ```ts
109
+ const identifier: string | null = await file1.getIdentifier();
110
+ const tags: Record<string, string> = await file1.getTags();
111
+ ```
112
+
113
+ ### WalrusBlobs
114
+
115
+ You can also get a `WalrusBlob` instead of a `WalrusFile` if you have the blobId:
93
116
 
94
117
  ```ts
95
- const file = new TextEncoder().encode('Hello from the TS SDK!!!\n');
118
+ const blob = await walrusClient.getBlob({ blobId });
119
+ ```
96
120
 
97
- const { blobId } = await walrusClient.writeBlob({
98
- blob: file,
99
- deletable: false,
121
+ If the blob is a quilt, you can read the files in the quilt:
122
+
123
+ ```ts
124
+ // Get all files:
125
+ const files = await blob.files();
126
+ // Get files by identifier
127
+ const [readme] = await blob.files({ identifiers: ['README.md'] });
128
+ // Get files by tag
129
+ const textFiles: WalrusFile[] = await blob.files({ tags: [{ 'content-type': 'text/plain' }] });
130
+ // Get files by quilt id
131
+ const filesById = await blob.files({ ids: [quiltID] });
132
+ ```
133
+
134
+ ### Writing files
135
+
136
+ You can also construct a `WalrusFile` from a `Uint8Array`, `Blob`, or a `string` which can then be
137
+ stored on walrus:
138
+
139
+ ```ts
140
+ const file1 = WalrusFile.from(new Uint8Array([1, 2, 3]));
141
+ const file2 = WalrusFile.from(new Blob([new Uint8Array([1, 2, 3])]));
142
+ const file3 = WalrusFile.from('Hello from the TS SDK!!!\n', {
143
+ identifier: 'README.md',
144
+ tags: {
145
+ 'content-type': 'text/plain',
146
+ },
147
+ });
148
+ ```
149
+
150
+ Once you have your files you can use the `writeFiles` method to write them to walrus.
151
+
152
+ Along with the files, you will also need to provide a `Signer` instance that signs and pays for the
153
+ transaction/storage fees. The signer's address will need to have sufficient `SUI` to cover the
154
+ transactions that register the blob, and certify its availability after it's been uploaded. The
155
+ Signer must own sufficient `WAL` to pay to store the blob for the specified number of epochs, as
156
+ well as the write fee for writing the blob.
157
+
158
+ The exact costs will depend on the size of the blobs, as well as the current gas and storage prices.
159
+
160
+ ```ts
161
+ const results: {
162
+ id: string;
163
+ blobId: string;
164
+ blobObject: Blob.$inferType;
165
+ }[] = walrusClient.writeFiles({
166
+ files: [file1, file2, file3],
100
167
  epochs: 3,
168
+ deletable: true,
101
169
  signer: keypair,
102
170
  });
103
171
  ```
104
172
 
105
- ### Writing blobs with an upload relay
173
+ Currently the provided files will all be written into a single quilt. Future versions of the SDK may
174
+ optimize how files are stored to be more efficient by splitting files into multiple quilts.
175
+
176
+ The current quilt encoding is less efficient for single files, so writing multiple files together is
177
+ recommended when possible. Writing raw blobs directly is also possible using the `writeBlob` API
178
+ (described below)
179
+
180
+ ### Writing files in browser environments
181
+
182
+ When the transactions to upload a blob are signed by a wallet in a browser, some wallets will use
183
+ popups to prompt the user for a signature. If the popups are not opened in direct response to a user
184
+ interaction, they may be blocked by the browser.
185
+
186
+ To avoid this, we need to ensure that we execute the transactions that register and certify the blob
187
+ in separate events handlers by creating separate buttons for the user to click for each step.
188
+
189
+ The `client.writeFilesFlow` method returns an object with a set of methods that break the write flow
190
+ into several smaller steps:
191
+
192
+ 1. `encode` - Encodes the files and generates a blobId
193
+ 2. `register` - returns a transaction that will register the blob on-chain
194
+ 3. `upload` - Uploads the data to storage nodes
195
+ 4. `certify` - returns a transaction that will certify the blob on-chain
196
+ 5. `listFiles` - returns a list of the created files
197
+
198
+ Here's a simplified example showing the core API usage with separate user interactions:
199
+
200
+ ```tsx
201
+ // Step 1: Create and encode the flow (can be done immediately when file is selected)
202
+ const flow = walrusClient.writeFilesFlow({
203
+ files: [
204
+ WalrusFile.from({
205
+ contents: new Uint8Array(fileData),
206
+ identifier: 'my-file.txt',
207
+ }),
208
+ ],
209
+ });
210
+
211
+ await flow.encode();
212
+
213
+ // Step 2: Register the blob (triggered by user clicking a register button after the encode step)
214
+ async function handleRegister() {
215
+ const registerTx = flow.register({
216
+ epochs: 3,
217
+ owner: currentAccount.address,
218
+ deletable: true,
219
+ });
220
+ const { digest } = await signAndExecuteTransaction({ transaction: registerTx });
221
+ // Step 3: Upload the data to storage nodes
222
+ // This can be done immediately after the register step, or as a separate step the user initiates
223
+ await flow.upload({ digest });
224
+ }
225
+
226
+ // Step 4: Certify the blob (triggered by user clicking a certify button after the blob is uploaded)
227
+ async function handleCertify() {
228
+ const certifyTx = flow.certify();
229
+
230
+ await signAndExecuteTransaction({ transaction: certifyTx });
231
+
232
+ // Step 5: Get the new files
233
+ const files = await flow.listFiles();
234
+ console.log('Uploaded files', files);
235
+ }
236
+ ```
237
+
238
+ This approach ensures that each transaction signing step is separated into different user
239
+ interactions, allowing wallet popups to work properly without being blocked by the browser.
240
+
241
+ ## Writing blobs with an upload relay
106
242
 
107
243
  Writing blobs directly from a client requires a lot of requests to write data to all of the storage
108
244
  nodes. An upload relay can be used to offload the work of these writes to a server, reducing
109
245
  complexity for the client.
110
246
 
111
- To use an upload relay, you can add the `uploadRelay` option when creating your `WalrusClient`:
247
+ To use an upload relay, you can add the `uploadRelay` option when creating your `WalrusClient`.
112
248
 
113
249
  ```ts
114
250
  const client = new SuiClient({
@@ -185,12 +321,42 @@ const client = new SuiClient({
185
321
  );
186
322
  ```
187
323
 
188
- ### Full API
324
+ ## Interacting with blobs directly
325
+
326
+ In case you do not want to use the `WalrusFile` abstractions, you can use the `readBlob` and
327
+ `writeBlob` APIs directly
328
+
329
+ ### Reading blobs
330
+
331
+ The `readBlob` method will read a blob given the blobId and return Uint8Array containing the blobs
332
+ content:
333
+
334
+ ```ts
335
+ const blob = await walrusClient.readBlob({ blobId });
336
+ ```
337
+
338
+ ### Writing Blobs
339
+
340
+ Thw writeBlob method can be used to write a blob (as a `Uint8Array`) to walrus. You will need to
341
+ specify how long the blob should be stored for, and if the blob should be deletable.
342
+
343
+ ```ts
344
+ const file = new TextEncoder().encode('Hello from the TS SDK!!!\n');
345
+
346
+ const { blobId } = await walrusClient.writeBlob({
347
+ blob: file,
348
+ deletable: false,
349
+ epochs: 3,
350
+ signer: keypair,
351
+ });
352
+ ```
353
+
354
+ ## Full API
189
355
 
190
356
  For a complete overview of the available methods on the `WalrusClient` you can reference type
191
357
  [TypeDocs](http://sdk.mystenlabs.com/typedoc/classes/_mysten_walrus.WalrusClient.html)
192
358
 
193
- ### Examples
359
+ ## Examples
194
360
 
195
361
  There are a number of simple
196
362
  [examples you can reference](https://github.com/MystenLabs/ts-sdks/tree/main/packages/walrus/examples)
@@ -281,7 +447,7 @@ const walrusClient = new WalrusClient({
281
447
  });
282
448
  ```
283
449
 
284
- ### Loading the wasm module in vite or client side apps
450
+ ## Loading the wasm module in vite or client side apps
285
451
 
286
452
  The walrus SDK requires wasm bindings to encode and decode blobs. When running in node or bun and
287
453
  some bundlers this will work without any additional configuration.
@@ -322,7 +488,7 @@ const nextConfig: NextConfig = {
322
488
  };
323
489
  ```
324
490
 
325
- ### Known fetch limitations you might run into
491
+ ## Known fetch limitations you might run into
326
492
 
327
493
  - Some nodes can be slow to respond. When running in node, the default connectTimeout is 10 seconds
328
494
  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 }: WriteFilesFlowOptions): WriteFilesFlow;
599
687
  }