Skip to content

Commit

Permalink
feat: add identicons option in getProfileData function (#18)
Browse files Browse the repository at this point in the history
* feat: add identicons option in getProfileData function

* fix: rename identicons to useDefaultPfp
  • Loading branch information
irisdv authored Nov 28, 2023
1 parent 8d1cbcd commit c92fa25
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 25 deletions.
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": "1.5.5",
"version": "1.5.6",
"description": "JavaScript library for Starknet ID",
"private": false,
"license": "MIT",
Expand Down
110 changes: 88 additions & 22 deletions packages/core/__test__/profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,28 +132,6 @@ describe("test starknetid.js sdk", () => {
0,
],
},
{
contractAddress: IdentityContract,
entrypoint: "set_verifier_data",
calldata: [
"1", // token_id
shortString.encodeShortString("nft_pp_contract"), // field
NFTContract, // value
0,
],
},
{
contractAddress: IdentityContract,
entrypoint: "set_extended_verifier_data",
calldata: [
"1", // token_id
shortString.encodeShortString("nft_pp_id"), // field
"2", // length
1, // value
0,
0,
],
},
]);
await provider.waitForTransaction(transaction_hash2);
});
Expand All @@ -172,6 +150,93 @@ describe("test starknetid.js sdk", () => {
});
});

test("getProfileData should return an undefined profile picture url", async () => {
const starknetIdNavigator = new StarknetIdNavigator(
provider,
constants.StarknetChainId.SN_GOERLI,
{
naming: NamingContract,
identity: IdentityContract,
},
);
expect(starknetIdNavigator).toBeInstanceOf(StarknetIdNavigator);
const profile = await starknetIdNavigator.getProfileData(
account.address,
false,
otherAccount.address,
otherAccount.address,
otherAccount.address,
);
const expectedProfile = {
name: "ben.stark",
twitter: undefined,
github: undefined,
discord: "123",
proofOfPersonhood: false,
profilePicture: undefined,
};
expect(profile).toStrictEqual(expectedProfile);
});

test("getProfileData should return an identicon url", async () => {
const starknetIdNavigator = new StarknetIdNavigator(
provider,
constants.StarknetChainId.SN_GOERLI,
{
naming: NamingContract,
identity: IdentityContract,
},
);
expect(starknetIdNavigator).toBeInstanceOf(StarknetIdNavigator);
const profile = await starknetIdNavigator.getProfileData(
account.address,
true,
otherAccount.address,
otherAccount.address,
otherAccount.address,
);
const expectedProfile = {
name: "ben.stark",
twitter: undefined,
github: undefined,
discord: "123",
proofOfPersonhood: false,
profilePicture: "https://starknet.id/api/identicons/1",
};
expect(profile).toStrictEqual(expectedProfile);
});
});

describe("getProfileData with nft profile picture", () => {
beforeAll(async () => {
// Add nft pp verifier data
const { transaction_hash } = await otherAccount.execute([
{
contractAddress: IdentityContract,
entrypoint: "set_verifier_data",
calldata: [
"1", // token_id
shortString.encodeShortString("nft_pp_contract"), // field
NFTContract, // value
0,
],
},
{
contractAddress: IdentityContract,
entrypoint: "set_extended_verifier_data",
calldata: [
"1", // token_id
shortString.encodeShortString("nft_pp_id"), // field
"2", // length
1, // value
0,
0,
],
},
]);
await provider.waitForTransaction(transaction_hash);
});

test("getProfileData should return the right values", async () => {
const starknetIdNavigator = new StarknetIdNavigator(
provider,
Expand All @@ -184,6 +249,7 @@ describe("test starknetid.js sdk", () => {
expect(starknetIdNavigator).toBeInstanceOf(StarknetIdNavigator);
const profile = await starknetIdNavigator.getProfileData(
account.address,
true,
otherAccount.address,
otherAccount.address,
otherAccount.address,
Expand Down
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": "1.5.5",
"version": "1.5.6",
"keywords": [
"starknet",
"starknetid",
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/starknetIdNavigator/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ export class StarknetIdNavigator implements StarknetIdNavigatorInterface {

public async getProfileData(
address: string,
useDefaultPfp: boolean = true,
verifier?: string,
pfp_verifier?: string,
pop_verifier?: string,
Expand Down Expand Up @@ -529,6 +530,8 @@ export class StarknetIdNavigator implements StarknetIdNavigatorInterface {
// extract nft_image from profile data
const profilePicture = profilePictureMetadata
? await this.fetchImageUrl(profilePictureMetadata)
: useDefaultPfp
? `https://starknet.id/api/identicons/${data[1][0].toString()}`
: undefined;

return {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/starknetIdNavigator/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,18 @@ export abstract class StarknetIdNavigatorInterface {
* Get user stark profile data from his address
* Use this function to retrive starkname, profile picture url, social networks ids and proof of personhood verification status.
* If no verifier is provided, it will use the starknet.id verifiers contract addresses
* If no NFT is set as profile picture, it will return zeros.
* If no NFT is set as profile picture it will return the starknetid pfp url for this address. To disable this behavior, set the useDefaultPfp parameter to false.
*
* @param address (string)
* @param useDefaultPfp boolean to return the default starknetid url if no profile picture is set (optional)
* @param verifier contract address for social networks (optional)
* @param pfp_verifier contract address for profile picture (optional)
* @param pop_verifier contract address for proof of personhood (optional)
* @returns StarkProfile
*/
public abstract getProfileData(
address: string,
useDefaultPfp?: boolean,
verifier?: string,
pfp_verifier?: string,
pop_verifier?: string,
Expand Down

0 comments on commit c92fa25

Please sign in to comment.