@healchain/sdk 1.0.0 → 1.1.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.
- package/package.json +1 -1
- package/src/index.js +72 -4
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* HealChain SDK v1.
|
|
2
|
+
* HealChain SDK v1.1.0
|
|
3
3
|
* REST API client for HealChain — self-healing decentralized storage.
|
|
4
4
|
*
|
|
5
5
|
* Browser (ESM) and Node.js compatible.
|
|
@@ -241,14 +241,82 @@ class HealChain {
|
|
|
241
241
|
* @property {number} bytes - Original data size in bytes
|
|
242
242
|
* @property {string} chainId - Chain where data was stored
|
|
243
243
|
*/
|
|
244
|
-
async retrieve(id) {
|
|
245
|
-
const
|
|
246
|
-
|
|
244
|
+
async retrieve(id, options = {}) {
|
|
245
|
+
const chain = options.chain ?? this.chain ?? 'auto';
|
|
246
|
+
const result = await this._fetch(
|
|
247
|
+
`/retrieve?id=${encodeURIComponent(String(id))}&chain=${encodeURIComponent(chain)}`
|
|
248
|
+
);
|
|
249
|
+
const record = {
|
|
247
250
|
recordId: String(result.recordId),
|
|
248
251
|
data: result.data,
|
|
249
252
|
text: result.text,
|
|
250
253
|
bytes: result.bytes,
|
|
251
254
|
chainId: result.chainId ?? null,
|
|
255
|
+
storageType: result.storageType ?? 'on_chain',
|
|
256
|
+
shards: result.shards ?? null,
|
|
257
|
+
};
|
|
258
|
+
// Fetch full ci-sha4096 digest — best-effort, non-blocking
|
|
259
|
+
try {
|
|
260
|
+
record.ciDigestHash = await this.getCiDigest(id);
|
|
261
|
+
} catch (_) {
|
|
262
|
+
// digest not yet available for old records — silently omit
|
|
263
|
+
}
|
|
264
|
+
return record;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// ── storeFile() ────────────────────────────────────────────────────────────
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Store a file (raw bytes) on-chain or via IPFS hybrid (auto-detected by size).
|
|
271
|
+
* Files ≤1MB go on-chain. Files >1MB use IPFS hybrid storage.
|
|
272
|
+
*
|
|
273
|
+
* @param {Uint8Array|ArrayBuffer|Buffer} buffer - File bytes
|
|
274
|
+
* @param {object} [options]
|
|
275
|
+
* @param {string} [options.label='sdk upload'] - Record label
|
|
276
|
+
* @param {number} [options.dataShards] - Override RS data shards
|
|
277
|
+
* @param {number} [options.parityShards] - Override RS parity shards
|
|
278
|
+
* @param {string} [options.chain] - Chain name or 'auto'
|
|
279
|
+
* @param {string} [options.preference] - Chain selection preference
|
|
280
|
+
* @returns {Promise<StoreResult>}
|
|
281
|
+
*/
|
|
282
|
+
async storeFile(buffer, options = {}) {
|
|
283
|
+
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
284
|
+
return this.store(bytes, options);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// ── getCiDigest() ──────────────────────────────────────────────────────────
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Get the full 512-byte ci-sha4096 digest hex for a record.
|
|
291
|
+
* This is the authoritative digest stored at oracle fulfillment time.
|
|
292
|
+
*
|
|
293
|
+
* @param {string|number} recordId - Record ID
|
|
294
|
+
* @returns {Promise<string>} Full digest hex (1024 hex chars = 512 bytes)
|
|
295
|
+
*/
|
|
296
|
+
async getCiDigest(recordId) {
|
|
297
|
+
const result = await this._fetch(`/ciDigest?recordId=${encodeURIComponent(String(recordId))}`);
|
|
298
|
+
return result.ciDigestHash;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// ── verifyIntegrity() ──────────────────────────────────────────────────────
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* High-level audit helper: retrieve a record and its ci-sha4096 digest.
|
|
305
|
+
*
|
|
306
|
+
* @param {string|number} recordId - Record ID
|
|
307
|
+
* @param {object} [options]
|
|
308
|
+
* @param {string} [options.chain] - Chain name or 'auto'
|
|
309
|
+
* @returns {Promise<{record: RetrieveResult, ciDigestHash: string|null}>}
|
|
310
|
+
*/
|
|
311
|
+
async verifyIntegrity(recordId, options = {}) {
|
|
312
|
+
const chain = options.chain ?? this.chain ?? 'auto';
|
|
313
|
+
const [record, ciDigestHash] = await Promise.allSettled([
|
|
314
|
+
this._fetch(`/retrieve?id=${encodeURIComponent(String(recordId))}&chain=${encodeURIComponent(chain)}`),
|
|
315
|
+
this.getCiDigest(recordId),
|
|
316
|
+
]);
|
|
317
|
+
return {
|
|
318
|
+
record: record.status === 'fulfilled' ? record.value : null,
|
|
319
|
+
ciDigestHash: ciDigestHash.status === 'fulfilled' ? ciDigestHash.value : null,
|
|
252
320
|
};
|
|
253
321
|
}
|
|
254
322
|
|