1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

chore: wip

This commit is contained in:
AdriGeorge 2024-10-23 10:33:39 +03:00
parent 696f4b5ca8
commit 2c25699b25
13 changed files with 149 additions and 86 deletions

View File

@ -1,4 +1,5 @@
import { DDO } from '.'
import { DDOv4Type } from './DDO/versions/DDOv4'
import { VerifiableCredentialType } from './DDO/versions/VerifiableCredential'
export interface AssetNft {
/**
@ -137,7 +138,7 @@ export interface AssetLastEvent {
datetime: string
}
export interface Asset extends DDO {
export interface Asset extends DDOv4Type, VerifiableCredentialType {
/**
* Contains information about the ERC721 NFT contract which represents the intellectual property of the publisher.
* @type {string}

View File

@ -1,40 +0,0 @@
import { AdditionalVerifiableCredentials } from './AdditionalVerifiableCredentials'
import { CredentialSubject } from './CredentialSubject'
export interface VerifiableCredential {
/**
* Contexts used for validation.
* @type {string[]}
*/
'@context': string[]
/**
* id optional for verifiable credential
* @type {string}
*/
id?: string
/**
* @type {CredentialSubject}
*/
credentialSubject: CredentialSubject
/**
* Version information in SemVer notation
* referring to the DDO spec version
* @type {string}
*/
version: string
/**
* Id of issuer
* @type {string}
*/
issuer: string
/**
* Additional ddos
* @type {AdditionalVerifiableCredentials[]}
*/
additionalDdos?: AdditionalVerifiableCredentials[]
}

View File

@ -0,0 +1,14 @@
export interface BaseDDOType {
/**
* Contexts used for validation.
* @type {string[]}
*/
'@context': string[]
/**
* Version information in SemVer notation
* referring to the DDO spec version
* @type {string}
*/
version: string
}

View File

@ -1,29 +1,17 @@
import { Service, Metadata, Credentials, Event } from '..'
import { BaseDDOType } from './BaseDDO'
import { Service, Metadata, Credentials, Event } from '../..'
/**
* DID Descriptor Object.
* Contains metadata about the asset, and define access in at least one service.
*/
export interface DDO {
/**
* Contexts used for validation.
* @type {string[]}
*/
'@context': string[]
export interface DDOv4Type extends BaseDDOType {
/**
* DID, descentralized ID.
* Computed as sha256(address of NFT contract + chainId)
* @type {string}
*/
id: string
/**
* Version information in SemVer notation
* referring to the DDO spec version
* @type {string}
*/
version: string
id?: string
/**
* NFT contract address

View File

@ -0,0 +1,28 @@
import { AdditionalVerifiableCredentials } from '../AdditionalVerifiableCredentials'
import { CredentialSubject } from '../CredentialSubject'
import { BaseDDOType } from './BaseDDO'
export interface VerifiableCredentialType extends BaseDDOType {
/**
* id optional for verifiable credential
* @type {string}
*/
id?: string
/**
* @type {CredentialSubject}
*/
credentialSubject: CredentialSubject
/**
* Id of issuer
* @type {string}
*/
issuer: string
/**
* Additional ddos
* @type {AdditionalVerifiableCredentials[]}
*/
additionalDdos?: AdditionalVerifiableCredentials[]
}

View File

@ -1,5 +1,5 @@
export * from './DDO/Credentials'
export * from './DDO/DDO'
export * from './DDO/versions/DDOv4'
export * from './DDO/Event'
export * from './DDO/Metadata'
export * from './DDO/ConsumerParameter'

View File

@ -1,6 +1,7 @@
import fetch from 'cross-fetch'
import { LoggerInstance, sleep } from '../utils'
import { Asset, DDO, ValidateMetadata } from '../@types'
import { Asset, ValidateMetadata } from '../@types'
import { DDO_V4 } from '../utils/DDO/DDO_V4'
export interface SearchQuery {
from?: number
@ -91,7 +92,7 @@ export class Aquarius {
* @param {AbortSignal} signal abort signal
* @return {Promise<ValidateMetadata>}.
*/
public async validate(ddo: DDO, signal?: AbortSignal): Promise<ValidateMetadata> {
public async validate(ddo: any, signal?: AbortSignal): Promise<ValidateMetadata> {
const status: ValidateMetadata = {
valid: false
}

33
src/utils/DDO/DDO_V4.ts Normal file
View File

@ -0,0 +1,33 @@
import { Asset, Metadata, Service } from '../../@types'
import { BaseDDOType } from '../../@types/DDO/versions/BaseDDO'
export class DDO_V4 implements BaseDDOType {
'@context': string[]
id: string
version: string
nftAddress: string
chainId: number
metadata: Metadata
services: Service[]
constructor(data: any) {
this['@context'] = data['@context']
this.id = data.id
this.version = data.version
this.nftAddress = data.nftAddress
this.chainId = data.chainId
this.metadata = data.metadata
this.services = data.services
}
/**
* Get param to order an asset based on the specified pricing schema and configuration.
* @param {Asset} asset - The asset to be ordered.
* @param {number} [serviceIndex=0] - Index of the service within the asset.
*/
getOrderAssetParams(asset: Asset, serviceIndex: number = 0) {
const did = asset.id
const serviceId = asset.services[serviceIndex].id
return { did, serviceId }
}
}

View File

@ -0,0 +1,23 @@
import { DDO_V4 } from './DDO_V4'
import { VerifiableCredential } from './VerifiableCredential'
type AssetType = DDO_V4 | VerifiableCredential
export class DDOFactory {
static createDDO(data: any): AssetType {
const { version } = data
switch (version) {
case '4.1.0':
case '4.3.0':
case '4.5.0':
case '4.7.0':
return new DDO_V4(data)
case '5.0.0':
return new VerifiableCredential(data)
default:
throw new Error(`Unsupported DDO version: ${version}`)
}
}
}

View File

@ -0,0 +1,30 @@
import { CredentialSubject } from '../../@types/DDO/CredentialSubject'
import { BaseDDOType } from '../../@types/DDO/versions/BaseDDO'
import { VerifiableCredentialType } from '../../@types/DDO/versions/VerifiableCredential'
export class VerifiableCredential implements BaseDDOType {
'@context': string[]
id?: string
version: string
credentialSubject: CredentialSubject
issuer: string
constructor(data: any) {
this['@context'] = data['@context']
this.id = data.id
this.version = data.version
this.credentialSubject = data.credentialSubject
this.issuer = data.issuer
}
/**
* Get param to order an asset based on the specified pricing schema and configuration.
* @param {Asset} asset - The asset to be ordered.
* @param {number} [serviceIndex=0] - Index of the service within the asset.
*/
getOrderAssetParams(asset: VerifiableCredentialType, serviceIndex: number = 0) {
const did = asset.credentialSubject.id
const serviceId = asset.credentialSubject.services[serviceIndex].id
return { did, serviceId }
}
}

5
src/utils/DDOType.ts Normal file
View File

@ -0,0 +1,5 @@
import { DDOFactory } from './DDO/DdoFactory'
export async function getDDOType(assetData: any) {
return DDOFactory.createDDO(assetData)
}

View File

@ -15,7 +15,7 @@ import {
approveWei
} from '../index'
import Decimal from 'decimal.js'
import { isVerifiableCredential } from './verifiableCredential'
import { DDOFactory } from './DDO/DdoFactory'
/**
* Orders an asset based on the specified pricing schema and configuration.
@ -48,21 +48,10 @@ export async function orderAsset(
serviceIndex: number = 0,
fixedRateIndex: number = 0
) {
let datatokenAddress: string
let serviceId: string
let dataTokenAddressFirstIndex: string
let did: string
if (isVerifiableCredential(asset)) {
did = (asset as any).credentialSubject.id
datatokenAddress = (asset as any).credentialSubject.datatokens[datatokenIndex].address
dataTokenAddressFirstIndex = (asset as any).credentialSubject.datatokens[0].address
serviceId = (asset as any).credentialSubject.services[serviceIndex].id
} else {
did = asset.id
datatokenAddress = asset.datatokens[datatokenIndex].address
dataTokenAddressFirstIndex = asset.datatokens[0].address
serviceId = asset.services[serviceIndex].id
}
const assetDDO = DDOFactory.createDDO(asset)
const { did, serviceId } = assetDDO.getOrderAssetParams(asset, serviceIndex)
const datatokenAddress = asset.datatokens[datatokenIndex].address
const dataTokenAddressFirstIndex = asset.datatokens[0].address
const consumeMarketFeeToken =
asset.stats.price.tokenAddress || '0x0000000000000000000000000000000000000000'
if (!consumeMarketOrderFee)

View File

@ -1,9 +0,0 @@
import { Asset } from '../@types'
export const isVerifiableCredential = (asset: Asset): boolean => {
return (
(asset as any).type &&
Array.isArray((asset as any).type) &&
(asset as any).type.includes('VerifiableCredential')
)
}