market/src/components/Publish/_utils.ts

102 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-11-11 12:26:29 +01:00
import { sha256 } from 'js-sha256'
import {
dateToStringNoMS,
transformTags,
2021-11-11 10:22:22 +01:00
getUrlFileExtension
} from '@utils/ddo'
import { FormPublishData } from './_types'
function encryptMe(files: string | FileMetadata[]): string {
throw new Error('Function not implemented.')
}
export function getFieldContent(
fieldName: string,
fields: FormFieldContent[]
): FormFieldContent {
return fields.filter((field: FormFieldContent) => field.name === fieldName)[0]
}
2021-11-11 12:26:29 +01:00
export function transformPublishFormToDdo(
values: FormPublishData,
datatokenAddress: string,
nftAddress: string
): DDO {
const did = sha256(`${nftAddress}${values.chainId}`)
const currentTime = dateToStringNoMS(new Date())
2021-11-11 10:22:22 +01:00
const { type, name, description, tags, author, termsAndConditions } =
2021-11-11 12:26:29 +01:00
values.metadata
const {
access,
files,
links,
image,
containerTag,
entrypoint,
providerUrl,
timeout
2021-11-11 12:26:29 +01:00
} = values.services[0]
const fileUrl = typeof files !== 'string' && files[0].url
2021-11-11 10:22:22 +01:00
const metadata: Metadata = {
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(fileUrl),
version: '0.1',
container: {
2021-11-11 12:26:29 +01:00
entrypoint,
image,
2021-11-11 10:22:22 +01:00
tag: containerTag,
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 service: Service = {
type: access,
2021-11-11 10:22:22 +01:00
files: encryptMe(files),
2021-11-11 12:26:29 +01:00
datatokenAddress,
serviceEndpoint: providerUrl,
2021-11-11 10:22:22 +01:00
timeout,
...(access === 'compute' && {
compute: {
namespace: '',
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',
2021-11-11 12:26:29 +01:00
chainId: values.chainId,
2021-11-11 10:22:22 +01:00
metadata,
services: [service]
}
return newDdo
}