market/src/components/Publish/_utils.ts

118 lines
2.8 KiB
TypeScript
Raw Normal View History

import { getEncryptedFileUrls } from '@utils/provider'
2021-11-11 12:26:29 +01:00
import { sha256 } from 'js-sha256'
import slugify from 'slugify'
import { FormPublishData } from './_types'
export function getFieldContent(
fieldName: string,
fields: FormFieldContent[]
): FormFieldContent {
return fields.filter((field: FormFieldContent) => field.name === fieldName)[0]
}
function getUrlFileExtension(fileUrl: string): string {
const splittedFileUrl = fileUrl.split('.')
return splittedFileUrl[splittedFileUrl.length - 1]
}
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,
datatokenAddress: string,
nftAddress: string
): Promise<DDO> {
2021-11-19 14:47:58 +01:00
const { metadata, services } = values
const { chainId, accountId } = values.user
const did = sha256(`${nftAddress}${chainId}`)
const currentTime = dateToStringNoMS(new Date())
const {
type,
name,
description,
tags,
links,
author,
termsAndConditions,
dockerImageCustom,
dockerImageCustomTag,
dockerImageCustomEntrypoint
} = metadata
const { access, files, providerUrl, timeout } = services[0]
const filesEncrypted = await getEncryptedFileUrls(
files as string[],
providerUrl,
did,
accountId
)
2021-11-11 10:22:22 +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',
links,
additionalInformation: {
termsAndConditions
},
...(type === 'algorithm' && {
algorithm: {
language: getUrlFileExtension(files[0]),
2021-11-11 10:22:22 +01:00
version: '0.1',
container: {
entrypoint: dockerImageCustomEntrypoint,
image: dockerImageCustom,
tag: dockerImageCustomTag,
2021-11-11 12:26:29 +01:00
checksum: '' // how to get? Is it user input?
2021-11-11 10:22:22 +01:00
}
}
})
}
const newService: Service = {
type: access,
files: filesEncrypted,
2021-11-11 12:26:29 +01:00
datatokenAddress,
serviceEndpoint: providerUrl,
2021-11-11 10:22:22 +01:00
timeout,
...(access === 'compute' && {
compute: {
namespace: 'ocean-compute',
2021-11-11 10:22:22 +01:00
cpu: 1,
gpu: 1,
gpuType: '',
memory: '',
volumeSize: '',
allowRawAlgorithm: false,
allowNetworkAccess: false,
publisherTrustedAlgorithmPublishers: null,
publisherTrustedAlgorithms: null
}
})
}
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,
version: '4.0.0',
chainId,
metadata: newMetadata,
services: [newService]
}
return newDdo
}