diff --git a/package-lock.json b/package-lock.json index 75bcbbd..f778b6a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "Apache-2.0", "dependencies": { "@oceanprotocol/contracts": "^2.0.4", - "@oceanprotocol/lib": "^3.4.1", + "@oceanprotocol/lib": "^3.4.3", "cross-fetch": "^3.1.5", "crypto-js": "^4.1.1", "decimal.js": "^10.4.1", @@ -1147,10 +1147,9 @@ "license": "Apache-2.0" }, "node_modules/@oceanprotocol/lib": { - "version": "3.4.1", - "resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-3.4.1.tgz", - "integrity": "sha512-DhdNI6CF8O7ylTWNqLEdcqP9iWKFo9FDayfJvhNTL4mgWy2N1E3qzFMbsH2NAHN3fo+PbJ2KTfedpFt24r0LhA==", - "license": "Apache-2.0", + "version": "3.4.3", + "resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-3.4.3.tgz", + "integrity": "sha512-S7qvnPORuiyu096AzlkvAH4pMzdTMcz9SMIVlo1UZ0neGV28yJYUjCYxgsTltHjn30Rn6eN5JhAuwPgUehLMIg==", "dependencies": { "@oasisprotocol/sapphire-paratime": "^1.3.2", "@oceanprotocol/contracts": "^2.2.0", @@ -10058,9 +10057,9 @@ "integrity": "sha512-QhewXdtTebycRSZEdkAdvJkSMMnGZyxldlw2eX4VOJto8wymyNdxuhjL/tiaZ5xO7SS5BqURricx9170hfh2kQ==" }, "@oceanprotocol/lib": { - "version": "3.4.1", - "resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-3.4.1.tgz", - "integrity": "sha512-DhdNI6CF8O7ylTWNqLEdcqP9iWKFo9FDayfJvhNTL4mgWy2N1E3qzFMbsH2NAHN3fo+PbJ2KTfedpFt24r0LhA==", + "version": "3.4.3", + "resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-3.4.3.tgz", + "integrity": "sha512-S7qvnPORuiyu096AzlkvAH4pMzdTMcz9SMIVlo1UZ0neGV28yJYUjCYxgsTltHjn30Rn6eN5JhAuwPgUehLMIg==", "requires": { "@oasisprotocol/sapphire-paratime": "^1.3.2", "@oceanprotocol/contracts": "^2.2.0", diff --git a/package.json b/package.json index f2e0354..2fff2be 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ }, "dependencies": { "@oceanprotocol/contracts": "^2.0.4", - "@oceanprotocol/lib": "^3.4.1", + "@oceanprotocol/lib": "^3.4.3", "cross-fetch": "^3.1.5", "crypto-js": "^4.1.1", "decimal.js": "^10.4.1", diff --git a/src/commands.ts b/src/commands.ts index d2c50c0..c25e1f1 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -2,7 +2,7 @@ import fs from "fs"; import os from "os"; import util from "util"; import { - createAsset, + createAssetUtil, handleComputeOrder, updateAssetMetadata, downloadFile, @@ -92,17 +92,15 @@ export class Commands { const encryptDDO = args[2] === "false" ? false : true; try { // add some more checks - const urlAssetId = await createAsset( + const urlAssetId = await createAssetUtil( asset.nft.name, asset.nft.symbol, this.signer, asset.services[0].files, asset, - this.providerUrl, + this.providerUrl || this.macOsProviderUrl, this.config, this.aquarius, - 1, - this.macOsProviderUrl, encryptDDO ); console.log("Asset published. ID: " + urlAssetId); @@ -124,17 +122,15 @@ export class Commands { } const encryptDDO = args[2] === "false" ? false : true; // add some more checks - const algoDid = await createAsset( + const algoDid = await createAssetUtil( algoAsset.nft.name, algoAsset.nft.symbol, this.signer, algoAsset.services[0].files, algoAsset, - this.providerUrl, + this.providerUrl || this.macOsProviderUrl, this.config, this.aquarius, - 1, - this.macOsProviderUrl, encryptDDO ); // add some more checks diff --git a/src/helpers.ts b/src/helpers.ts index 4e38a11..bdeba7a 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,17 +1,12 @@ -import { SHA256 } from "crypto-js"; import { ethers, Signer } from "ethers"; import fetch from "cross-fetch"; import { promises as fs } from "fs"; -import { createHash } from "crypto"; import * as path from "path"; import * as sapphire from '@oasisprotocol/sapphire-paratime'; import { AccesslistFactory, Aquarius, - DatatokenCreateParams, Nft, - NftCreateData, - NftFactory, ProviderInstance, ZERO_ADDRESS, approveWei, @@ -22,14 +17,12 @@ import { DDO, orderAsset, getEventFromTx, - DispenserCreationParams, - FreCreationParams, DownloadResponse, Asset, ProviderFees, ComputeAlgorithm, LoggerInstance, - Datatoken4 + createAsset } from "@oceanprotocol/lib"; import { hexlify } from "ethers/lib/utils"; @@ -67,7 +60,7 @@ export async function downloadFile( return { data, filename }; } -export async function createAsset( +export async function createAssetUtil( name: string, symbol: string, owner: Signer, @@ -76,167 +69,36 @@ export async function createAsset( providerUrl: string, config: Config, aquariusInstance: Aquarius, + encryptDDO: boolean = true, templateIndex: number = 1, + providerFeeToken: string = ZERO_ADDRESS, + accessListFactory?: string, + allowAccessList?: string, + denyAccessList?: string, macOsProviderUrl?: string, - encryptDDO: boolean = true, + ) { const { chainId } = await owner.provider.getNetwork(); - const nft = new Nft(owner, chainId); - const nftFactory = new NftFactory(config.nftFactoryAddress, owner); - let wrappedSigner - let allowListAddress if(templateIndex === 4){ // Wrap the signer for Sapphire wrappedSigner = sapphire.wrap(owner); // Create Access List Factory - const accessListFactory = new AccesslistFactory(config.accessListFactory, wrappedSigner, chainId); + const accessListFactoryObj = new AccesslistFactory(config.accessListFactory, wrappedSigner, chainId); // Create Allow List - allowListAddress = await accessListFactory.deployAccessListContract( + await accessListFactoryObj.deployAccessListContract( 'AllowList', 'ALLOW', ['https://oceanprotocol.com/nft/'], false, await owner.getAddress(), [await owner.getAddress(), ZERO_ADDRESS] - ); + ) + return await createAsset(name, symbol, wrappedSigner, assetUrl, templateIndex, ddo, encryptDDO, providerUrl || macOsProviderUrl, providerFeeToken, aquariusInstance, accessListFactory, allowAccessList, denyAccessList); } - - ddo.chainId = parseInt(chainId.toString(10)); - const nftParamsAsset: NftCreateData = { - name, - symbol, - templateIndex, - tokenURI: "aaa", - transferable: true, - owner: await owner.getAddress(), - }; - const datatokenParams: DatatokenCreateParams = { - templateIndex, - cap: "100000", - feeAmount: "0", - paymentCollector: await owner.getAddress(), - feeToken: config.oceanTokenAddress, - minter: await owner.getAddress(), - mpFeeAddress: ZERO_ADDRESS, - }; - - let bundleNFT; - if (!ddo.stats?.price?.value) { - bundleNFT = await nftFactory.createNftWithDatatoken( - nftParamsAsset, - datatokenParams - ); - } else if (ddo?.stats?.price?.value === "0") { - const dispenserParams: DispenserCreationParams = { - dispenserAddress: config.dispenserAddress, - maxTokens: "1", - maxBalance: "100000000", - withMint: true, - allowedSwapper: ZERO_ADDRESS, - }; - - bundleNFT = await nftFactory.createNftWithDatatokenWithDispenser( - nftParamsAsset, - datatokenParams, - dispenserParams - ); - } else { - const fixedPriceParams: FreCreationParams = { - fixedRateAddress: config.fixedRateExchangeAddress, - baseTokenAddress: config.oceanTokenAddress, - owner: await owner.getAddress(), - marketFeeCollector: await owner.getAddress(), - baseTokenDecimals: 18, - datatokenDecimals: 18, - fixedRate: ddo.stats.price.value, - marketFee: "0", - allowedConsumer: await owner.getAddress(), - withMint: true, - }; - - bundleNFT = await nftFactory.createNftWithDatatokenWithFixedRate( - nftParamsAsset, - datatokenParams, - fixedPriceParams - ); - } - - const trxReceipt = await bundleNFT.wait(); - // events have been emitted - const nftCreatedEvent = getEventFromTx(trxReceipt, "NFTCreated"); - const tokenCreatedEvent = getEventFromTx(trxReceipt, "TokenCreated"); - - const nftAddress = nftCreatedEvent.args.newTokenAddress; - const datatokenAddressAsset = tokenCreatedEvent.args.newTokenAddress; - // create the files encrypted string - assetUrl.datatokenAddress = datatokenAddressAsset; - assetUrl.nftAddress = nftAddress; - ddo.services[0].files = templateIndex === 4 ? '' : await ProviderInstance.encrypt( - assetUrl, - chainId, - macOsProviderUrl || providerUrl - ); - ddo.services[0].datatokenAddress = datatokenAddressAsset; - ddo.services[0].serviceEndpoint = providerUrl; - - ddo.nftAddress = nftAddress; - ddo.id = - "did:op:" + - SHA256(ethers.utils.getAddress(nftAddress) + chainId.toString(10)); - - let metadata; - let metadataHash; - let flags; - if (encryptDDO) { - metadata = await ProviderInstance.encrypt( - ddo, - chainId, - macOsProviderUrl || providerUrl - ); - const validateResult = await aquariusInstance.validate(ddo); - metadataHash = validateResult.hash; - flags = 2 - } else { - const stringDDO = JSON.stringify(ddo); - const bytes = Buffer.from(stringDDO); - metadata = hexlify(bytes); - metadataHash = "0x" + createHash("sha256").update(metadata).digest("hex"); - flags = 0 - } - - await nft.setMetadata( - nftAddress, - await owner.getAddress(), - 0, - providerUrl, - "", - ethers.utils.hexlify(flags), - metadata, - metadataHash - ); - - if(templateIndex === 4){ // Use Datatoken4 for file object - const datatoken = new Datatoken4( - wrappedSigner, - ethers.utils.toUtf8Bytes(JSON.stringify(assetUrl.files)), - chainId, - config - ); - - // Set file object - await datatoken.setFileObject(datatokenAddressAsset, await wrappedSigner.getAddress()); - - // Set allow list for the datatoken - await datatoken.setAllowListContract( - datatokenAddressAsset, - allowListAddress, - await wrappedSigner.getAddress() - );} - - return ddo.id; + return await createAsset(name, symbol, owner, assetUrl, templateIndex, ddo, encryptDDO, providerUrl || macOsProviderUrl, providerFeeToken, aquariusInstance); } diff --git a/src/publishAsset.ts b/src/publishAsset.ts index 57a08ce..d158802 100644 --- a/src/publishAsset.ts +++ b/src/publishAsset.ts @@ -5,7 +5,7 @@ import { Aquarius, DDO } from '@oceanprotocol/lib'; -import { createAsset, updateAssetMetadata } from './helpers'; +import { createAssetUtil, updateAssetMetadata } from './helpers'; export interface PublishAssetParams { title: string; @@ -81,7 +81,7 @@ export async function publishAsset(params: PublishAssetParams, signer: Signer, c }; // Other networks - const did = await createAsset( + const did = await createAssetUtil( params.title, 'OCEAN-NFT', signer, @@ -89,8 +89,7 @@ export async function publishAsset(params: PublishAssetParams, signer: Signer, c metadata, params.providerUrl, config, - aquarius, - params.template || 1 + aquarius );