Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sequential feed #250

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/account/account-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CryptoJS from 'crypto-js'
import { bytesToHex } from '../utils/hex'
import { mnemonicToSeed, prepareEthAddress, privateKeyToBytes } from '../utils/wallet'
import { isChunkAlreadyExistsError, isInsufficientFundsError } from '../utils/error'
import { FeedType } from '../feed/types'

export class AccountData {
/**
Expand Down Expand Up @@ -95,7 +96,13 @@ export class AccountData {

if (isAddressOptions(options)) {
const address = prepareEthAddress(options.address)
const encryptedMnemonic = await getEncryptedMnemonic(this.connection.bee, username, address)
const encryptedMnemonic = await getEncryptedMnemonic(
this.connection.bee,
username,
address,
this.connection.options?.feedType ?? FeedType.Epoch,
this.connection.options?.requestOptions,
)
mnemonic = decryptText(password, encryptedMnemonic)
}

Expand Down
1 change: 0 additions & 1 deletion src/account/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * as Account from './account'
export * as Encryption from '../utils/encryption'
export * as Utils from './utils'
//TODO export every exportable
21 changes: 15 additions & 6 deletions src/account/mnemonic.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Bee, Reference, Utils } from '@ethersphere/bee-js'
import { Bee, BeeRequestOptions, Reference, Utils } from '@ethersphere/bee-js'
import { assertBase64UrlData, assertUsername } from './utils'
import { Wallet } from 'ethers'
import { getFeedData, writeFeedDataRaw } from '../feed/api'
import { getFeedData } from '../feed/api'
import { Connection } from '../connection/connection'
import { stringToBytes } from '../utils/bytes'
import { writeEpochFeedDataRaw } from '../feed/epoch'
import { FeedType } from '../feed/types'

/**
* Downloads encrypted mnemonic phrase from swarm chunk for version 1 account
Expand All @@ -13,13 +15,20 @@ import { stringToBytes } from '../utils/bytes'
* @param bee Bee client
* @param username FDP account username
* @param address FDP account address
*
* @param feedType
* @param requestOptions download data requestOptions
* @returns encrypted mnemonic phrase in Base64url format
*/
export async function getEncryptedMnemonic(bee: Bee, username: string, address: Utils.EthAddress): Promise<string> {
export async function getEncryptedMnemonic(
bee: Bee,
username: string,
address: Utils.EthAddress,
feedType: FeedType,
requestOptions?: BeeRequestOptions,
): Promise<string> {
assertUsername(username)

return (await getFeedData(bee, username, address)).data.chunkContent().text()
return (await getFeedData(bee, username, address, feedType, requestOptions)).data.chunkContent().text()
}

/**
Expand All @@ -41,5 +50,5 @@ export async function uploadEncryptedMnemonic(
assertUsername(username)
assertBase64UrlData(encryptedMnemonic)

return writeFeedDataRaw(connection, username, stringToBytes(encryptedMnemonic), wallet)
return writeEpochFeedDataRaw(connection, username, stringToBytes(encryptedMnemonic), wallet)
}
2 changes: 1 addition & 1 deletion src/cache/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Epoch } from '../feed/lookup/epoch'
import { Epoch } from '../feed/epoch'

export const DEFAULT_CACHE_OPTIONS: CacheOptions = {
isUseCache: true,
Expand Down
2 changes: 1 addition & 1 deletion src/cache/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import CryptoJS from 'crypto-js'
import { CacheEpochData, CacheEpochDataPrepared, CacheInfo, CacheObject, ProcessCacheDataOptions } from './types'
import { Epoch } from '../feed/lookup/epoch'
import { Epoch } from '../feed/epoch'

/**
* Creates cache key
Expand Down
71 changes: 50 additions & 21 deletions src/content-items/handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Connection } from '../connection/connection'
import { utils } from 'ethers'
import { Reference, BeeRequestOptions } from '@ethersphere/bee-js'
import { BeeRequestOptions, Reference } from '@ethersphere/bee-js'
import { getUnixTimestamp } from '../utils/time'
import { writeFeedData } from '../feed/api'
import { getRawDirectoryMetadataBytes } from '../directory/adapter'
Expand All @@ -12,7 +12,7 @@ import { RawMetadataWithEpoch } from './types'
import { prepareEthAddress } from '../utils/wallet'
import { PodPasswordBytes } from '../utils/encryption'
import { DataUploadOptions } from '../file/types'
import { getNextEpoch } from '../feed/lookup/utils'
import { FeedType, WriteFeedOptions } from '../feed/types'

export const DEFAULT_UPLOAD_OPTIONS: DataUploadOptions = {
blockSize: 1000000,
Expand All @@ -28,6 +28,7 @@ export const DEFAULT_UPLOAD_OPTIONS: DataUploadOptions = {
* @param parentPath parent path
* @param entryPath entry path
* @param isFile define if entry is file or directory
* @param feedType feed type
* @param downloadOptions download options
*/
export async function addEntryToDirectory(
Expand All @@ -37,6 +38,7 @@ export async function addEntryToDirectory(
parentPath: string,
entryPath: string,
isFile: boolean,
feedType: FeedType,
downloadOptions?: BeeRequestOptions,
): Promise<Reference> {
if (!parentPath) {
Expand All @@ -54,7 +56,14 @@ export async function addEntryToDirectory(
let parentData: RawDirectoryMetadata | undefined
let metadataWithEpoch: RawMetadataWithEpoch | undefined
try {
metadataWithEpoch = await getRawMetadata(connection.bee, parentPath, address, podPassword, downloadOptions)
metadataWithEpoch = await getRawMetadata(
connection.bee,
parentPath,
address,
podPassword,
connection.options?.feedType ?? FeedType.Epoch,
downloadOptions,
)
assertRawDirectoryMetadata(metadataWithEpoch.metadata)
parentData = metadataWithEpoch.metadata
} catch (e) {
Expand All @@ -71,13 +80,19 @@ export async function addEntryToDirectory(
parentData.fileOrDirNames.push(itemToAdd)
parentData.meta.modificationTime = getUnixTimestamp()

const epoch = metadataWithEpoch.epoch
const writeFeedOptions: WriteFeedOptions = {
feedType,
...(feedType === FeedType.Epoch && epoch ? { epochOptions: { epoch, isGetNextEpoch: true } } : {}),
}

return writeFeedData(
connection,
parentPath,
getRawDirectoryMetadataBytes(parentData),
wallet,
podPassword,
getNextEpoch(metadataWithEpoch.epoch),
writeFeedOptions,
)
}

Expand All @@ -91,13 +106,15 @@ export async function deleteItem(
itemMetaPath: string,
wallet: utils.HDNode,
podPassword: PodPasswordBytes,
writeFeedOptions: WriteFeedOptions,
): Promise<void> {
let pathInfo
try {
pathInfo = await getPathInfo(
connection.bee,
itemMetaPath,
prepareEthAddress(wallet.address),
connection.options?.feedType ?? FeedType.Epoch,
connection.options?.requestOptions,
)
// eslint-disable-next-line no-empty
Expand All @@ -108,15 +125,24 @@ export async function deleteItem(
return
}

let metaPathEpoch

// if information is stored under the path, calculate the next level of epoch
if (pathInfo) {
pathInfo.lookupAnswer.epoch.level = pathInfo.lookupAnswer.epoch.getNextLevel(pathInfo.lookupAnswer.epoch.time)
metaPathEpoch = pathInfo.lookupAnswer.epoch
// for epoch feed additionally calculate the next epoch level
if (writeFeedOptions.feedType === FeedType.Epoch) {
// if information is stored under the path, calculate the next level of epoch
if (pathInfo) {
if (!pathInfo.lookupAnswer.epoch) {
throw new Error('Epoch is not defined')
}

if (!writeFeedOptions.epochOptions) {
writeFeedOptions.epochOptions = {}
}

writeFeedOptions.epochOptions.epoch = pathInfo.lookupAnswer.epoch
writeFeedOptions.epochOptions.isGetNextLevel = true
}
}

await deleteFeedData(connection, itemMetaPath, wallet, podPassword, metaPathEpoch)
await deleteFeedData(connection, itemMetaPath, wallet, podPassword, writeFeedOptions)
}

/**
Expand All @@ -139,11 +165,13 @@ export async function removeEntryFromDirectory(
isFile: boolean,
downloadOptions?: BeeRequestOptions,
): Promise<Reference> {
const feedType = connection.options?.feedType ?? FeedType.Epoch
const metadataWithEpoch = await getRawMetadata(
connection.bee,
parentPath,
prepareEthAddress(wallet.address),
podPassword,
connection.options?.feedType ?? FeedType.Epoch,
downloadOptions,
)
const parentData = metadataWithEpoch.metadata
Expand All @@ -158,14 +186,15 @@ export async function removeEntryFromDirectory(
}

parentData.fileOrDirNames = fileOrDirNames.filter(name => name !== itemToRemove)
await deleteItem(connection, fullPath, wallet, podPassword)

return writeFeedData(
connection,
parentPath,
getRawDirectoryMetadataBytes(parentData),
wallet,
podPassword,
getNextEpoch(metadataWithEpoch.epoch),
)
await deleteItem(connection, fullPath, wallet, podPassword, {
feedType,
})

return writeFeedData(connection, parentPath, getRawDirectoryMetadataBytes(parentData), wallet, podPassword, {
feedType,
epochOptions: {
epoch: metadataWithEpoch.epoch,
isGetNextEpoch: true,
},
})
}
11 changes: 9 additions & 2 deletions src/content-items/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Epoch } from '../feed/lookup/epoch'
import { Epoch } from '../feed/epoch'
import { RawDirectoryMetadata, RawFileMetadata } from '../pod/types'
import { BeeRequestOptions } from '@ethersphere/bee-js'
import { CacheInfo } from '../cache/types'
Expand Down Expand Up @@ -26,8 +26,15 @@ export interface DownloadOptions {
* Metadata of a file or directory with epoch
*/
export interface RawMetadataWithEpoch {
epoch: Epoch
/**
* Metadata of a file or directory
*/
metadata: RawDirectoryMetadata | RawFileMetadata

/**
* Epoch of the metadata from the epoch feed
*/
epoch?: Epoch
}

/**
Expand Down
24 changes: 16 additions & 8 deletions src/content-items/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import CryptoJS from 'crypto-js'
import { isObject } from '../utils/type'
import { Connection } from '../connection/connection'
import { utils, Wallet } from 'ethers'
import { Epoch } from '../feed/lookup/epoch'
import { stringToBytes } from '../utils/bytes'
import { FeedType, WriteFeedOptions } from '../feed/types'

/**
* Get raw metadata by path
Expand All @@ -19,16 +19,18 @@ import { stringToBytes } from '../utils/bytes'
* @param path path with information
* @param address Ethereum address of the pod which owns the path
* @param podPassword bytes for data encryption from pod metadata
* @param feedType feed type
* @param requestOptions options for downloading
*/
export async function getRawMetadata(
bee: Bee,
path: string,
address: EthAddress,
podPassword: PodPasswordBytes,
feedType: FeedType,
requestOptions?: BeeRequestOptions,
): Promise<RawMetadataWithEpoch> {
const feedData = await getFeedData(bee, path, address, requestOptions)
const feedData = await getFeedData(bee, path, address, feedType, requestOptions)
const data = decryptJson(podPassword, feedData.data.chunkContent())
let metadata

Expand All @@ -52,16 +54,18 @@ export async function getRawMetadata(
* @param bee Bee instance
* @param fullPath full path to the item
* @param address uploader address
* @param feedType feed type
* @param requestOptions options for downloading
*/
export async function isItemExists(
bee: Bee,
fullPath: string,
address: EthAddress,
feedType: FeedType,
requestOptions: BeeRequestOptions | undefined,
): Promise<boolean> {
try {
return (await getFeedData(bee, fullPath, address, requestOptions)).data.text() === DELETE_FEED_MAGIC_WORD
return (await getFeedData(bee, fullPath, address, feedType, requestOptions)).data.text() === DELETE_FEED_MAGIC_WORD
} catch (e) {
return false
}
Expand All @@ -74,16 +78,18 @@ export async function isItemExists(
* @param bee Bee instance
* @param fullPath full path to the item
* @param address uploader address
* @param feedType feed type
* @param downloadOptions options for downloading
*/
export async function assertItemIsNotExists(
contentType: string,
bee: Bee,
fullPath: string,
address: EthAddress,
feedType: FeedType,
downloadOptions: BeeRequestOptions | undefined,
): Promise<void> {
if (await isItemExists(bee, fullPath, address, downloadOptions)) {
if (await isItemExists(bee, fullPath, address, feedType, downloadOptions)) {
throw new Error(`${contentType} "${fullPath}" already uploaded to the network`)
}
}
Expand Down Expand Up @@ -129,9 +135,10 @@ export async function getPathInfo(
bee: Bee,
path: string,
address: EthAddress,
feedType: FeedType,
requestOptions?: BeeRequestOptions,
): Promise<PathInformation> {
const lookupAnswer = await getFeedData(bee, path, address, requestOptions)
const lookupAnswer = await getFeedData(bee, path, address, feedType, requestOptions)

return {
isDeleted: lookupAnswer.data.text() === DELETE_FEED_MAGIC_WORD,
Expand Down Expand Up @@ -160,12 +167,13 @@ export async function getCreationPathInfo(
bee: Bee,
fullPath: string,
address: EthAddress,
feedType: FeedType,
requestOptions?: BeeRequestOptions,
): Promise<PathInformation | undefined> {
// check that if directory uploaded - than it should be marked as deleted
let pathInfo
try {
pathInfo = await getPathInfo(bee, fullPath, address, requestOptions)
pathInfo = await getPathInfo(bee, fullPath, address, feedType, requestOptions)
assertItemDeleted(pathInfo, fullPath)
// eslint-disable-next-line no-empty
} catch (e) {}
Expand All @@ -181,7 +189,7 @@ export async function deleteFeedData(
topic: string,
wallet: utils.HDNode | Wallet,
podPassword: PodPasswordBytes,
epoch?: Epoch,
writeFeedOptions: WriteFeedOptions,
): Promise<Reference> {
return writeFeedData(connection, topic, stringToBytes(DELETE_FEED_MAGIC_WORD), wallet, podPassword, epoch)
return writeFeedData(connection, topic, stringToBytes(DELETE_FEED_MAGIC_WORD), wallet, podPassword, writeFeedOptions)
}
2 changes: 2 additions & 0 deletions src/directory/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { uploadData } from '../file/handler'
import { assertNodeFileInfo, isBrowserFileInfo } from './types'
import { DirectoryItem } from '../content-items/types'
import { FeedType } from '../feed/types'

/**
* Directory related class
Expand All @@ -44,6 +45,7 @@ export class Directory {
path,
podAddress,
pod.password,
this.accountData.connection?.options?.feedType ?? FeedType.Epoch,
isRecursive,
this.accountData.connection.options?.requestOptions,
)
Expand Down
Loading