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: pod subscriptions #295

Merged
merged 6 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@ethersphere/bee-js": "^6.2.0",
"@fairdatasociety/fdp-contracts-js": "^3.10.0",
"crypto-js": "^4.2.0",
"elliptic": "^6.5.4",
"ethers": "^5.5.2",
"js-sha3": "^0.9.2"
},
Expand Down
6 changes: 4 additions & 2 deletions src/fdp-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Connection } from './connection/connection'
import { Options } from './types'
import { Directory } from './directory/directory'
import { File } from './file/file'
import { ENS } from '@fairdatasociety/fdp-contracts-js'
import { ENS, DataHub } from '@fairdatasociety/fdp-contracts-js'
import { CacheInfo, DEFAULT_CACHE_OPTIONS } from './cache/types'

export class FdpStorage {
Expand All @@ -15,6 +15,7 @@ export class FdpStorage {
public readonly directory: Directory
public readonly file: File
public readonly ens: ENS
public readonly dataHub: DataHub
public readonly cache: CacheInfo

constructor(beeUrl: string, postageBatchId: BatchId, options?: Options) {
Expand All @@ -24,8 +25,9 @@ export class FdpStorage {
}
this.connection = new Connection(new Bee(beeUrl), postageBatchId, this.cache, options)
this.ens = new ENS(options?.ensOptions, null, options?.ensDomain)
this.dataHub = new DataHub(options?.dataHubOptions, null, options?.ensDomain)
this.account = new AccountData(this.connection, this.ens)
this.personalStorage = new PersonalStorage(this.account)
this.personalStorage = new PersonalStorage(this.account, this.ens, this.dataHub)
this.directory = new Directory(this.account)
this.file = new File(this.account)
}
Expand Down
52 changes: 49 additions & 3 deletions src/pod/personal-storage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ec as EC } from 'elliptic'
import { SharedPod, PodReceiveOptions, PodShareInfo, PodsList, Pod, PodsListPrepared } from './types'
import { assertAccount } from '../account/utils'
import { writeFeedData } from '../feed/api'
Expand All @@ -15,21 +16,29 @@ import {
podPreparedToPod,
sharedPodPreparedToSharedPod,
podListToJSON,
assertPodShareInfo,
} from './utils'
import { getExtendedPodsList } from './api'
import { uploadBytes } from '../file/utils'
import { stringToBytes } from '../utils/bytes'
import { bytesToString, stringToBytes } from '../utils/bytes'
import { Reference, Utils } from '@ethersphere/bee-js'
import { assertEncryptedReference, EncryptedReference } from '../utils/hex'
import { assertEncryptedReference, EncryptedReference, HexString } from '../utils/hex'
import { prepareEthAddress, preparePrivateKey } from '../utils/wallet'
import { getCacheKey, setEpochCache } from '../cache/utils'
import { getPodsList } from './cache/api'
import { getNextEpoch } from '../feed/lookup/utils'
import { DataHub, ENS, ENS_DOMAIN, Subscription } from '@fairdatasociety/fdp-contracts-js'
import { decryptBytes } from '../utils/encryption'
import { namehash } from 'ethers/lib/utils'

export const POD_TOPIC = 'Pods'

export class PersonalStorage {
constructor(private accountData: AccountData) {}
constructor(
private accountData: AccountData,
private ens: ENS,
private dataHub: DataHub,
) {}

/**
* Gets the list of pods for the active account
Expand Down Expand Up @@ -198,4 +207,41 @@ export class PersonalStorage {

return sharedPodPreparedToSharedPod(pod)
}

async getSubscriptions(ens: string): Promise<Subscription[]> {
const subItems = await this.dataHub.getAllSubItemsForNameHash(namehash(`${ens}.${ENS_DOMAIN}`))

const subscriptions: Subscription[] = []

for (let i = 0; i < subItems.length; i++) {
const sub = await this.dataHub.getSubBy(subItems[i].subHash)

subscriptions.push(sub)
}

return subscriptions
}

async openSubscribedPod(subHash: HexString, swarmLocation: HexString): Promise<unknown> {
const sub = await this.dataHub.getSubBy(subHash)

const publicKeyHex = await this.ens.getPublicKeyByUsernameHash(sub.fdpSellerNameHash)

const wallet = this.accountData.wallet!

const ec = new EC('secp256k1')

const privateKey = ec.keyFromPrivate(wallet.privateKey)
const publicKey = ec.keyFromPublic(publicKeyHex.substring(2), 'hex')

const secret = privateKey.derive(publicKey.getPublic()).toString(16)

const encryptedData = await this.accountData.connection.bee.downloadData(swarmLocation)

const data = JSON.parse(bytesToString(decryptBytes(secret, encryptedData)))

assertPodShareInfo(data)

return data
}
}
6 changes: 5 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BeeRequestOptions } from '@ethersphere/bee-js'
import { EnsEnvironment } from '@fairdatasociety/fdp-contracts-js'
import { DataHubEnvironment, EnsEnvironment } from '@fairdatasociety/fdp-contracts-js'
import { CacheOptions } from './cache/types'

export { DirectoryItem, FileItem } from './content-items/types'
Expand Down Expand Up @@ -42,6 +42,10 @@ export interface Options {
* FDP-contracts options
*/
ensOptions?: EnsEnvironment
/**
* FDP-contracts options
*/
dataHubOptions?: DataHubEnvironment
/**
* ENS domain for usernames
*/
Expand Down