/** * @module bin-helpers * * Centralized isomorphic helper functions for creating, hashing, persisting, * and registering BinIbGib_V1 binary stones. */ import { extractErrorMsg, hash } from '@ibgib/helper-gib/dist/helpers/utils-helper.mjs'; import { getGib } from '@ibgib/ts-gib/dist/V1/transforms/transform-helper.mjs'; import { getIbGibAddr } from '@ibgib/ts-gib/dist/helper.mjs'; import { IbGibAddr } from '@ibgib/ts-gib/dist/types.mjs'; import { GLOBAL_LOG_A_LOT } from '../../core-constants.mjs'; import { MetaspaceService } from '../../witness/space/metaspace/metaspace-types.mjs'; import { IbGibSpaceAny } from '../../witness/space/space-base-v1.mjs'; import { putInSpace, registerNewIbGib } from '../../witness/space/space-helper.mjs'; import { getBinIb } from '../other/ibgib-helper.mjs'; import { BinIbGib_V1 } from './bin-types.mjs'; const logalot = GLOBAL_LOG_A_LOT; const lcFile = `[bin-helpers]`; export interface CreateBinIbGibOpts { /** Raw binary data as Uint8Array or UTF-8 string */ data: Uint8Array | string; /** Optional extension (e.g. 'txt', 'png', etc.) without dot */ ext?: string; /** Optional encoding (e.g. 'base64') */ encoding?: string; /** Space to persist the binary stone to */ space?: IbGibSpaceAny; /** Metaspace to register the new ibgib with (and broadcast) */ metaspace?: MetaspaceService | any; } export interface CreateBinIbGibResult { binIbGib: BinIbGib_V1; binAddr: IbGibAddr; binHash: string; } /** * Creates a fully formed, content-addressed BinIbGib_V1 stone. * Computes SHA-256 binary hash, determines bin ib, computes the gib stone hash, * and optionally persists to the target space and registers with the metaspace. */ export async function createBinIbGib({ data, ext, encoding, space, metaspace, }: CreateBinIbGibOpts): Promise { const lc = `${lcFile}[${createBinIbGib.name}]`; try { if (data === undefined || data === null) { throw new Error(`data is required for createBinIbGib (E: 8a91c01b4d8349fa89e3bb8893d94b26)`); } let binData: Uint8Array; let binHash: string; if (typeof data === 'string') { binData = new TextEncoder().encode(data); binHash = await hash({ s: data }); } else { binData = data; const sourceBuffer = binData.byteOffset === 0 && binData.byteLength === binData.buffer.byteLength ? binData.buffer : binData.buffer.slice(binData.byteOffset, binData.byteOffset + binData.byteLength); const hashBuf = await crypto.subtle.digest('SHA-256', sourceBuffer as ArrayBuffer); binHash = Array.from(new Uint8Array(hashBuf)).map(b => b.toString(16).padStart(2, '0')).join(''); } const binIb = getBinIb({ binHash, binExt: ext, binEncoding: encoding }); const binGib = await getGib({ ibGib: { ib: binIb, data: binData as any }, hasTjp: false, }); const binIbGib: BinIbGib_V1 = { ib: binIb, gib: binGib, data: binData as any, }; const binAddr = getIbGibAddr({ ibGib: binIbGib }); if (space) { if (metaspace) { await metaspace.put({ ibGib: binIbGib, space }); await metaspace.registerNewIbGib({ ibGib: binIbGib, space }); } else { await putInSpace({ ibGib: binIbGib, space }); await registerNewIbGib({ ibGib: binIbGib, space }); } } if (logalot) { console.log(`${lc} Created BinIbGib stone at ${binAddr}`); } return { binIbGib, binAddr, binHash, }; } catch (error) { console.error(`${lc} ${extractErrorMsg(error)} (E: d2871f76bc0347efa2926be5e72d2426)`); throw error; } }