@ckbfs/api 1.2.3 → 1.2.5

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.
@@ -0,0 +1,190 @@
1
+ import {
2
+ decodeWitnessContent,
3
+ decodeMultipleWitnessContents,
4
+ extractFileFromWitnesses,
5
+ decodeFileFromWitnessData,
6
+ saveFileFromWitnessData
7
+ } from '../src/index';
8
+
9
+ /**
10
+ * Simple demonstration of the new witness decoding APIs
11
+ * This script shows how to use the new methods with mock data
12
+ */
13
+
14
+ // Mock witness data for demonstration
15
+ const mockWitnesses = [
16
+ '0x', // Empty witness (typical for secp256k1 signature)
17
+ '0x434b42465300486f6c6c6f2c20574b424653212054686973206973206120746573742066696c652e', // CKBFS witness with "Hello, CKBFS! This is a test file."
18
+ '0x434b42465300204d6f726520636f6e74656e7420696e207365636f6e64206368756e6b2e', // CKBFS witness with " More content in second chunk."
19
+ '0x434b42465300204c61737420636875726b206f662074686520746573742066696c652e' // CKBFS witness with " Last chunk of the test file."
20
+ ];
21
+
22
+ const mockIndexes = [1, 2, 3]; // Indexes of CKBFS witnesses
23
+ const mockFilename = 'demo-file.txt';
24
+ const mockContentType = 'text/plain';
25
+
26
+ /**
27
+ * Demo 1: Decode individual witnesses
28
+ */
29
+ function demo1_DecodeIndividualWitnesses() {
30
+ console.log('=== Demo 1: Decode Individual Witnesses ===');
31
+
32
+ for (let i = 0; i < mockWitnesses.length; i++) {
33
+ const witness = mockWitnesses[i];
34
+ const decoded = decodeWitnessContent(witness);
35
+
36
+ if (decoded && decoded.isValid) {
37
+ const contentText = new TextDecoder().decode(decoded.content);
38
+ console.log(`Witness ${i}: "${contentText}" (${decoded.content.length} bytes)`);
39
+ } else {
40
+ console.log(`Witness ${i}: Not a CKBFS witness or invalid`);
41
+ }
42
+ }
43
+ console.log('');
44
+ }
45
+
46
+ /**
47
+ * Demo 2: Decode multiple witnesses at once
48
+ */
49
+ function demo2_DecodeMultipleWitnesses() {
50
+ console.log('=== Demo 2: Decode Multiple Witnesses ===');
51
+
52
+ // Get only the CKBFS witnesses
53
+ const ckbfsWitnesses = mockIndexes.map(idx => mockWitnesses[idx]);
54
+
55
+ const combinedContent = decodeMultipleWitnessContents(ckbfsWitnesses, true);
56
+ const contentText = new TextDecoder().decode(combinedContent);
57
+
58
+ console.log(`Combined content from ${ckbfsWitnesses.length} witnesses:`);
59
+ console.log(`"${contentText}"`);
60
+ console.log(`Total size: ${combinedContent.length} bytes`);
61
+ console.log('');
62
+ }
63
+
64
+ /**
65
+ * Demo 3: Extract file using witness indexes
66
+ */
67
+ function demo3_ExtractFileFromWitnesses() {
68
+ console.log('=== Demo 3: Extract File Using Indexes ===');
69
+
70
+ const extractedContent = extractFileFromWitnesses(mockWitnesses, mockIndexes);
71
+ const contentText = new TextDecoder().decode(extractedContent);
72
+
73
+ console.log(`Extracted content using indexes [${mockIndexes.join(', ')}]:`);
74
+ console.log(`"${contentText}"`);
75
+ console.log(`Size: ${extractedContent.length} bytes`);
76
+ console.log('');
77
+ }
78
+
79
+ /**
80
+ * Demo 4: High-level decode with metadata
81
+ */
82
+ function demo4_DecodeWithMetadata() {
83
+ console.log('=== Demo 4: Decode with Metadata ===');
84
+
85
+ const decodedFile = decodeFileFromWitnessData({
86
+ witnesses: mockWitnesses,
87
+ indexes: mockIndexes,
88
+ filename: mockFilename,
89
+ contentType: mockContentType
90
+ });
91
+
92
+ console.log('Decoded file information:');
93
+ console.log(`- Filename: ${decodedFile.filename}`);
94
+ console.log(`- Content Type: ${decodedFile.contentType}`);
95
+ console.log(`- Size: ${decodedFile.size} bytes`);
96
+
97
+ const contentText = new TextDecoder().decode(decodedFile.content);
98
+ console.log(`- Content: "${contentText}"`);
99
+ console.log('');
100
+ }
101
+
102
+ /**
103
+ * Demo 5: Save file from witness data
104
+ */
105
+ function demo5_SaveFileFromWitnessData() {
106
+ console.log('=== Demo 5: Save File from Witness Data ===');
107
+
108
+ try {
109
+ const savedPath = saveFileFromWitnessData({
110
+ witnesses: mockWitnesses,
111
+ indexes: mockIndexes,
112
+ filename: mockFilename,
113
+ contentType: mockContentType
114
+ }, './demo-output.txt');
115
+
116
+ console.log(`File saved successfully to: ${savedPath}`);
117
+
118
+ // Verify the saved file
119
+ const fs = require('fs');
120
+ if (fs.existsSync(savedPath)) {
121
+ const savedContent = fs.readFileSync(savedPath, 'utf-8');
122
+ console.log(`Verified saved content: "${savedContent}"`);
123
+ }
124
+ } catch (error) {
125
+ console.error('Error saving file:', error);
126
+ }
127
+ console.log('');
128
+ }
129
+
130
+ /**
131
+ * Demo 6: Error handling with invalid data
132
+ */
133
+ function demo6_ErrorHandling() {
134
+ console.log('=== Demo 6: Error Handling ===');
135
+
136
+ // Test with invalid witness
137
+ const invalidWitness = '0x1234567890abcdef'; // Not a CKBFS witness
138
+ const decoded = decodeWitnessContent(invalidWitness);
139
+ console.log(`Invalid witness result: ${decoded ? 'Valid' : 'Invalid (expected)'}`);
140
+
141
+ // Test with out-of-range indexes
142
+ const outOfRangeIndexes = [1, 2, 10]; // Index 10 doesn't exist
143
+ try {
144
+ const content = extractFileFromWitnesses(mockWitnesses, outOfRangeIndexes);
145
+ console.log(`Out-of-range handling: Successfully extracted ${content.length} bytes (partial content)`);
146
+ } catch (error) {
147
+ console.log(`Out-of-range handling: Error caught - ${error}`);
148
+ }
149
+
150
+ console.log('');
151
+ }
152
+
153
+ /**
154
+ * Main demo function
155
+ */
156
+ function main() {
157
+ console.log('CKBFS Witness Decoding APIs Demonstration');
158
+ console.log('==========================================');
159
+ console.log('');
160
+
161
+ console.log('Mock data:');
162
+ console.log(`- Total witnesses: ${mockWitnesses.length}`);
163
+ console.log(`- CKBFS witness indexes: [${mockIndexes.join(', ')}]`);
164
+ console.log(`- Expected filename: ${mockFilename}`);
165
+ console.log(`- Expected content type: ${mockContentType}`);
166
+ console.log('');
167
+
168
+ // Run all demos
169
+ demo1_DecodeIndividualWitnesses();
170
+ demo2_DecodeMultipleWitnesses();
171
+ demo3_ExtractFileFromWitnesses();
172
+ demo4_DecodeWithMetadata();
173
+ demo5_SaveFileFromWitnessData();
174
+ demo6_ErrorHandling();
175
+
176
+ console.log('=== Summary ===');
177
+ console.log('All demos completed successfully!');
178
+ console.log('');
179
+ console.log('Key benefits of the new witness decoding APIs:');
180
+ console.log('- Direct access to witness content without blockchain queries');
181
+ console.log('- Flexible handling of individual or multiple witnesses');
182
+ console.log('- Built-in error handling for invalid data');
183
+ console.log('- Metadata preservation and file operations');
184
+ console.log('- Suitable for offline processing and caching scenarios');
185
+ }
186
+
187
+ // Run the demo if this file is executed directly
188
+ if (require.main === module) {
189
+ main();
190
+ }
@@ -0,0 +1 @@
1
+ Hello CKBFS from direct content!