From 8b9b6dea9ca4817b1bfeb1c594f9d2a7e37b5277 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Levente=20T=C3=B3th?= Date: Thu, 6 Jun 2024 17:08:40 +0200 Subject: [PATCH] feat: use soc api --- src/feed/index.ts | 47 ++++++++++++++++++++++++++++++++------------- src/modules/feed.ts | 2 +- src/modules/soc.ts | 34 ++++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 16 deletions(-) diff --git a/src/feed/index.ts b/src/feed/index.ts index 8b2858e4..ed95e671 100644 --- a/src/feed/index.ts +++ b/src/feed/index.ts @@ -1,11 +1,12 @@ import { uploadSingleOwnerChunkData } from '../chunk/soc' -import { Chunk } from '../chunk/cac' -import { FeedUpdateOptions, FetchFeedUpdateResponse, fetchFeedUpdate } from '../modules/feed' +import { ChunkParam } from '../chunk/cac' +import { FeedUpdateOptions, FetchFeedUpdateResponse, fetchLatestFeedUpdate } from '../modules/feed' +import * as socAPI from '../modules/soc' import { Address, BatchId, BeeRequestOptions, - BytesReference, + Data, FEED_INDEX_HEX_LENGTH, FeedReader, FeedWriter, @@ -18,7 +19,7 @@ import { import { Bytes, makeBytes } from '../utils/bytes' import { EthAddress, HexEthAddress, makeHexEthAddress } from '../utils/eth' import { keccak256Hash } from '../utils/hash' -import { HexString, bytesToHex, makeHexString } from '../utils/hex' +import { HexString, bytesToHex, hexToBytes, makeHexString } from '../utils/hex' import { assertAddress } from '../utils/type' import { makeFeedIdentifier } from './identifier' import type { FeedType } from './type' @@ -37,11 +38,6 @@ export type Index = number | Epoch | IndexBytes | string export interface FeedUploadOptions extends UploadOptions, FeedUpdateOptions {} -export interface FeedUpdate { - timestamp: number - reference: BytesReference -} - export async function findNextIndex( requestOptions: BeeRequestOptions, owner: HexEthAddress, @@ -49,7 +45,7 @@ export async function findNextIndex( options?: FeedUpdateOptions, ): Promise> { try { - const feedUpdate = await fetchFeedUpdate(requestOptions, owner, topic, options) + const feedUpdate = await fetchLatestFeedUpdate(requestOptions, owner, topic, options) return makeHexString(feedUpdate.feedIndexNext, FEED_INDEX_HEX_LENGTH) } catch (e: any) { @@ -64,7 +60,7 @@ export async function updateFeed( requestOptions: BeeRequestOptions, signer: Signer, topic: Topic, - payload: Uint8Array | Chunk, + payload: Uint8Array | ChunkParam, postageBatchId: BatchId, options?: FeedUploadOptions, ): Promise { @@ -82,6 +78,17 @@ export function getFeedUpdateChunkReference(owner: EthAddress, topic: Topic, ind return keccak256Hash(identifier, owner) } +export async function downloadFeedUpdate( + requestOptions: BeeRequestOptions, + owner: EthAddress, + topic: Topic, + index: Index, +): Promise { + const identifier = makeFeedIdentifier(topic, index) + + return socAPI.download(requestOptions, bytesToHex(owner), bytesToHex(identifier)) +} + export function makeFeedReader( requestOptions: BeeRequestOptions, type: FeedType, @@ -93,7 +100,17 @@ export function makeFeedReader( owner, topic, async download(options?: FeedUpdateOptions): Promise { - return fetchFeedUpdate(requestOptions, owner, topic, { ...options, type }) + if (!options?.index && options?.index !== 0) { + return fetchLatestFeedUpdate(requestOptions, owner, topic, { ...options, type }) + } + + const update = await downloadFeedUpdate(requestOptions, hexToBytes(owner), topic, options.index) + + return { + data: update, + feedIndex: options.index, + feedIndexNext: '', + } }, } } @@ -104,7 +121,11 @@ export function makeFeedWriter( topic: Topic, signer: Signer, ): FeedWriter { - const upload = async (postageBatchId: string | Address, payload: Uint8Array | Chunk, options?: FeedUploadOptions) => { + const upload = async ( + postageBatchId: string | Address, + payload: Uint8Array | ChunkParam, + options?: FeedUploadOptions, + ) => { assertAddress(postageBatchId) return updateFeed(requestOptions, signer, topic, payload, postageBatchId, { ...options, type }) diff --git a/src/modules/feed.ts b/src/modules/feed.ts index 3b9b70c4..53a55346 100644 --- a/src/modules/feed.ts +++ b/src/modules/feed.ts @@ -107,7 +107,7 @@ function readFeedUpdateHeaders(headers: Record): FeedUpdateHeade * @param topic Topic in hex * @param options Additional options, like index, at, type */ -export async function fetchFeedUpdate( +export async function fetchLatestFeedUpdate( requestOptions: BeeRequestOptions, owner: HexEthAddress, topic: Topic, diff --git a/src/modules/soc.ts b/src/modules/soc.ts index 975cde08..a8b160e1 100644 --- a/src/modules/soc.ts +++ b/src/modules/soc.ts @@ -1,5 +1,14 @@ -import { BatchId, BeeRequestOptions, Reference, ReferenceResponse, UploadOptions } from '../types' -import { extractUploadHeaders } from '../utils/headers' +import { + BatchId, + BeeRequestOptions, + Data, + DownloadRedundancyOptions, + Reference, + ReferenceResponse, + UploadOptions, +} from '../types' +import { wrapBytesWithHelpers } from '../utils/bytes' +import { extractDownloadHeaders, extractUploadHeaders } from '../utils/headers' import { http } from '../utils/http' const socEndpoint = 'soc' @@ -38,3 +47,24 @@ export async function upload( return response.data.reference } + +/** + * Download data as a byte array + * + * @param ky + * @param hash Bee content reference + */ +export async function download( + requestOptions: BeeRequestOptions, + owner: string, + identifier: string, + options?: DownloadRedundancyOptions, +): Promise { + const response = await http(requestOptions, { + responseType: 'arraybuffer', + url: `${socEndpoint}/${owner}/${identifier}`, + headers: extractDownloadHeaders(options), + }) + + return wrapBytesWithHelpers(new Uint8Array(response.data)) +}