diff --git a/.gitignore b/.gitignore index 015756e56..3a85faca8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ node_modules/ .yarn test/ test.* -yarn-error.log \ No newline at end of file +yarn-error.log +.env \ No newline at end of file diff --git a/package.json b/package.json index de9cd0061..44d0b1db4 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "axios": "^1.6.7", "casper-cep78-js-client": "^1.4.0", "casper-js-sdk": "^2.13.3", + "dotenv": "^16.4.5", "ethers": "^6.10.0", "hashconnect": "^0.2.9", "near-api-js": "^5.0.0", diff --git a/src/handlers/casper/index.ts b/src/handlers/casper/index.ts index 168cda60b..2fdfb3077 100644 --- a/src/handlers/casper/index.ts +++ b/src/handlers/casper/index.ts @@ -26,6 +26,8 @@ import { PurseIdentifier, RuntimeArgs, } from "casper-js-sdk"; +import type { Any } from "../utils/any"; +import { pinata } from "../utils/pinata"; import { CLAIM_WASM } from "./claim.wasm"; import { getDeploy } from "./get-deploy"; import { LOCK_WASM } from "./lock.wasm"; @@ -298,9 +300,13 @@ export function casperHandler({ destinationChain, to, tokenId, - metaDataUri, + _metaDataUri, extraArgs, ) { + const cc = new CasperClient(rpc); + const nftContract = new Contracts.Contract(cc); + nftContract.setContractHash(sourceNft); + const metadata = await getMetaData(nftContract, tokenId); const nft_storage_exists = await checkStorage( bc, sourceNft.replace("hash-", ""), @@ -316,7 +322,7 @@ export function casperHandler({ source_nft_contract_address_arg: CLValueBuilder.byteArray( convertHashStrToHashBuff(sourceNft), ), - metadata_arg: CLValueBuilder.string(metaDataUri), + metadata_arg: CLValueBuilder.string(metadata), amount: CLValueBuilder.u512(extraArgs?.amount || "110000000000"), }); const n = new Contracts.Contract(cc); @@ -344,7 +350,7 @@ export function casperHandler({ destinationChain, to, tokenId, - metaDataUri, + metadata, extraArgs, ); } @@ -510,3 +516,64 @@ async function checkStorage(bc: Contracts.Contract, sourceNft: string) { return ret; } + +async function getMetaData(nftContract: Contracts.Contract, tokenId: string) { + // CEP78 = 0, --> metadata_cep78 + // NFT721 = 1, --> metadata_nft721 + // Raw = 2, --> metadata_raw + // CustomValidated = 3 --> metadata_custom_validated + + let data: Any | undefined; + + try { + data = ( + await nftContract.queryContractDictionary("metadata_cep78", tokenId) + ).toJSON(); + if (data?.token_uri) { + data = data?.token_uri; + } + console.log("metadata_cep78", data); + } catch (ex) { + try { + data = ( + await nftContract.queryContractDictionary("metadata_nft721", tokenId) + ).toJSON(); + if (data?.token_uri) { + data = data?.token_uri; + } + console.log("metadata_nft721", data.toJSON()); + } catch (ex) { + try { + data = ( + await nftContract.queryContractDictionary("metadata_raw", tokenId) + ).toJSON(); + if (data?.token_uri) { + data = data?.token_uri; + } + console.log("metadata_raw", data.toJSON()); + } catch (ex) { + try { + data = ( + await nftContract.queryContractDictionary( + "metadata_custom_validated", + tokenId, + ) + ).toJSON(); + if (data?.token_uri) { + data = data?.token_uri; + } + console.log("metadata_custom_validated", data.toJSON()); + } catch (ex) {} + } + } + } + if (data) { + if (typeof data === "object") { + const pinResponse = await pinata.upload.json(data.toJSON()); + const metadata = `https://xpnetwork.infura-ipfs.io/ipfs/ ${pinResponse.IpfsHash}`; + console.log({ metadata }); + return metadata; + } + return data; + } +} diff --git a/src/handlers/secret/index.ts b/src/handlers/secret/index.ts index 684e7e621..1a1ad18bc 100644 --- a/src/handlers/secret/index.ts +++ b/src/handlers/secret/index.ts @@ -1,11 +1,14 @@ -import { StdSignature, toBase64, validateAddress } from "secretjs"; -import { Metadata } from "secretjs/dist/extensions/snip1155/types"; -import { Pubkey } from "secretjs/dist/wallet_amino"; -import { Lock721, Lock1155 } from "../../contractsTypes/secret/secretBridge"; +import { type StdSignature, toBase64, validateAddress } from "secretjs"; +import type { Metadata } from "secretjs/dist/extensions/snip1155/types"; +import type { Pubkey } from "secretjs/dist/wallet_amino"; +import type { + Lock721, + Lock1155, +} from "../../contractsTypes/secret/secretBridge"; import { raise } from "../ton"; -import { TokenInfo } from "../types"; +import type { TokenInfo } from "../types"; import { pinata } from "../utils"; -import { +import type { GetOwnedTokensResponse, TNftInfo, TSecretHandler, diff --git a/src/handlers/utils/any.ts b/src/handlers/utils/any.ts new file mode 100644 index 000000000..1ee12d94d --- /dev/null +++ b/src/handlers/utils/any.ts @@ -0,0 +1,2 @@ +// biome-ignore lint/suspicious/noExplicitAny: +export type Any = any; diff --git a/src/handlers/utils/pinata.ts b/src/handlers/utils/pinata.ts index 58f5b2997..ee499320c 100644 --- a/src/handlers/utils/pinata.ts +++ b/src/handlers/utils/pinata.ts @@ -1,5 +1,7 @@ +import { config } from "dotenv"; import { PinataSDK } from "pinata-web3"; +config(); export const pinata = new PinataSDK({ - pinataJwt: `${""}`, + pinataJwt: `${process.env.PINATA_JWT}`, }); diff --git a/yarn.lock b/yarn.lock index 161f56270..1333accd5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2155,6 +2155,11 @@ dns-packet@^5.6.1: dependencies: "@leichtgewicht/ip-codec" "^2.0.1" +dotenv@^16.4.5: + version "16.4.5" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f" + integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg== + ed25519-hd-key@1.1.2: version "1.1.2" resolved "https://registry.yarnpkg.com/ed25519-hd-key/-/ed25519-hd-key-1.1.2.tgz#168dcf08419694be7bba3319e7d64e4a5cfe5d44"