From c2b008c0f37f222d46ba2ed66da388069c65e3a0 Mon Sep 17 00:00:00 2001 From: Albert Folch Date: Tue, 21 May 2024 12:22:40 +0200 Subject: [PATCH] fix: ipfs logic when uri is just a cid --- package-lock.json | 34 +++++++++++++++++----------------- package.json | 2 +- src/lib/utils/ipfs.test.ts | 7 +++++++ src/lib/utils/ipfs.ts | 7 +++++++ 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8f1009bf2..a46749eb4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "dependencies": { "@apollo/client": "^3.8.1", "@bosonprotocol/chat-sdk": "^1.3.1-alpha.9", - "@bosonprotocol/react-kit": "^0.30.0", + "@bosonprotocol/react-kit": "^0.30.1", "@davatar/react": "^1.10.4", "@ethersproject/address": "^5.6.1", "@ethersproject/units": "^5.7.0", @@ -2488,9 +2488,9 @@ } }, "node_modules/@bosonprotocol/common": { - "version": "1.27.2", - "resolved": "https://registry.npmjs.org/@bosonprotocol/common/-/common-1.27.2.tgz", - "integrity": "sha512-WmHJU4ORlHncgh/g3TkO1e+ErsR8vUVAIdGXCV9tX6ZlB3rodFVivDw4tZpZCKX556wRbrA4eRIRV1BMejga7w==", + "version": "1.27.3", + "resolved": "https://registry.npmjs.org/@bosonprotocol/common/-/common-1.27.3.tgz", + "integrity": "sha512-XXBpWLopa/pwODJPiXHbRGpffHtZ5/u3bYABPKM7QeoPf4WSdxH+skF4r722NwN0kvY5F18ykUt/52UcZsWk+g==", "dependencies": { "@bosonprotocol/metadata": "^1.16.1", "@ethersproject/abi": "^5.5.0", @@ -2502,11 +2502,11 @@ } }, "node_modules/@bosonprotocol/core-sdk": { - "version": "1.40.2", - "resolved": "https://registry.npmjs.org/@bosonprotocol/core-sdk/-/core-sdk-1.40.2.tgz", - "integrity": "sha512-KtRU8lr7wV9d20SdLdnKJEC8zH8qplbV6a9ltWWibqjiEPaGxviTs2sq/iFnGIMDjGCyssfzfAN48d5WDzU1NQ==", + "version": "1.40.3", + "resolved": "https://registry.npmjs.org/@bosonprotocol/core-sdk/-/core-sdk-1.40.3.tgz", + "integrity": "sha512-wBlo9+WJfh55B3CgRA5GHpILLaCyfHkdSvYIei/7pE73QUSGwcln9/x2VH25cq2E+iI3Qr1sG8we2XTNlLx+rw==", "dependencies": { - "@bosonprotocol/common": "^1.27.2", + "@bosonprotocol/common": "^1.27.3", "@ethersproject/abi": "^5.5.0", "@ethersproject/address": "^5.5.0", "@ethersproject/bignumber": "^5.5.0", @@ -2523,11 +2523,11 @@ } }, "node_modules/@bosonprotocol/ethers-sdk": { - "version": "1.14.2", - "resolved": "https://registry.npmjs.org/@bosonprotocol/ethers-sdk/-/ethers-sdk-1.14.2.tgz", - "integrity": "sha512-8l/QPSvxId7oylejWBCa7wwsVOQQCxo781gmejlhz1xPqWhoqj5lJ+yDcdrtuQkmT8gAjlRZEIv0iONnk9un0w==", + "version": "1.14.3", + "resolved": "https://registry.npmjs.org/@bosonprotocol/ethers-sdk/-/ethers-sdk-1.14.3.tgz", + "integrity": "sha512-GKq+pWSzlPSbh7L9lGcKgvW8yEspyEyp70wjmcwuYrKfi+IACFyxQQMtwd7a+yvhH8FXUYPIlL9jybl2PfICaw==", "dependencies": { - "@bosonprotocol/common": "^1.27.2" + "@bosonprotocol/common": "^1.27.3" }, "peerDependencies": { "ethers": "^5.5.0" @@ -2551,13 +2551,13 @@ } }, "node_modules/@bosonprotocol/react-kit": { - "version": "0.30.0", - "resolved": "https://registry.npmjs.org/@bosonprotocol/react-kit/-/react-kit-0.30.0.tgz", - "integrity": "sha512-Ywf8ggn9eZ5wTC84h1Rm6WxmmnsoTCPmQ15nvz5eJ6XUu6LHfUtE0NF1iKdUimb5feB8slfXALxvxQGCbTGcLg==", + "version": "0.30.1", + "resolved": "https://registry.npmjs.org/@bosonprotocol/react-kit/-/react-kit-0.30.1.tgz", + "integrity": "sha512-K5kKDnFFR920Fk2tJ+QwXOmekWE7Y7wOcNqpw9/QH7xm5GOxLnPrSRwe1HQFjjZthFMuEi2zp1pHcK6cI/8IvA==", "dependencies": { "@bosonprotocol/chat-sdk": "^1.3.1-alpha.9", - "@bosonprotocol/core-sdk": "^1.40.2", - "@bosonprotocol/ethers-sdk": "^1.14.2", + "@bosonprotocol/core-sdk": "^1.40.3", + "@bosonprotocol/ethers-sdk": "^1.14.3", "@bosonprotocol/ipfs-storage": "^1.11.3", "@davatar/react": "1.11.1", "@ethersproject/units": "5.6.0", diff --git a/package.json b/package.json index da0566125..c3b32908c 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "dependencies": { "@apollo/client": "^3.8.1", "@bosonprotocol/chat-sdk": "^1.3.1-alpha.9", - "@bosonprotocol/react-kit": "^0.30.0", + "@bosonprotocol/react-kit": "^0.30.1", "@davatar/react": "^1.10.4", "@ethersproject/address": "^5.6.1", "@ethersproject/units": "^5.7.0", diff --git a/src/lib/utils/ipfs.test.ts b/src/lib/utils/ipfs.test.ts index c3cd6e082..2ed56c323 100644 --- a/src/lib/utils/ipfs.test.ts +++ b/src/lib/utils/ipfs.test.ts @@ -87,4 +87,11 @@ describe("getIpfsGatewayUrl", () => { const result = getIpfsGatewayUrl(uri); expect(result).toBe(expected); }); + + it("should return a sanitized URL when the input URI is just the cid", () => { + const cid = "QmSeuDdMZqHxTqvraUKzKKJn9Vo9RELsA8xJUbDn6BGc6d"; + const expected = `https://ipfs.io/ipfs/${cid}`; + const result = getIpfsGatewayUrl(cid); + expect(result).toBe(expected); + }); }); diff --git a/src/lib/utils/ipfs.ts b/src/lib/utils/ipfs.ts index d54abd3e7..331891f06 100644 --- a/src/lib/utils/ipfs.ts +++ b/src/lib/utils/ipfs.ts @@ -19,6 +19,13 @@ export function getIpfsGatewayUrl( } const { gateway = CONFIG.ipfsGateway } = opts; try { + try { + CID.parse(uri); + const cid = uri; + return `${gateway}/${cid}`.replace(/([^:]\/)\/+/g, "$1"); + } catch { + // if uri it's not the cid only, then continue as expected and ignore this error + } const url = new URL( uri.startsWith("ipfs://") ? uri.replace("ipfs://", "https://") : uri );