diff --git a/package-lock.json b/package-lock.json index 42effab..cfc873b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "@oasisprotocol/sapphire-ethers-v6": "^6.0.1", "@oceanprotocol/contracts": "^3.2.0-rc.1", - "@oceanprotocol/ddo-js": "^1.0.0", + "@oceanprotocol/ddo-js": "^1.1.0", "@oceanprotocol/lib": "^10.1.0", "chalk": "^6.0.1", "commander": "^15.0.0", @@ -1933,16 +1933,18 @@ "license": "Apache-2.0" }, "node_modules/@oceanprotocol/ddo-js": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/@oceanprotocol/ddo-js/-/ddo-js-1.0.0.tgz", - "integrity": "sha512-FSw3rDimhf/aHARzQVN7Ve4xGkgK4cRRr8AJtc3XRH0Knc7WK8ga+TJkx9LvS1KjH7hVePSD9Q7400oJQe0Iqg==", - "license": "Apache-2.0", + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@oceanprotocol/ddo-js/-/ddo-js-1.1.0.tgz", + "integrity": "sha512-9/CYpaD0qzA/smmK63nMDnnd3XYNGGreb8y7uUwiC6ODqEKyvhEdMnSj2Rya4oNpyiJQ9cJhOsNGSEmYDhynKw==", "dependencies": { "ethers": "^6.17.0", "jsonld": "^9.0.0", - "n3": "^2.2.0", + "n3": "^2.13.12", "rdf-literal": "^2.0.0", "rdf-validate-shacl": "^0.6.5" + }, + "engines": { + "node": ">=22" } }, "node_modules/@oceanprotocol/lib": { @@ -4938,10 +4940,9 @@ } }, "node_modules/n3": { - "version": "2.2.5", - "resolved": "https://registry.npmjs.org/n3/-/n3-2.2.5.tgz", - "integrity": "sha512-lR/0N7zVayC6N0uWqrMkPpJn8Up4+qBsjWiBnoOWWEi/ldTcygs2Dw1iAgozzDXLW/fcxXFfbMzpGHlc/1MBLQ==", - "license": "MIT", + "version": "2.13.12", + "resolved": "https://registry.npmjs.org/n3/-/n3-2.13.12.tgz", + "integrity": "sha512-z944NsyJi58WXcYevtNkUL2JhVr0KP6nvED9AgymQq5V35TDaNn1N8kO+OWJk/kj4PR0L/ilWB3d7/bWceg1CQ==", "dependencies": { "buffer": "^6.0.3", "readable-stream": "^4.0.0" diff --git a/package.json b/package.json index 13f2b8a..0665fe9 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,7 @@ "dependencies": { "@oasisprotocol/sapphire-ethers-v6": "^6.0.1", "@oceanprotocol/contracts": "^3.2.0-rc.1", - "@oceanprotocol/ddo-js": "^1.0.0", + "@oceanprotocol/ddo-js": "^1.1.0", "@oceanprotocol/lib": "^10.1.0", "chalk": "^6.0.1", "commander": "^15.0.0", diff --git a/src/commands.ts b/src/commands.ts index c042385..dbf0994 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -14,7 +14,6 @@ import { computeJobChainIds, summarizeComputeEnvFees, prefundedLockUnits, - getDdoChainId, resolveSubsidyProviderAddresses, } from "./helpers.js"; import { @@ -58,7 +57,15 @@ import { ServiceStatusNumber, ServiceTemplatePublic, } from "@oceanprotocol/lib"; -import { Asset, DDOManager } from "@oceanprotocol/ddo-js"; +import { + Asset, + DDOManager, + addTrustedAlgorithm, + getDdoChainId, + isComputeService, + isDdoOwner, + removeTrustedAlgorithm, +} from "@oceanprotocol/ddo-js"; import { Signer, ethers, getAddress } from "ethers"; import { stdin as input, stdout as output } from "node:process"; import { createInterface } from "readline/promises"; @@ -205,13 +212,6 @@ export class Commands { this.config = this.configFor(chainId); } - // A DDO's chainId lives at the top level in 4.1.0 DDOs but under - // `credentialSubject.chainId` in v5 DDOs. Delegates to the shared `getDdoChainId` - // helper so routing and compute ordering read the chain the same way. - private ddoChainId(ddo: unknown): unknown { - return getDdoChainId(ddo); - } - // Category (b): re-point at the chain the asset lives on (its DDO's chainId). Returns // false (already logged) if the chainId is missing/invalid or not configured, so the // caller can bail. Also fixes the old bug where publish ignored the DDO's chainId. @@ -334,9 +334,7 @@ export class Commands { const encryptDDO = args[2] === "false" ? false : true; // The chain is the one the DDO declares — publish on that chain, not the RPC's // default. (Previously the DDO's chainId was ignored.) - if ( - !(await this.routeToAssetChain(this.ddoChainId(asset), "Metadata file")) - ) + if (!(await this.routeToAssetChain(getDdoChainId(asset), "Metadata file"))) return; try { const ddoInstance = DDOManager.getDDOClass(asset); @@ -373,10 +371,7 @@ export class Commands { } const encryptDDO = args[2] === "false" ? false : true; if ( - !(await this.routeToAssetChain( - this.ddoChainId(algoAsset), - "Metadata file", - )) + !(await this.routeToAssetChain(getDdoChainId(algoAsset), "Metadata file")) ) return; // add some more checks @@ -434,7 +429,7 @@ export class Commands { asset[key] = updateJson[key]; } - if (!(await this.routeToAssetChain(this.ddoChainId(asset), "DDO"))) return; + if (!(await this.routeToAssetChain(getDdoChainId(asset), "DDO"))) return; const updateAssetTx = await updateAssetMetadata( this.signer, @@ -529,13 +524,12 @@ export class Commands { return; } - if (!(await this.routeToAssetChain(this.ddoChainId(dataDdo), "DDO"))) - return; + if (!(await this.routeToAssetChain(getDdoChainId(dataDdo), "DDO"))) return; const ddoInstance = DDOManager.getDDOClass(dataDdo); - const { services, version } = ddoInstance.getDDOFields(); - const serviceId = args[3] ? args[3] : services[0].id; - const service = services.find((s) => s.id === serviceId); + const { version } = ddoInstance.getDDOFields(); + const serviceId = args[3] ? args[3] : ddoInstance.getServiceByIndex(0)?.id; + const service = ddoInstance.getService(serviceId); if (!service) { console.error( chalk.red(`Service ID "${serviceId}" not found in DDO ${did}.`), @@ -579,7 +573,7 @@ export class Commands { this.config, ); // Order the same service that policy retrieval and getDownloadUrl target. - const serviceIndex = services.findIndex((s) => s.id === serviceId); + const serviceIndex = ddoInstance.getServiceIndex(serviceId); let tx; try { tx = await this.orderWithRetry(() => @@ -723,18 +717,15 @@ export class Commands { version?: string; } | null = null; if (algoDdo) { - const ddoAlgoInstance = DDOManager.getDDOClass(algoDdo); - const { services: servicesAlgo, version: versionAlgo } = - ddoAlgoInstance.getDDOFields(); + const ddoInstanceAlgo = DDOManager.getDDOClass(algoDdo); + const { version: versionAlgo } = ddoInstanceAlgo.getDDOFields(); const algoServiceIdInput = args[9] as string | undefined; if ( typeof algoServiceIdInput === "string" && algoServiceIdInput.trim().length > 0 ) { const expectedAlgoServiceId = algoServiceIdInput.trim(); - const matchAlgoSvc = servicesAlgo.find( - (s: any) => s.id === expectedAlgoServiceId, - ); + const matchAlgoSvc = ddoInstanceAlgo.getService(expectedAlgoServiceId); if (!matchAlgoSvc) { console.error( `Algorithm Service ID "${expectedAlgoServiceId}" not found in algo DDO ${algoDdo.id}. ` + @@ -764,15 +755,13 @@ export class Commands { const dataDdo = ddos[i]; if (!dataDdo) continue; const ddoInstanceDdo = DDOManager.getDDOClass(dataDdo); - const { services: servicesDdo, version: versionDdo } = - ddoInstanceDdo.getDDOFields(); - let chosenServiceId = assets[i].serviceId || servicesDdo[0].id; + const { version: versionDdo } = ddoInstanceDdo.getDDOFields(); + const defaultService = ddoInstanceDdo.getServiceByIndex(0); + let chosenServiceId = assets[i].serviceId || defaultService?.id; if (inputServices.length > 0) { const expectedServiceId = inputServices[i]; if (expectedServiceId) { - const match = servicesDdo.find( - (s: any) => s.id === expectedServiceId, - ); + const match = ddoInstanceDdo.getService(expectedServiceId); if (!match) { console.error( `Service ID "${expectedServiceId}" not found in dataset ${dataDdo.id}. ` + @@ -785,8 +774,8 @@ export class Commands { providerURI = match.serviceEndpoint; } } - } else if (i === 0 && servicesDdo[0]?.serviceEndpoint) { - providerURI = servicesDdo[0].serviceEndpoint; + } else if (i === 0 && defaultService?.serviceEndpoint) { + providerURI = defaultService.serviceEndpoint; } assets[i].serviceId = chosenServiceId; const canStartCompute = await isOrderable( @@ -1157,18 +1146,16 @@ export class Commands { } | null = null; if (algoDdo) { const ddoInstanceAlgo = DDOManager.getDDOClass(algoDdo); - const { services: servicesAlgo, version: versionAlgo } = - ddoInstanceAlgo.getDDOFields(); + const { version: versionAlgo } = ddoInstanceAlgo.getDDOFields(); const algoServiceIdInput = args[10] as string | undefined; - let chosenAlgoServiceId = algo.serviceId || servicesAlgo[0].id; + let chosenAlgoServiceId = + algo.serviceId || ddoInstanceAlgo.getServiceByIndex(0)?.id; if ( typeof algoServiceIdInput === "string" && algoServiceIdInput.trim().length > 0 ) { const expectedAlgoServiceId = algoServiceIdInput.trim(); - const matchAlgoSvc = servicesAlgo.find( - (s: any) => s.id === expectedAlgoServiceId, - ); + const matchAlgoSvc = ddoInstanceAlgo.getService(expectedAlgoServiceId); if (!matchAlgoSvc) { console.error( `Algorithm Service ID "${expectedAlgoServiceId}" not found in algo DDO ${algoDdo.id}. ` + @@ -1178,9 +1165,7 @@ export class Commands { } chosenAlgoServiceId = expectedAlgoServiceId; } - algoServiceIndex = servicesAlgo.findIndex( - (s: any) => s.id === chosenAlgoServiceId, - ); + algoServiceIndex = ddoInstanceAlgo.getServiceIndex(chosenAlgoServiceId); if (algoServiceIndex < 0) { console.error( `Could not resolve serviceIndex for algorithm serviceId ${chosenAlgoServiceId}`, @@ -1212,15 +1197,13 @@ export class Commands { continue; } const ddoInstanceDdo = DDOManager.getDDOClass(dataDdo); - const { services: servicesDdo, version: versionDdo } = - ddoInstanceDdo.getDDOFields(); - let chosenServiceId = assets[i].serviceId || servicesDdo[0].id; + const { version: versionDdo } = ddoInstanceDdo.getDDOFields(); + const defaultService = ddoInstanceDdo.getServiceByIndex(0); + let chosenServiceId = assets[i].serviceId || defaultService?.id; if (inputServices.length > 0) { const expectedServiceId = inputServices[i]; if (expectedServiceId) { - const match = servicesDdo.find( - (s: any) => s.id === expectedServiceId, - ); + const match = ddoInstanceDdo.getService(expectedServiceId); if (!match) { console.error( `Service ID "${expectedServiceId}" not found in dataset ${dataDdo.id}. ` + @@ -1233,12 +1216,11 @@ export class Commands { providerURI = match.serviceEndpoint; } } - } else if (i === 0 && servicesDdo[0]?.serviceEndpoint) { - providerURI = servicesDdo[0].serviceEndpoint; + } else if (i === 0 && defaultService?.serviceEndpoint) { + providerURI = defaultService.serviceEndpoint; } - const chosenServiceIndex = servicesDdo.findIndex( - (s: any) => s.id === chosenServiceId, - ); + const chosenServiceIndex = + ddoInstanceDdo.getServiceIndex(chosenServiceId); if (chosenServiceIndex < 0) { console.error( `Could not resolve serviceIndex for dataset ${dataDdo.id} with serviceId ${chosenServiceId}`, @@ -1741,17 +1723,14 @@ export class Commands { } | null = null; if (algoDdo) { const ddoInstanceAlgo = DDOManager.getDDOClass(algoDdo); - const { services: servicesAlgo, version: versionAlgo } = - ddoInstanceAlgo.getDDOFields(); + const { version: versionAlgo } = ddoInstanceAlgo.getDDOFields(); const algoServiceIdInput = args[6] as string | undefined; if ( typeof algoServiceIdInput === "string" && algoServiceIdInput.trim().length > 0 ) { const expectedAlgoServiceId = algoServiceIdInput.trim(); - const matchAlgoSvc = servicesAlgo.find( - (s: any) => s.id === expectedAlgoServiceId, - ); + const matchAlgoSvc = ddoInstanceAlgo.getService(expectedAlgoServiceId); if (!matchAlgoSvc) { console.error( `Algorithm Service ID "${expectedAlgoServiceId}" not found in algo DDO ${algoDdo.id}. ` + @@ -1781,15 +1760,13 @@ export class Commands { const dataDdo = ddos[i]; if (!dataDdo) continue; const ddoInstanceDdo = DDOManager.getDDOClass(dataDdo); - const { services: servicesDdo, version: versionDdo } = - ddoInstanceDdo.getDDOFields(); - let chosenServiceId = assets[i].serviceId || servicesDdo[0].id; + const { version: versionDdo } = ddoInstanceDdo.getDDOFields(); + const defaultService = ddoInstanceDdo.getServiceByIndex(0); + let chosenServiceId = assets[i].serviceId || defaultService?.id; if (inputServices.length > 0) { const expectedServiceId = inputServices[i]; if (expectedServiceId) { - const match = servicesDdo.find( - (s: any) => s.id === expectedServiceId, - ); + const match = ddoInstanceDdo.getService(expectedServiceId); if (!match) { console.error( `Service ID "${expectedServiceId}" not found in dataset ${dataDdo.id}. ` + @@ -1802,8 +1779,8 @@ export class Commands { providerURI = match.serviceEndpoint; } } - } else if (i === 0 && servicesDdo[0]?.serviceEndpoint) { - providerURI = servicesDdo[0].serviceEndpoint; + } else if (i === 0 && defaultService?.serviceEndpoint) { + providerURI = defaultService.serviceEndpoint; } assets[i].serviceId = chosenServiceId; const canStartCompute = await isOrderable( @@ -2995,18 +2972,17 @@ export class Commands { return; } // Route to the dataset's chain before the owner check / metadata update. - if (!(await this.routeToAssetChain(this.ddoChainId(asset), "DDO"))) return; + if (!(await this.routeToAssetChain(getDdoChainId(asset), "DDO"))) return; const ddoInstance = DDOManager.getDDOClass(asset); - const { indexedMetadata } = ddoInstance.getAssetFields(); - const { services } = ddoInstance.getDDOFields(); - if (indexedMetadata.nft.owner !== (await this.signer.getAddress())) { + const computeService = ddoInstance.getServiceByIndex(0); + if (!isDdoOwner(asset, await this.signer.getAddress())) { console.error( "You are not the owner of this asset, and there for you cannot update it.", ); return; } - if (services[0].type !== "compute") { + if (!isComputeService(computeService)) { console.error( "Error getting computeService for " + args[1] + @@ -3053,10 +3029,14 @@ export class Commands { filesChecksum: filesChecksum?.[0]?.checksum, serviceId: servicesAlgo[0].id, }; - if (!services[0].compute.publisherTrustedAlgorithms) { - services[0].compute.publisherTrustedAlgorithms = []; + // Replaces any earlier entry for the same algorithm DID + service, so + // re-allowing an updated algorithm does not keep its old checksums trusted. + try { + addTrustedAlgorithm(computeService, trustedAlgorithm); + } catch (e) { + console.error("Error adding trusted algorithm: ", e); + return; } - services[0].compute.publisherTrustedAlgorithms.push(trustedAlgorithm); try { const txid = await updateAssetMetadata( this.signer, @@ -3087,17 +3067,16 @@ export class Commands { return; } // Route to the dataset's chain before the owner check / metadata update. - if (!(await this.routeToAssetChain(this.ddoChainId(asset), "DDO"))) return; + if (!(await this.routeToAssetChain(getDdoChainId(asset), "DDO"))) return; const ddoInstance = DDOManager.getDDOClass(asset); - const { indexedMetadata } = ddoInstance.getAssetFields(); - const { services } = ddoInstance.getDDOFields(); - if (indexedMetadata.nft.owner !== (await this.signer.getAddress())) { + const computeService = ddoInstance.getServiceByIndex(0); + if (!isDdoOwner(asset, await this.signer.getAddress())) { console.error( "You are not the owner of this asset, and there for you cannot update it.", ); return; } - if (services[0].type !== "compute") { + if (!isComputeService(computeService)) { console.error( "Error getting computeService for " + args[1] + @@ -3106,8 +3085,8 @@ export class Commands { return; } if ( - !services[0].compute.publisherTrustedAlgorithms || - services[0].compute.publisherTrustedAlgorithms.length === 0 + !computeService.compute?.publisherTrustedAlgorithms || + computeService.compute.publisherTrustedAlgorithms.length === 0 ) { console.error( "Asset " + @@ -3117,14 +3096,8 @@ export class Commands { return; } const encryptDDO = args[3] === "false" ? false : true; - const indexToDelete = - services[0].compute.publisherTrustedAlgorithms.findIndex( - (item) => item.did === args[2], - ); - - if (indexToDelete !== -1) { - services[0].compute.publisherTrustedAlgorithms.splice(indexToDelete, 1); - } else { + // Removes every entry for the DID, including duplicates left by older tools. + if (removeTrustedAlgorithm(computeService, args[2]) === 0) { console.error( " " + args[2] + diff --git a/src/helpers.ts b/src/helpers.ts index c1f2c80..25f0ad2 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -2,7 +2,13 @@ import { ethers, hexlify, Signer, toBeHex } from "ethers"; import { promises as fs, readFileSync } from "fs"; import * as path from "path"; import { wrapEthersSigner } from "@oasisprotocol/sapphire-ethers-v6"; -import { Asset, DDO, DDOManager } from "@oceanprotocol/ddo-js"; +import { + Asset, + DDO, + DDOManager, + getDdoChainId, + isComputeService, +} from "@oceanprotocol/ddo-js"; import { AccesslistFactory, Aquarius, @@ -260,9 +266,8 @@ export async function handleComputeOrder( consumeMarkerFee?: ConsumeMarketFee, ) { const ddoInstance = DDOManager.getDDOClass(asset); - const { services } = ddoInstance.getDDOFields(); - const service = services?.[serviceIndex]; - if (!Number.isInteger(serviceIndex) || serviceIndex < 0 || !service) { + const service = ddoInstance.getServiceByIndex(serviceIndex); + if (!service) { throw new Error( `Service index ${serviceIndex} not found in asset ${asset.id}`, ); @@ -338,24 +343,19 @@ export async function isOrderable( algorithm: ComputeAlgorithm, algorithmDDO: Asset | DDO, ): Promise { - const ddoInstanceAsset = DDOManager.getDDOClass(asset); - const { services: servicesAsset } = ddoInstanceAsset.getDDOFields(); - const datasetService = servicesAsset.find((s) => s.id === serviceId); + const datasetService = DDOManager.getDDOClass(asset).getService(serviceId); if (!datasetService) return false; - if (datasetService.type === "compute") { + if (isComputeService(datasetService)) { // A DID-based algorithm carries a `documentId` (and, for convenience, its // `meta`); a raw algorithm carries only `meta` (a fileObject, no DID). Check // `documentId` first so published algorithms are validated against the // dataset's provider rather than the raw-algorithm allowance. if (algorithm.documentId) { - const servicesAlgo = algorithmDDO - ? DDOManager.getDDOClass(algorithmDDO).getDDOFields().services + const algoService = algorithmDDO + ? DDOManager.getDDOClass(algorithmDDO).getService(algorithm.serviceId) : undefined; - const algoService = servicesAlgo?.find( - (s) => s.id === algorithm.serviceId, - ); - if (algoService && algoService.type === "compute") { + if (isComputeService(algoService)) { if (algoService.serviceEndpoint !== datasetService.serviceEndpoint) { LoggerInstance.error( "ERROR: Both assets with compute service are not served by the same provider", @@ -453,14 +453,14 @@ export async function resolveComputeInputs( ); return null; } - const { services } = DDOManager.getDDOClass(dataDdo).getDDOFields(); - if (!services?.[0]) { + const service = DDOManager.getDDOClass(dataDdo).getServiceByIndex(0); + if (!service) { console.error(`Error: dataset ${token} has no services.`); return null; } assets.push({ documentId: dataDdo.id, - serviceId: services[0].id, + serviceId: service.id, }); ddos.push(dataDdo); } else if (token && typeof token === "object") { @@ -487,8 +487,8 @@ export async function resolveComputeInputs( let providerURI = fallbackProviderURI; const firstDdo = ddos.find((d) => d !== null); if (firstDdo) { - const { services } = DDOManager.getDDOClass(firstDdo).getDDOFields(); - providerURI = services[0].serviceEndpoint; + providerURI = + DDOManager.getDDOClass(firstDdo).getServiceByIndex(0).serviceEndpoint; } // Resolve the algorithm (single entry: DID or raw object) @@ -511,9 +511,10 @@ export async function resolveComputeInputs( ); return null; } - const { services, metadata } = - DDOManager.getDDOClass(algoDdo).getDDOFields(); - if (!services?.[0]) { + const algoInstance = DDOManager.getDDOClass(algoDdo); + const { metadata } = algoInstance.getDDOFields(); + const service = algoInstance.getServiceByIndex(0); + if (!service) { console.error(`Error: algorithm ${algoToken} has no services.`); return null; } @@ -523,7 +524,7 @@ export async function resolveComputeInputs( } algo = { documentId: algoDdo.id, - serviceId: services[0].id, + serviceId: service.id, meta: metadata.algorithm, }; } else if (algoToken && typeof algoToken === "object") { @@ -675,17 +676,6 @@ export function toBoolean(value) { // Multi-chain compute helpers (Phase 3). // --------------------------------------------------------------------------- -// A DDO's chainId lives at the top level in 4.1.0 DDOs but under -// `credentialSubject.chainId` in v5 DDOs — read whichever is present so both metadata -// versions resolve to the right chain (publish/edit routing and compute ordering alike). -export function getDdoChainId(ddo: unknown): unknown { - const d = ddo as { - chainId?: unknown; - credentialSubject?: { chainId?: unknown }; - }; - return d?.chainId ?? d?.credentialSubject?.chainId; -} - /** * The set of chainIds a compute job actually touches: the payment/escrow chain plus * every DID-based dataset/algorithm DDO's own chain (a job may mix assets across diff --git a/test/computeChains.unit.test.ts b/test/computeChains.unit.test.ts index 130994a..ef55c28 100644 --- a/test/computeChains.unit.test.ts +++ b/test/computeChains.unit.test.ts @@ -1,9 +1,6 @@ import { expect } from "chai"; -import { - computeJobChainIds, - summarizeComputeEnvFees, - getDdoChainId, -} from "../src/helpers.js"; +import { getDdoChainId } from "@oceanprotocol/ddo-js"; +import { computeJobChainIds, summarizeComputeEnvFees } from "../src/helpers.js"; // Pure unit tests for the Phase 3 multi-chain-compute helpers. No infra.