@@ -8,12 +8,16 @@ import {
88 Approvals,
99 Balance,
1010 Blob,
11+ BlobSourceInfo,
1112 BlobStatus,
13+ BlobTuple,
1214 CreditApproval,
1315 CreditStats,
16+ Delegate,
1417 StorageStats,
1518 SubnetStats,
1619 Subscriber,
20+ Subscription,
1721 SubscriptionGroup
1822} from "../types/BlobTypes.sol " ;
1923import {KeyValue} from "../types/CommonTypes.sol " ;
@@ -122,16 +126,55 @@ library LibBlob {
122126 /// @param data The encoded subscription ID.
123127 /// @return decoded The decoded subscription ID.
124128 function decodeSubscriptionId (bytes memory data ) internal view returns (string memory ) {
125- // If the leading indicator is `a1`, it's a mapping with a single key-value pair; else, default
126- if (data[0 ] == hex "a1 " ) {
127- // Decode the mapping with Key and subscription ID bytes (a single key-value pair)
128- bytes [2 ][] memory decoded = data.decodeCborMappingToBytes ();
129- // Second value is the subscription ID bytes (ignore the first value `Key` key)
130- bytes memory subscriptionId = decoded[0 ][1 ].decodeCborBytesArrayToBytes ();
131- return string (subscriptionId);
132- } else {
129+ // If not a mapping with key-value pair, return default
130+ if (data[0 ] != hex "a1 " ) {
133131 return "Default " ;
134132 }
133+
134+ // Decode the mapping and return subscription ID bytes
135+ bytes [2 ][] memory decoded = data.decodeCborMappingToBytes ();
136+ return string (decoded[0 ][1 ].decodeCborBytesArrayToBytes ());
137+ }
138+
139+ /// @dev Decode a delegate from CBOR.
140+ /// @param data The encoded CBOR array of a delegate.
141+ /// @return origin The delegate origin address
142+ /// @return caller The delegate caller address
143+ function decodeDelegate (bytes memory data ) internal view returns (address origin , address caller ) {
144+ if (data[0 ] == hex "00 " || data[0 ] == hex "f6 " ) {
145+ return (address (0 ), address (0 ));
146+ }
147+ bytes [] memory decoded = data.decodeCborArrayToBytes ();
148+ origin = decoded[0 ].decodeCborAddress ();
149+ caller = decoded[1 ].decodeCborAddress ();
150+ }
151+
152+ /// @dev Decode a subscription from CBOR.
153+ /// @param data The encoded CBOR array of a subscription.
154+ /// @return subscription The decoded subscription.
155+ function decodeSubscription (bytes memory data ) internal view returns (Subscription memory subscription ) {
156+ bytes [] memory decoded = data.decodeCborArrayToBytes ();
157+ subscription.added = decoded[0 ].decodeCborBytesToUint64 ();
158+ subscription.expiry = decoded[1 ].decodeCborBytesToUint64 ();
159+ subscription.autoRenew = decoded[2 ].decodeCborBool ();
160+ subscription.source = string (decoded[3 ].decodeCborBlobHashOrNodeId ());
161+ (subscription.delegate.origin, subscription.delegate.caller) = decodeDelegate (decoded[4 ]);
162+ subscription.failed = decoded[5 ].decodeCborBool ();
163+ }
164+
165+ /// @dev Decode a subscription group from CBOR.
166+ /// @param subscriptionGroupBytes The encoded subscription group bytes
167+ /// @return group The decoded subscription group
168+ function decodeSubscriptionGroup (bytes [2 ][] memory subscriptionGroupBytes )
169+ internal
170+ view
171+ returns (SubscriptionGroup[] memory group )
172+ {
173+ group = new SubscriptionGroup [](subscriptionGroupBytes.length );
174+ for (uint256 j = 0 ; j < subscriptionGroupBytes.length ; j++ ) {
175+ group[j].subscriptionId = decodeSubscriptionId (subscriptionGroupBytes[j][0 ]);
176+ group[j].subscription = decodeSubscription (subscriptionGroupBytes[j][1 ]);
177+ }
135178 }
136179
137180 /// @dev Decode subscribers from CBOR.
@@ -142,12 +185,7 @@ library LibBlob {
142185 subscribers = new Subscriber [](decoded.length );
143186 for (uint256 i = 0 ; i < decoded.length ; i++ ) {
144187 subscribers[i].subscriber = decoded[i][0 ].decodeCborAddress ();
145- bytes [2 ][] memory subscriptionGroupBytes = decoded[i][1 ].decodeCborMappingToBytes ();
146- subscribers[i].subscriptionGroup = new SubscriptionGroup [](subscriptionGroupBytes.length );
147- for (uint256 j = 0 ; j < subscriptionGroupBytes.length ; j++ ) {
148- subscribers[i].subscriptionGroup[j].subscriptionId = decodeSubscriptionId (subscriptionGroupBytes[j][0 ]);
149- subscribers[i].subscriptionGroup[j].subscription = subscriptionGroupBytes[j][1 ];
150- }
188+ subscribers[i].subscriptionGroup = decodeSubscriptionGroup (decoded[i][1 ].decodeCborMappingToBytes ());
151189 }
152190 }
153191
@@ -157,12 +195,38 @@ library LibBlob {
157195 function decodeBlob (bytes memory data ) internal view returns (Blob memory blob ) {
158196 bytes [] memory decoded = data.decodeCborArrayToBytes ();
159197 if (decoded.length == 0 ) return blob;
198+
160199 blob.size = decoded[0 ].decodeCborBytesToUint64 ();
161- blob.metadataHash = string (decoded[1 ].decodeBlobHash ());
200+ blob.metadataHash = string (decoded[1 ].decodeCborBlobHashOrNodeId ());
162201 blob.subscribers = decodeSubscribers (decoded[2 ]);
163202 blob.status = decodeBlobStatus (decoded[3 ]);
164203 }
165204
205+ /// @dev Decode a blob source info from CBOR.
206+ /// @param data The encoded CBOR array of a blob source info.
207+ /// @return sourceInfo The decoded blob source info.
208+ function decodeBlobSourceInfo (bytes memory data ) internal view returns (BlobSourceInfo memory sourceInfo ) {
209+ bytes [] memory decodedOuter = data.decodeCborArrayToBytes ();
210+ bytes [] memory decodedInner = decodedOuter[0 ].decodeCborArrayToBytes ();
211+ sourceInfo.subscriber = decodedInner[0 ].decodeCborAddress ();
212+ sourceInfo.subscriptionId = decodeSubscriptionId (decodedInner[1 ]);
213+ sourceInfo.source = string (decodedInner[2 ].decodeCborBlobHashOrNodeId ());
214+ }
215+
216+ /// @dev Decode pending blobs from CBOR.
217+ /// @param data The encoded CBOR array of pending blobs.
218+ /// @return blobs The decoded pending blobs.
219+ function decodePendingBlobs (bytes memory data ) internal view returns (BlobTuple[] memory blobs ) {
220+ bytes [] memory decoded = data.decodeCborArrayToBytes ();
221+ if (decoded.length == 0 ) return blobs;
222+ blobs = new BlobTuple [](decoded.length );
223+ for (uint256 i = 0 ; i < decoded.length ; i++ ) {
224+ bytes [] memory blobTuple = decoded[i].decodeCborArrayToBytes ();
225+ blobs[i].blobHash = string (blobTuple[0 ].decodeCborBlobHashOrNodeId ());
226+ blobs[i].sourceInfo = decodeBlobSourceInfo (blobTuple[1 ]);
227+ }
228+ }
229+
166230 /// @dev Helper function to encode approve credit params.
167231 /// @param from (address): Account address that is approving the credit.
168232 /// @param receiver (address): Account address that is receiving the approval.
@@ -328,12 +392,20 @@ library LibBlob {
328392 return accountToBalance (account);
329393 }
330394
395+ /// @dev Get a blob.
396+ /// @param blobHash The hash of the blob.
397+ /// @return blob The blob.
331398 function getBlob (string memory blobHash ) external view returns (Blob memory blob ) {
332399 bytes memory params = blobHash.encodeCborBlobHashOrNodeId ();
333400 bytes memory data = LibWasm.readFromWasmActor (ACTOR_ID, METHOD_GET_BLOB, params);
334401 return decodeBlob (data);
335402 }
336403
404+ /// @dev Get the status of a blob.
405+ /// @param subscriber The address of the subscriber.
406+ /// @param blobHash The hash of the blob.
407+ /// @param subscriptionId The subscription ID.
408+ /// @return status The status of the blob.
337409 function getBlobStatus (address subscriber , string memory blobHash , string memory subscriptionId )
338410 external
339411 view
@@ -348,16 +420,24 @@ library LibBlob {
348420 return decodeBlobStatus (data);
349421 }
350422
351- function getPendingBlobs (uint32 size ) external view returns (bytes memory ) {
423+ /// @dev Get pending blobs.
424+ /// @param size The size of the blobs to get.
425+ /// @return blobs The pending blobs.
426+ function getPendingBlobs (uint32 size ) external view returns (BlobTuple[] memory blobs ) {
352427 bytes memory params = size.encodeCborUint64 ();
353- return LibWasm.readFromWasmActor (ACTOR_ID, METHOD_GET_PENDING_BLOBS, params);
428+ bytes memory data = LibWasm.readFromWasmActor (ACTOR_ID, METHOD_GET_PENDING_BLOBS, params);
429+ return decodePendingBlobs (data);
354430 }
355431
432+ /// @dev Get the number of pending blobs.
433+ /// @return count The number of pending blobs.
356434 function getPendingBlobsCount () external view returns (uint64 ) {
357435 bytes memory data = LibWasm.readFromWasmActor (ACTOR_ID, METHOD_GET_PENDING_BLOBS_COUNT);
358436 return data.decodeCborBytesToUint64 ();
359437 }
360438
439+ /// @dev Get the number of pending bytes.
440+ /// @return count The number of pending bytes.
361441 function getPendingBytesCount () external view returns (uint64 ) {
362442 bytes memory data = LibWasm.readFromWasmActor (ACTOR_ID, METHOD_GET_PENDING_BYTES_COUNT);
363443 return data.decodeCborBytesToUint64 ();
0 commit comments