2021-11-23 13:53:09 +01:00
|
|
|
import { mapTimeoutStringToSeconds } from '@utils/ddo'
|
2021-11-16 22:06:02 +01:00
|
|
|
import { getEncryptedFileUrls } from '@utils/provider'
|
2021-11-11 12:26:29 +01:00
|
|
|
import { sha256 } from 'js-sha256'
|
2021-11-11 14:40:38 +01:00
|
|
|
import slugify from 'slugify'
|
2021-11-29 16:57:12 +01:00
|
|
|
import {
|
|
|
|
algorithmContainerPresets,
|
|
|
|
computeEnvironmentDefaults
|
|
|
|
} from './_constants'
|
2021-10-28 11:38:40 +02:00
|
|
|
import { FormPublishData } from './_types'
|
|
|
|
|
|
|
|
export function getFieldContent(
|
|
|
|
fieldName: string,
|
|
|
|
fields: FormFieldContent[]
|
|
|
|
): FormFieldContent {
|
|
|
|
return fields.filter((field: FormFieldContent) => field.name === fieldName)[0]
|
|
|
|
}
|
|
|
|
|
2021-11-11 14:40:38 +01:00
|
|
|
function getUrlFileExtension(fileUrl: string): string {
|
|
|
|
const splittedFileUrl = fileUrl.split('.')
|
|
|
|
return splittedFileUrl[splittedFileUrl.length - 1]
|
|
|
|
}
|
|
|
|
|
2021-11-29 14:25:30 +01:00
|
|
|
function getAlgorithmContainerPreset(
|
|
|
|
dockerImage: string
|
|
|
|
): MetadataAlgorithmContainer {
|
|
|
|
if (dockerImage === '') return
|
|
|
|
|
|
|
|
const preset = algorithmContainerPresets.find(
|
|
|
|
(preset) => `${preset.image}:${preset.tag}` === dockerImage
|
|
|
|
)
|
|
|
|
return preset
|
|
|
|
}
|
|
|
|
|
2021-11-11 14:40:38 +01:00
|
|
|
function dateToStringNoMS(date: Date): string {
|
|
|
|
return date.toISOString().replace(/\.[0-9]{3}Z/, 'Z')
|
|
|
|
}
|
|
|
|
|
|
|
|
function transformTags(value: string): string[] {
|
|
|
|
const originalTags = value?.split(',')
|
|
|
|
const transformedTags = originalTags?.map((tag) => slugify(tag).toLowerCase())
|
|
|
|
return transformedTags
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function transformPublishFormToDdo(
|
2021-11-11 12:26:29 +01:00
|
|
|
values: FormPublishData,
|
2021-11-23 13:53:09 +01:00
|
|
|
// Those 2 are only passed during actual publishing process
|
|
|
|
// so we can always assume if they are not passed, we are on preview.
|
|
|
|
datatokenAddress?: string,
|
|
|
|
nftAddress?: string
|
2021-11-11 14:40:38 +01:00
|
|
|
): Promise<DDO> {
|
2021-11-23 13:53:09 +01:00
|
|
|
const { metadata, services, user } = values
|
|
|
|
const { chainId, accountId } = user
|
2021-11-11 08:51:13 +01:00
|
|
|
const {
|
2021-11-15 13:44:27 +01:00
|
|
|
type,
|
|
|
|
name,
|
|
|
|
description,
|
|
|
|
tags,
|
|
|
|
author,
|
|
|
|
termsAndConditions,
|
2021-11-29 14:25:30 +01:00
|
|
|
dockerImage,
|
2021-11-15 13:44:27 +01:00
|
|
|
dockerImageCustom,
|
|
|
|
dockerImageCustomTag,
|
2021-11-29 14:25:30 +01:00
|
|
|
dockerImageCustomEntrypoint,
|
|
|
|
dockerImageCustomChecksum
|
2021-11-15 13:44:27 +01:00
|
|
|
} = metadata
|
2021-11-23 16:34:43 +01:00
|
|
|
const { access, files, links, providerUrl, timeout } = services[0]
|
2021-11-11 08:51:13 +01:00
|
|
|
|
2021-11-26 11:35:41 +01:00
|
|
|
const did = nftAddress ? `0x${sha256(`${nftAddress}${chainId}`)}` : '0x...'
|
|
|
|
const currentTime = dateToStringNoMS(new Date())
|
|
|
|
|
2021-11-23 16:34:43 +01:00
|
|
|
// Transform from files[0].url to string[] assuming only 1 file
|
|
|
|
const filesTransformed = files?.length && files[0].valid && [files[0].url]
|
|
|
|
const linksTransformed = links?.length && links[0].valid && [links[0].url]
|
2021-11-11 10:22:22 +01:00
|
|
|
|
2021-11-11 14:40:38 +01:00
|
|
|
const newMetadata: Metadata = {
|
2021-11-11 10:22:22 +01:00
|
|
|
created: currentTime,
|
|
|
|
updated: currentTime,
|
|
|
|
type,
|
|
|
|
name,
|
|
|
|
description,
|
|
|
|
tags: transformTags(tags),
|
|
|
|
author,
|
|
|
|
license: 'https://market.oceanprotocol.com/terms',
|
2021-11-23 16:34:43 +01:00
|
|
|
links: linksTransformed,
|
2021-11-11 10:22:22 +01:00
|
|
|
additionalInformation: {
|
|
|
|
termsAndConditions
|
|
|
|
},
|
2021-11-29 14:25:30 +01:00
|
|
|
...(type === 'algorithm' &&
|
|
|
|
dockerImage !== '' && {
|
|
|
|
algorithm: {
|
|
|
|
language: filesTransformed?.length
|
|
|
|
? getUrlFileExtension(filesTransformed[0])
|
|
|
|
: '',
|
|
|
|
version: '0.1',
|
|
|
|
container: {
|
|
|
|
entrypoint:
|
|
|
|
dockerImage === 'custom'
|
|
|
|
? dockerImageCustomEntrypoint
|
|
|
|
: getAlgorithmContainerPreset(dockerImage).entrypoint,
|
|
|
|
image:
|
|
|
|
dockerImage === 'custom'
|
|
|
|
? dockerImageCustom
|
|
|
|
: getAlgorithmContainerPreset(dockerImage).image,
|
|
|
|
tag:
|
|
|
|
dockerImage === 'custom'
|
|
|
|
? dockerImageCustomTag
|
|
|
|
: getAlgorithmContainerPreset(dockerImage).tag,
|
|
|
|
checksum:
|
|
|
|
dockerImage === 'custom'
|
|
|
|
? dockerImageCustomChecksum
|
|
|
|
: getAlgorithmContainerPreset(dockerImage).checksum
|
|
|
|
}
|
2021-11-11 10:22:22 +01:00
|
|
|
}
|
2021-11-29 14:25:30 +01:00
|
|
|
})
|
2021-11-11 10:22:22 +01:00
|
|
|
}
|
2021-10-28 11:38:40 +02:00
|
|
|
|
2021-11-26 11:35:41 +01:00
|
|
|
// Encrypt just created string[] of urls
|
2021-11-23 16:34:43 +01:00
|
|
|
const filesEncrypted =
|
|
|
|
files?.length &&
|
|
|
|
files[0].valid &&
|
2021-11-25 15:20:00 +01:00
|
|
|
(await getEncryptedFileUrls(
|
|
|
|
filesTransformed,
|
|
|
|
providerUrl.url,
|
|
|
|
did,
|
|
|
|
accountId
|
|
|
|
))
|
2021-11-23 16:34:43 +01:00
|
|
|
|
2021-11-11 14:40:38 +01:00
|
|
|
const newService: Service = {
|
2021-11-11 08:51:13 +01:00
|
|
|
type: access,
|
2021-11-23 16:34:43 +01:00
|
|
|
files: filesEncrypted || '',
|
2021-11-11 12:26:29 +01:00
|
|
|
datatokenAddress,
|
2021-11-25 15:20:00 +01:00
|
|
|
serviceEndpoint: providerUrl.url,
|
2021-11-23 13:53:09 +01:00
|
|
|
timeout: mapTimeoutStringToSeconds(timeout),
|
2021-11-11 10:22:22 +01:00
|
|
|
...(access === 'compute' && {
|
2021-11-29 16:57:12 +01:00
|
|
|
compute: computeEnvironmentDefaults
|
2021-11-11 10:22:22 +01:00
|
|
|
})
|
2021-10-28 11:38:40 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 08:51:13 +01:00
|
|
|
const newDdo: DDO = {
|
2021-11-11 09:55:35 +01:00
|
|
|
'@context': ['https://w3id.org/did/v1'],
|
2021-11-11 12:26:29 +01:00
|
|
|
id: did,
|
2021-11-11 08:51:13 +01:00
|
|
|
version: '4.0.0',
|
2021-11-11 14:40:38 +01:00
|
|
|
chainId,
|
|
|
|
metadata: newMetadata,
|
2021-11-23 13:53:09 +01:00
|
|
|
services: [newService],
|
2021-11-26 11:35:41 +01:00
|
|
|
// Only added for DDO preview, reflecting Asset response,
|
|
|
|
// again, we can assume if `datatokenAddress` is not passed,
|
|
|
|
// we are on preview.
|
2021-11-23 13:53:09 +01:00
|
|
|
...(!datatokenAddress && {
|
|
|
|
dataTokenInfo: {
|
|
|
|
name: values.services[0].dataTokenOptions.name,
|
|
|
|
symbol: values.services[0].dataTokenOptions.symbol
|
|
|
|
},
|
|
|
|
nft: {
|
|
|
|
owner: accountId
|
|
|
|
}
|
|
|
|
})
|
2021-10-28 11:38:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return newDdo
|
|
|
|
}
|