2021-11-11 14:40:38 +01:00
|
|
|
import axios, { AxiosResponse } from 'axios'
|
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-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
|
|
|
async function getEncryptedFileUrls(
|
|
|
|
files: string[],
|
|
|
|
providerUrl: string,
|
|
|
|
did: string,
|
|
|
|
accountId: string
|
|
|
|
): Promise<string> {
|
|
|
|
try {
|
|
|
|
// https://github.com/oceanprotocol/provider/blob/v4main/API.md#encrypt-endpoint
|
|
|
|
const url = `${providerUrl}/api/v1/services/encrypt`
|
|
|
|
const response: AxiosResponse<{ encryptedDocument: string }> =
|
|
|
|
await axios.post(url, {
|
|
|
|
documentId: did,
|
|
|
|
signature: '', // TODO: add signature
|
|
|
|
publisherAddress: accountId,
|
|
|
|
document: files
|
|
|
|
})
|
|
|
|
return response?.data?.encryptedDocument
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error parsing json: ' + error.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-11-11 14:40:38 +01:00
|
|
|
): Promise<DDO> {
|
|
|
|
const { chainId, accountId, metadata, services } = values
|
|
|
|
const did = sha256(`${nftAddress}${chainId}`)
|
2021-10-28 11:38:40 +02:00
|
|
|
const currentTime = dateToStringNoMS(new Date())
|
2021-11-11 14:55:35 +01:00
|
|
|
const { type, name, description, tags, links, author, termsAndConditions } =
|
|
|
|
metadata
|
2021-11-11 08:51:13 +01:00
|
|
|
const {
|
|
|
|
access,
|
|
|
|
files,
|
2021-10-28 11:38:40 +02:00
|
|
|
image,
|
|
|
|
containerTag,
|
|
|
|
entrypoint,
|
2021-11-11 08:51:13 +01:00
|
|
|
providerUrl,
|
|
|
|
timeout
|
2021-11-11 14:40:38 +01:00
|
|
|
} = services[0]
|
2021-11-11 08:51:13 +01:00
|
|
|
|
2021-11-11 14:40:38 +01:00
|
|
|
const filesEncrypted = await getEncryptedFileUrls(
|
|
|
|
files as string[],
|
|
|
|
providerUrl,
|
|
|
|
did,
|
|
|
|
accountId
|
|
|
|
)
|
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',
|
|
|
|
links,
|
|
|
|
additionalInformation: {
|
|
|
|
termsAndConditions
|
|
|
|
},
|
|
|
|
...(type === 'algorithm' && {
|
|
|
|
algorithm: {
|
2021-11-11 14:40:38 +01:00
|
|
|
language: getUrlFileExtension(files[0]),
|
2021-11-11 10:22:22 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-10-28 11:38:40 +02:00
|
|
|
|
2021-11-11 14:40:38 +01:00
|
|
|
const newService: Service = {
|
2021-11-11 08:51:13 +01:00
|
|
|
type: access,
|
2021-11-11 14:40:38 +01:00
|
|
|
files: filesEncrypted,
|
2021-11-11 12:26:29 +01:00
|
|
|
datatokenAddress,
|
2021-11-11 08:51:13 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
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,
|
|
|
|
services: [newService]
|
2021-10-28 11:38:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return newDdo
|
|
|
|
}
|