Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ipfs images #37

Merged
merged 2 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
87 changes: 64 additions & 23 deletions packages/core/__test__/profile_mainnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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,
Expand Down Expand Up @@ -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();
});
});
});
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "starknetid.js",
"version": "3.1.0",
"version": "3.2.0",
"keywords": [
"starknet",
"starknetid",
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/starknetIdNavigator/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> => {
try {
const response = await fetch(url);
const response = await fetch(parseImageUrl(url));

if (!response.ok) {
throw new Error("Network response was not ok");
Expand All @@ -31,7 +37,7 @@ export const fetchImageUrl = async (url: string): Promise<string> => {

// 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";
}
Expand Down
Loading