Skip to content

Commit

Permalink
feat: subscription pods improvements (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomicvladan authored Mar 5, 2024
1 parent cd57747 commit ba89596
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 12 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
"dependencies": {
"@ethersphere/bee-js": "^6.2.0",
"@fairdatasociety/fdp-contracts-js": "^3.10.0",
"@fairdatasociety/fdp-contracts-js": "^3.10.1",
"crypto-js": "^4.2.0",
"elliptic": "^6.5.4",
"ethers": "^5.5.2",
Expand Down
27 changes: 27 additions & 0 deletions src/directory/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { uploadData } from '../file/handler'
import { assertNodeFileInfo, isBrowserFileInfo } from './types'
import { DirectoryItem } from '../content-items/types'
import { prepareEthAddress } from '../utils/wallet'
import { PodShareInfo } from '../pod/types'
import { Utils } from '@ethersphere/bee-js'

/**
* Directory related class
Expand Down Expand Up @@ -99,6 +101,31 @@ export class Directory {
}
}

/**
* Get files and directories under the given path from a shared pod
*
* Account is required, postage batch id is not required
*
* @param pod a PodShareInfo object
* @param path path to start searching from
* @param isRecursive search with recursion or not
*/
async readFromShared(pod: PodShareInfo, path: string, isRecursive?: boolean): Promise<DirectoryItem> {
const { podName, podAddress, password } = pod
assertAccount(this.accountData)
assertPodName(pod.podName)

return await readDirectory(
this.accountData,
podName,
path,
prepareEthAddress(podAddress),
Utils.hexToBytes(password),
isRecursive,
this.accountData.connection.options?.requestOptions,
)
}

/**
* Creates a directory
*
Expand Down
39 changes: 37 additions & 2 deletions src/file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import {
uploadBytes,
} from './utils'
import { writeFeedData } from '../feed/api'
import { downloadData, getFileMetadataWithBlocks, uploadData, uploadDataBlock } from './handler'
import {
downloadArbitraryPodData,
downloadData,
getFileMetadataWithBlocks,
uploadData,
uploadDataBlock,
} from './handler'
import { getFileMetadataRawBytes, rawFileMetadataToFileMetadata } from './adapter'
import {
DataDownloadOptions,
Expand All @@ -23,7 +29,7 @@ import {
FileShareInfo,
} from './types'
import { addEntryToDirectory, DEFAULT_UPLOAD_OPTIONS, removeEntryFromDirectory } from '../content-items/handler'
import { BeeRequestOptions, Reference } from '@ethersphere/bee-js'
import { BeeRequestOptions, Reference, Utils } from '@ethersphere/bee-js'
import { getRawMetadata } from '../content-items/utils'
import { assertRawFileMetadata, combine, splitPath } from '../directory/utils'
import { assertEncryptedReference, EncryptedReference } from '../utils/hex'
Expand Down Expand Up @@ -59,6 +65,35 @@ export class File {
)
}

/**
* Downloads file content from any pod
*
* Account is required, postage batch id is not required
*
* @param podAddress pod address
* @param podPassword pod password
* @param fullPath full path of the file
* @param options download options
*/
async downloadArbitraryPodData(
podAddress: string,
podPassword: string,
fullPath: string,
options?: DataDownloadOptions,
): Promise<Uint8Array> {
assertAccount(this.accountData)
assertFullPathWithName(fullPath)

return downloadArbitraryPodData(
this.accountData,
prepareEthAddress(podAddress),
Utils.hexToBytes(podPassword),
fullPath,
this.accountData.connection.options?.requestOptions,
options,
)
}

/**
* Uploads file content
*
Expand Down
60 changes: 59 additions & 1 deletion src/file/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,33 @@ export async function getFileMetadataWithBlocks(
dataDownloadOptions = dataDownloadOptions ?? {}
updateDownloadProgress(dataDownloadOptions, DownloadProgressType.GetPodInfo)
const { podAddress, pod } = await getExtendedPodsListByAccountData(accountData, podName)

return getFileMetadataWithBlocksAndPod(bee, accountData, podAddress, pod.password, fullPath, downloadOptions)
}

/**
* Gets file metadata with blocks
*
* @param bee Bee
* @param accountData account data
* @param podAddress pod address
* @param podPassword pod password
* @param fullPath full path to the file
* @param downloadOptions download options
* @param dataDownloadOptions data download options
*/
export async function getFileMetadataWithBlocksAndPod(
bee: Bee,
accountData: AccountData,
podAddress: EthAddress,
podPassword: PodPasswordBytes,
fullPath: string,
downloadOptions?: BeeRequestOptions,
dataDownloadOptions?: DataDownloadOptions,
): Promise<FileMetadataWithBlocks> {
dataDownloadOptions = dataDownloadOptions ?? {}
updateDownloadProgress(dataDownloadOptions, DownloadProgressType.GetPathInfo)
const fileMetadata = await getFileMetadata(bee, fullPath, podAddress, pod.password, downloadOptions)
const fileMetadata = await getFileMetadata(bee, fullPath, podAddress, podPassword, downloadOptions)
updateDownloadProgress(dataDownloadOptions, DownloadProgressType.DownloadBlocksMeta)
const blocks = await downloadBlocksManifest(bee, fileMetadata.blocksReference, downloadOptions)
await prepareDataByMeta(fileMetadata, blocks.blocks, accountData.connection.bee, downloadOptions)
Expand Down Expand Up @@ -191,6 +216,39 @@ export async function downloadData(
return prepareDataByMeta(fileMeta, fileMeta.blocks, bee, downloadOptions, dataDownloadOptions)
}

/**
* Downloads file parts and compile them into Data
*
* @param accountData account data
* @param podAddress pod address
* @param podPassword pod password
* @param fullPath full path to the file
* @param downloadOptions download options
* @param dataDownloadOptions data download options
*/
export async function downloadArbitraryPodData(
accountData: AccountData,
podAddress: EthAddress,
podPassword: PodPasswordBytes,
fullPath: string,
downloadOptions?: BeeRequestOptions,
dataDownloadOptions?: DataDownloadOptions,
): Promise<Data> {
dataDownloadOptions = dataDownloadOptions ?? {}
const bee = accountData.connection.bee
const fileMeta = await getFileMetadataWithBlocksAndPod(
bee,
accountData,
podAddress,
podPassword,
fullPath,
downloadOptions,
dataDownloadOptions,
)

return prepareDataByMeta(fileMeta, fileMeta.blocks, bee, downloadOptions, dataDownloadOptions)
}

/**
* Interface representing information about blocks.
* @interface
Expand Down
34 changes: 34 additions & 0 deletions src/pod/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,37 @@ export async function getExtendedPodsList(
epoch,
}
}

/**
* Gets pods list with pod addresses
*
* @param accountData account data
* @param bee Bee client
* @param userWallet root wallet for downloading and decrypting data
* @param seed seed of wallet owns the FDP account
* @param downloadOptions request options
*/
export async function getPodListExtended(
accountData: AccountData,
bee: Bee,
userWallet: utils.HDNode,
seed: Uint8Array,
downloadOptions?: DownloadOptions,
): Promise<ExtendedPodInfo[]> {
const { podsList, epoch } = await getPodsListCached(accountData, bee, userWallet, downloadOptions)

const extendedPodList: ExtendedPodInfo[] = []

for (const pod of podsList.pods) {
const podWallet = await getWalletByIndex(seed, pod.index, downloadOptions?.cacheInfo)

extendedPodList.push({
pod,
podAddress: prepareEthAddress(podWallet.address),
podWallet,
epoch,
})
}

return extendedPodList
}
18 changes: 17 additions & 1 deletion src/pod/personal-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import {
podsListPreparedToPodsList,
sharedPodPreparedToSharedPod,
uploadPodDataV2,
ExtendedPodInfo,
} from './utils'
import { getExtendedPodsList } from './api'
import { getExtendedPodsList, getPodListExtended } from './api'
import { uploadBytes } from '../file/utils'
import { bytesToString, stringToBytes } from '../utils/bytes'
import { Reference, Utils } from '@ethersphere/bee-js'
Expand Down Expand Up @@ -74,6 +75,21 @@ export class PersonalStorage {
return podsListPreparedToPodsList(podsList)
}

async listExtended(): Promise<ExtendedPodInfo[]> {
assertAccount(this.accountData)

return getPodListExtended(
this.accountData,
this.accountData.connection.bee,
this.accountData.wallet!,
this.accountData.seed!,
{
requestOptions: this.accountData.connection.options?.requestOptions,
cacheInfo: this.accountData.connection.cacheInfo,
},
)
}

/**
* Creates new pod with passed name
*
Expand Down

0 comments on commit ba89596

Please sign in to comment.