1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00
market/src/@utils/provider.ts
EnzoVezzaro f6d11e5e6f
adding ipfs / arweave support (#1765)
* support storage type publish

* adding storageType to edit form

* add IPFS type

* fix testst and rollback ipfs typing

* update oceanjs lib

* fix Ipfs type

* Update package-lock.json

* added graphql and smartcontract options UI (WIP)

* update package.json

* removed graphql and smartcontracts

* make various fixes for edit and publish with IPFS (missing Arweave)

* removed no-case-declarations lines

* moved ipfs utils

* renamed getFileUrlInfo to getFileInfo

* added is-ipfs to jest mock

* Update package-lock.json

* fix things

* npm is fun

* rename url to file in getFileInfo

* refactor publish form (storage type field)

* fix tab value when changing tabs

* refactoring edit form

* more refactor edit form

* fix edit validation

* fix validation when input is empty on edit form

* fix validation when loading asset in edit form

* change URL to file confirmed

* change messages based on service type

* Update form.json

* fix FileInput tests and added ipfs / arweave tests

* removed unnecessary comment

* Update index.tsx

* cleanup logic

* update @oceanprotocol/lib

* Update package-lock.json

* fix test error

* fix test assetsWithAccessDetails

Co-authored-by: Matthias Kretschmann <m@kretschmann.io>
2022-11-25 15:07:55 +01:00

150 lines
3.4 KiB
TypeScript

import {
Arweave,
ComputeAlgorithm,
ComputeAsset,
ComputeEnvironment,
downloadFileBrowser,
FileInfo,
Ipfs,
LoggerInstance,
ProviderComputeInitializeResults,
ProviderInstance,
UrlFile
} from '@oceanprotocol/lib'
import Web3 from 'web3'
import { getValidUntilTime } from './compute'
export async function initializeProviderForCompute(
dataset: AssetExtended,
algorithm: AssetExtended,
accountId: string,
computeEnv: ComputeEnvironment = null
): Promise<ProviderComputeInitializeResults> {
const computeAsset: ComputeAsset = {
documentId: dataset.id,
serviceId: dataset.services[0].id,
transferTxId: dataset.accessDetails.validOrderTx
}
const computeAlgo: ComputeAlgorithm = {
documentId: algorithm.id,
serviceId: algorithm.services[0].id,
transferTxId: algorithm.accessDetails.validOrderTx
}
const validUntil = getValidUntilTime(
computeEnv?.maxJobDuration,
dataset.services[0].timeout,
algorithm.services[0].timeout
)
try {
return await ProviderInstance.initializeCompute(
[computeAsset],
computeAlgo,
computeEnv?.id,
validUntil,
dataset.services[0].serviceEndpoint,
accountId
)
} catch (error) {
LoggerInstance.error(`Error initializing provider for the compute job!`)
return null
}
}
// TODO: Why do we have these one line functions ?!?!?!
export async function getEncryptedFiles(
files: any,
providerUrl: string
): Promise<string> {
try {
// https://github.com/oceanprotocol/provider/blob/v4main/API.md#encrypt-endpoint
const response = await ProviderInstance.encrypt(files, providerUrl)
return response
} catch (error) {
console.error('Error parsing json: ' + error.message)
}
}
export async function getFileDidInfo(
did: string,
serviceId: string,
providerUrl: string,
withChecksum = false
): Promise<FileInfo[]> {
try {
const response = await ProviderInstance.checkDidFiles(
did,
serviceId,
providerUrl,
withChecksum
)
return response
} catch (error) {
LoggerInstance.error(error.message)
}
}
export async function getFileInfo(
file: string,
providerUrl: string,
storageType: string
): Promise<FileInfo[]> {
try {
let response
switch (storageType) {
case 'ipfs': {
const fileIPFS: Ipfs = {
type: 'ipfs',
hash: file
}
response = await ProviderInstance.getFileInfo(fileIPFS, providerUrl)
break
}
case 'arweave': {
const fileArweave: Arweave = {
type: 'arweave',
transactionId: file
}
response = await ProviderInstance.getFileInfo(fileArweave, providerUrl)
break
}
default: {
const fileUrl: UrlFile = {
type: 'url',
index: 0,
url: file,
method: 'get'
}
response = await ProviderInstance.getFileInfo(fileUrl, providerUrl)
break
}
}
return response
} catch (error) {
LoggerInstance.error(error.message)
}
}
export async function downloadFile(
web3: Web3,
asset: AssetExtended,
accountId: string,
validOrderTx?: string
) {
const downloadUrl = await ProviderInstance.getDownloadUrl(
asset.id,
accountId,
asset.services[0].id,
0,
validOrderTx || asset.accessDetails.validOrderTx,
asset.services[0].serviceEndpoint,
web3
)
await downloadFileBrowser(downloadUrl)
}