From 991588ed64d6f96c31752038b3023f315a4cf23b Mon Sep 17 00:00:00 2001 From: Iris Date: Mon, 29 Apr 2024 16:57:30 +0200 Subject: [PATCH] fix: ipfs images (#37) * fix: ipfs images * fix: delete commented code --- package.json | 2 +- .../core/__test__/profile_mainnet.test.ts | 87 ++++++++++++++----- packages/core/package.json | 2 +- .../core/src/starknetIdNavigator/internal.ts | 10 ++- 4 files changed, 74 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index d42685e..fc1fc34 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "starknetid.js", - "version": "3.1.0", + "version": "3.2.0", "description": "JavaScript library for Starknet ID", "private": false, "license": "MIT", diff --git a/packages/core/__test__/profile_mainnet.test.ts b/packages/core/__test__/profile_mainnet.test.ts index 5246a96..eb40de4 100644 --- a/packages/core/__test__/profile_mainnet.test.ts +++ b/packages/core/__test__/profile_mainnet.test.ts @@ -42,16 +42,19 @@ describe("test starknetid.js sdk on mainnet", () => { "0x061b6c0a78f9edf13cea17b50719f3344533fadd470b8cb29c2b4318014f52d3", false, ); + + expect( + profile.profilePicture && + profile.profilePicture.startsWith("https://img.starkurabu.com"), + ).toBeTruthy(); const expectedProfile = { name: "fricoben.stark", twitter: "1255853529866145794", github: "78437165", discord: "662387807901188096", proofOfPersonhood: true, - profilePicture: - "https://img.starkurabu.com/41538374869489910341448844649168906.png", }; - expect(profile).toStrictEqual(expectedProfile); + expect(profile).toMatchObject(expectedProfile); }); test("getProfileData with a duck pfp", async () => { @@ -75,6 +78,27 @@ describe("test starknetid.js sdk on mainnet", () => { expect(profile).toStrictEqual(expectedProfile); }); + test("getProfileData with a everai pfp", async () => { + const starknetIdNavigator = new StarknetIdNavigator( + provider, + constants.StarknetChainId.SN_MAIN, + ); + expect(starknetIdNavigator).toBeInstanceOf(StarknetIdNavigator); + const profile = await starknetIdNavigator.getProfileData( + "0x007b275f7524f39b99a51c7134bc44204fedc5dd1e982e920eb2047c6c2a71f0", + ); + const expectedProfile = { + name: "rob.cacango.stark", + twitter: undefined, + github: undefined, + discord: undefined, + proofOfPersonhood: false, + profilePicture: + "https://gateway.pinata.cloud/ipfs/QmZS7maV678eJW7wJaVXJc28aKXzdZrwS1hmBmSy6bUVJh/1925.jpg", + }; + expect(profile).toStrictEqual(expectedProfile); + }); + test("getProfileData on an address with no identity", async () => { const starknetIdNavigator = new StarknetIdNavigator( provider, @@ -153,28 +177,45 @@ describe("test starknetid.js sdk on mainnet", () => { "0x06fb5e4e650bb6ceb80923c008e81122129092efc7e6d6f3f5c9ac4eead25355", // blobbert "0x061b6c0a78f9edf13cea17b50719f3344533fadd470b8cb29c2b4318014f52d3", // starkurabu "0x0097095403155fcbFA72AA53270D6eDd0DCC830bBb9264455517DF3e508633E5", // nothing + "0x007b275f7524f39b99a51c7134bc44204fedc5dd1e982e920eb2047c6c2a71f0", // everai pfp ]); - const expectedProfile = [ - { - name: "iris.stark", - profilePicture: - "https://api.briq.construction/v1/preview/starknet-mainnet-dojo/0x6cff01dd5d1e2ec5e792d66bd6edae386bd022b4ffd993c76c08cd000000003.png", - }, - { - name: "rmz.stark", - profilePicture: "https://starknet.id/api/identicons/891050699740", - }, - { - name: "fricoben.stark", - profilePicture: - "https://img.starkurabu.com/41538374869489910341448844649168906.png", - }, - { - name: undefined, - profilePicture: "https://starknet.id/api/identicons/0", - }, + console.log("profiles", profiles); + const expectedProfiles = [ + { name: "iris.stark" }, + { name: "rmz.stark" }, + { name: "fricoben.stark" }, + { name: undefined }, + { name: "rob.cacango.stark" }, ]; - expect(profiles).toStrictEqual(expectedProfile); + + profiles.forEach((profile, index) => { + expect(profile.name).toEqual(expectedProfiles[index].name); + }); + + expect( + profiles[0].profilePicture && + profiles[0].profilePicture.startsWith( + "https://api.briq.construction", + ), + ).toBeTruthy(); + expect( + profiles[1].profilePicture && + profiles[1].profilePicture.startsWith("https://starknet.id"), + ).toBeTruthy(); + expect( + profiles[2].profilePicture && + profiles[2].profilePicture.startsWith("https://img.starkurabu.com"), + ).toBeTruthy(); + expect( + profiles[3].profilePicture && + profiles[3].profilePicture.startsWith("https://starknet.id"), + ).toBeTruthy(); + expect( + profiles[4].profilePicture && + profiles[4].profilePicture.startsWith( + "https://gateway.pinata.cloud/ipfs/", + ), + ).toBeTruthy(); }); }); }); diff --git a/packages/core/package.json b/packages/core/package.json index 5be5055..a062928 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "starknetid.js", - "version": "3.1.0", + "version": "3.2.0", "keywords": [ "starknet", "starknetid", diff --git a/packages/core/src/starknetIdNavigator/internal.ts b/packages/core/src/starknetIdNavigator/internal.ts index 3afd1dc..22de76e 100644 --- a/packages/core/src/starknetIdNavigator/internal.ts +++ b/packages/core/src/starknetIdNavigator/internal.ts @@ -19,9 +19,15 @@ export const parseBase64Image = (metadata: string): string => { return JSON.parse(atob(metadata.split(",")[1].slice(0, -1))).image; }; +export const parseImageUrl = (url: string): string => { + return url.startsWith("ipfs://") + ? url.replace("ipfs://", "https://gateway.pinata.cloud/ipfs/") + : url; +}; + export const fetchImageUrl = async (url: string): Promise => { try { - const response = await fetch(url); + const response = await fetch(parseImageUrl(url)); if (!response.ok) { throw new Error("Network response was not ok"); @@ -31,7 +37,7 @@ export const fetchImageUrl = async (url: string): Promise => { // Check if the "image" key exists and is not null if (data.image) { - return data.image; + return parseImageUrl(data.image); } else { return "Image is not set"; }