Skip to content

Commit

Permalink
feat: ability to upload any amount of pods
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorShadurin committed Dec 6, 2023
1 parent 3844760 commit ca47aaa
Show file tree
Hide file tree
Showing 21 changed files with 522 additions and 162 deletions.
26 changes: 25 additions & 1 deletion package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"@fairdatasociety/fdp-contracts-js": "^3.8.0",
"crypto-js": "^4.2.0",
"ethers": "^5.5.2",
"js-sha3": "^0.9.2"
"js-sha3": "^0.9.2",
"pako": "^2.1.0"
},
"devDependencies": {
"@babel/core": "^7.23.2",
Expand All @@ -79,6 +80,7 @@
"@types/jest": "^29.5.6",
"@types/jest-environment-puppeteer": "^5.0.5",
"@types/node": "^20.8.9",
"@types/pako": "^2.0.3",
"@types/webpack-bundle-analyzer": "^4.6.2",
"@types/ws": "^8.5.8",
"@typescript-eslint/eslint-plugin": "^6.9.0",
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
40 changes: 28 additions & 12 deletions src/content-items/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bee, Reference, BeeRequestOptions } from '@ethersphere/bee-js'
import { Bee, Reference, BeeRequestOptions, Data } from '@ethersphere/bee-js'
import { EthAddress } from '@ethersphere/bee-js/dist/types/utils/eth'
import { RawDirectoryMetadata, RawFileMetadata } from '../pod/types'
import { DELETE_FEED_MAGIC_WORD, getFeedData, writeFeedData } from '../feed/api'
Expand All @@ -12,6 +12,32 @@ import { utils, Wallet } from 'ethers'
import { Epoch } from '../feed/lookup/epoch'
import { stringToBytes } from '../utils/bytes'

/**
* Extracts metadata from encrypted source data using a pod password
*
* @param {Data} sourceData - The encrypted source data from which to extract metadata.
* @param {PodPasswordBytes} podPassword - The pod password used to decrypt the source data.
* @throws {Error} If the metadata is invalid.
* @returns {RawDirectoryMetadata | RawFileMetadata} The extracted metadata.
*/
export function extractMetadata(
sourceData: Data,
podPassword: PodPasswordBytes,
): RawDirectoryMetadata | RawFileMetadata {
const data = decryptJson(podPassword, sourceData)
let metadata

if (isRawDirectoryMetadata(data)) {
metadata = data as RawDirectoryMetadata
} else if (isRawFileMetadata(data)) {
metadata = data as RawFileMetadata
} else {
throw new Error('Invalid metadata')
}

return metadata
}

/**
* Get raw metadata by path
*
Expand All @@ -29,20 +55,10 @@ export async function getRawMetadata(
requestOptions?: BeeRequestOptions,
): Promise<RawMetadataWithEpoch> {
const feedData = await getFeedData(bee, path, address, requestOptions)
const data = decryptJson(podPassword, feedData.data.chunkContent())
let metadata

if (isRawDirectoryMetadata(data)) {
metadata = data as RawDirectoryMetadata
} else if (isRawFileMetadata(data)) {
metadata = data as RawFileMetadata
} else {
throw new Error('Invalid metadata')
}

return {
epoch: feedData.epoch,
metadata,
metadata: extractMetadata(feedData.data.chunkContent(), podPassword),
}
}

Expand Down
18 changes: 13 additions & 5 deletions src/feed/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Bee, Data, Reference, BeeRequestOptions, Utils } from '@ethersphere/bee
import { bmtHashString } from '../account/utils'
import { getId } from './handler'
import { lookup } from './lookup/linear'
import { Epoch, HIGHEST_LEVEL } from './lookup/epoch'
import { Epoch, getFirstEpoch } from './lookup/epoch'
import { bytesToHex } from '../utils/hex'
import { getUnixTimestamp } from '../utils/time'
import { LookupAnswer } from './types'
Expand Down Expand Up @@ -57,14 +57,22 @@ export async function writeFeedData(
podPassword: PodPasswordBytes,
epoch?: Epoch,
): Promise<Reference> {
if (!epoch) {
epoch = new Epoch(HIGHEST_LEVEL, getUnixTimestamp())
}
epoch = prepareEpoch(epoch)
data = encryptBytes(podPassword, data)

const topicHash = bmtHashString(topic)
const id = getId(topicHash, epoch.time, epoch.level)
const socWriter = connection.bee.makeSOCWriter(wallet.privateKey)

return socWriter.upload(connection.postageBatchId, id, data)
}

/**
* Prepares an epoch for further processing.
*
* @param {Epoch} [epoch] - The epoch to prepare. If not provided, a new epoch will be created.
*
* @return {Epoch} The prepared epoch.
*/
export function prepareEpoch(epoch?: Epoch): Epoch {
return epoch ?? getFirstEpoch(getUnixTimestamp())
}
2 changes: 1 addition & 1 deletion src/file/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class File {
assertAccount(this.accountData, { writeRequired: true })
assertPodName(podName)

return uploadData(podName, fullPath, data, this.accountData, options)
return (await uploadData(podName, fullPath, data, this.accountData, options)).meta
}

/**
Expand Down
Loading

0 comments on commit ca47aaa

Please sign in to comment.