SHA256
Добавить учёт публикации блоков и обновить загрузку UI
This commit is contained in:
@@ -188,6 +188,39 @@ export async function uploadFileWithTurbo({
|
||||
};
|
||||
}
|
||||
|
||||
/** Загружает готовый подписанный DataItem, сохраняя исходные bytes и ID. */
|
||||
export async function uploadSignedBlockDataItem({ login, storagePwd, bytes, expectedId }) {
|
||||
if (!(bytes instanceof Uint8Array) || bytes.length === 0) throw new Error('Empty signed DataItem');
|
||||
const alreadyUploaded = async () => {
|
||||
try {
|
||||
const response = await fetch(`https://turbo.ardrive.io/tx/${encodeURIComponent(expectedId)}/status`);
|
||||
if (!response.ok) return false;
|
||||
const status = String((await response.json())?.status || '').toUpperCase();
|
||||
return ['PENDING', 'CONFIRMED', 'BUNDLED', 'FINALIZED'].includes(status);
|
||||
} catch { return false; }
|
||||
};
|
||||
if (await alreadyUploaded()) return { id: expectedId, alreadyUploaded: true };
|
||||
const { turbo } = await getTurboContextForStoredSolanaKey({ login, storagePwd });
|
||||
let result;
|
||||
try {
|
||||
result = await turbo.uploadSignedDataItem({
|
||||
dataItemStreamFactory: () => new ReadableStream({
|
||||
start(controller) { controller.enqueue(bytes); controller.close(); },
|
||||
}),
|
||||
dataItemSizeFactory: () => bytes.length,
|
||||
});
|
||||
} catch (error) {
|
||||
// Потерянный ответ Turbo или повтор того же DataItem не создаёт новый блок.
|
||||
if (await alreadyUploaded()) return { id: expectedId, alreadyUploaded: true };
|
||||
if (/data item exists|already exists/i.test(String(error?.message || error))) {
|
||||
return { id: expectedId, alreadyUploaded: true };
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
if (String(result?.id || '') !== expectedId) throw new Error('Turbo returned another DataItem ID');
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function topUpTurboWithSolana({
|
||||
login,
|
||||
storagePwd,
|
||||
|
||||
Reference in New Issue
Block a user