@snaha/swarm-id 0.0.1
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.
- package/README.md +431 -0
- package/dist/chunk/bmt.d.ts +17 -0
- package/dist/chunk/bmt.d.ts.map +1 -0
- package/dist/chunk/cac.d.ts +18 -0
- package/dist/chunk/cac.d.ts.map +1 -0
- package/dist/chunk/constants.d.ts +10 -0
- package/dist/chunk/constants.d.ts.map +1 -0
- package/dist/chunk/encrypted-cac.d.ts +48 -0
- package/dist/chunk/encrypted-cac.d.ts.map +1 -0
- package/dist/chunk/encryption.d.ts +86 -0
- package/dist/chunk/encryption.d.ts.map +1 -0
- package/dist/chunk/index.d.ts +6 -0
- package/dist/chunk/index.d.ts.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/proxy/act/act.d.ts +78 -0
- package/dist/proxy/act/act.d.ts.map +1 -0
- package/dist/proxy/act/crypto.d.ts +44 -0
- package/dist/proxy/act/crypto.d.ts.map +1 -0
- package/dist/proxy/act/grantee-list.d.ts +82 -0
- package/dist/proxy/act/grantee-list.d.ts.map +1 -0
- package/dist/proxy/act/history.d.ts +183 -0
- package/dist/proxy/act/history.d.ts.map +1 -0
- package/dist/proxy/act/index.d.ts +104 -0
- package/dist/proxy/act/index.d.ts.map +1 -0
- package/dist/proxy/chunking-encrypted.d.ts +14 -0
- package/dist/proxy/chunking-encrypted.d.ts.map +1 -0
- package/dist/proxy/chunking.d.ts +15 -0
- package/dist/proxy/chunking.d.ts.map +1 -0
- package/dist/proxy/download-data.d.ts +16 -0
- package/dist/proxy/download-data.d.ts.map +1 -0
- package/dist/proxy/feed-manifest.d.ts +62 -0
- package/dist/proxy/feed-manifest.d.ts.map +1 -0
- package/dist/proxy/feeds/epochs/async-finder.d.ts +77 -0
- package/dist/proxy/feeds/epochs/async-finder.d.ts.map +1 -0
- package/dist/proxy/feeds/epochs/epoch.d.ts +88 -0
- package/dist/proxy/feeds/epochs/epoch.d.ts.map +1 -0
- package/dist/proxy/feeds/epochs/finder.d.ts +67 -0
- package/dist/proxy/feeds/epochs/finder.d.ts.map +1 -0
- package/dist/proxy/feeds/epochs/index.d.ts +35 -0
- package/dist/proxy/feeds/epochs/index.d.ts.map +1 -0
- package/dist/proxy/feeds/epochs/test-utils.d.ts +93 -0
- package/dist/proxy/feeds/epochs/test-utils.d.ts.map +1 -0
- package/dist/proxy/feeds/epochs/types.d.ts +109 -0
- package/dist/proxy/feeds/epochs/types.d.ts.map +1 -0
- package/dist/proxy/feeds/epochs/updater.d.ts +68 -0
- package/dist/proxy/feeds/epochs/updater.d.ts.map +1 -0
- package/dist/proxy/feeds/epochs/utils.d.ts +22 -0
- package/dist/proxy/feeds/epochs/utils.d.ts.map +1 -0
- package/dist/proxy/feeds/index.d.ts +5 -0
- package/dist/proxy/feeds/index.d.ts.map +1 -0
- package/dist/proxy/feeds/sequence/async-finder.d.ts +14 -0
- package/dist/proxy/feeds/sequence/async-finder.d.ts.map +1 -0
- package/dist/proxy/feeds/sequence/finder.d.ts +17 -0
- package/dist/proxy/feeds/sequence/finder.d.ts.map +1 -0
- package/dist/proxy/feeds/sequence/index.d.ts +23 -0
- package/dist/proxy/feeds/sequence/index.d.ts.map +1 -0
- package/dist/proxy/feeds/sequence/types.d.ts +80 -0
- package/dist/proxy/feeds/sequence/types.d.ts.map +1 -0
- package/dist/proxy/feeds/sequence/updater.d.ts +26 -0
- package/dist/proxy/feeds/sequence/updater.d.ts.map +1 -0
- package/dist/proxy/index.d.ts +6 -0
- package/dist/proxy/index.d.ts.map +1 -0
- package/dist/proxy/manifest-builder.d.ts +183 -0
- package/dist/proxy/manifest-builder.d.ts.map +1 -0
- package/dist/proxy/mantaray-encrypted.d.ts +27 -0
- package/dist/proxy/mantaray-encrypted.d.ts.map +1 -0
- package/dist/proxy/mantaray.d.ts +26 -0
- package/dist/proxy/mantaray.d.ts.map +1 -0
- package/dist/proxy/types.d.ts +29 -0
- package/dist/proxy/types.d.ts.map +1 -0
- package/dist/proxy/upload-data.d.ts +17 -0
- package/dist/proxy/upload-data.d.ts.map +1 -0
- package/dist/proxy/upload-encrypted-data.d.ts +103 -0
- package/dist/proxy/upload-encrypted-data.d.ts.map +1 -0
- package/dist/schemas.d.ts +240 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/storage/debounced-uploader.d.ts +62 -0
- package/dist/storage/debounced-uploader.d.ts.map +1 -0
- package/dist/storage/utilization-store.d.ts +108 -0
- package/dist/storage/utilization-store.d.ts.map +1 -0
- package/dist/swarm-id-auth.d.ts +74 -0
- package/dist/swarm-id-auth.d.ts.map +1 -0
- package/dist/swarm-id-auth.js +2 -0
- package/dist/swarm-id-auth.js.map +1 -0
- package/dist/swarm-id-client.d.ts +878 -0
- package/dist/swarm-id-client.d.ts.map +1 -0
- package/dist/swarm-id-client.js +2 -0
- package/dist/swarm-id-client.js.map +1 -0
- package/dist/swarm-id-proxy.d.ts +236 -0
- package/dist/swarm-id-proxy.d.ts.map +1 -0
- package/dist/swarm-id-proxy.js +2 -0
- package/dist/swarm-id-proxy.js.map +1 -0
- package/dist/swarm-id.esm.js +2 -0
- package/dist/swarm-id.esm.js.map +1 -0
- package/dist/swarm-id.umd.js +2 -0
- package/dist/swarm-id.umd.js.map +1 -0
- package/dist/sync/index.d.ts +9 -0
- package/dist/sync/index.d.ts.map +1 -0
- package/dist/sync/key-derivation.d.ts +25 -0
- package/dist/sync/key-derivation.d.ts.map +1 -0
- package/dist/sync/restore-account.d.ts +28 -0
- package/dist/sync/restore-account.d.ts.map +1 -0
- package/dist/sync/serialization.d.ts +16 -0
- package/dist/sync/serialization.d.ts.map +1 -0
- package/dist/sync/store-interfaces.d.ts +53 -0
- package/dist/sync/store-interfaces.d.ts.map +1 -0
- package/dist/sync/sync-account.d.ts +44 -0
- package/dist/sync/sync-account.d.ts.map +1 -0
- package/dist/sync/types.d.ts +13 -0
- package/dist/sync/types.d.ts.map +1 -0
- package/dist/test-fixtures.d.ts +17 -0
- package/dist/test-fixtures.d.ts.map +1 -0
- package/dist/types-BD_VkNn0.js +2 -0
- package/dist/types-BD_VkNn0.js.map +1 -0
- package/dist/types-lJCaT-50.js +2 -0
- package/dist/types-lJCaT-50.js.map +1 -0
- package/dist/types.d.ts +2157 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/account-payload.d.ts +94 -0
- package/dist/utils/account-payload.d.ts.map +1 -0
- package/dist/utils/account-state-snapshot.d.ts +38 -0
- package/dist/utils/account-state-snapshot.d.ts.map +1 -0
- package/dist/utils/backup-encryption.d.ts +127 -0
- package/dist/utils/backup-encryption.d.ts.map +1 -0
- package/dist/utils/batch-utilization.d.ts +432 -0
- package/dist/utils/batch-utilization.d.ts.map +1 -0
- package/dist/utils/constants.d.ts +11 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/hex.d.ts +17 -0
- package/dist/utils/hex.d.ts.map +1 -0
- package/dist/utils/key-derivation.d.ts +92 -0
- package/dist/utils/key-derivation.d.ts.map +1 -0
- package/dist/utils/storage-managers.d.ts +65 -0
- package/dist/utils/storage-managers.d.ts.map +1 -0
- package/dist/utils/swarm-id-export.d.ts +24 -0
- package/dist/utils/swarm-id-export.d.ts.map +1 -0
- package/dist/utils/ttl.d.ts +49 -0
- package/dist/utils/ttl.d.ts.map +1 -0
- package/dist/utils/url.d.ts +41 -0
- package/dist/utils/url.d.ts.map +1 -0
- package/dist/utils/versioned-storage.d.ts +131 -0
- package/dist/utils/versioned-storage.d.ts.map +1 -0
- package/package.json +78 -0
- package/src/chunk/bmt.test.ts +217 -0
- package/src/chunk/bmt.ts +57 -0
- package/src/chunk/cac.test.ts +214 -0
- package/src/chunk/cac.ts +65 -0
- package/src/chunk/constants.ts +18 -0
- package/src/chunk/encrypted-cac.test.ts +385 -0
- package/src/chunk/encrypted-cac.ts +131 -0
- package/src/chunk/encryption.test.ts +352 -0
- package/src/chunk/encryption.ts +300 -0
- package/src/chunk/index.ts +47 -0
- package/src/index.ts +430 -0
- package/src/proxy/act/act.test.ts +278 -0
- package/src/proxy/act/act.ts +158 -0
- package/src/proxy/act/bee-compat.test.ts +948 -0
- package/src/proxy/act/crypto.test.ts +436 -0
- package/src/proxy/act/crypto.ts +376 -0
- package/src/proxy/act/grantee-list.test.ts +393 -0
- package/src/proxy/act/grantee-list.ts +239 -0
- package/src/proxy/act/history.test.ts +360 -0
- package/src/proxy/act/history.ts +413 -0
- package/src/proxy/act/index.test.ts +748 -0
- package/src/proxy/act/index.ts +853 -0
- package/src/proxy/chunking-encrypted.ts +95 -0
- package/src/proxy/chunking.ts +65 -0
- package/src/proxy/download-data.ts +448 -0
- package/src/proxy/feed-manifest.ts +174 -0
- package/src/proxy/feeds/epochs/async-finder.ts +372 -0
- package/src/proxy/feeds/epochs/epoch.test.ts +249 -0
- package/src/proxy/feeds/epochs/epoch.ts +181 -0
- package/src/proxy/feeds/epochs/finder.ts +282 -0
- package/src/proxy/feeds/epochs/index.ts +73 -0
- package/src/proxy/feeds/epochs/integration.test.ts +1336 -0
- package/src/proxy/feeds/epochs/test-utils.ts +274 -0
- package/src/proxy/feeds/epochs/types.ts +128 -0
- package/src/proxy/feeds/epochs/updater.ts +192 -0
- package/src/proxy/feeds/epochs/utils.ts +62 -0
- package/src/proxy/feeds/index.ts +5 -0
- package/src/proxy/feeds/sequence/async-finder.ts +31 -0
- package/src/proxy/feeds/sequence/finder.ts +73 -0
- package/src/proxy/feeds/sequence/index.ts +54 -0
- package/src/proxy/feeds/sequence/integration.test.ts +966 -0
- package/src/proxy/feeds/sequence/types.ts +103 -0
- package/src/proxy/feeds/sequence/updater.ts +71 -0
- package/src/proxy/index.ts +5 -0
- package/src/proxy/manifest-builder.test.ts +427 -0
- package/src/proxy/manifest-builder.ts +679 -0
- package/src/proxy/mantaray-encrypted.ts +78 -0
- package/src/proxy/mantaray.ts +104 -0
- package/src/proxy/types.ts +32 -0
- package/src/proxy/upload-data.ts +189 -0
- package/src/proxy/upload-encrypted-data.ts +658 -0
- package/src/schemas.ts +299 -0
- package/src/storage/debounced-uploader.ts +192 -0
- package/src/storage/utilization-store.ts +397 -0
- package/src/swarm-id-client.test.ts +99 -0
- package/src/swarm-id-client.ts +3095 -0
- package/src/swarm-id-proxy.ts +3891 -0
- package/src/sync/index.ts +28 -0
- package/src/sync/restore-account.ts +90 -0
- package/src/sync/serialization.ts +39 -0
- package/src/sync/store-interfaces.ts +62 -0
- package/src/sync/sync-account.test.ts +302 -0
- package/src/sync/sync-account.ts +396 -0
- package/src/sync/types.ts +11 -0
- package/src/test-fixtures.ts +109 -0
- package/src/types.ts +1651 -0
- package/src/utils/account-state-snapshot.test.ts +595 -0
- package/src/utils/account-state-snapshot.ts +94 -0
- package/src/utils/backup-encryption.test.ts +442 -0
- package/src/utils/backup-encryption.ts +352 -0
- package/src/utils/batch-utilization.ts +1309 -0
- package/src/utils/constants.ts +20 -0
- package/src/utils/hex.ts +27 -0
- package/src/utils/key-derivation.ts +197 -0
- package/src/utils/storage-managers.ts +365 -0
- package/src/utils/ttl.ts +129 -0
- package/src/utils/url.test.ts +136 -0
- package/src/utils/url.ts +71 -0
- package/src/utils/versioned-storage.ts +323 -0
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Batch Utilization Tracking for Swarm Storage
|
|
3
|
+
*
|
|
4
|
+
* This module implements utilization tracking for mutable postage batches.
|
|
5
|
+
* It manages two counter arrays:
|
|
6
|
+
* - Utilization counters (local, uint8): Track slots 0-255 per bucket for utilization chunks
|
|
7
|
+
* - Data counters (on-chain, uint32): Track slots 256+ per bucket for data chunks
|
|
8
|
+
*
|
|
9
|
+
* The system uses pre-calculation to handle the circular dependency of storing
|
|
10
|
+
* utilization data that tracks the storage of itself.
|
|
11
|
+
*/
|
|
12
|
+
import { Stamper, BatchId, Topic, Identifier, type Bee, EthAddress, type EnvelopeWithBatchId } from "@ethersphere/bee-js";
|
|
13
|
+
import { type ContentAddressedChunk } from "../chunk";
|
|
14
|
+
import { type Chunk as CafeChunk } from "cafe-utility";
|
|
15
|
+
import type { UtilizationStoreDB } from "../storage/utilization-store";
|
|
16
|
+
/** Number of buckets in a postage batch (2^16) */
|
|
17
|
+
export declare const NUM_BUCKETS = 65536;
|
|
18
|
+
/** Bucket depth parameter (determines bucket count) */
|
|
19
|
+
export declare const BUCKET_DEPTH = 16;
|
|
20
|
+
/** Number of slots reserved per bucket for utilization chunks (0-3) */
|
|
21
|
+
export declare const UTILIZATION_SLOTS_PER_BUCKET = 4;
|
|
22
|
+
/** Starting slot index for data chunks */
|
|
23
|
+
export declare const DATA_COUNTER_START = 4;
|
|
24
|
+
/** Size of each chunk in bytes */
|
|
25
|
+
export declare const CHUNK_SIZE = 4096;
|
|
26
|
+
/** Batch depth for N=256 slots per bucket with 65536 buckets */
|
|
27
|
+
export declare const DEFAULT_BATCH_DEPTH = 24;
|
|
28
|
+
/**
|
|
29
|
+
* Metadata for a single utilization chunk
|
|
30
|
+
*/
|
|
31
|
+
export interface ChunkMetadata {
|
|
32
|
+
/** Chunk index (0-63) */
|
|
33
|
+
index: number;
|
|
34
|
+
/**
|
|
35
|
+
* Content hash / CAC reference (same thing for content-addressed chunks)
|
|
36
|
+
* Empty string means never uploaded
|
|
37
|
+
*/
|
|
38
|
+
contentHash: string;
|
|
39
|
+
/** Last upload timestamp */
|
|
40
|
+
lastUpload: number;
|
|
41
|
+
/** Whether this chunk needs uploading */
|
|
42
|
+
dirty: boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Utilization state for a postage batch
|
|
46
|
+
*
|
|
47
|
+
* This new version stores utilization data as 64 chunks on Swarm
|
|
48
|
+
* with IndexedDB caching for performance.
|
|
49
|
+
*/
|
|
50
|
+
export interface BatchUtilizationState {
|
|
51
|
+
/** Batch ID this state belongs to */
|
|
52
|
+
batchId: BatchId;
|
|
53
|
+
/** Data counters (65,536 uint32 values) */
|
|
54
|
+
dataCounters: Uint32Array;
|
|
55
|
+
/** Metadata for each of the 64 utilization chunks */
|
|
56
|
+
chunks: ChunkMetadata[];
|
|
57
|
+
/** Topic for SOC storage */
|
|
58
|
+
topic: Topic;
|
|
59
|
+
/** Last sync timestamp */
|
|
60
|
+
lastSync: number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Chunk with bucket assignment
|
|
64
|
+
*/
|
|
65
|
+
export interface ChunkWithBucket {
|
|
66
|
+
chunk: ContentAddressedChunk;
|
|
67
|
+
bucket: number;
|
|
68
|
+
slot: number;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Result of calculating utilization update
|
|
72
|
+
*/
|
|
73
|
+
export interface UtilizationUpdate {
|
|
74
|
+
/** Updated data counters */
|
|
75
|
+
dataCounters: Uint32Array;
|
|
76
|
+
/** Utilization chunks to upload */
|
|
77
|
+
utilizationChunks: ChunkWithBucket[];
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Calculate which bucket a chunk belongs to based on its address.
|
|
81
|
+
* Uses the first 2 bytes of the chunk address as a big-endian uint16.
|
|
82
|
+
*
|
|
83
|
+
* This matches bee-js Stamper implementation.
|
|
84
|
+
*
|
|
85
|
+
* @param chunkAddress - The chunk's content address (32 bytes)
|
|
86
|
+
* @returns Bucket index (0-65535)
|
|
87
|
+
*/
|
|
88
|
+
export declare function toBucket(chunkAddress: Uint8Array): number;
|
|
89
|
+
/**
|
|
90
|
+
* Calculate bucket assignments for multiple chunks
|
|
91
|
+
*/
|
|
92
|
+
export declare function assignChunksToBuckets(chunks: ContentAddressedChunk[]): ChunkWithBucket[];
|
|
93
|
+
/**
|
|
94
|
+
* Number of utilization chunks (64 chunks of 4KB each = 262KB total)
|
|
95
|
+
* Each chunk contains 1,024 buckets (1,024 × 4 bytes = 4,096 bytes)
|
|
96
|
+
*/
|
|
97
|
+
export declare const NUM_UTILIZATION_CHUNKS = 64;
|
|
98
|
+
export declare const BUCKETS_PER_CHUNK = 1024;
|
|
99
|
+
/**
|
|
100
|
+
* Calculate which utilization chunk a bucket belongs to
|
|
101
|
+
* @param bucketIndex - Bucket index (0-65535)
|
|
102
|
+
* @returns Chunk index (0-63)
|
|
103
|
+
*/
|
|
104
|
+
export declare function getChunkIndexForBucket(bucketIndex: number): number;
|
|
105
|
+
/**
|
|
106
|
+
* Calculate the offset of a bucket within its chunk
|
|
107
|
+
* @param bucketIndex - Bucket index (0-65535)
|
|
108
|
+
* @returns Offset within chunk (0-1023)
|
|
109
|
+
*/
|
|
110
|
+
export declare function getBucketOffsetInChunk(bucketIndex: number): number;
|
|
111
|
+
/**
|
|
112
|
+
* Extract a 4KB chunk from the dataCounters array
|
|
113
|
+
* @param dataCounters - Full array of 65,536 counters
|
|
114
|
+
* @param chunkIndex - Index of chunk to extract (0-63)
|
|
115
|
+
* @returns 4KB Uint8Array containing serialized counters for this chunk
|
|
116
|
+
*/
|
|
117
|
+
export declare function extractChunk(dataCounters: Uint32Array, chunkIndex: number): Uint8Array;
|
|
118
|
+
/**
|
|
119
|
+
* Merge a 4KB chunk back into the dataCounters array
|
|
120
|
+
* @param dataCounters - Full array of 65,536 counters (modified in place)
|
|
121
|
+
* @param chunkIndex - Index of chunk to merge (0-63)
|
|
122
|
+
* @param chunkData - 4KB Uint8Array containing serialized counters
|
|
123
|
+
*/
|
|
124
|
+
export declare function mergeChunk(dataCounters: Uint32Array, chunkIndex: number, chunkData: Uint8Array): void;
|
|
125
|
+
/**
|
|
126
|
+
* Tracks which utilization chunks have been modified and need uploading
|
|
127
|
+
*/
|
|
128
|
+
export declare class DirtyChunkTracker {
|
|
129
|
+
private dirtyChunks;
|
|
130
|
+
constructor();
|
|
131
|
+
/**
|
|
132
|
+
* Mark a bucket as dirty (marks its containing chunk)
|
|
133
|
+
* @param bucketIndex - Bucket index (0-65535)
|
|
134
|
+
*/
|
|
135
|
+
markDirty(bucketIndex: number): void;
|
|
136
|
+
/**
|
|
137
|
+
* Mark a chunk as clean (uploaded successfully)
|
|
138
|
+
* @param chunkIndex - Chunk index (0-63)
|
|
139
|
+
*/
|
|
140
|
+
markClean(chunkIndex: number): void;
|
|
141
|
+
/**
|
|
142
|
+
* Get array of dirty chunk indices
|
|
143
|
+
* @returns Sorted array of chunk indices that need uploading
|
|
144
|
+
*/
|
|
145
|
+
getDirtyChunks(): number[];
|
|
146
|
+
/**
|
|
147
|
+
* Check if there are any dirty chunks
|
|
148
|
+
* @returns true if there are chunks waiting to be uploaded
|
|
149
|
+
*/
|
|
150
|
+
hasDirtyChunks(): boolean;
|
|
151
|
+
/**
|
|
152
|
+
* Clear all dirty markers
|
|
153
|
+
*/
|
|
154
|
+
clear(): void;
|
|
155
|
+
/**
|
|
156
|
+
* Get number of dirty chunks
|
|
157
|
+
* @returns Count of chunks waiting to be uploaded
|
|
158
|
+
*/
|
|
159
|
+
get count(): number;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Create a topic for batch utilization storage
|
|
163
|
+
* Topic format: `batch-utilization:{batchId}`
|
|
164
|
+
*
|
|
165
|
+
* @param batchId - Batch ID
|
|
166
|
+
* @returns Topic for this batch's utilization data
|
|
167
|
+
*/
|
|
168
|
+
export declare function makeBatchUtilizationTopic(batchId: BatchId): Topic;
|
|
169
|
+
/**
|
|
170
|
+
* Create an identifier for a specific utilization chunk
|
|
171
|
+
* Identifier: Keccak256(topic || chunkIndex)
|
|
172
|
+
*
|
|
173
|
+
* @param topic - Batch utilization topic
|
|
174
|
+
* @param chunkIndex - Chunk index (0-63)
|
|
175
|
+
* @returns Identifier for this chunk
|
|
176
|
+
*/
|
|
177
|
+
export declare function makeChunkIdentifier(topic: Topic, chunkIndex: number): Identifier;
|
|
178
|
+
/**
|
|
179
|
+
* Upload an encrypted utilization chunk to Swarm as CAC
|
|
180
|
+
*
|
|
181
|
+
* Architecture: Just upload encrypted chunk data as CAC (immutable)
|
|
182
|
+
*
|
|
183
|
+
* @param bee - Bee client instance
|
|
184
|
+
* @param batchId - Batch ID (for logging)
|
|
185
|
+
* @param postageBatchId - Postage stamp batch ID
|
|
186
|
+
* @param chunkIndex - Chunk index (0-63)
|
|
187
|
+
* @param data - Chunk data to upload (4KB)
|
|
188
|
+
* @param encryptionKey - Encryption key (32 bytes)
|
|
189
|
+
* @returns CAC reference
|
|
190
|
+
*/
|
|
191
|
+
export declare function uploadUtilizationChunk(bee: Bee, stamper: Stamper, chunkIndex: number, data: Uint8Array, encryptionKey: Uint8Array): Promise<Uint8Array>;
|
|
192
|
+
/**
|
|
193
|
+
* Download and decrypt a utilization chunk from Swarm by CAC reference
|
|
194
|
+
*
|
|
195
|
+
* @param bee - Bee client instance
|
|
196
|
+
* @param cacReference - CAC reference (32 bytes)
|
|
197
|
+
* @param chunkIndex - Chunk index (for logging)
|
|
198
|
+
* @param encryptionKey - Encryption key (32 bytes)
|
|
199
|
+
* @returns Decrypted chunk data (4KB) or undefined if not found
|
|
200
|
+
*/
|
|
201
|
+
export declare function downloadUtilizationChunk(bee: Bee, cacReference: Uint8Array, chunkIndex: number, encryptionKey: Uint8Array): Promise<Uint8Array | undefined>;
|
|
202
|
+
/**
|
|
203
|
+
* Serialize Uint32Array to bytes (little-endian)
|
|
204
|
+
*/
|
|
205
|
+
export declare function serializeUint32Array(arr: Uint32Array): Uint8Array;
|
|
206
|
+
/**
|
|
207
|
+
* Deserialize bytes to Uint32Array (little-endian)
|
|
208
|
+
*/
|
|
209
|
+
export declare function deserializeUint32Array(bytes: Uint8Array): Uint32Array;
|
|
210
|
+
/**
|
|
211
|
+
* Split data into 4KB chunks
|
|
212
|
+
*/
|
|
213
|
+
export declare function splitIntoChunks(data: Uint8Array): ContentAddressedChunk[];
|
|
214
|
+
/**
|
|
215
|
+
* Reconstruct data from chunks
|
|
216
|
+
*/
|
|
217
|
+
export declare function reconstructFromChunks(chunks: ContentAddressedChunk[], originalLength: number): Uint8Array;
|
|
218
|
+
/**
|
|
219
|
+
* Initialize a new batch utilization state
|
|
220
|
+
*
|
|
221
|
+
* Reserves slots 0-3 per bucket for utilization metadata chunks,
|
|
222
|
+
* and starts data chunks at slot 4 (DATA_COUNTER_START).
|
|
223
|
+
*
|
|
224
|
+
* With 65,536 buckets and ~64 utilization chunks, the probability
|
|
225
|
+
* of any bucket getting 4+ utilization chunks is negligible (< 0.0000001%).
|
|
226
|
+
*/
|
|
227
|
+
export declare function initializeBatchUtilization(batchId: BatchId): BatchUtilizationState;
|
|
228
|
+
/**
|
|
229
|
+
* Calculate max slots per bucket based on batch depth
|
|
230
|
+
*/
|
|
231
|
+
export declare function calculateMaxSlotsPerBucket(batchDepth: number): number;
|
|
232
|
+
/**
|
|
233
|
+
* Check if a bucket has capacity for more chunks
|
|
234
|
+
*/
|
|
235
|
+
export declare function hasBucketCapacity(dataCounter: number, batchDepth: number): boolean;
|
|
236
|
+
/**
|
|
237
|
+
* Pre-calculate utilization update after writing data chunks.
|
|
238
|
+
*
|
|
239
|
+
* This solves the circular dependency problem:
|
|
240
|
+
* 1. Assign buckets/slots to data chunks
|
|
241
|
+
* 2. Update data counters
|
|
242
|
+
* 3. Serialize data counters into utilization chunks
|
|
243
|
+
* 4. Calculate where utilization chunks will land
|
|
244
|
+
* 5. Assign slots 0-N to utilization chunks per bucket
|
|
245
|
+
*
|
|
246
|
+
* Note: Utilization chunks always start from slot 0 since mutable stamps
|
|
247
|
+
* allow overwriting. No need to track previous positions.
|
|
248
|
+
*
|
|
249
|
+
* @param state - Current utilization state
|
|
250
|
+
* @param dataChunks - Data chunks to be written
|
|
251
|
+
* @param batchDepth - Batch depth parameter
|
|
252
|
+
* @returns Updated state and utilization chunks to upload
|
|
253
|
+
*/
|
|
254
|
+
export declare function calculateUtilizationUpdate(state: BatchUtilizationState, dataChunks: ContentAddressedChunk[], batchDepth: number): UtilizationUpdate;
|
|
255
|
+
/**
|
|
256
|
+
* Create a Stamper with custom bucket state for mutable stamping
|
|
257
|
+
*
|
|
258
|
+
* @param privateKey - Private key for signing
|
|
259
|
+
* @param batchId - Batch ID
|
|
260
|
+
* @param bucketState - Custom bucket heights (for resuming or mutable overwrites)
|
|
261
|
+
* @param batchDepth - Batch depth parameter
|
|
262
|
+
*/
|
|
263
|
+
export declare function createStamper(privateKey: Uint8Array | string, batchId: BatchId, bucketState: Uint32Array, batchDepth: number): Stamper;
|
|
264
|
+
/**
|
|
265
|
+
* Prepare bucket state for stamping chunks with specific slots
|
|
266
|
+
*
|
|
267
|
+
* @param chunksWithBuckets - Chunks with assigned buckets and slots
|
|
268
|
+
* @returns Bucket state array for Stamper
|
|
269
|
+
*/
|
|
270
|
+
export declare function prepareBucketState(chunksWithBuckets: ChunkWithBucket[]): Uint32Array;
|
|
271
|
+
/**
|
|
272
|
+
* Convert utilization data counters to Stamper bucket state
|
|
273
|
+
*
|
|
274
|
+
* Each dataCounter[bucket] represents the number of slots used in that bucket.
|
|
275
|
+
* The Stamper's bucket state should start at the next available slot.
|
|
276
|
+
*
|
|
277
|
+
* @param dataCounters - Current utilization counters (65536 buckets)
|
|
278
|
+
* @returns Bucket state array for Stamper (65536 entries)
|
|
279
|
+
*/
|
|
280
|
+
export declare function utilizationToBucketState(dataCounters: Uint32Array): Uint32Array;
|
|
281
|
+
/**
|
|
282
|
+
* Load utilization state with cache hierarchy
|
|
283
|
+
*
|
|
284
|
+
* Load order:
|
|
285
|
+
* 1. Try IndexedDB cache (all 64 chunks)
|
|
286
|
+
* 2. If incomplete, download missing chunks from Swarm
|
|
287
|
+
* 3. If not found, initialize new state
|
|
288
|
+
* 4. Cache downloaded chunks in IndexedDB
|
|
289
|
+
*
|
|
290
|
+
* @param batchId - Batch ID
|
|
291
|
+
* @param options - Load options with bee, owner, encryption key, and cache
|
|
292
|
+
* @returns Utilization state
|
|
293
|
+
*/
|
|
294
|
+
export declare function loadUtilizationState(batchId: BatchId, options: {
|
|
295
|
+
bee: Bee;
|
|
296
|
+
owner: EthAddress;
|
|
297
|
+
encryptionKey: Uint8Array;
|
|
298
|
+
cache: UtilizationStoreDB;
|
|
299
|
+
}): Promise<BatchUtilizationState>;
|
|
300
|
+
/**
|
|
301
|
+
* Save utilization state with incremental upload
|
|
302
|
+
*
|
|
303
|
+
* Only uploads dirty chunks to minimize network traffic.
|
|
304
|
+
* Updates IndexedDB cache with new chunk data.
|
|
305
|
+
*
|
|
306
|
+
* @param state - Current utilization state (modified in place)
|
|
307
|
+
* @param options - Save options
|
|
308
|
+
*/
|
|
309
|
+
export declare function saveUtilizationState(state: BatchUtilizationState, options: {
|
|
310
|
+
bee: Bee;
|
|
311
|
+
stamper: Stamper;
|
|
312
|
+
encryptionKey: Uint8Array;
|
|
313
|
+
cache: UtilizationStoreDB;
|
|
314
|
+
tracker: DirtyChunkTracker;
|
|
315
|
+
}): Promise<void>;
|
|
316
|
+
/**
|
|
317
|
+
* Update utilization state after writing data chunks
|
|
318
|
+
*
|
|
319
|
+
* This function:
|
|
320
|
+
* 1. Loads current state (from cache or Swarm)
|
|
321
|
+
* 2. Updates bucket counters for new data chunks
|
|
322
|
+
* 3. Marks affected utilization chunks as dirty
|
|
323
|
+
* 4. Returns state and tracker for later upload
|
|
324
|
+
*
|
|
325
|
+
* @param batchId - Batch ID
|
|
326
|
+
* @param dataChunks - Data chunks that were written
|
|
327
|
+
* @param batchDepth - Batch depth parameter
|
|
328
|
+
* @param options - Load options for state retrieval
|
|
329
|
+
* @returns Updated state and dirty chunk tracker
|
|
330
|
+
*/
|
|
331
|
+
export declare function updateAfterWrite(batchId: BatchId, dataChunks: ContentAddressedChunk[], batchDepth: number, options: {
|
|
332
|
+
bee: Bee;
|
|
333
|
+
owner: EthAddress;
|
|
334
|
+
encryptionKey: Uint8Array;
|
|
335
|
+
cache: UtilizationStoreDB;
|
|
336
|
+
}): Promise<{
|
|
337
|
+
state: BatchUtilizationState;
|
|
338
|
+
tracker: DirtyChunkTracker;
|
|
339
|
+
}>;
|
|
340
|
+
/**
|
|
341
|
+
* Calculate current utilization fraction for a batch
|
|
342
|
+
*
|
|
343
|
+
* @param state - Current utilization state
|
|
344
|
+
* @param batchDepth - Batch depth parameter
|
|
345
|
+
* @returns Utilization as decimal fraction (0-1)
|
|
346
|
+
*/
|
|
347
|
+
export declare function calculateUtilization(state: BatchUtilizationState, batchDepth: number): number;
|
|
348
|
+
/**
|
|
349
|
+
* Stamper wrapper that maintains bucket state from utilization data
|
|
350
|
+
*
|
|
351
|
+
* This class wraps the cafe-utility Stamper and:
|
|
352
|
+
* - Loads bucket state from cached utilization data on creation
|
|
353
|
+
* - Tracks which buckets/slots are used during stamping
|
|
354
|
+
* - Provides a flush() method to persist updates back to cache
|
|
355
|
+
*
|
|
356
|
+
* This ensures the Stamper always has accurate knowledge of which
|
|
357
|
+
* buckets/slots are already used, preventing overwrites.
|
|
358
|
+
*/
|
|
359
|
+
export declare class UtilizationAwareStamper implements Stamper {
|
|
360
|
+
private stamper;
|
|
361
|
+
private utilizationState;
|
|
362
|
+
private cache;
|
|
363
|
+
private dirty;
|
|
364
|
+
private dirtyBuckets;
|
|
365
|
+
readonly batchId: BatchId;
|
|
366
|
+
readonly depth: number;
|
|
367
|
+
get signer(): import("@ethersphere/bee-js").PrivateKey;
|
|
368
|
+
get buckets(): Uint32Array<ArrayBufferLike>;
|
|
369
|
+
get maxSlot(): number;
|
|
370
|
+
private constructor();
|
|
371
|
+
/**
|
|
372
|
+
* Create a UtilizationAwareStamper with bucket state from cache
|
|
373
|
+
*
|
|
374
|
+
* @param privateKey - Signer private key
|
|
375
|
+
* @param batchId - Postage batch ID
|
|
376
|
+
* @param depth - Batch depth
|
|
377
|
+
* @param cache - Utilization cache database
|
|
378
|
+
* @param _owner - Owner address (required for validation, reserved for future Swarm upload)
|
|
379
|
+
* @param _encryptionKey - Encryption key (required for validation, reserved for future Swarm upload)
|
|
380
|
+
* @returns New UtilizationAwareStamper instance
|
|
381
|
+
*/
|
|
382
|
+
static create(privateKey: Uint8Array | string, batchId: BatchId, depth: number, cache: UtilizationStoreDB, _owner: EthAddress, _encryptionKey: Uint8Array): Promise<UtilizationAwareStamper>;
|
|
383
|
+
/**
|
|
384
|
+
* Stamp a chunk (implements Stamper interface)
|
|
385
|
+
*
|
|
386
|
+
* Delegates to underlying stamper and tracks which buckets are used.
|
|
387
|
+
*
|
|
388
|
+
* @param chunk - Chunk to stamp
|
|
389
|
+
* @returns Envelope with batch ID and signature
|
|
390
|
+
*/
|
|
391
|
+
stamp(chunk: CafeChunk): EnvelopeWithBatchId;
|
|
392
|
+
/**
|
|
393
|
+
* Get bucket state (implements Stamper interface)
|
|
394
|
+
*/
|
|
395
|
+
getState(): Uint32Array;
|
|
396
|
+
/**
|
|
397
|
+
* Flush dirty utilization chunks to cache
|
|
398
|
+
*
|
|
399
|
+
* This persists any bucket state changes made during stamping.
|
|
400
|
+
* Should be called after all stamping operations are complete.
|
|
401
|
+
*/
|
|
402
|
+
flush(): Promise<void>;
|
|
403
|
+
/**
|
|
404
|
+
* Get current utilization state
|
|
405
|
+
*
|
|
406
|
+
* @returns Current utilization state
|
|
407
|
+
*/
|
|
408
|
+
getUtilizationState(): BatchUtilizationState;
|
|
409
|
+
/**
|
|
410
|
+
* Apply utilization update from another tab.
|
|
411
|
+
* Updates local bucket counters to match leader's state.
|
|
412
|
+
*
|
|
413
|
+
* @param buckets Array of bucket indices and their new counter values
|
|
414
|
+
*/
|
|
415
|
+
applyUtilizationUpdate(buckets: Array<{
|
|
416
|
+
index: number;
|
|
417
|
+
value: number;
|
|
418
|
+
}>): void;
|
|
419
|
+
/**
|
|
420
|
+
* Get bucket counter values for broadcasting to other tabs.
|
|
421
|
+
* Returns only the dirty buckets with their current values.
|
|
422
|
+
*
|
|
423
|
+
* IMPORTANT: Call this BEFORE flush() as flush() clears dirtyBuckets.
|
|
424
|
+
*
|
|
425
|
+
* @returns Array of bucket index/value pairs for broadcasting
|
|
426
|
+
*/
|
|
427
|
+
getBucketUpdatesForBroadcast(): Array<{
|
|
428
|
+
index: number;
|
|
429
|
+
value: number;
|
|
430
|
+
}>;
|
|
431
|
+
}
|
|
432
|
+
//# sourceMappingURL=batch-utilization.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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AAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,GAC/C,IAAI;IAyBP;;;;;;;OAOG;IACH,4BAA4B,IAAI,KAAK,CAAC;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;CAMxE"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Time and Session Constants
|
|
3
|
+
*
|
|
4
|
+
* Centralized constants for time units and default session durations.
|
|
5
|
+
*/
|
|
6
|
+
export declare const SECOND = 1000;
|
|
7
|
+
export declare const MINUTE: number;
|
|
8
|
+
export declare const HOUR: number;
|
|
9
|
+
export declare const DAY: number;
|
|
10
|
+
export declare const DEFAULT_SESSION_DURATION: number;
|
|
11
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/utils/constants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH,eAAO,MAAM,MAAM,OAAQ,CAAA;AAC3B,eAAO,MAAM,MAAM,QAAc,CAAA;AACjC,eAAO,MAAM,IAAI,QAAc,CAAA;AAC/B,eAAO,MAAM,GAAG,QAAY,CAAA;AAM5B,eAAO,MAAM,wBAAwB,QAAW,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hex utilities
|
|
3
|
+
*
|
|
4
|
+
* Re-exports hex conversion functions from key-derivation
|
|
5
|
+
*/
|
|
6
|
+
import type { Address } from "../schemas";
|
|
7
|
+
export { hexToUint8Array, uint8ArrayToHex } from "./key-derivation";
|
|
8
|
+
/**
|
|
9
|
+
* Create a validated hex address string (40 lowercase hex chars).
|
|
10
|
+
* Accepts optional 0x prefix and any case.
|
|
11
|
+
*
|
|
12
|
+
* @param input - Ethereum address string (with or without 0x prefix)
|
|
13
|
+
* @returns Normalized 40-character lowercase hex string
|
|
14
|
+
* @throws {Error} If the input is not a valid 40-char hex address
|
|
15
|
+
*/
|
|
16
|
+
export declare function hexAddress(input: string): Address;
|
|
17
|
+
//# sourceMappingURL=hex.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hex.d.ts","sourceRoot":"","sources":["../../src/utils/hex.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AAEzC,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAEnE;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAQjD"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Swarm Identity - Key Derivation Utilities
|
|
3
|
+
*
|
|
4
|
+
* Provides cryptographic functions for deriving app-specific secrets
|
|
5
|
+
* from a master identity key.
|
|
6
|
+
*/
|
|
7
|
+
import { PrivateKey } from "@ethersphere/bee-js";
|
|
8
|
+
/**
|
|
9
|
+
* Derive an app-specific secret from a master key and app origin
|
|
10
|
+
*
|
|
11
|
+
* Uses HMAC-SHA256 to create a deterministic, unique secret for each app.
|
|
12
|
+
* The same master key + app origin will always produce the same secret.
|
|
13
|
+
*
|
|
14
|
+
* @param masterKey - The master identity key (hex string)
|
|
15
|
+
* @param appOrigin - The app's origin (e.g., "https://swarm-app.local:8080")
|
|
16
|
+
* @returns The derived secret as a hex string
|
|
17
|
+
*/
|
|
18
|
+
export declare function deriveSecret(masterKey: string, appOrigin: string): Promise<string>;
|
|
19
|
+
/**
|
|
20
|
+
* Generate a random master key for testing/demo purposes
|
|
21
|
+
*
|
|
22
|
+
* In production, this would be derived from a user's mnemonic or
|
|
23
|
+
* imported from an existing identity.
|
|
24
|
+
*
|
|
25
|
+
* @returns A random 32-byte key as a hex string
|
|
26
|
+
*/
|
|
27
|
+
export declare function generateMasterKey(): Promise<string>;
|
|
28
|
+
/**
|
|
29
|
+
* Convert a hex string to Uint8Array
|
|
30
|
+
*
|
|
31
|
+
* @param hexString - Hex string (e.g., "deadbeef")
|
|
32
|
+
* @returns Uint8Array
|
|
33
|
+
*/
|
|
34
|
+
export declare function hexToUint8Array(hexString: string): Uint8Array;
|
|
35
|
+
/**
|
|
36
|
+
* Convert a Uint8Array to hex string
|
|
37
|
+
*
|
|
38
|
+
* @param bytes - Uint8Array to convert
|
|
39
|
+
* @returns Hex string (e.g., "deadbeef")
|
|
40
|
+
*/
|
|
41
|
+
export declare function uint8ArrayToHex(bytes: Uint8Array): string;
|
|
42
|
+
/**
|
|
43
|
+
* Verify that a derived secret matches the expected value
|
|
44
|
+
*
|
|
45
|
+
* Useful for testing.
|
|
46
|
+
*
|
|
47
|
+
* @param masterKey - Master key hex string
|
|
48
|
+
* @param appOrigin - App origin
|
|
49
|
+
* @param expectedSecret - Expected secret hex string
|
|
50
|
+
* @returns true if the derived secret matches the expected secret
|
|
51
|
+
*/
|
|
52
|
+
export declare function verifySecret(masterKey: string, appOrigin: string, expectedSecret: string): Promise<boolean>;
|
|
53
|
+
/**
|
|
54
|
+
* Derive an identity-specific master key from account master key and identity ID
|
|
55
|
+
*
|
|
56
|
+
* Uses HMAC-SHA256 to create a deterministic, unique key for each identity.
|
|
57
|
+
* This enables hierarchical key derivation: Account → Identity → App.
|
|
58
|
+
* The same account master key + identity ID will always produce the same identity key.
|
|
59
|
+
*
|
|
60
|
+
* @param accountMasterKey - The account's master key (hex string)
|
|
61
|
+
* @param identityId - The identity's unique identifier
|
|
62
|
+
* @returns The derived identity master key as a hex string
|
|
63
|
+
*/
|
|
64
|
+
export declare function deriveIdentityKey(accountMasterKey: string, identityId: string): Promise<string>;
|
|
65
|
+
/**
|
|
66
|
+
* Derive account backup key from account master key
|
|
67
|
+
*
|
|
68
|
+
* Used for signing account feed updates
|
|
69
|
+
*
|
|
70
|
+
* @param accountMasterKey - Account master key (hex string)
|
|
71
|
+
* @param accountId - Account ID (EthAddress hex string)
|
|
72
|
+
* @returns 32-byte account backup key (as hex string)
|
|
73
|
+
*/
|
|
74
|
+
export declare function deriveAccountBackupKey(accountMasterKey: string, accountId: string): Promise<string>;
|
|
75
|
+
/**
|
|
76
|
+
* Derive account Swarm encryption key from account master key
|
|
77
|
+
*
|
|
78
|
+
* Used for encrypting account snapshot data before upload to Swarm
|
|
79
|
+
*
|
|
80
|
+
* @param accountMasterKey - Account master key (hex string)
|
|
81
|
+
* @returns 32-byte encryption key (as hex string)
|
|
82
|
+
*/
|
|
83
|
+
export declare function deriveAccountSwarmEncryptionKey(accountMasterKey: string): Promise<string>;
|
|
84
|
+
/**
|
|
85
|
+
* Convert backup key to PrivateKey for feed signing
|
|
86
|
+
*/
|
|
87
|
+
export declare function backupKeyToPrivateKey(backupKeyHex: string): PrivateKey;
|
|
88
|
+
export declare const utils: {
|
|
89
|
+
hexToUint8Array: typeof hexToUint8Array;
|
|
90
|
+
uint8ArrayToHex: typeof uint8ArrayToHex;
|
|
91
|
+
};
|
|
92
|
+
//# sourceMappingURL=key-derivation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-derivation.d.ts","sourceRoot":"","sources":["../../src/utils/key-derivation.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAA;AAEhD;;;;;;;;;GASG;AACH,wBAAsB,YAAY,CAChC,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,MAAM,CAAC,CAuBjB;AAED;;;;;;;GAOG;AACH,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,MAAM,CAAC,CAOzD;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,UAAU,CAa7D;AAED;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAIzD;AAED;;;;;;;;;GASG;AACH,wBAAsB,YAAY,CAChC,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,cAAc,EAAE,MAAM,GACrB,OAAO,CAAC,OAAO,CAAC,CAGlB;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,iBAAiB,CACrC,gBAAgB,EAAE,MAAM,EACxB,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,MAAM,CAAC,CAuBjB;AAED;;;;;;;;GAQG;AACH,wBAAsB,sBAAsB,CAC1C,gBAAgB,EAAE,MAAM,EACxB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,MAAM,CAAC,CAEjB;AAED;;;;;;;GAOG;AACH,wBAAsB,+BAA+B,CACnD,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,CAAC,CAEjB;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,YAAY,EAAE,MAAM,GAAG,UAAU,CAEtE;AAGD,eAAO,MAAM,KAAK;;;CAGjB,CAAA"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pre-configured Storage Managers for Entity Types
|
|
3
|
+
*
|
|
4
|
+
* Provides ready-to-use storage managers for accounts, identities,
|
|
5
|
+
* connected apps, and postage stamps with versioning support.
|
|
6
|
+
*/
|
|
7
|
+
import { VersionedStorageManager } from "./versioned-storage";
|
|
8
|
+
import type { Account, Identity, ConnectedApp, PostageStamp } from "../types";
|
|
9
|
+
import { type NetworkSettings } from "../schemas";
|
|
10
|
+
/**
|
|
11
|
+
* Serialize Account for storage
|
|
12
|
+
*/
|
|
13
|
+
export declare function serializeAccount(account: Account): Record<string, unknown>;
|
|
14
|
+
/**
|
|
15
|
+
* Serialize Identity for storage
|
|
16
|
+
*/
|
|
17
|
+
export declare function serializeIdentity(identity: Identity): Record<string, unknown>;
|
|
18
|
+
/**
|
|
19
|
+
* Serialize ConnectedApp for storage
|
|
20
|
+
*/
|
|
21
|
+
export declare function serializeConnectedApp(app: ConnectedApp): Record<string, unknown>;
|
|
22
|
+
/**
|
|
23
|
+
* Serialize PostageStamp for storage
|
|
24
|
+
*/
|
|
25
|
+
export declare function serializePostageStamp(stamp: PostageStamp): Record<string, unknown>;
|
|
26
|
+
/**
|
|
27
|
+
* Create storage manager for accounts
|
|
28
|
+
*/
|
|
29
|
+
export declare function createAccountsStorageManager(): VersionedStorageManager<Account>;
|
|
30
|
+
/**
|
|
31
|
+
* Create storage manager for identities
|
|
32
|
+
*/
|
|
33
|
+
export declare function createIdentitiesStorageManager(): VersionedStorageManager<Identity>;
|
|
34
|
+
/**
|
|
35
|
+
* Create storage manager for connected apps
|
|
36
|
+
*/
|
|
37
|
+
export declare function createConnectedAppsStorageManager(): VersionedStorageManager<ConnectedApp>;
|
|
38
|
+
/**
|
|
39
|
+
* Invalidate all connected app entries for a given app URL.
|
|
40
|
+
* Sets lastConnectedAt to 0 and connectedUntil to undefined so
|
|
41
|
+
* isConnectionValid() returns false and reconnect won't happen on refresh.
|
|
42
|
+
*/
|
|
43
|
+
export declare function disconnectApp(appUrl: string): void;
|
|
44
|
+
/**
|
|
45
|
+
* Create storage manager for postage stamps
|
|
46
|
+
*/
|
|
47
|
+
export declare function createPostageStampsStorageManager(): VersionedStorageManager<PostageStamp>;
|
|
48
|
+
/**
|
|
49
|
+
* Serialize NetworkSettings for storage
|
|
50
|
+
*/
|
|
51
|
+
export declare function serializeNetworkSettings(settings: NetworkSettings): Record<string, unknown>;
|
|
52
|
+
/**
|
|
53
|
+
* Singleton storage manager interface for network settings
|
|
54
|
+
*/
|
|
55
|
+
export interface NetworkSettingsStorageManager {
|
|
56
|
+
load(): NetworkSettings | undefined;
|
|
57
|
+
save(settings: NetworkSettings): void;
|
|
58
|
+
clear(): void;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Create storage manager for network settings (singleton)
|
|
62
|
+
* Unlike other storage managers, this stores a single object, not an array
|
|
63
|
+
*/
|
|
64
|
+
export declare function createNetworkSettingsStorageManager(): NetworkSettingsStorageManager;
|
|
65
|
+
//# sourceMappingURL=storage-managers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"storage-managers.d.ts","sourceRoot":"","sources":["../../src/utils/storage-managers.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EACL,uBAAuB,EAGxB,MAAM,qBAAqB,CAAA;AAC5B,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,UAAU,CAAA;AAQ7E,OAAO,EAML,KAAK,eAAe,EACrB,MAAM,YAAY,CAAA;AAkEnB;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAuC1E;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAS7E;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,YAAY,GAChB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAWzB;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,YAAY,GAClB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAgBzB;AAMD;;GAEG;AACH,wBAAgB,4BAA4B,IAAI,uBAAuB,CAAC,OAAO,CAAC,CAU/E;AAED;;GAEG;AACH,wBAAgB,8BAA8B,IAAI,uBAAuB,CAAC,QAAQ,CAAC,CAUlF;AAED;;GAEG;AACH,wBAAgB,iCAAiC,IAAI,uBAAuB,CAAC,YAAY,CAAC,CAUzF;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CASlD;AAED;;GAEG;AACH,wBAAgB,iCAAiC,IAAI,uBAAuB,CAAC,YAAY,CAAC,CAUzF;AAuBD;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,eAAe,GACxB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAKzB;AAED;;GAEG;AACH,MAAM,WAAW,6BAA6B;IAC5C,IAAI,IAAI,eAAe,GAAG,SAAS,CAAA;IACnC,IAAI,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI,CAAA;IACrC,KAAK,IAAI,IAAI,CAAA;CACd;AAED;;;GAGG;AACH,wBAAgB,mCAAmC,IAAI,6BAA6B,CA6CnF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Swarm ID Export/Import Module
|
|
3
|
+
*
|
|
4
|
+
* Provides serialization and deserialization for the .swarmid export format.
|
|
5
|
+
* Delegates to account-payload for the unified payload schema.
|
|
6
|
+
*/
|
|
7
|
+
import type { Account, Identity, ConnectedApp, PostageStamp } from "../schemas";
|
|
8
|
+
import type { AccountPayload, AccountPayloadResult } from "./account-payload";
|
|
9
|
+
export type { ExportedConnectedApp } from "./account-payload";
|
|
10
|
+
export { AccountPayloadSchemaV1 as SwarmIdExportSchemaV1 } from "./account-payload";
|
|
11
|
+
export type SwarmIdExport = AccountPayload;
|
|
12
|
+
export type SwarmIdImportResult = AccountPayloadResult;
|
|
13
|
+
/**
|
|
14
|
+
* Serialize account data into a plain object suitable for JSON export.
|
|
15
|
+
* Extracts accountId + metadata from the Account, then delegates to
|
|
16
|
+
* the shared account-payload serializer.
|
|
17
|
+
*/
|
|
18
|
+
export declare function serializeSwarmIdExport(account: Account, identities: Identity[], connectedApps: ConnectedApp[], postageStamps: PostageStamp[]): Record<string, unknown>;
|
|
19
|
+
/**
|
|
20
|
+
* Deserialize and validate a .swarmid export file.
|
|
21
|
+
* Delegates to the shared account-payload deserializer.
|
|
22
|
+
*/
|
|
23
|
+
export declare function deserializeSwarmIdExport(data: unknown): SwarmIdImportResult;
|
|
24
|
+
//# sourceMappingURL=swarm-id-export.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"swarm-id-export.d.ts","sourceRoot":"","sources":["../../src/utils/swarm-id-export.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAK/E,OAAO,KAAK,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAA;AAG7E,YAAY,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAA;AAC7D,OAAO,EAAE,sBAAsB,IAAI,qBAAqB,EAAE,MAAM,mBAAmB,CAAA;AAMnF,MAAM,MAAM,aAAa,GAAG,cAAc,CAAA;AAE1C,MAAM,MAAM,mBAAmB,GAAG,oBAAoB,CAAA;AAMtD;;;;GAIG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,OAAO,EAChB,UAAU,EAAE,QAAQ,EAAE,EACtB,aAAa,EAAE,YAAY,EAAE,EAC7B,aAAa,EAAE,YAAY,EAAE,GAC5B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAczB;AAMD;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,IAAI,EAAE,OAAO,GAAG,mBAAmB,CAE3E"}
|